aggregator.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * @file
  4. * Used to aggregate syndicated content (RSS, RDF, and Atom).
  5. */
  6. use Drupal\Core\Url;
  7. use Drupal\aggregator\Entity\Feed;
  8. use Drupal\Core\Routing\RouteMatchInterface;
  9. /**
  10. * Denotes that a feed's items should never expire.
  11. *
  12. * @deprecated in drupal:8.3.0 and is removed from drupal:9.0.0.
  13. * Use \Drupal\aggregator\FeedStorageInterface::CLEAR_NEVER instead.
  14. *
  15. * @see https://www.drupal.org/node/2831620
  16. */
  17. const AGGREGATOR_CLEAR_NEVER = 0;
  18. /**
  19. * Implements hook_help().
  20. */
  21. function aggregator_help($route_name, RouteMatchInterface $route_match) {
  22. switch ($route_name) {
  23. case 'help.page.aggregator':
  24. $path_validator = \Drupal::pathValidator();
  25. $output = '';
  26. $output .= '<h3>' . t('About') . '</h3>';
  27. $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>';
  28. $output .= '<h3>' . t('Uses') . '</h3>';
  29. $output .= '<dl>';
  30. // Check if the aggregator sources View is enabled.
  31. if ($url = $path_validator->getUrlIfValid('aggregator/sources')) {
  32. $output .= '<dt>' . t('Viewing feeds') . '</dt>';
  33. $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>';
  34. }
  35. $output .= '<dt>' . t('Adding, editing, and deleting feeds') . '</dt>';
  36. $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>';
  37. $output .= '<dt>' . t('Configuring the display of feed items') . '</dt>';
  38. $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>';
  39. $output .= '<dt>' . t('Discarding old feed items') . '</dt>';
  40. $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>';
  41. $output .= '<dt>' . t('<abbr title="Outline Processor Markup Language">OPML</abbr> integration') . '</dt>';
  42. // Check if the aggregator opml View is enabled.
  43. if ($url = $path_validator->getUrlIfValid('aggregator/opml')) {
  44. $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>';
  45. }
  46. $output .= '<dt>' . t('Configuring cron') . '</dt>';
  47. $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>';
  48. $output .= '</dl>';
  49. return $output;
  50. case 'aggregator.admin_overview':
  51. // Don't use placeholders for possibility to change URLs for translators.
  52. $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>';
  53. $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>';
  54. return $output;
  55. case 'aggregator.feed_add':
  56. return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may only have one entry.') . '</p>';
  57. case 'aggregator.opml_add':
  58. 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>';
  59. }
  60. }
  61. /**
  62. * Implements hook_theme().
  63. */
  64. function aggregator_theme() {
  65. return [
  66. 'aggregator_feed' => [
  67. 'render element' => 'elements',
  68. 'file' => 'aggregator.theme.inc',
  69. ],
  70. 'aggregator_item' => [
  71. 'render element' => 'elements',
  72. 'file' => 'aggregator.theme.inc',
  73. ],
  74. ];
  75. }
  76. /**
  77. * Implements hook_entity_extra_field_info().
  78. *
  79. * By default this function creates pseudo-fields that mask the description and
  80. * image base fields. These pseudo-fields are omitted if:
  81. * - a module makes the field's display configurable via the field UI by means
  82. * of BaseFieldDefinition::setDisplayConfigurable()
  83. * - AND the additional entity type property
  84. * 'enable_base_field_custom_preprocess_skipping' has been set using
  85. * hook_entity_type_build().
  86. */
  87. function aggregator_entity_extra_field_info() {
  88. $extra = [];
  89. $entity_type_manager = \Drupal::entityTypeManager();
  90. $entity_field_manager = \Drupal::service('entity_field.manager');
  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. 'more_link' => [
  99. 'label' => t('More link'),
  100. 'description' => t('A more link to the feed detail page'),
  101. 'weight' => 5,
  102. ],
  103. 'feed_icon' => [
  104. 'label' => t('Feed icon'),
  105. 'description' => t('An icon that links to the feed URL'),
  106. 'weight' => 6,
  107. ],
  108. ],
  109. ];
  110. // Create Feed image and description pseudo-fields. Skip this if the field
  111. // display is configurable and skipping has been enabled.
  112. // @todo https://www.drupal.org/project/drupal/issues/3015623
  113. // Eventually delete this code and matching lines in FeedViewBuilder. Using
  114. // field formatters is more flexible and consistent.
  115. $skip_custom_preprocessing = $entity_type_manager->getDefinition('aggregator_feed')->get('enable_base_field_custom_preprocess_skipping');
  116. $base_field_definitions = $entity_field_manager->getBaseFieldDefinitions('aggregator_feed');
  117. if (!$skip_custom_preprocessing || !$base_field_definitions['image']->isDisplayConfigurable('view')) {
  118. $extra['aggregator_feed']['aggregator_feed']['display']['image'] = [
  119. 'label' => t('Image'),
  120. 'description' => t('The feed image'),
  121. 'weight' => 2,
  122. ];
  123. }
  124. if (!$skip_custom_preprocessing || !$base_field_definitions['description']->isDisplayConfigurable('view')) {
  125. $extra['aggregator_feed']['aggregator_feed']['display']['description'] = [
  126. 'label' => t('Description'),
  127. 'description' => t('The description of this feed'),
  128. 'weight' => 3,
  129. ];
  130. }
  131. // Create Item description pseudo-field. Skip this if the field display is
  132. // configurable and skipping has been enabled.
  133. // @todo https://www.drupal.org/project/drupal/issues/3015623
  134. // Eventually delete this code and matching lines in ItemViewBuilder. Using
  135. // field formatters is more flexible and consistent.
  136. $skip_custom_preprocessing = $entity_type_manager->getDefinition('aggregator_item')->get('enable_base_field_custom_preprocess_skipping');
  137. $base_field_definitions = $entity_field_manager->getBaseFieldDefinitions('aggregator_item');
  138. if (!$skip_custom_preprocessing || !$base_field_definitions['description']->isDisplayConfigurable('view')) {
  139. $extra['aggregator_item']['aggregator_item']['display']['description'] = [
  140. 'label' => t('Description'),
  141. 'description' => t('The description of this feed item'),
  142. 'weight' => 2,
  143. ];
  144. }
  145. return $extra;
  146. }
  147. /**
  148. * Implements hook_cron().
  149. *
  150. * Queues news feeds for updates once their refresh interval has elapsed.
  151. */
  152. function aggregator_cron() {
  153. $queue = \Drupal::queue('aggregator_feeds');
  154. $ids = \Drupal::entityTypeManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
  155. foreach (Feed::loadMultiple($ids) as $feed) {
  156. if ($queue->createItem($feed)) {
  157. // Add timestamp to avoid queueing item more than once.
  158. $feed->setQueuedTime(REQUEST_TIME);
  159. $feed->save();
  160. }
  161. }
  162. // Delete queued timestamp after 6 hours assuming the update has failed.
  163. $ids = \Drupal::entityQuery('aggregator_feed')
  164. ->condition('queued', REQUEST_TIME - (3600 * 6), '<')
  165. ->execute();
  166. if ($ids) {
  167. $feeds = Feed::loadMultiple($ids);
  168. foreach ($feeds as $feed) {
  169. $feed->setQueuedTime(0);
  170. $feed->save();
  171. }
  172. }
  173. }
  174. /**
  175. * Gets the list of allowed tags.
  176. *
  177. * @return array
  178. * The list of allowed tags.
  179. *
  180. * @internal
  181. */
  182. function _aggregator_allowed_tags() {
  183. return preg_split('/\s+|<|>/', \Drupal::config('aggregator.settings')->get('items.allowed_html'), -1, PREG_SPLIT_NO_EMPTY);
  184. }
  185. /**
  186. * Implements hook_preprocess_HOOK() for block templates.
  187. */
  188. function aggregator_preprocess_block(&$variables) {
  189. if ($variables['configuration']['provider'] == 'aggregator') {
  190. $variables['attributes']['role'] = 'complementary';
  191. }
  192. }