X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Faggregator%2Fsrc%2FItemsImporter.php;fp=web%2Fcore%2Fmodules%2Faggregator%2Fsrc%2FItemsImporter.php;h=aa18d65c9afc946603185edc1991ffc72fa1fad4;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/aggregator/src/ItemsImporter.php b/web/core/modules/aggregator/src/ItemsImporter.php new file mode 100644 index 000000000..aa18d65c9 --- /dev/null +++ b/web/core/modules/aggregator/src/ItemsImporter.php @@ -0,0 +1,153 @@ +fetcherManager = $fetcher_manager; + $this->processorManager = $processor_manager; + $this->parserManager = $parser_manager; + $this->config = $config_factory->get('aggregator.settings'); + $this->logger = $logger; + } + + /** + * {@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); + } + + // Store instances in an array so we dont have to instantiate new objects. + $processor_instances = []; + 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)) { + if (!$feed->getWebsiteUrl()) { + $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) { + $this->logger->notice('Updated URL for feed %title to %url.', ['%title' => $feed->label(), '%url' => $feed->getUrl()]); + } + + $this->logger->notice('There is new syndicated content from %site.', ['%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); + } + } + + // Processing is done, call postProcess on enabled processors. + foreach ($processor_instances as $instance) { + $instance->postProcess($feed); + } + + return $has_new_content; + } + +}