You are here

function aggregator_refresh in Drupal 6

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

Checks a news feed for new items.

Parameters

$feed: An associative array describing the feed to be refreshed.

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

File

modules/aggregator/aggregator.module, line 583
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' => $feed['title'],
      )));
      break;
    case 301:
      $feed['url'] = $result->redirect_url;
      watchdog('aggregator', 'Updated URL for feed %title to %url.', array(
        '%title' => $feed['title'],
        '%url' => $feed['url'],
      ));

    // Deliberate no break.
    case 200:
    case 302:
    case 307:

      // Filter the input data:
      if (aggregator_parse_feed($result->data, $feed)) {
        $modified = empty($result->headers['Last-Modified']) ? 0 : 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 (!empty($image['LINK']) && !empty($image['URL']) && !empty($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;
        }
        $etag = empty($result->headers['ETag']) ? '' : $result->headers['ETag'];

        // 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, $etag, $modified, $feed['fid']);

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