Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / aggregator / src / Entity / Feed.php
1 <?php
2
3 namespace Drupal\aggregator\Entity;
4
5 use Drupal\Core\Entity\ContentEntityBase;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\Field\BaseFieldDefinition;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\aggregator\FeedInterface;
10
11 /**
12  * Defines the aggregator feed entity class.
13  *
14  * @ContentEntityType(
15  *   id = "aggregator_feed",
16  *   label = @Translation("Aggregator feed"),
17  *   handlers = {
18  *     "storage" = "Drupal\aggregator\FeedStorage",
19  *     "storage_schema" = "Drupal\aggregator\FeedStorageSchema",
20  *     "view_builder" = "Drupal\aggregator\FeedViewBuilder",
21  *     "access" = "Drupal\aggregator\FeedAccessControlHandler",
22  *     "views_data" = "Drupal\aggregator\AggregatorFeedViewsData",
23  *     "form" = {
24  *       "default" = "Drupal\aggregator\FeedForm",
25  *       "delete" = "Drupal\aggregator\Form\FeedDeleteForm",
26  *       "delete_items" = "Drupal\aggregator\Form\FeedItemsDeleteForm",
27  *     },
28  *     "route_provider" = {
29  *       "html" = "Drupal\aggregator\FeedHtmlRouteProvider",
30  *     },
31  *   },
32  *   links = {
33  *     "canonical" = "/aggregator/sources/{aggregator_feed}",
34  *     "edit-form" = "/aggregator/sources/{aggregator_feed}/configure",
35  *     "delete-form" = "/aggregator/sources/{aggregator_feed}/delete",
36  *   },
37  *   field_ui_base_route = "aggregator.admin_overview",
38  *   base_table = "aggregator_feed",
39  *   render_cache = FALSE,
40  *   entity_keys = {
41  *     "id" = "fid",
42  *     "label" = "title",
43  *     "langcode" = "langcode",
44  *     "uuid" = "uuid",
45  *   }
46  * )
47  */
48 class Feed extends ContentEntityBase implements FeedInterface {
49
50   /**
51    * {@inheritdoc}
52    */
53   public function label() {
54     return $this->get('title')->value;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function deleteItems() {
61     \Drupal::service('aggregator.items.importer')->delete($this);
62
63     // Reset feed.
64     $this->setLastCheckedTime(0);
65     $this->setHash('');
66     $this->setEtag('');
67     $this->setLastModified(0);
68     $this->save();
69
70     return $this;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function refreshItems() {
77     $success = \Drupal::service('aggregator.items.importer')->refresh($this);
78
79     // Regardless of successful or not, indicate that it has been checked.
80     $this->setLastCheckedTime(REQUEST_TIME);
81     $this->setQueuedTime(0);
82     $this->save();
83
84     return $success;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public static function preCreate(EntityStorageInterface $storage, array &$values) {
91     $values += [
92       'link' => '',
93       'description' => '',
94       'image' => '',
95     ];
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public static function preDelete(EntityStorageInterface $storage, array $entities) {
102     foreach ($entities as $entity) {
103       // Notify processors to delete stored items.
104       \Drupal::service('aggregator.items.importer')->delete($entity);
105     }
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public static function postDelete(EntityStorageInterface $storage, array $entities) {
112     parent::postDelete($storage, $entities);
113     if (\Drupal::moduleHandler()->moduleExists('block')) {
114       // Make sure there are no active blocks for these feeds.
115       $ids = \Drupal::entityQuery('block')
116         ->condition('plugin', 'aggregator_feed_block')
117         ->condition('settings.feed', array_keys($entities))
118         ->execute();
119       if ($ids) {
120         $block_storage = \Drupal::entityManager()->getStorage('block');
121         $block_storage->delete($block_storage->loadMultiple($ids));
122       }
123     }
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
130     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
131     $fields = parent::baseFieldDefinitions($entity_type);
132
133     $fields['fid']->setLabel(t('Feed ID'))
134       ->setDescription(t('The ID of the aggregator feed.'));
135
136     $fields['uuid']->setDescription(t('The aggregator feed UUID.'));
137
138     $fields['langcode']->setLabel(t('Language code'))
139       ->setDescription(t('The feed language code.'));
140
141     $fields['title'] = BaseFieldDefinition::create('string')
142       ->setLabel(t('Title'))
143       ->setDescription(t('The name of the feed (or the name of the website providing the feed).'))
144       ->setRequired(TRUE)
145       ->setSetting('max_length', 255)
146       ->setDisplayOptions('form', [
147         'type' => 'string_textfield',
148         'weight' => -5,
149       ])
150       ->setDisplayConfigurable('form', TRUE)
151       ->addConstraint('FeedTitle');
152
153     $fields['url'] = BaseFieldDefinition::create('uri')
154       ->setLabel(t('URL'))
155       ->setDescription(t('The fully-qualified URL of the feed.'))
156       ->setRequired(TRUE)
157       ->setDisplayOptions('form', [
158         'type' => 'uri',
159         'weight' => -3,
160       ])
161       ->setDisplayConfigurable('form', TRUE)
162       ->addConstraint('FeedUrl');
163
164     $intervals = [900, 1800, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200];
165     $period = array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($intervals, $intervals));
166     $period[AGGREGATOR_CLEAR_NEVER] = t('Never');
167
168     $fields['refresh'] = BaseFieldDefinition::create('list_integer')
169       ->setLabel(t('Update interval'))
170       ->setDescription(t('The length of time between feed updates. Requires a correctly configured cron maintenance task.'))
171       ->setDefaultValue(3600)
172       ->setSetting('unsigned', TRUE)
173       ->setRequired(TRUE)
174       ->setSetting('allowed_values', $period)
175       ->setDisplayOptions('form', [
176         'type' => 'options_select',
177         'weight' => -2,
178       ])
179       ->setDisplayConfigurable('form', TRUE);
180
181     $fields['checked'] = BaseFieldDefinition::create('timestamp')
182       ->setLabel(t('Checked'))
183       ->setDescription(t('Last time feed was checked for new items, as Unix timestamp.'))
184       ->setDefaultValue(0)
185       ->setDisplayOptions('view', [
186         'label' => 'inline',
187         'type' => 'timestamp_ago',
188         'weight' => 1,
189       ])
190       ->setDisplayConfigurable('view', TRUE);
191
192     $fields['queued'] = BaseFieldDefinition::create('timestamp')
193       ->setLabel(t('Queued'))
194       ->setDescription(t('Time when this feed was queued for refresh, 0 if not queued.'))
195       ->setDefaultValue(0);
196
197     $fields['link'] = BaseFieldDefinition::create('uri')
198       ->setLabel(t('URL'))
199       ->setDescription(t('The link of the feed.'))
200       ->setDisplayOptions('view', [
201         'label' => 'inline',
202         'weight' => 4,
203       ])
204       ->setDisplayConfigurable('view', TRUE);
205
206     $fields['description'] = BaseFieldDefinition::create('string_long')
207       ->setLabel(t('Description'))
208       ->setDescription(t("The parent website's description that comes from the @description element in the feed.", ['@description' => '<description>']));
209
210     $fields['image'] = BaseFieldDefinition::create('uri')
211       ->setLabel(t('Image'))
212       ->setDescription(t('An image representing the feed.'));
213
214     $fields['hash'] = BaseFieldDefinition::create('string')
215       ->setLabel(t('Hash'))
216       ->setSetting('is_ascii', TRUE)
217       ->setDescription(t('Calculated hash of the feed data, used for validating cache.'));
218
219     $fields['etag'] = BaseFieldDefinition::create('string')
220       ->setLabel(t('Etag'))
221       ->setDescription(t('Entity tag HTTP response header, used for validating cache.'));
222
223     // This is updated by the fetcher and not when the feed is saved, therefore
224     // it's a timestamp and not a changed field.
225     $fields['modified'] = BaseFieldDefinition::create('timestamp')
226       ->setLabel(t('Modified'))
227       ->setDescription(t('When the feed was last modified, as a Unix timestamp.'));
228
229     return $fields;
230   }
231
232   /**
233    * {@inheritdoc}
234    */
235   public function getUrl() {
236     return $this->get('url')->value;
237   }
238
239   /**
240    * {@inheritdoc}
241    */
242   public function getRefreshRate() {
243     return $this->get('refresh')->value;
244   }
245
246   /**
247    * {@inheritdoc}
248    */
249   public function getLastCheckedTime() {
250     return $this->get('checked')->value;
251   }
252
253   /**
254    * {@inheritdoc}
255    */
256   public function getQueuedTime() {
257     return $this->get('queued')->value;
258   }
259
260   /**
261    * {@inheritdoc}
262    */
263   public function getWebsiteUrl() {
264     return $this->get('link')->value;
265   }
266
267   /**
268    * {@inheritdoc}
269    */
270   public function getDescription() {
271     return $this->get('description')->value;
272   }
273
274   /**
275    * {@inheritdoc}
276    */
277   public function getImage() {
278     return $this->get('image')->value;
279   }
280
281   /**
282    * {@inheritdoc}
283    */
284   public function getHash() {
285     return $this->get('hash')->value;
286   }
287
288   /**
289    * {@inheritdoc}
290    */
291   public function getEtag() {
292     return $this->get('etag')->value;
293   }
294
295   /**
296    * {@inheritdoc}
297    */
298   public function getLastModified() {
299     return $this->get('modified')->value;
300   }
301
302   /**
303    * {@inheritdoc}
304    */
305   public function setTitle($title) {
306     $this->set('title', $title);
307     return $this;
308   }
309
310   /**
311    * {@inheritdoc}
312    */
313   public function setUrl($url) {
314     $this->set('url', $url);
315     return $this;
316   }
317
318   /**
319    * {@inheritdoc}
320    */
321   public function setRefreshRate($refresh) {
322     $this->set('refresh', $refresh);
323     return $this;
324   }
325
326   /**
327    * {@inheritdoc}
328    */
329   public function setLastCheckedTime($checked) {
330     $this->set('checked', $checked);
331     return $this;
332   }
333
334   /**
335    * {@inheritdoc}
336    */
337   public function setQueuedTime($queued) {
338     $this->set('queued', $queued);
339     return $this;
340   }
341
342   /**
343    * {@inheritdoc}
344    */
345   public function setWebsiteUrl($link) {
346     $this->set('link', $link);
347     return $this;
348   }
349
350   /**
351    * {@inheritdoc}
352    */
353   public function setDescription($description) {
354     $this->set('description', $description);
355     return $this;
356   }
357
358   /**
359    * {@inheritdoc}
360    */
361   public function setImage($image) {
362     $this->set('image', $image);
363     return $this;
364   }
365
366   /**
367    * {@inheritdoc}
368    */
369   public function setHash($hash) {
370     $this->set('hash', $hash);
371     return $this;
372   }
373
374   /**
375    * {@inheritdoc}
376    */
377   public function setEtag($etag) {
378     $this->set('etag', $etag);
379     return $this;
380   }
381
382   /**
383    * {@inheritdoc}
384    */
385   public function setLastModified($modified) {
386     $this->set('modified', $modified);
387     return $this;
388   }
389
390 }