aggregator.module 8.6 KB

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