Pull merge.
[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  * @see https://www.drupal.org/node/2831620
18  */
19 const AGGREGATOR_CLEAR_NEVER = 0;
20
21 /**
22  * Implements hook_help().
23  */
24 function aggregator_help($route_name, RouteMatchInterface $route_match) {
25   switch ($route_name) {
26     case 'help.page.aggregator':
27       $path_validator = \Drupal::pathValidator();
28       $output = '';
29       $output .= '<h3>' . t('About') . '</h3>';
30       $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>';
31       $output .= '<h3>' . t('Uses') . '</h3>';
32       $output .= '<dl>';
33       // Check if the aggregator sources View is enabled.
34       if ($url = $path_validator->getUrlIfValid('aggregator/sources')) {
35         $output .= '<dt>' . t('Viewing feeds') . '</dt>';
36         $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>';
37       }
38       $output .= '<dt>' . t('Adding, editing, and deleting feeds') . '</dt>';
39       $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>';
40       $output .= '<dt>' . t('Configuring the display of feed items') . '</dt>';
41       $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>';
42       $output .= '<dt>' . t('Discarding old feed items') . '</dt>';
43       $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>';
44
45       $output .= '<dt>' . t('<abbr title="Outline Processor Markup Language">OPML</abbr> integration') . '</dt>';
46       // Check if the aggregator opml View is enabled.
47       if ($url = $path_validator->getUrlIfValid('aggregator/opml')) {
48         $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>';
49       }
50       $output .= '<dt>' . t('Configuring cron') . '</dt>';
51       $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>';
52       $output .= '</dl>';
53       return $output;
54
55     case 'aggregator.admin_overview':
56       // Don't use placeholders for possibility to change URLs for translators.
57       $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>';
58       $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>';
59       return $output;
60
61     case 'aggregator.feed_add':
62       return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may only have one entry.') . '</p>';
63
64     case 'aggregator.opml_add':
65       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>';
66   }
67 }
68
69 /**
70  * Implements hook_theme().
71  */
72 function aggregator_theme() {
73   return [
74     'aggregator_feed' => [
75       'render element' => 'elements',
76       'file' => 'aggregator.theme.inc',
77     ],
78     'aggregator_item' => [
79       'render element' => 'elements',
80       'file' => 'aggregator.theme.inc',
81     ],
82   ];
83 }
84
85 /**
86  * Implements hook_entity_extra_field_info().
87  */
88 function aggregator_entity_extra_field_info() {
89   $extra = [];
90
91   $extra['aggregator_feed']['aggregator_feed'] = [
92     'display' => [
93       'items' => [
94         'label' => t('Items'),
95         'description' => t('Items associated with this feed'),
96         'weight' => 0,
97       ],
98       // @todo Move to a formatter at https://www.drupal.org/node/2339917.
99       'image' => [
100         'label' => t('Image'),
101         'description' => t('The feed image'),
102         'weight' => 2,
103       ],
104       // @todo Move to a formatter at https://www.drupal.org/node/2149845.
105       'description' => [
106         'label' => t('Description'),
107         'description' => t('The description of this feed'),
108         'weight' => 3,
109       ],
110       'more_link' => [
111         'label' => t('More link'),
112         'description' => t('A more link to the feed detail page'),
113         'weight' => 5,
114       ],
115       'feed_icon' => [
116         'label' => t('Feed icon'),
117         'description' => t('An icon that links to the feed URL'),
118         'weight' => 6,
119       ],
120     ],
121   ];
122
123   $extra['aggregator_item']['aggregator_item'] = [
124     'display' => [
125       // @todo Move to a formatter at https://www.drupal.org/node/2149845.
126       'description' => [
127         'label' => t('Description'),
128         'description' => t('The description of this feed item'),
129         'weight' => 2,
130       ],
131     ],
132   ];
133
134   return $extra;
135 }
136
137 /**
138  * Implements hook_cron().
139  *
140  * Queues news feeds for updates once their refresh interval has elapsed.
141  */
142 function aggregator_cron() {
143   $queue = \Drupal::queue('aggregator_feeds');
144
145   $ids = \Drupal::entityManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
146   foreach (Feed::loadMultiple($ids) as $feed) {
147     if ($queue->createItem($feed)) {
148       // Add timestamp to avoid queueing item more than once.
149       $feed->setQueuedTime(REQUEST_TIME);
150       $feed->save();
151     }
152   }
153
154   // Delete queued timestamp after 6 hours assuming the update has failed.
155   $ids = \Drupal::entityQuery('aggregator_feed')
156     ->condition('queued', REQUEST_TIME - (3600 * 6), '<')
157     ->execute();
158
159   if ($ids) {
160     $feeds = Feed::loadMultiple($ids);
161     foreach ($feeds as $feed) {
162       $feed->setQueuedTime(0);
163       $feed->save();
164     }
165   }
166 }
167
168 /**
169  * Gets the list of allowed tags.
170  *
171  * @return array
172  *   The list of allowed tags.
173  *
174  * @internal
175  */
176 function _aggregator_allowed_tags() {
177   return preg_split('/\s+|<|>/', \Drupal::config('aggregator.settings')->get('items.allowed_html'), -1, PREG_SPLIT_NO_EMPTY);
178 }
179
180 /**
181  * Implements hook_preprocess_HOOK() for block templates.
182  */
183 function aggregator_preprocess_block(&$variables) {
184   if ($variables['configuration']['provider'] == 'aggregator') {
185     $variables['attributes']['role'] = 'complementary';
186   }
187 }