update.fetch.inc 5.99 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Code required only when fetching information about available updates.
 */

8
/**
9 10 11 12
 * Batch callback: Processes a step in batch for fetching available update data.
 *
 * @param $context
 *   Reference to an array used for Batch API storage.
13
 *
14
 * @see \Drupal\update\UpdateManager::fetchDataBatch()
15 16 17
 *
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal::service('update.manager')->fetchDataBatch().
18
 */
19 20
function update_fetch_data_batch(&$context) {
  \Drupal::service('update.manager')->fetchDataBatch($context);
21 22 23
}

/**
24
 * Attempts to drain the queue of tasks for release history data to fetch.
25 26
 *
 * @see \Drupal\update\UpdateFetcher::fetchData()
27 28 29
 *
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal::service('update.processor')->fetchData().
30
 */
31
function _update_fetch_data() {
32
  \Drupal::service('update.processor')->fetchData();
33 34 35
}

/**
36
 * Processes a task to fetch available update data for a single project.
37
 *
38 39
 * Once the release history XML data is downloaded, it is parsed and saved in an
 * entry just for that project.
40 41 42
 *
 * @param $project
 *   Associative array of information about the project to fetch data for.
43
 *
44 45
 * @return
 *   TRUE if we fetched parsable XML, otherwise FALSE.
46 47
 *
 * @see \Drupal\update\UpdateFetcher::processFetchTask()
48 49 50
 *
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal::service('update.processor')->processFetchTask().
51 52
 */
function _update_process_fetch_task($project) {
53
  return \Drupal::service('update.processor')->processFetchTask($project);
54 55 56
}

/**
57
 * Clears out all the available update data and initiates re-fetching.
58 59
 *
 * @see \Drupal\update\UpdateManager::refreshUpdateData()
60 61 62
 *
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal::service('update.manager')->refreshUpdateData().
63 64
 */
function _update_refresh() {
65
  \Drupal::service('update.manager')->refreshUpdateData();
66
}
67

68
/**
69
 * Adds a task to the queue for fetching release history data for a project.
70 71
 *
 * We only create a new fetch task if there's no task already in the queue for
72
 * this particular project (based on 'update_fetch_task' key-value collection).
73 74 75
 *
 * @param $project
 *   Associative array of information about a project as created by
76
 *   update_get_projects(), including keys such as 'name' (short name), and the
77
 *   'info' array with data from a .info.yml file for the project.
78
 *
79
 * @see \Drupal\update\UpdateFetcher::createFetchTask()
80 81 82
 *
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal::service('update.processor')->createFetchTask().
83 84
 */
function _update_create_fetch_task($project) {
85
  \Drupal::service('update.processor')->createFetchTask($project);
86 87 88
}

/**
89
 * Performs any notifications that should be done once cron fetches new data.
90
 *
91 92
 * This method checks the status of the site using the new data and, depending
 * on the configuration of the site, notifies administrators via e-mail if there
93 94 95 96 97
 * are new releases or missing security updates.
 *
 * @see update_requirements()
 */
function _update_cron_notify() {
98
  $update_config = \Drupal::config('update.settings');
99
  module_load_install('update');
100 101
  $status = update_requirements('runtime');
  $params = array();
102
  $notify_all = ($update_config->get('notification.threshold') == 'all');
103
  foreach (array('core', 'contrib') as $report_type) {
104
    $type = 'update_' . $report_type;
105
    if (isset($status[$type]['severity'])
106
        && ($status[$type]['severity'] == REQUIREMENT_ERROR || ($notify_all && $status[$type]['reason'] == UPDATE_NOT_CURRENT))) {
107 108 109 110
      $params[$report_type] = $status[$type]['reason'];
    }
  }
  if (!empty($params)) {
111
    $notify_list = $update_config->get('notification.emails');
112
    if (!empty($notify_list)) {
113
      $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->id;
114
      foreach ($notify_list as $target) {
115
        if ($target_user = user_load_by_mail($target)) {
116
          $target_langcode = $target_user->getPreferredLangcode();
117 118
        }
        else {
119
          $target_langcode = $default_langcode;
120
        }
121
        $message = drupal_mail('update', 'status_notify', $target, $target_langcode, $params);
122 123 124
        // Track when the last mail was successfully sent to avoid sending
        // too many e-mails.
        if ($message['result']) {
125
          \Drupal::state()->set('update.last_email_notification', REQUEST_TIME);
126
        }
127 128 129 130 131 132
      }
    }
  }
}

/**
133
 * Parses the XML of the Drupal release history info files.
134
 *
135 136
 * @param $raw_xml
 *   A raw XML string of available release data for a given project.
137 138
 *
 * @return
139 140
 *   Array of parsed data about releases for a given project, or NULL if there
 *   was an error parsing the string.
141
 */
142 143 144 145 146 147 148 149 150 151
function update_parse_xml($raw_xml) {
  try {
    $xml = new SimpleXMLElement($raw_xml);
  }
  catch (Exception $e) {
    // SimpleXMLElement::__construct produces an E_WARNING error message for
    // each error found in the XML data and throws an exception if errors
    // were detected. Catch any exception and return failure (NULL).
    return;
  }
152 153 154 155
  // If there is no valid project data, the XML is invalid, so return failure.
  if (!isset($xml->short_name)) {
    return;
  }
156
  $data = array();
157
  foreach ($xml as $k => $v) {
158
    $data[$k] = (string) $v;
159 160
  }
  $data['releases'] = array();
161 162
  if (isset($xml->releases)) {
    foreach ($xml->releases->children() as $release) {
163
      $version = (string) $release->version;
164 165
      $data['releases'][$version] = array();
      foreach ($release->children() as $k => $v) {
166
        $data['releases'][$version][$k] = (string) $v;
167 168 169 170
      }
      $data['releases'][$version]['terms'] = array();
      if ($release->terms) {
        foreach ($release->terms->children() as $term) {
171 172
          if (!isset($data['releases'][$version]['terms'][(string) $term->name])) {
            $data['releases'][$version]['terms'][(string) $term->name] = array();
173
          }
174
          $data['releases'][$version]['terms'][(string) $term->name][] = (string) $term->value;
175
        }
176 177 178
      }
    }
  }
179
  return $data;
180
}