aggregator.module 8.6 KB

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