update.fetch.inc 5.32 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
 * Clears out all the available update data and initiates re-fetching.
37 38
 *
 * @see \Drupal\update\UpdateManager::refreshUpdateData()
39 40 41
 *
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal::service('update.manager')->refreshUpdateData().
42 43
 */
function _update_refresh() {
44
  \Drupal::service('update.manager')->refreshUpdateData();
45
}
46

47
/**
48
 * Adds a task to the queue for fetching release history data for a project.
49 50
 *
 * We only create a new fetch task if there's no task already in the queue for
51
 * this particular project (based on 'update_fetch_task' key-value collection).
52 53 54
 *
 * @param $project
 *   Associative array of information about a project as created by
55
 *   update_get_projects(), including keys such as 'name' (short name), and the
56
 *   'info' array with data from a .info.yml file for the project.
57
 *
58
 * @see \Drupal\update\UpdateFetcher::createFetchTask()
59 60 61
 *
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal::service('update.processor')->createFetchTask().
62 63
 */
function _update_create_fetch_task($project) {
64
  \Drupal::service('update.processor')->createFetchTask($project);
65 66 67
}

/**
68
 * Performs any notifications that should be done once cron fetches new data.
69
 *
70
 * This method checks the status of the site using the new data and, depending
71
 * on the configuration of the site, notifies administrators via email if there
72 73 74 75 76
 * are new releases or missing security updates.
 *
 * @see update_requirements()
 */
function _update_cron_notify() {
77
  $update_config = \Drupal::config('update.settings');
78
  module_load_install('update');
79 80
  $status = update_requirements('runtime');
  $params = array();
81
  $notify_all = ($update_config->get('notification.threshold') == 'all');
82
  foreach (array('core', 'contrib') as $report_type) {
83
    $type = 'update_' . $report_type;
84
    if (isset($status[$type]['severity'])
85
        && ($status[$type]['severity'] == REQUIREMENT_ERROR || ($notify_all && $status[$type]['reason'] == UPDATE_NOT_CURRENT))) {
86 87 88 89
      $params[$report_type] = $status[$type]['reason'];
    }
  }
  if (!empty($params)) {
90
    $notify_list = $update_config->get('notification.emails');
91
    if (!empty($notify_list)) {
92
      $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->id;
93
      foreach ($notify_list as $target) {
94
        if ($target_user = user_load_by_mail($target)) {
95
          $target_langcode = $target_user->getPreferredLangcode();
96 97
        }
        else {
98
          $target_langcode = $default_langcode;
99
        }
100
        $message = drupal_mail('update', 'status_notify', $target, $target_langcode, $params);
101
        // Track when the last mail was successfully sent to avoid sending
102
        // too many emails.
103
        if ($message['result']) {
104
          \Drupal::state()->set('update.last_email_notification', REQUEST_TIME);
105
        }
106 107 108 109 110 111
      }
    }
  }
}

/**
112
 * Parses the XML of the Drupal release history info files.
113
 *
114 115
 * @param $raw_xml
 *   A raw XML string of available release data for a given project.
116 117
 *
 * @return
118 119
 *   Array of parsed data about releases for a given project, or NULL if there
 *   was an error parsing the string.
120
 */
121 122 123 124 125 126 127 128 129 130
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;
  }
131 132 133 134
  // If there is no valid project data, the XML is invalid, so return failure.
  if (!isset($xml->short_name)) {
    return;
  }
135
  $data = array();
136
  foreach ($xml as $k => $v) {
137
    $data[$k] = (string) $v;
138 139
  }
  $data['releases'] = array();
140 141
  if (isset($xml->releases)) {
    foreach ($xml->releases->children() as $release) {
142
      $version = (string) $release->version;
143 144
      $data['releases'][$version] = array();
      foreach ($release->children() as $k => $v) {
145
        $data['releases'][$version][$k] = (string) $v;
146 147 148 149
      }
      $data['releases'][$version]['terms'] = array();
      if ($release->terms) {
        foreach ($release->terms->children() as $term) {
150 151
          if (!isset($data['releases'][$version]['terms'][(string) $term->name])) {
            $data['releases'][$version]['terms'][(string) $term->name] = array();
152
          }
153
          $data['releases'][$version]['terms'][(string) $term->name][] = (string) $term->value;
154
        }
155 156 157
      }
    }
  }
158
  return $data;
159
}