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