Backup of db before drupal security update
[yaffs-website] / web / core / modules / aggregator / aggregator.module
1 <?php
2
3 /**
4  * @file
5  * Used to aggregate syndicated content (RSS, RDF, and Atom).
6  */
7
8 use Drupal\aggregator\Entity\Feed;
9 use Drupal\Core\Routing\RouteMatchInterface;
10
11 /**
12  * Denotes that a feed's items should never expire.
13  *
14  * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
15  *   Use \Drupal\aggregator\FeedStorageInterface::CLEAR_NEVER instead.
16  */
17 const AGGREGATOR_CLEAR_NEVER = 0;
18
19 /**
20  * Implements hook_help().
21  */
22 function aggregator_help($route_name, RouteMatchInterface $route_match) {
23   switch ($route_name) {
24     case 'help.page.aggregator':
25       $path_validator = \Drupal::pathValidator();
26       $output = '';
27       $output .= '<h3>' . t('About') . '</h3>';
28       $output .= '<p>' . t('The Aggregator module is an on-site syndicator and news reader that gathers and displays fresh content from RSS-, RDF-, and Atom-based feeds made available across the web. Thousands of sites (particularly news sites and blogs) publish their latest headlines in feeds, using a number of standardized XML-based formats. For more information, see the <a href=":aggregator-module">online documentation for the Aggregator module</a>.', [':aggregator-module' => 'https://www.drupal.org/documentation/modules/aggregator']) . '</p>';
29       $output .= '<h3>' . t('Uses') . '</h3>';
30       $output .= '<dl>';
31       // Check if the aggregator sources View is enabled.
32       if ($url = $path_validator->getUrlIfValid('aggregator/sources')) {
33         $output .= '<dt>' . t('Viewing feeds') . '</dt>';
34         $output .= '<dd>' . t('Users view feed content in the <a href=":aggregator">main aggregator display</a>, or by <a href=":aggregator-sources">their source</a> (usually via an RSS feed reader). The most recent content in a feed can be displayed as a block through the <a href=":admin-block">Blocks administration page</a>.', [':aggregator' => \Drupal::url('aggregator.page_last'), ':aggregator-sources' => $url->toString(), ':admin-block' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#']) . '</dd>';
35       }
36       $output .= '<dt>' . t('Adding, editing, and deleting feeds') . '</dt>';
37       $output .= '<dd>' . t('Administrators can add, edit, and delete feeds, and choose how often to check each feed for newly updated items on the <a href=":feededit">Aggregator administration page</a>.', [':feededit' => \Drupal::url('aggregator.admin_overview')]) . '</dd>';
38       $output .= '<dt>' . t('Configuring the display of feed items') . '</dt>';
39       $output .= '<dd>' . t('Administrators can choose how many items are displayed in the listing pages, which HTML tags are allowed in the content of feed items, and whether they should be trimmed to a maximum number of characters on the <a href=":settings">Aggregator settings page</a>.', [':settings' => \Drupal::url('aggregator.admin_settings')]) . '</dd>';
40       $output .= '<dt>' . t('Discarding old feed items') . '</dt>';
41       $output .= '<dd>' . t('Administrators can choose whether to discard feed items that are older than a specified period of time on the <a href=":settings">Aggregator settings page</a>. This requires a correctly configured cron maintenance task (see below).', [':settings' => \Drupal::url('aggregator.admin_settings')]) . '<dd>';
42
43       $output .= '<dt>' . t('<abbr title="Outline Processor Markup Language">OPML</abbr> integration') . '</dt>';
44       // Check if the aggregator opml View is enabled.
45       if ($url = $path_validator->getUrlIfValid('aggregator/opml')) {
46         $output .= '<dd>' . t('A <a href=":aggregator-opml">machine-readable OPML file</a> of all feeds is available. OPML is an XML-based file format used to share outline-structured information such as a list of RSS feeds. Feeds can also be <a href=":import-opml">imported via an OPML file</a>.', [':aggregator-opml' => $url->toString(), ':import-opml' => \Drupal::url('aggregator.opml_add')]) . '</dd>';
47       }
48       $output .= '<dt>' . t('Configuring cron') . '</dt>';
49       $output .= '<dd>' . t('A working <a href=":cron">cron maintenance task</a> is required to update feeds automatically.', [':cron' => \Drupal::url('system.cron_settings')]) . '</dd>';
50       $output .= '</dl>';
51       return $output;
52
53     case 'aggregator.admin_overview':
54       // Don't use placeholders for possibility to change URLs for translators.
55       $output = '<p>' . t('Many sites publish their headlines and posts in feeds, using a number of standardized XML-based formats. The aggregator supports <a href="http://en.wikipedia.org/wiki/Rss">RSS</a>, <a href="http://en.wikipedia.org/wiki/Resource_Description_Framework">RDF</a>, and <a href="http://en.wikipedia.org/wiki/Atom_%28standard%29">Atom</a>.') . '</p>';
56       $output .= '<p>' . t('Current feeds are listed below, and <a href=":addfeed">new feeds may be added</a>. For each feed, the <em>latest items</em> block may be enabled at the <a href=":block">blocks administration page</a>.', [':addfeed' => \Drupal::url('aggregator.feed_add'), ':block' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#']) . '</p>';
57       return $output;
58
59     case 'aggregator.feed_add':
60       return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may only have one entry.') . '</p>';
61
62     case 'aggregator.opml_add':
63       return '<p>' . t('<abbr title="Outline Processor Markup Language">OPML</abbr> is an XML format for exchanging feeds between aggregators. A single OPML document may contain many feeds. Aggregator uses this file to import all feeds at once. Upload a file from your computer or enter a URL where the OPML file can be downloaded.') . '</p>';
64   }
65 }
66
67 /**
68  * Implements hook_theme().
69  */
70 function aggregator_theme() {
71   return [
72     'aggregator_feed' => [
73       'render element' => 'elements',
74       'file' => 'aggregator.theme.inc',
75     ],
76     'aggregator_item' => [
77       'render element' => 'elements',
78       'file' => 'aggregator.theme.inc',
79     ],
80   ];
81 }
82
83 /**
84  * Implements hook_entity_extra_field_info().
85  */
86 function aggregator_entity_extra_field_info() {
87   $extra = [];
88
89   $extra['aggregator_feed']['aggregator_feed'] = [
90     'display' => [
91       'items' => [
92         'label' => t('Items'),
93         'description' => t('Items associated with this feed'),
94         'weight' => 0,
95       ],
96       // @todo Move to a formatter at https://www.drupal.org/node/2339917.
97       'image' => [
98         'label' => t('Image'),
99         'description' => t('The feed image'),
100         'weight' => 2,
101       ],
102       // @todo Move to a formatter at https://www.drupal.org/node/2149845.
103       'description' => [
104         'label' => t('Description'),
105         'description' => t('The description of this feed'),
106         'weight' => 3,
107       ],
108       'more_link' => [
109         'label' => t('More link'),
110         'description' => t('A more link to the feed detail page'),
111         'weight' => 5,
112       ],
113       'feed_icon' => [
114         'label' => t('Feed icon'),
115         'description' => t('An icon that links to the feed URL'),
116         'weight' => 6,
117       ],
118     ],
119   ];
120
121   $extra['aggregator_item']['aggregator_item'] = [
122     'display' => [
123       // @todo Move to a formatter at https://www.drupal.org/node/2149845.
124       'description' => [
125         'label' => t('Description'),
126         'description' => t('The description of this feed item'),
127         'weight' => 2,
128       ],
129     ],
130   ];
131
132   return $extra;
133 }
134
135 /**
136  * Implements hook_cron().
137  *
138  * Queues news feeds for updates once their refresh interval has elapsed.
139  */
140 function aggregator_cron() {
141   $queue = \Drupal::queue('aggregator_feeds');
142
143   $ids = \Drupal::entityManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
144   foreach (Feed::loadMultiple($ids) as $feed) {
145     if ($queue->createItem($feed)) {
146       // Add timestamp to avoid queueing item more than once.
147       $feed->setQueuedTime(REQUEST_TIME);
148       $feed->save();
149     }
150   }
151
152   // Delete queued timestamp after 6 hours assuming the update has failed.
153   $ids = \Drupal::entityQuery('aggregator_feed')
154     ->condition('queued', REQUEST_TIME - (3600 * 6), '<')
155     ->execute();
156
157   if ($ids) {
158     $feeds = Feed::loadMultiple($ids);
159     foreach ($feeds as $feed) {
160       $feed->setQueuedTime(0);
161       $feed->save();
162     }
163   }
164 }
165
166 /**
167  * Gets the list of allowed tags.
168  *
169  * @return array
170  *   The list of allowed tags.
171  *
172  * @internal
173  */
174 function _aggregator_allowed_tags() {
175   return preg_split('/\s+|<|>/', \Drupal::config('aggregator.settings')->get('items.allowed_html'), -1, PREG_SPLIT_NO_EMPTY);
176 }
177
178 /**
179  * Implements hook_preprocess_HOOK() for block templates.
180  */
181 function aggregator_preprocess_block(&$variables) {
182   if ($variables['configuration']['provider'] == 'aggregator') {
183     $variables['attributes']['role'] = 'complementary';
184   }
185 }