ItemsImporter.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Drupal\aggregator;
  3. use Drupal\aggregator\Plugin\AggregatorPluginManager;
  4. use Drupal\Component\Plugin\Exception\PluginException;
  5. use Drupal\Core\Config\ConfigFactoryInterface;
  6. use Psr\Log\LoggerInterface;
  7. /**
  8. * Defines an importer of aggregator items.
  9. */
  10. class ItemsImporter implements ItemsImporterInterface {
  11. /**
  12. * The aggregator fetcher manager.
  13. *
  14. * @var \Drupal\aggregator\Plugin\AggregatorPluginManager
  15. */
  16. protected $fetcherManager;
  17. /**
  18. * The aggregator processor manager.
  19. *
  20. * @var \Drupal\aggregator\Plugin\AggregatorPluginManager
  21. */
  22. protected $processorManager;
  23. /**
  24. * The aggregator parser manager.
  25. *
  26. * @var \Drupal\aggregator\Plugin\AggregatorPluginManager
  27. */
  28. protected $parserManager;
  29. /**
  30. * The aggregator.settings config object.
  31. *
  32. * @var \Drupal\Core\Config\Config
  33. */
  34. protected $config;
  35. /**
  36. * A logger instance.
  37. *
  38. * @var \Psr\Log\LoggerInterface
  39. */
  40. protected $logger;
  41. /**
  42. * Constructs an Importer object.
  43. *
  44. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  45. * The factory for configuration objects.
  46. * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $fetcher_manager
  47. * The aggregator fetcher plugin manager.
  48. * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $parser_manager
  49. * The aggregator parser plugin manager.
  50. * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $processor_manager
  51. * The aggregator processor plugin manager.
  52. * @param \Psr\Log\LoggerInterface $logger
  53. * A logger instance.
  54. */
  55. public function __construct(ConfigFactoryInterface $config_factory, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager, LoggerInterface $logger) {
  56. $this->fetcherManager = $fetcher_manager;
  57. $this->processorManager = $processor_manager;
  58. $this->parserManager = $parser_manager;
  59. $this->config = $config_factory->get('aggregator.settings');
  60. $this->logger = $logger;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function delete(FeedInterface $feed) {
  66. foreach ($this->processorManager->getDefinitions() as $id => $definition) {
  67. $this->processorManager->createInstance($id)->delete($feed);
  68. }
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function refresh(FeedInterface $feed) {
  74. // Store feed URL to track changes.
  75. $feed_url = $feed->getUrl();
  76. // Fetch the feed.
  77. try {
  78. $success = $this->fetcherManager->createInstance($this->config->get('fetcher'))->fetch($feed);
  79. }
  80. catch (PluginException $e) {
  81. $success = FALSE;
  82. watchdog_exception('aggregator', $e);
  83. }
  84. // Store instances in an array so we don't have to instantiate new objects.
  85. $processor_instances = [];
  86. foreach ($this->config->get('processors') as $processor) {
  87. try {
  88. $processor_instances[$processor] = $this->processorManager->createInstance($processor);
  89. }
  90. catch (PluginException $e) {
  91. watchdog_exception('aggregator', $e);
  92. }
  93. }
  94. // We store the hash of feed data in the database. When refreshing a
  95. // feed we compare stored hash and new hash calculated from downloaded
  96. // data. If both are equal we say that feed is not updated.
  97. $hash = hash('sha256', $feed->source_string);
  98. $has_new_content = $success && ($feed->getHash() != $hash);
  99. if ($has_new_content) {
  100. // Parse the feed.
  101. try {
  102. if ($this->parserManager->createInstance($this->config->get('parser'))->parse($feed)) {
  103. if (!$feed->getWebsiteUrl()) {
  104. $feed->setWebsiteUrl($feed->getUrl());
  105. }
  106. $feed->setHash($hash);
  107. // Update feed with parsed data.
  108. $feed->save();
  109. // Log if feed URL has changed.
  110. if ($feed->getUrl() != $feed_url) {
  111. $this->logger->notice('Updated URL for feed %title to %url.', ['%title' => $feed->label(), '%url' => $feed->getUrl()]);
  112. }
  113. $this->logger->notice('There is new syndicated content from %site.', ['%site' => $feed->label()]);
  114. // If there are items on the feed, let enabled processors process them.
  115. if (!empty($feed->items)) {
  116. foreach ($processor_instances as $instance) {
  117. $instance->process($feed);
  118. }
  119. }
  120. }
  121. }
  122. catch (PluginException $e) {
  123. watchdog_exception('aggregator', $e);
  124. }
  125. }
  126. // Processing is done, call postProcess on enabled processors.
  127. foreach ($processor_instances as $instance) {
  128. $instance->postProcess($feed);
  129. }
  130. return $has_new_content;
  131. }
  132. }