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