announcements_feed.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @file
  4. * Announcements feed helper functions.
  5. */
  6. /**
  7. * Returns the list of announcements.
  8. *
  9. * @return array
  10. * A build array with announcements.
  11. */
  12. function announcements_feed_get_announcements() {
  13. drupal_set_title(t('Community announcements'));
  14. drupal_add_css(drupal_get_path('module', 'announcements_feed')
  15. . '/announcements_feed.css', array(
  16. 'group' => CSS_DEFAULT,
  17. 'every_page' => TRUE,
  18. ));
  19. try {
  20. $announcements = announcements_feed_get_all_announcements();
  21. }
  22. catch (Exception $e) {
  23. drupal_set_message(t('An error occurred while parsing the announcements feed, check the logs for more information.'), 'error');
  24. return array();
  25. }
  26. $build = array();
  27. foreach ($announcements as $announcement) {
  28. $key = $announcement['featured'] ? '#featured' : '#standard';
  29. $build[$key][] = $announcement;
  30. }
  31. $build = array_merge($build, array(
  32. '#theme' => 'announcements_feed',
  33. '#count' => count($announcements),
  34. '#feed_link' => variable_get('announcements_feed_link', ANNOUNCEMENTS_FEED_DEFAULT_LINK),
  35. ));
  36. return $build;
  37. }
  38. /**
  39. * Generate an array of announcements with keys.
  40. *
  41. * @return array
  42. * An array of announcements.
  43. */
  44. function announcements_feed_get_all_announcements() {
  45. $announcements = announcements_feed_fetch();
  46. $announcements_feed = array();
  47. foreach ($announcements as $announcement) {
  48. $announcements_feed[] = array(
  49. 'id' => $announcement['id'],
  50. 'title' => $announcement['title'],
  51. 'link' => $announcement['url'],
  52. 'date_modified' => $announcement['date_modified'],
  53. 'date_published' => $announcement['date_published'],
  54. 'teaser' => $announcement['content_html'],
  55. 'version' => $announcement['_drupalorg']['version'],
  56. 'featured' => (bool) $announcement['_drupalorg']['featured'],
  57. );
  58. }
  59. return $announcements_feed;
  60. }
  61. /**
  62. * Fetches the feed either from a local cache or fresh remotely.
  63. *
  64. * The feed follows the "JSON Feed" format:
  65. * - https://www.jsonfeed.org/version/1.1/
  66. *
  67. * The structure of an announcement item in the feed is:
  68. * - id: Id.
  69. * - title: Title of the announcement.
  70. * - content_html: Announcement teaser.
  71. * - url: URL
  72. * - date_modified: Last updated timestamp.
  73. * - date_published: Created timestamp.
  74. * - _drupalorg.featured: 1 if featured, 0 if not featured.
  75. * - _drupalorg.version: Target version of Drupal, as a Composer version.
  76. *
  77. * @param bool $force
  78. * (optional) Whether to always fetch new items or not. Defaults to FALSE.
  79. *
  80. * @return array
  81. * An array of announcements from the feed relevant to the Drupal version.
  82. * The array is empty if there were no matching announcements. If an error
  83. * occurred while fetching/decoding the feed, it is thrown as an exception.
  84. *
  85. * @throws Exception
  86. */
  87. function announcements_feed_fetch($force = FALSE) {
  88. $announcements = cache_get('announcements_feed');
  89. if ($force || empty($announcements)) {
  90. $announcements_feed_json_url = variable_get('announcements_feed_json_url', ANNOUNCEMENTS_FEED_DEFAULT_JSON_URL);
  91. $response = drupal_http_request($announcements_feed_json_url);
  92. if ($response->code == 200) {
  93. $feeds = json_decode($response->data, TRUE);
  94. if (!isset($feeds['items'])) {
  95. watchdog('announcements_feed', 'The feed format is not valid.', NULL, WATCHDOG_ERROR);
  96. throw new Exception('Announcements feed JSON format is invalid');
  97. }
  98. $announcements = array();
  99. if ($feeds['items']) {
  100. $announcements = $feeds['items'];
  101. }
  102. $announcements = array_filter($announcements, 'announcements_feed_filter_announcements');
  103. cache_set('announcements_feed', $announcements, 'cache', REQUEST_TIME + variable_get('announcements_feed_max_age', ANNOUNCEMENTS_FEED_DEFAULT_MAX_AGE));
  104. }
  105. else {
  106. watchdog(
  107. 'announcements_feed',
  108. 'The feed failed to fetch with an error code: @code, error message: @message.',
  109. array('@code' => $response->code, '@message' => $response->error),
  110. WATCHDOG_ERROR
  111. );
  112. throw new Exception($response->error, $response->code);
  113. }
  114. }
  115. else {
  116. $announcements = $announcements->data;
  117. }
  118. // The drupal.org endpoint is sorted by created date in descending order.
  119. // We will limit the announcements based on the configuration limit.
  120. $announcements_feed_limit = variable_get('announcements_feed_limit', ANNOUNCEMENTS_FEED_DEFAULT_LIMIT);
  121. $announcements = array_slice($announcements, 0, $announcements_feed_limit);
  122. // For the remaining announcements, put all the featured announcements
  123. // before the rest.
  124. uasort($announcements, 'announcements_feed_sort_featured');
  125. return $announcements;
  126. }
  127. /**
  128. * Sort the elements of announcements_feed by values in comparison function.
  129. */
  130. function announcements_feed_sort_featured($a, $b) {
  131. $a_value = (int) $a['_drupalorg']['featured'];
  132. $b_value = (int) $b['_drupalorg']['featured'];
  133. if ($a_value == $b_value) {
  134. return 0;
  135. }
  136. return ($a_value < $b_value) ? -1 : 1;
  137. }
  138. /**
  139. * Filter the announcements relevant to the Drupal version used with valid URL controlled by drupal.org.
  140. *
  141. * @param array $announcement
  142. * Announcement feed array item to check.
  143. *
  144. * @return bool
  145. * Return TRUE if $announcement is relevant and the URL is valid.
  146. */
  147. function announcements_feed_filter_announcements($announcement) {
  148. $announcement_url = '';
  149. $announcement_version = '';
  150. if (!empty($announcement['url'])) {
  151. $announcement_url = $announcement['url'];
  152. }
  153. if (!empty($announcement['_drupalorg']['version'])) {
  154. $announcement_version = $announcement['_drupalorg']['version'];
  155. }
  156. return announcements_feed_validate_url($announcement_url) && announcements_feed_is_relevant_item($announcement_version);
  157. }
  158. /**
  159. * Check whether the version given is relevant to the Drupal version used.
  160. *
  161. * @param string $version
  162. * Version to check.
  163. *
  164. * @return bool
  165. * Return TRUE if the version matches Drupal version.
  166. */
  167. function announcements_feed_is_relevant_item($version) {
  168. if ($version == '*') {
  169. return TRUE;
  170. }
  171. // Split the version if received in || formats.
  172. $version_patterns = '/\|\|/';
  173. $all_versions = preg_split($version_patterns, $version);
  174. // The operation is optional and defaults to equals.
  175. $p_op = '(?P<operation>!=|\^|==|=|<|<=|>|>=|<>)?';
  176. $operations = '=';
  177. // Extracts major version from version string like 7, 8, 9.
  178. $p_major = '(?P<major>\d+)';
  179. // Extracts minor version from version string.
  180. $p_minor = '(?P<minor>(?:\d+|x)(?:-[A-Za-z]+\d+)?)';
  181. foreach ($all_versions as $version) {
  182. if (preg_match("/^\s*$p_op\s*$p_major(\.$p_minor)?/", $version, $matches)) {
  183. $feed_version = $matches['major'];
  184. if (!empty($matches['minor'])) {
  185. $feed_version = $matches['major'] . '.' . $matches['minor'];
  186. }
  187. if (!empty($matches['operation'])) {
  188. $operations = $matches['operation'];
  189. if ($operations == '^') {
  190. $operations = '>=';
  191. }
  192. }
  193. if (isset($operations) && version_compare(VERSION, $feed_version, $operations)) {
  194. return TRUE;
  195. }
  196. }
  197. }
  198. return FALSE;
  199. }
  200. /**
  201. * Check whether a link is controlled by drupal.org.
  202. *
  203. * @param string $url
  204. * URL to check.
  205. *
  206. * @return bool
  207. * Return TRUE if the URL is controlled by drupal.org.
  208. */
  209. function announcements_feed_validate_url($url) {
  210. if (empty($url)) {
  211. return FALSE;
  212. }
  213. $host = parse_url($url, PHP_URL_HOST);
  214. // First character can only be a letter or a digit.
  215. // @see https://www.rfc-editor.org/rfc/rfc1123#page-13
  216. return $host && preg_match('/^([a-zA-Z0-9][a-zA-Z0-9\-_]*\.)?drupal\.org$/', $host);
  217. }