annotate core/modules/update/update.install @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Install, update, and uninstall functions for the Update Manager module.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Core\Url;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Implements hook_requirements().
Chris@0 12 *
Chris@0 13 * @return
Chris@0 14 * An array describing the status of the site regarding available updates. If
Chris@0 15 * there is no update data, only one record will be returned, indicating that
Chris@0 16 * the status of core can't be determined. If data is available, there will be
Chris@0 17 * two records: one for core, and another for all of contrib (assuming there
Chris@0 18 * are any contributed modules or themes enabled on the site). In addition to
Chris@0 19 * the fields expected by hook_requirements ('value', 'severity', and
Chris@0 20 * optionally 'description'), this array will contain a 'reason' attribute,
Chris@0 21 * which is an integer constant to indicate why the given status is being
Chris@0 22 * returned (UPDATE_NOT_SECURE, UPDATE_NOT_CURRENT, or UPDATE_UNKNOWN). This
Chris@0 23 * is used for generating the appropriate email notification messages during
Chris@0 24 * update_cron(), and might be useful for other modules that invoke
Chris@0 25 * update_requirements() to find out if the site is up to date or not.
Chris@0 26 *
Chris@0 27 * @see _update_message_text()
Chris@0 28 * @see _update_cron_notify()
Chris@0 29 */
Chris@0 30 function update_requirements($phase) {
Chris@0 31 $requirements = [];
Chris@0 32 if ($phase == 'runtime') {
Chris@0 33 if ($available = update_get_available(FALSE)) {
Chris@0 34 module_load_include('inc', 'update', 'update.compare');
Chris@0 35 $data = update_calculate_project_data($available);
Chris@0 36 // First, populate the requirements for core:
Chris@0 37 $requirements['update_core'] = _update_requirement_check($data['drupal'], 'core');
Chris@0 38 // We don't want to check drupal a second time.
Chris@0 39 unset($data['drupal']);
Chris@0 40 if (!empty($data)) {
Chris@0 41 // Now, sort our $data array based on each project's status. The
Chris@0 42 // status constants are numbered in the right order of precedence, so
Chris@0 43 // we just need to make sure the projects are sorted in ascending
Chris@0 44 // order of status, and we can look at the first project we find.
Chris@0 45 uasort($data, '_update_project_status_sort');
Chris@0 46 $first_project = reset($data);
Chris@0 47 $requirements['update_contrib'] = _update_requirement_check($first_project, 'contrib');
Chris@0 48 }
Chris@0 49 }
Chris@0 50 else {
Chris@0 51 $requirements['update_core']['title'] = t('Drupal core update status');
Chris@0 52 $requirements['update_core']['value'] = t('No update data available');
Chris@0 53 $requirements['update_core']['severity'] = REQUIREMENT_WARNING;
Chris@0 54 $requirements['update_core']['reason'] = UPDATE_UNKNOWN;
Chris@0 55 $requirements['update_core']['description'] = _update_no_data();
Chris@0 56 }
Chris@0 57 }
Chris@0 58 return $requirements;
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * Implements hook_install().
Chris@0 63 */
Chris@0 64 function update_install() {
Chris@0 65 $queue = \Drupal::queue('update_fetch_tasks', TRUE);
Chris@0 66 $queue->createQueue();
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Implements hook_uninstall().
Chris@0 71 */
Chris@0 72 function update_uninstall() {
Chris@0 73 \Drupal::state()->delete('update.last_check');
Chris@0 74 \Drupal::state()->delete('update.last_email_notification');
Chris@0 75
Chris@0 76 $queue = \Drupal::queue('update_fetch_tasks');
Chris@0 77 $queue->deleteQueue();
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * Fills in the requirements array.
Chris@0 82 *
Chris@0 83 * This is shared for both core and contrib to generate the right elements in
Chris@0 84 * the array for hook_requirements().
Chris@0 85 *
Chris@0 86 * @param $project
Chris@0 87 * Array of information about the project we're testing as returned by
Chris@0 88 * update_calculate_project_data().
Chris@0 89 * @param $type
Chris@0 90 * What kind of project this is ('core' or 'contrib').
Chris@0 91 *
Chris@0 92 * @return
Chris@0 93 * An array to be included in the nested $requirements array.
Chris@0 94 *
Chris@0 95 * @see hook_requirements()
Chris@0 96 * @see update_requirements()
Chris@0 97 * @see update_calculate_project_data()
Chris@0 98 */
Chris@0 99 function _update_requirement_check($project, $type) {
Chris@0 100 $requirement = [];
Chris@0 101 if ($type == 'core') {
Chris@0 102 $requirement['title'] = t('Drupal core update status');
Chris@0 103 }
Chris@0 104 else {
Chris@0 105 $requirement['title'] = t('Module and theme update status');
Chris@0 106 }
Chris@0 107 $status = $project['status'];
Chris@0 108 if ($status != UPDATE_CURRENT) {
Chris@0 109 $requirement['reason'] = $status;
Chris@0 110 $requirement['severity'] = REQUIREMENT_ERROR;
Chris@12 111 // When updates are available, append the available updates link to the
Chris@12 112 // message from _update_message_text(), and format the two translated
Chris@12 113 // strings together in a single paragraph.
Chris@0 114 $requirement['description'][] = ['#markup' => _update_message_text($type, $status)];
Chris@12 115 if (!in_array($status, [UPDATE_UNKNOWN, UPDATE_NOT_CHECKED, UPDATE_NOT_FETCHED, UPDATE_FETCH_PENDING])) {
Chris@12 116 if (_update_manager_access()) {
Chris@18 117 $requirement['description'][] = ['#prefix' => ' ', '#markup' => t('See the <a href=":available_updates">available updates</a> page for more information and to install your missing updates.', [':available_updates' => Url::fromRoute('update.report_update')->toString()])];
Chris@12 118 }
Chris@12 119 else {
Chris@18 120 $requirement['description'][] = ['#prefix' => ' ', '#markup' => t('See the <a href=":available_updates">available updates</a> page for more information.', [':available_updates' => Url::fromRoute('update.status')->toString()])];
Chris@12 121 }
Chris@0 122 }
Chris@0 123 }
Chris@0 124 switch ($status) {
Chris@0 125 case UPDATE_NOT_SECURE:
Chris@0 126 $requirement_label = t('Not secure!');
Chris@0 127 break;
Chris@0 128 case UPDATE_REVOKED:
Chris@0 129 $requirement_label = t('Revoked!');
Chris@0 130 break;
Chris@0 131 case UPDATE_NOT_SUPPORTED:
Chris@0 132 $requirement_label = t('Unsupported release');
Chris@0 133 break;
Chris@0 134 case UPDATE_NOT_CURRENT:
Chris@0 135 $requirement_label = t('Out of date');
Chris@0 136 $requirement['severity'] = REQUIREMENT_WARNING;
Chris@0 137 break;
Chris@0 138 case UPDATE_UNKNOWN:
Chris@0 139 case UPDATE_NOT_CHECKED:
Chris@0 140 case UPDATE_NOT_FETCHED:
Chris@12 141 case UPDATE_FETCH_PENDING:
Chris@0 142 $requirement_label = isset($project['reason']) ? $project['reason'] : t('Can not determine status');
Chris@0 143 $requirement['severity'] = REQUIREMENT_WARNING;
Chris@0 144 break;
Chris@0 145 default:
Chris@0 146 $requirement_label = t('Up to date');
Chris@0 147 }
Chris@0 148 if ($status != UPDATE_CURRENT && $type == 'core' && isset($project['recommended'])) {
Chris@0 149 $requirement_label .= ' ' . t('(version @version available)', ['@version' => $project['recommended']]);
Chris@0 150 }
Chris@0 151 $requirement['value'] = \Drupal::l($requirement_label, new Url(_update_manager_access() ? 'update.report_update' : 'update.status'));
Chris@0 152 return $requirement;
Chris@0 153 }
Chris@0 154
Chris@0 155 /**
Chris@0 156 * Rebuild the router to ensure admin/reports/updates/check has CSRF protection.
Chris@0 157 */
Chris@0 158 function update_update_8001() {
Chris@0 159 // Empty update forces a call to drupal_flush_all_caches() which rebuilds the
Chris@0 160 // router.
Chris@17 161
Chris@17 162 // Use hook_post_update_NAME() instead to clear the cache.The use
Chris@17 163 // of hook_update_N to clear the cache has been deprecated see
Chris@17 164 // https://www.drupal.org/node/2960601 for more details.
Chris@0 165 }