Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/aggregator/src/ItemsImporter.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\aggregator; | |
4 | |
5 use Drupal\aggregator\Plugin\AggregatorPluginManager; | |
6 use Drupal\Component\Plugin\Exception\PluginException; | |
7 use Drupal\Core\Config\ConfigFactoryInterface; | |
8 use Psr\Log\LoggerInterface; | |
9 | |
10 /** | |
11 * Defines an importer of aggregator items. | |
12 */ | |
13 class ItemsImporter implements ItemsImporterInterface { | |
14 | |
15 /** | |
16 * The aggregator fetcher manager. | |
17 * | |
18 * @var \Drupal\aggregator\Plugin\AggregatorPluginManager | |
19 */ | |
20 protected $fetcherManager; | |
21 | |
22 /** | |
23 * The aggregator processor manager. | |
24 * | |
25 * @var \Drupal\aggregator\Plugin\AggregatorPluginManager | |
26 */ | |
27 protected $processorManager; | |
28 | |
29 /** | |
30 * The aggregator parser manager. | |
31 * | |
32 * @var \Drupal\aggregator\Plugin\AggregatorPluginManager | |
33 */ | |
34 protected $parserManager; | |
35 | |
36 /** | |
37 * The aggregator.settings config object. | |
38 * | |
39 * @var \Drupal\Core\Config\Config | |
40 */ | |
41 protected $config; | |
42 | |
43 /** | |
44 * A logger instance. | |
45 * | |
46 * @var \Psr\Log\LoggerInterface | |
47 */ | |
48 protected $logger; | |
49 | |
50 /** | |
51 * Constructs an Importer object. | |
52 * | |
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 | |
62 * A logger instance. | |
63 */ | |
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; | |
70 } | |
71 | |
72 /** | |
73 * {@inheritdoc} | |
74 */ | |
75 public function delete(FeedInterface $feed) { | |
76 foreach ($this->processorManager->getDefinitions() as $id => $definition) { | |
77 $this->processorManager->createInstance($id)->delete($feed); | |
78 } | |
79 } | |
80 | |
81 /** | |
82 * {@inheritdoc} | |
83 */ | |
84 public function refresh(FeedInterface $feed) { | |
85 // Store feed URL to track changes. | |
86 $feed_url = $feed->getUrl(); | |
87 | |
88 // Fetch the feed. | |
89 try { | |
90 $success = $this->fetcherManager->createInstance($this->config->get('fetcher'))->fetch($feed); | |
91 } | |
92 catch (PluginException $e) { | |
93 $success = FALSE; | |
94 watchdog_exception('aggregator', $e); | |
95 } | |
96 | |
97 // Store instances in an array so we dont have to instantiate new objects. | |
98 $processor_instances = []; | |
99 foreach ($this->config->get('processors') as $processor) { | |
100 try { | |
101 $processor_instances[$processor] = $this->processorManager->createInstance($processor); | |
102 } | |
103 catch (PluginException $e) { | |
104 watchdog_exception('aggregator', $e); | |
105 } | |
106 } | |
107 | |
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); | |
113 | |
114 if ($has_new_content) { | |
115 // Parse the feed. | |
116 try { | |
117 if ($this->parserManager->createInstance($this->config->get('parser'))->parse($feed)) { | |
118 if (!$feed->getWebsiteUrl()) { | |
119 $feed->setWebsiteUrl($feed->getUrl()); | |
120 } | |
121 $feed->setHash($hash); | |
122 // Update feed with parsed data. | |
123 $feed->save(); | |
124 | |
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()]); | |
128 } | |
129 | |
130 $this->logger->notice('There is new syndicated content from %site.', ['%site' => $feed->label()]); | |
131 | |
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); | |
136 } | |
137 } | |
138 } | |
139 } | |
140 catch (PluginException $e) { | |
141 watchdog_exception('aggregator', $e); | |
142 } | |
143 } | |
144 | |
145 // Processing is done, call postProcess on enabled processors. | |
146 foreach ($processor_instances as $instance) { | |
147 $instance->postProcess($feed); | |
148 } | |
149 | |
150 return $has_new_content; | |
151 } | |
152 | |
153 } |