function aggregator_refresh

Checks a news feed for new items.

Parameters

\Drupal\aggregator\Plugin\Core\Entity\Feed $feed: An object describing the feed to be refreshed.

6 calls to aggregator_refresh()
AggregatorController::feedRefresh in drupal/core/modules/aggregator/lib/Drupal/aggregator/Controller/AggregatorController.php
Refreshes a feed, then redirects to the overview page.
FeedParserTest::testAtomSample in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/FeedParserTest.php
Tests a feed that uses the Atom format.
FeedParserTest::testHtmlEntitiesSample in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/FeedParserTest.php
Tests a feed that uses HTML entities in item titles.
FeedParserTest::testRedirectFeed in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/FeedParserTest.php
Tests error handling when an invalid feed is added.
FeedParserTest::testRSS091Sample in drupal/core/modules/aggregator/lib/Drupal/aggregator/Tests/FeedParserTest.php
Tests a feed that uses the RSS 0.91 format.

... See full list

1 string reference to 'aggregator_refresh'
aggregator_queue_info in drupal/core/modules/aggregator/aggregator.module
Implements hook_queue_info().

File

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

Code

function aggregator_refresh(Feed $feed) {

  // Store feed URL to track changes.
  $feed_url = $feed->url->value;
  $config = config('aggregator.settings');

  // Fetch the feed.
  $fetcher_manager = Drupal::service('plugin.manager.aggregator.fetcher');
  try {
    $success = $fetcher_manager
      ->createInstance($config
      ->get('fetcher'))
      ->fetch($feed);
  } catch (PluginException $e) {
    $success = FALSE;
    watchdog_exception('aggregator', $e);
  }

  // Retrieve processor manager now.
  $processor_manager = Drupal::service('plugin.manager.aggregator.processor');

  // Store instances in an array so we dont have to instantiate new objects.
  $processor_instances = array();
  foreach ($config
    ->get('processors') as $processor) {
    try {
      $processor_instances[$processor] = $processor_manager
        ->createInstance($processor);
    } catch (PluginException $e) {
      watchdog_exception('aggregator', $e);
    }
  }

  // We store the hash of feed data in the database. When refreshing a
  // feed we compare stored hash and new hash calculated from downloaded
  // data. If both are equal we say that feed is not updated.
  $hash = hash('sha256', $feed->source_string);
  if ($success && $feed->hash->value != $hash) {

    // Parse the feed.
    $parser_manager = Drupal::service('plugin.manager.aggregator.parser');
    try {
      if ($parser_manager
        ->createInstance($config
        ->get('parser'))
        ->parse($feed)) {
        if (empty($feed->link->value)) {
          $feed->link->value = $feed->url->value;
        }
        $feed->hash->value = $hash;

        // Update feed with parsed data.
        $feed
          ->save();

        // Log if feed URL has changed.
        if ($feed->url->value != $feed_url) {
          watchdog('aggregator', 'Updated URL for feed %title to %url.', array(
            '%title' => $feed
              ->label(),
            '%url' => $feed->url->value,
          ));
        }
        watchdog('aggregator', 'There is new syndicated content from %site.', array(
          '%site' => $feed
            ->label(),
        ));
        drupal_set_message(t('There is new syndicated content from %site.', array(
          '%site' => $feed
            ->label(),
        )));

        // If there are items on the feed, let enabled processors process them.
        if (!empty($feed->items)) {
          foreach ($processor_instances as $instance) {
            $instance
              ->process($feed);
          }
        }
      }
    } catch (PluginException $e) {
      watchdog_exception('aggregator', $e);
    }
  }
  else {
    drupal_set_message(t('There is no new syndicated content from %site.', array(
      '%site' => $feed
        ->label(),
    )));
  }

  // Regardless of successful or not, indicate that this feed has been checked.
  $feed->checked->value = REQUEST_TIME;
  $feed->queued->value = 0;
  $feed
    ->save();

  // Processing is done, call postProcess on enabled processors.
  foreach ($processor_instances as $instance) {
    $instance
      ->postProcess($feed);
  }
}