You are here

function aggregator_refresh in Drupal 4

Same name and namespace in other branches
  1. 5 modules/aggregator/aggregator.module \aggregator_refresh()
  2. 6 modules/aggregator/aggregator.module \aggregator_refresh()
  3. 7 modules/aggregator/aggregator.module \aggregator_refresh()

Checks a news feed for new items.

2 calls to aggregator_refresh()
aggregator_admin_refresh_feed in modules/aggregator.module
Menu callback; refreshes a feed, then redirects to the overview page.
aggregator_cron in modules/aggregator.module
Implementation of hook_cron().

File

modules/aggregator.module, line 691
Used to aggregate syndicated content (RSS, RDF, and Atom).

Code

function aggregator_refresh($feed) {
  global $channel, $image;

  // Generate conditional GET headers.
  $headers = array();
  if ($feed['etag']) {
    $headers['If-None-Match'] = $feed['etag'];
  }
  if ($feed['modified']) {
    $headers['If-Modified-Since'] = gmdate('D, d M Y H:i:s', $feed['modified']) . ' GMT';
  }

  // Request feed.
  $result = drupal_http_request($feed['url'], $headers);

  // Process HTTP response code.
  switch ($result->code) {
    case 304:
      db_query('UPDATE {aggregator_feed} SET checked = %d WHERE fid = %d', time(), $feed['fid']);
      drupal_set_message(t('There is no new syndicated content from %site.', array(
        '%site' => theme('placeholder', $feed['title']),
      )));
      break;
    case 301:
      $feed['url'] = $result->redirect_url;
      watchdog('aggregator', t('Updated URL for feed %title to %url.', array(
        '%title' => theme('placeholder', $feed['title']),
        '%url' => theme('placeholder', $feed['url']),
      )));
    case 200:
    case 302:
    case 307:

      // Filter the input data:
      if (aggregator_parse_feed($result->data, $feed)) {
        if ($result->headers['Last-Modified']) {
          $modified = strtotime($result->headers['Last-Modified']);
        }

        /*
         ** Prepare the channel data:
         */
        foreach ($channel as $key => $value) {
          $channel[$key] = trim($value);
        }

        /*
         ** Prepare the image data (if any):
         */
        foreach ($image as $key => $value) {
          $image[$key] = trim($value);
        }
        if ($image['LINK'] && $image['URL'] && $image['TITLE']) {

          // Note, we should really use theme_image() here but that only works with local images it won't work with images fetched with a URL unless PHP version > 5
          $image = '<a href="' . check_url($image['LINK']) . '" class="feed-image"><img src="' . check_url($image['URL']) . '" alt="' . check_plain($image['TITLE']) . '" /></a>';
        }
        else {
          $image = NULL;
        }

        /*
         ** Update the feed data:
         */
        db_query("UPDATE {aggregator_feed} SET url = '%s', checked = %d, link = '%s', description = '%s', image = '%s', etag = '%s', modified = %d WHERE fid = %d", $feed['url'], time(), $channel['LINK'], $channel['DESCRIPTION'], $image, $result->headers['ETag'], $modified, $feed['fid']);

        /*
         ** Clear the cache:
         */
        cache_clear_all();
        watchdog('aggregator', t('There is new syndicated content from %site.', array(
          '%site' => theme('placeholder', $feed['title']),
        )));
        drupal_set_message(t('There is new syndicated content from %site.', array(
          '%site' => theme('placeholder', $feed['title']),
        )));
      }
      break;
    default:
      watchdog('aggregator', t('The RSS-feed from %site seems to be broken, due to "%error".', array(
        '%site' => theme('placeholder', $feed['title']),
        '%error' => theme('placeholder', $result->code . ' ' . $result->error),
      )), WATCHDOG_WARNING);
      drupal_set_message(t('The RSS-feed from %site seems to be broken, because of error "%error".', array(
        '%site' => theme('placeholder', $feed['title']),
        '%error' => theme('placeholder', $result->code . ' ' . $result->error),
      )));
  }
}