3 namespace Drupal\aggregator;
5 use Drupal\aggregator\Plugin\AggregatorPluginManager;
6 use Drupal\Component\Plugin\Exception\PluginException;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Psr\Log\LoggerInterface;
11 * Defines an importer of aggregator items.
13 class ItemsImporter implements ItemsImporterInterface {
16 * The aggregator fetcher manager.
18 * @var \Drupal\aggregator\Plugin\AggregatorPluginManager
20 protected $fetcherManager;
23 * The aggregator processor manager.
25 * @var \Drupal\aggregator\Plugin\AggregatorPluginManager
27 protected $processorManager;
30 * The aggregator parser manager.
32 * @var \Drupal\aggregator\Plugin\AggregatorPluginManager
34 protected $parserManager;
37 * The aggregator.settings config object.
39 * @var \Drupal\Core\Config\Config
46 * @var \Psr\Log\LoggerInterface
51 * Constructs an Importer object.
53 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
54 * The factory for configuration objects.
55 * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $fetcher_manager
56 * The aggregator fetcher plugin manager.
57 * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $parser_manager
58 * The aggregator parser plugin manager.
59 * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $processor_manager
60 * The aggregator processor plugin manager.
61 * @param \Psr\Log\LoggerInterface $logger
64 public function __construct(ConfigFactoryInterface $config_factory, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager, LoggerInterface $logger) {
65 $this->fetcherManager = $fetcher_manager;
66 $this->processorManager = $processor_manager;
67 $this->parserManager = $parser_manager;
68 $this->config = $config_factory->get('aggregator.settings');
69 $this->logger = $logger;
75 public function delete(FeedInterface $feed) {
76 foreach ($this->processorManager->getDefinitions() as $id => $definition) {
77 $this->processorManager->createInstance($id)->delete($feed);
84 public function refresh(FeedInterface $feed) {
85 // Store feed URL to track changes.
86 $feed_url = $feed->getUrl();
90 $success = $this->fetcherManager->createInstance($this->config->get('fetcher'))->fetch($feed);
92 catch (PluginException $e) {
94 watchdog_exception('aggregator', $e);
97 // Store instances in an array so we don't have to instantiate new objects.
98 $processor_instances = [];
99 foreach ($this->config->get('processors') as $processor) {
101 $processor_instances[$processor] = $this->processorManager->createInstance($processor);
103 catch (PluginException $e) {
104 watchdog_exception('aggregator', $e);
108 // We store the hash of feed data in the database. When refreshing a
109 // feed we compare stored hash and new hash calculated from downloaded
110 // data. If both are equal we say that feed is not updated.
111 $hash = hash('sha256', $feed->source_string);
112 $has_new_content = $success && ($feed->getHash() != $hash);
114 if ($has_new_content) {
117 if ($this->parserManager->createInstance($this->config->get('parser'))->parse($feed)) {
118 if (!$feed->getWebsiteUrl()) {
119 $feed->setWebsiteUrl($feed->getUrl());
121 $feed->setHash($hash);
122 // Update feed with parsed data.
125 // Log if feed URL has changed.
126 if ($feed->getUrl() != $feed_url) {
127 $this->logger->notice('Updated URL for feed %title to %url.', ['%title' => $feed->label(), '%url' => $feed->getUrl()]);
130 $this->logger->notice('There is new syndicated content from %site.', ['%site' => $feed->label()]);
132 // If there are items on the feed, let enabled processors process them.
133 if (!empty($feed->items)) {
134 foreach ($processor_instances as $instance) {
135 $instance->process($feed);
140 catch (PluginException $e) {
141 watchdog_exception('aggregator', $e);
145 // Processing is done, call postProcess on enabled processors.
146 foreach ($processor_instances as $instance) {
147 $instance->postProcess($feed);
150 return $has_new_content;