update.install 5.97 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * Install, update, and uninstall functions for the Update Manager module.
6 7
 */

8 9 10 11
/**
 * Implements hook_requirements().
 *
 * @return
12 13 14 15 16 17 18 19 20 21 22 23
 *   An array describing the status of the site regarding available updates. If
 *   there is no update data, only one record will be returned, indicating that
 *   the status of core can't be determined. If data is available, there will be
 *   two records: one for core, and another for all of contrib (assuming there
 *   are any contributed modules or themes enabled on the site). In addition to
 *   the fields expected by hook_requirements ('value', 'severity', and
 *   optionally 'description'), this array will contain a 'reason' attribute,
 *   which is an integer constant to indicate why the given status is being
 *   returned (UPDATE_NOT_SECURE, UPDATE_NOT_CURRENT, or UPDATE_UNKNOWN). This
 *   is used for generating the appropriate e-mail notification messages during
 *   update_cron(), and might be useful for other modules that invoke
 *   update_requirements() to find out if the site is up to date or not.
24 25 26 27 28
 *
 * @see _update_message_text()
 * @see _update_cron_notify()
 */
function update_requirements($phase) {
29
  $requirements = array();
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  if ($phase == 'runtime') {
    if ($available = update_get_available(FALSE)) {
      module_load_include('inc', 'update', 'update.compare');
      $data = update_calculate_project_data($available);
      // First, populate the requirements for core:
      $requirements['update_core'] = _update_requirement_check($data['drupal'], 'core');
      // We don't want to check drupal a second time.
      unset($data['drupal']);
      if (!empty($data)) {
        // Now, sort our $data array based on each project's status. The
        // status constants are numbered in the right order of precedence, so
        // we just need to make sure the projects are sorted in ascending
        // order of status, and we can look at the first project we find.
        uasort($data, '_update_project_status_sort');
        $first_project = reset($data);
        $requirements['update_contrib'] = _update_requirement_check($first_project, 'contrib');
      }
    }
    else {
      $requirements['update_core']['title'] = t('Drupal core update status');
      $requirements['update_core']['value'] = t('No update data available');
      $requirements['update_core']['severity'] = REQUIREMENT_WARNING;
      $requirements['update_core']['reason'] = UPDATE_UNKNOWN;
      $requirements['update_core']['description'] = _update_no_data();
    }
  }
56
  return $requirements;
57 58
}

59
/**
60
 * Implements hook_install().
61 62
 */
function update_install() {
63
  $queue = \Drupal::queue('update_fetch_tasks', TRUE);
64 65 66
  $queue->createQueue();
}

67
/**
68
 * Implements hook_uninstall().
69 70
 */
function update_uninstall() {
71 72
  \Drupal::state()->delete('update.last_check');
  \Drupal::state()->delete('update.last_email_notification');
73

74
  $queue = \Drupal::queue('update_fetch_tasks');
75
  $queue->deleteQueue();
76
}
77 78

/**
79
 * Fills in the requirements array.
80 81 82 83 84
 *
 * This is shared for both core and contrib to generate the right elements in
 * the array for hook_requirements().
 *
 * @param $project
85 86
 *   Array of information about the project we're testing as returned by
 *   update_calculate_project_data().
87
 * @param $type
88
 *   What kind of project this is ('core' or 'contrib').
89 90
 *
 * @return
91
 *   An array to be included in the nested $requirements array.
92 93 94 95
 *
 * @see hook_requirements()
 * @see update_requirements()
 * @see update_calculate_project_data()
96
 */
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
function _update_requirement_check($project, $type) {
  $requirement = array();
  if ($type == 'core') {
    $requirement['title'] = t('Drupal core update status');
  }
  else {
    $requirement['title'] = t('Module and theme update status');
  }
  $status = $project['status'];
  if ($status != UPDATE_CURRENT) {
    $requirement['reason'] = $status;
    $requirement['description'] = _update_message_text($type, $status, TRUE);
    $requirement['severity'] = REQUIREMENT_ERROR;
  }
  switch ($status) {
    case UPDATE_NOT_SECURE:
      $requirement_label = t('Not secure!');
      break;
    case UPDATE_REVOKED:
      $requirement_label = t('Revoked!');
      break;
    case UPDATE_NOT_SUPPORTED:
      $requirement_label = t('Unsupported release');
      break;
    case UPDATE_NOT_CURRENT:
      $requirement_label = t('Out of date');
      $requirement['severity'] = REQUIREMENT_WARNING;
      break;
    case UPDATE_UNKNOWN:
    case UPDATE_NOT_CHECKED:
    case UPDATE_NOT_FETCHED:
      $requirement_label = isset($project['reason']) ? $project['reason'] : t('Can not determine status');
      $requirement['severity'] = REQUIREMENT_WARNING;
      break;
    default:
      $requirement_label = t('Up to date');
  }
  if ($status != UPDATE_CURRENT && $type == 'core' && isset($project['recommended'])) {
    $requirement_label .= ' ' . t('(version @version available)', array('@version' => $project['recommended']));
  }
  $requirement['value'] = l($requirement_label, update_manager_access() ? 'admin/reports/updates/update' : 'admin/reports/updates');
  return $requirement;
139
}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

/**
 * Moves update settings from variables to config.
 */
function update_update_8000() {
  update_variables_to_config('update.settings', array(
    'update_check_disabled' => 'check.disabled_extensions',
    'update_check_frequency' => 'check.interval_days',
    'update_fetch_url' => 'fetch.url',
    'update_max_fetch_attempts' => 'fetch.max_attempts',
    'update_max_fetch_time' => 'fetch.timeout',
    'update_notify_emails' => 'notification.emails',
    'update_notification_threshold' => 'notification.threshold',
  ));
}
155 156 157 158 159 160 161 162 163 164 165 166 167

/**
 * Convert update_last_check, last_email_notification variables to state API.
 *
 * @ingroup config_upgrade
 */
function update_update_8001() {
  $variable_map = array(
    'update_last_check' => 'update.last_check',
    'update_last_email_notification' => 'update.last_email_notification'
  );
  update_variables_to_state($variable_map);
}
168 169 170 171 172 173 174

/**
 * Deletes the {cache_update} table.
 */
function update_update_8002() {
  db_drop_table('cache_update');
}