123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- <?php
- function update_manual_status() {
- _update_refresh();
- $batch = array(
- 'operations' => array(
- array('update_fetch_data_batch', array()),
- ),
- 'finished' => 'update_fetch_data_finished',
- 'title' => t('Checking available update data'),
- 'progress_message' => t('Trying to check available update data ...'),
- 'error_message' => t('Error checking available update data.'),
- 'file' => drupal_get_path('module', 'update') . '/update.fetch.inc',
- );
- batch_set($batch);
- batch_process('admin/reports/updates');
- }
- function update_fetch_data_batch(&$context) {
- $queue = DrupalQueue::get('update_fetch_tasks');
- if (empty($context['sandbox']['max'])) {
- $context['finished'] = 0;
- $context['sandbox']['max'] = $queue->numberOfItems();
- $context['sandbox']['progress'] = 0;
- $context['message'] = t('Checking available update data ...');
- $context['results']['updated'] = 0;
- $context['results']['failures'] = 0;
- $context['results']['processed'] = 0;
- }
-
- for ($i = 0; $i < 5; $i++) {
- if ($item = $queue->claimItem()) {
- if (_update_process_fetch_task($item->data)) {
- $context['results']['updated']++;
- $context['message'] = t('Checked available update data for %title.', array('%title' => $item->data['info']['name']));
- }
- else {
- $context['message'] = t('Failed to check available update data for %title.', array('%title' => $item->data['info']['name']));
- $context['results']['failures']++;
- }
- $context['sandbox']['progress']++;
- $context['results']['processed']++;
- $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
- $queue->deleteItem($item);
- }
- else {
-
-
-
-
-
-
-
- $context['finished'] = 1;
- return;
- }
- }
- }
- function update_fetch_data_finished($success, $results) {
- if ($success) {
- if (!empty($results)) {
- if (!empty($results['updated'])) {
- drupal_set_message(format_plural($results['updated'], 'Checked available update data for one project.', 'Checked available update data for @count projects.'));
- }
- if (!empty($results['failures'])) {
- drupal_set_message(format_plural($results['failures'], 'Failed to get available update data for one project.', 'Failed to get available update data for @count projects.'), 'error');
- }
- }
- }
- else {
- drupal_set_message(t('An error occurred trying to get available update data.'), 'error');
- }
- }
- function _update_fetch_data() {
- $queue = DrupalQueue::get('update_fetch_tasks');
- $end = time() + variable_get('update_max_fetch_time', UPDATE_MAX_FETCH_TIME);
- while (time() < $end && ($item = $queue->claimItem())) {
- _update_process_fetch_task($item->data);
- $queue->deleteItem($item);
- }
- }
- function _update_process_fetch_task($project) {
- global $base_url;
- $fail = &drupal_static(__FUNCTION__, array());
-
-
- $now = time();
- if (empty($fail)) {
-
-
- if (($cache = _update_cache_get('fetch_failures')) && ($cache->expire > $now)) {
- $fail = $cache->data;
- }
- }
- $max_fetch_attempts = variable_get('update_max_fetch_attempts', UPDATE_MAX_FETCH_ATTEMPTS);
- $success = FALSE;
- $available = array();
- $site_key = drupal_hmac_base64($base_url, drupal_get_private_key());
- $url = _update_build_fetch_url($project, $site_key);
- $fetch_url_base = _update_get_fetch_url_base($project);
- $project_name = $project['name'];
- if (empty($fail[$fetch_url_base]) || $fail[$fetch_url_base] < $max_fetch_attempts) {
- $xml = drupal_http_request($url);
- if (!isset($xml->error) && isset($xml->data)) {
- $data = $xml->data;
- }
- }
- if (!empty($data)) {
- $available = update_parse_xml($data);
-
- if (!empty($available)) {
-
- $success = TRUE;
- }
- }
- else {
- $available['project_status'] = 'not-fetched';
- if (empty($fail[$fetch_url_base])) {
- $fail[$fetch_url_base] = 1;
- }
- else {
- $fail[$fetch_url_base]++;
- }
- }
- $frequency = variable_get('update_check_frequency', 1);
- $cid = 'available_releases::' . $project_name;
- _update_cache_set($cid, $available, $now + (60 * 60 * 24 * $frequency));
-
- _update_cache_set('fetch_failures', $fail, $now + (60 * 5));
-
- variable_set('update_last_check', $now);
-
-
- _update_cache_clear('fetch_task::' . $project_name);
- return $success;
- }
- function _update_refresh() {
- module_load_include('inc', 'update', 'update.compare');
-
-
-
-
-
-
- _update_cache_clear('update_project_projects');
- _update_cache_clear('update_project_data');
- $projects = update_get_projects();
-
-
-
- _update_cache_clear('available_releases::', TRUE);
- foreach ($projects as $key => $project) {
- update_create_fetch_task($project);
- }
- }
- function _update_create_fetch_task($project) {
- $fetch_tasks = &drupal_static(__FUNCTION__, array());
- if (empty($fetch_tasks)) {
- $fetch_tasks = _update_get_cache_multiple('fetch_task');
- }
- $cid = 'fetch_task::' . $project['name'];
- if (empty($fetch_tasks[$cid])) {
- $queue = DrupalQueue::get('update_fetch_tasks');
- $queue->createItem($project);
-
-
-
-
-
- try {
- db_insert('cache_update')
- ->fields(array(
- 'cid' => $cid,
- 'created' => REQUEST_TIME,
- ))
- ->execute();
- }
- catch (Exception $e) {
-
- }
- $fetch_tasks[$cid] = REQUEST_TIME;
- }
- }
- function _update_build_fetch_url($project, $site_key = '') {
- $name = $project['name'];
- $url = _update_get_fetch_url_base($project);
- $url .= '/' . $name . '/' . DRUPAL_CORE_COMPATIBILITY;
-
-
- if (!empty($site_key) && (strpos($project['project_type'], 'disabled') === FALSE)) {
-
- $url .= (strpos($url, '?') !== FALSE) ? '&' : '?';
- $url .= 'site_key=';
- $url .= rawurlencode($site_key);
-
- if (!empty($project['info']['version'])) {
- $url .= '&version=';
- $url .= rawurlencode($project['info']['version']);
- }
-
- $list = array_keys($project['includes']);
- $url .= '&list=';
- $url .= rawurlencode(implode(',', $list));
- }
- return $url;
- }
- function _update_get_fetch_url_base($project) {
- return isset($project['info']['project status url']) ? $project['info']['project status url'] : variable_get('update_fetch_url', UPDATE_DEFAULT_URL);
- }
- function _update_cron_notify() {
- module_load_install('update');
- $status = update_requirements('runtime');
- $params = array();
- $notify_all = (variable_get('update_notification_threshold', 'all') == 'all');
- foreach (array('core', 'contrib') as $report_type) {
- $type = 'update_' . $report_type;
- if (isset($status[$type]['severity'])
- && ($status[$type]['severity'] == REQUIREMENT_ERROR || ($notify_all && $status[$type]['reason'] == UPDATE_NOT_CURRENT))) {
- $params[$report_type] = $status[$type]['reason'];
- }
- }
- if (!empty($params)) {
- $notify_list = variable_get('update_notify_emails', '');
- if (!empty($notify_list)) {
- $default_language = language_default();
- foreach ($notify_list as $target) {
- if ($target_user = user_load_by_mail($target)) {
- $target_language = user_preferred_language($target_user);
- }
- else {
- $target_language = $default_language;
- }
- $message = drupal_mail('update', 'status_notify', $target, $target_language, $params);
-
-
- if ($message['result']) {
- variable_set('update_last_email_notification', REQUEST_TIME);
- }
- }
- }
- }
- }
- function update_parse_xml($raw_xml) {
- try {
- $xml = new SimpleXMLElement($raw_xml);
- }
- catch (Exception $e) {
-
-
-
- return;
- }
-
- if (!isset($xml->short_name)) {
- return;
- }
- $short_name = (string) $xml->short_name;
- $data = array();
- foreach ($xml as $k => $v) {
- $data[$k] = (string) $v;
- }
- $data['releases'] = array();
- if (isset($xml->releases)) {
- foreach ($xml->releases->children() as $release) {
- $version = (string) $release->version;
- $data['releases'][$version] = array();
- foreach ($release->children() as $k => $v) {
- $data['releases'][$version][$k] = (string) $v;
- }
- $data['releases'][$version]['terms'] = array();
- if ($release->terms) {
- foreach ($release->terms->children() as $term) {
- if (!isset($data['releases'][$version]['terms'][(string) $term->name])) {
- $data['releases'][$version]['terms'][(string) $term->name] = array();
- }
- $data['releases'][$version]['terms'][(string) $term->name][] = (string) $term->value;
- }
- }
- }
- }
- return $data;
- }
|