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