ItemsImporter.php 4.57 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\aggregator;

use Drupal\aggregator\Plugin\AggregatorPluginManager;
use Drupal\Component\Plugin\Exception\PluginException;
7
use Drupal\Core\Config\ConfigFactoryInterface;
8
use Psr\Log\LoggerInterface;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/**
 * Defines an importer of aggregator items.
 */
class ItemsImporter implements ItemsImporterInterface {

  /**
   * The aggregator fetcher manager.
   *
   * @var \Drupal\aggregator\Plugin\AggregatorPluginManager
   */
  protected $fetcherManager;

  /**
   * The aggregator processor manager.
   *
   * @var \Drupal\aggregator\Plugin\AggregatorPluginManager
   */
  protected $processorManager;

  /**
   * The aggregator parser manager.
   *
   * @var \Drupal\aggregator\Plugin\AggregatorPluginManager
   */
  protected $parserManager;

  /**
   * The aggregator.settings config object.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

43 44 45 46 47 48 49
  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

50 51 52
  /**
   * Constructs an Importer object.
   *
53
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
54 55 56 57 58 59 60
   *   The factory for configuration objects.
   * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $fetcher_manager
   *   The aggregator fetcher plugin manager.
   * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $parser_manager
   *   The aggregator parser plugin manager.
   * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $processor_manager
   *   The aggregator processor plugin manager.
61 62
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
63
   */
64
  public function __construct(ConfigFactoryInterface $config_factory, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager, LoggerInterface $logger) {
65 66 67 68
    $this->fetcherManager = $fetcher_manager;
    $this->processorManager = $processor_manager;
    $this->parserManager = $parser_manager;
    $this->config = $config_factory->get('aggregator.settings');
69
    $this->logger = $logger;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  }

  /**
   * {@inheritdoc}
   */
  public function delete(FeedInterface $feed) {
    foreach ($this->processorManager->getDefinitions() as $id => $definition) {
      $this->processorManager->createInstance($id)->delete($feed);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function refresh(FeedInterface $feed) {
    // Store feed URL to track changes.
    $feed_url = $feed->getUrl();

    // Fetch the feed.
    try {
      $success = $this->fetcherManager->createInstance($this->config->get('fetcher'))->fetch($feed);
    }
    catch (PluginException $e) {
      $success = FALSE;
      watchdog_exception('aggregator', $e);
    }

97
    // Store instances in an array so we don't have to instantiate new objects.
98
    $processor_instances = [];
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    foreach ($this->config->get('processors') as $processor) {
      try {
        $processor_instances[$processor] = $this->processorManager->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);
    $has_new_content = $success && ($feed->getHash() != $hash);

    if ($has_new_content) {
      // Parse the feed.
      try {
        if ($this->parserManager->createInstance($this->config->get('parser'))->parse($feed)) {
118
          if (!$feed->getWebsiteUrl()) {
119 120 121 122 123 124 125 126
            $feed->setWebsiteUrl($feed->getUrl());
          }
          $feed->setHash($hash);
          // Update feed with parsed data.
          $feed->save();

          // Log if feed URL has changed.
          if ($feed->getUrl() != $feed_url) {
127
            $this->logger->notice('Updated URL for feed %title to %url.', ['%title' => $feed->label(), '%url' => $feed->getUrl()]);
128 129
          }

130
          $this->logger->notice('There is new syndicated content from %site.', ['%site' => $feed->label()]);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

          // 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);
      }
    }

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

    return $has_new_content;
  }

}