annotate core/modules/update/update.module @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 5fb285c0d0e3
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Handles updates of Drupal core and contributed projects.
Chris@0 6 *
Chris@0 7 * The module checks for available updates of Drupal core and any installed
Chris@0 8 * contributed modules and themes. It warns site administrators if newer
Chris@0 9 * releases are available via the system status report (admin/reports/status),
Chris@0 10 * the module and theme pages, and optionally via email. It also provides the
Chris@0 11 * ability to install contributed modules and themes via an user interface.
Chris@0 12 */
Chris@0 13
Chris@0 14 use Drupal\Core\Url;
Chris@0 15 use Drupal\Core\Form\FormStateInterface;
Chris@0 16 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 17 use Drupal\Core\Site\Settings;
Chris@0 18
Chris@0 19 // These are internally used constants for this code, do not modify.
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Project is missing security update(s).
Chris@0 23 *
Chris@0 24 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 25 * Use \Drupal\update\UpdateManagerInterface::NOT_SECURE instead.
Chris@0 26 *
Chris@0 27 * @see https://www.drupal.org/node/2831620
Chris@0 28 */
Chris@0 29 const UPDATE_NOT_SECURE = 1;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Current release has been unpublished and is no longer available.
Chris@0 33 *
Chris@0 34 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 35 * Use \Drupal\update\UpdateManagerInterface::REVOKED instead.
Chris@0 36 *
Chris@0 37 * @see https://www.drupal.org/node/2831620
Chris@0 38 */
Chris@0 39 const UPDATE_REVOKED = 2;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Current release is no longer supported by the project maintainer.
Chris@0 43 *
Chris@0 44 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 45 * Use \Drupal\update\UpdateManagerInterface::NOT_SUPPORTED instead.
Chris@0 46 *
Chris@0 47 * @see https://www.drupal.org/node/2831620
Chris@0 48 */
Chris@0 49 const UPDATE_NOT_SUPPORTED = 3;
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Project has a new release available, but it is not a security release.
Chris@0 53 *
Chris@0 54 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 55 * Use \Drupal\update\UpdateManagerInterface::NOT_CURRENT instead.
Chris@0 56 *
Chris@0 57 * @see https://www.drupal.org/node/2831620
Chris@0 58 */
Chris@0 59 const UPDATE_NOT_CURRENT = 4;
Chris@0 60
Chris@0 61 /**
Chris@0 62 * Project is up to date.
Chris@0 63 *
Chris@0 64 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 65 * Use \Drupal\update\UpdateManagerInterface::CURRENT instead.
Chris@0 66 *
Chris@0 67 * @see https://www.drupal.org/node/2831620
Chris@0 68 */
Chris@0 69 const UPDATE_CURRENT = 5;
Chris@0 70
Chris@0 71 /**
Chris@0 72 * Project's status cannot be checked.
Chris@0 73 *
Chris@0 74 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 75 * Use \Drupal\update\UpdateFetcherInterface::NOT_CHECKED instead.
Chris@0 76 *
Chris@0 77 * @see https://www.drupal.org/node/2831620
Chris@0 78 */
Chris@0 79 const UPDATE_NOT_CHECKED = -1;
Chris@0 80
Chris@0 81 /**
Chris@0 82 * No available update data was found for project.
Chris@0 83 *
Chris@0 84 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 85 * Use \Drupal\update\UpdateFetcherInterface::UNKNOWN instead.
Chris@0 86 *
Chris@0 87 * @see https://www.drupal.org/node/2831620
Chris@0 88 */
Chris@0 89 const UPDATE_UNKNOWN = -2;
Chris@0 90
Chris@0 91 /**
Chris@0 92 * There was a failure fetching available update data for this project.
Chris@0 93 *
Chris@0 94 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 95 * Use \Drupal\update\UpdateFetcherInterface::NOT_FETCHED instead.
Chris@0 96 *
Chris@0 97 * @see https://www.drupal.org/node/2831620
Chris@0 98 */
Chris@0 99 const UPDATE_NOT_FETCHED = -3;
Chris@0 100
Chris@0 101 /**
Chris@0 102 * We need to (re)fetch available update data for this project.
Chris@0 103 *
Chris@0 104 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
Chris@0 105 * Use \Drupal\update\UpdateFetcherInterface::FETCH_PENDING instead.
Chris@0 106 *
Chris@0 107 * @see https://www.drupal.org/node/2831620
Chris@0 108 */
Chris@0 109 const UPDATE_FETCH_PENDING = -4;
Chris@0 110
Chris@0 111 /**
Chris@0 112 * Implements hook_help().
Chris@0 113 */
Chris@0 114 function update_help($route_name, RouteMatchInterface $route_match) {
Chris@0 115 switch ($route_name) {
Chris@0 116 case 'help.page.update':
Chris@0 117 $output = '';
Chris@0 118 $output .= '<h3>' . t('About') . '</h3>';
Chris@0 119 $output .= '<p>' . t('The Update Manager module periodically checks for new versions of your site\'s software (including contributed modules and themes), and alerts administrators to available updates. The Update Manager system is also used by some other modules to manage updates and downloads; for example, the Interface Translation module uses the Update Manager to download translations from the localization server. Note that whenever the Update Manager system is used, anonymous usage statistics are sent to Drupal.org. If desired, you may disable the Update Manager module from the <a href=":modules">Extend page</a>; if you do so, functionality that depends on the Update Manager system will not work. For more information, see the <a href=":update">online documentation for the Update Manager module</a>.', [':update' => 'https://www.drupal.org/documentation/modules/update', ':modules' => \Drupal::url('system.modules_list')]) . '</p>';
Chris@0 120 // Only explain the Update manager if it has not been disabled.
Chris@0 121 if (_update_manager_access()) {
Chris@0 122 $output .= '<p>' . t('The Update Manager also allows administrators to update and install modules and themes through the administration interface.') . '</p>';
Chris@0 123 }
Chris@0 124 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 125 $output .= '<dl>';
Chris@0 126 $output .= '<dt>' . t('Checking for available updates') . '</dt>';
Chris@0 127 $output .= '<dd>' . t('The <a href=":update-report">Available updates report</a> displays core, contributed modules, and themes for which there are new releases available for download. On the report page, you can also check manually for updates. You can configure the frequency of update checks, which are performed during cron runs, and whether notifications are sent on the <a href=":update-settings">Update Manager settings page</a>.', [':update-report' => \Drupal::url('update.status'), ':update-settings' => \Drupal::url('update.settings')]) . '</dd>';
Chris@0 128 // Only explain the Update manager if it has not been disabled.
Chris@0 129 if (_update_manager_access()) {
Chris@0 130 $output .= '<dt>' . t('Performing updates through the Update page') . '</dt>';
Chris@0 131 $output .= '<dd>' . t('The Update Manager module allows administrators to perform updates directly from the <a href=":update-page">Update page</a>. It lists all available updates, and you can confirm whether you want to download them. If you don\'t have sufficient access rights to your web server, you could be prompted for your FTP/SSH password. Afterwards the files are transferred into your site installation, overwriting your old files. Direct links to the Update page are also displayed on the <a href=":modules_page">Extend page</a> and the <a href=":themes_page">Appearance page</a>.', [':modules_page' => \Drupal::url('system.modules_list'), ':themes_page' => \Drupal::url('system.themes_page'), ':update-page' => \Drupal::url('update.report_update')]) . '</dd>';
Chris@0 132 $output .= '<dt>' . t('Installing new modules and themes through the Install page') . '</dt>';
Chris@0 133 $output .= '<dd>' . t('You can also install new modules and themes in the same fashion, through the <a href=":install">Install page</a>, or by clicking the <em>Install new module/theme</em> links at the top of the <a href=":modules_page">Extend page</a> and the <a href=":themes_page">Appearance page</a>. In this case, you are prompted to provide either the URL to the download, or to upload a packaged release file from your local computer.', [':modules_page' => \Drupal::url('system.modules_list'), ':themes_page' => \Drupal::url('system.themes_page'), ':install' => \Drupal::url('update.report_install')]) . '</dd>';
Chris@0 134 }
Chris@0 135 $output .= '</dl>';
Chris@0 136 return $output;
Chris@0 137
Chris@0 138 case 'update.status':
Chris@0 139 return '<p>' . t('Here you can find information about available updates for your installed modules and themes. Note that each module or theme is part of a "project", which may or may not have the same name, and might include multiple modules or themes within it.') . '</p>';
Chris@0 140
Chris@0 141 case 'system.modules_list':
Chris@0 142 if (_update_manager_access()) {
Chris@0 143 $output = '<p>' . t('Regularly review and install <a href=":updates">available updates</a> to maintain a secure and current site. Always run the <a href=":update-php">update script</a> each time a module is updated.', [':update-php' => \Drupal::url('system.db_update'), ':updates' => \Drupal::url('update.status')]) . '</p>';
Chris@0 144 }
Chris@0 145 else {
Chris@0 146 $output = '<p>' . t('Regularly review <a href=":updates">available updates</a> to maintain a secure and current site. Always run the <a href=":update-php">update script</a> each time a module is updated.', [':update-php' => \Drupal::url('system.db_update'), ':updates' => \Drupal::url('update.status')]) . '</p>';
Chris@0 147 }
Chris@0 148 return $output;
Chris@0 149 }
Chris@0 150 }
Chris@0 151
Chris@0 152 /**
Chris@0 153 * Implements hook_page_top().
Chris@0 154 */
Chris@0 155 function update_page_top() {
Chris@0 156 /** @var \Drupal\Core\Routing\AdminContext $admin_context */
Chris@0 157 $admin_context = \Drupal::service('router.admin_context');
Chris@0 158 $route_match = \Drupal::routeMatch();
Chris@0 159 if ($admin_context->isAdminRoute($route_match->getRouteObject()) && \Drupal::currentUser()->hasPermission('administer site configuration')) {
Chris@0 160 $route_name = \Drupal::routeMatch()->getRouteName();
Chris@0 161 switch ($route_name) {
Chris@0 162 // These pages don't need additional nagging.
Chris@0 163 case 'update.theme_update':
Chris@0 164 case 'system.theme_install':
Chris@0 165 case 'update.module_update':
Chris@0 166 case 'update.module_install':
Chris@0 167 case 'update.status':
Chris@0 168 case 'update.report_update':
Chris@0 169 case 'update.report_install':
Chris@0 170 case 'update.settings':
Chris@0 171 case 'system.status':
Chris@0 172 case 'update.confirmation_page':
Chris@0 173 return;
Chris@0 174
Chris@0 175 // If we are on the appearance or modules list, display a detailed report
Chris@0 176 // of the update status.
Chris@0 177 case 'system.themes_page':
Chris@0 178 case 'system.modules_list':
Chris@0 179 $verbose = TRUE;
Chris@0 180 break;
Chris@0 181
Chris@0 182 }
Chris@0 183 module_load_install('update');
Chris@0 184 $status = update_requirements('runtime');
Chris@0 185 foreach (['core', 'contrib'] as $report_type) {
Chris@0 186 $type = 'update_' . $report_type;
Chris@0 187 // hook_requirements() supports render arrays therefore we need to render
Chris@0 188 // them before using drupal_set_message().
Chris@0 189 if (isset($status[$type]['description']) && is_array($status[$type]['description'])) {
Chris@0 190 $status[$type]['description'] = \Drupal::service('renderer')->renderPlain($status[$type]['description']);
Chris@0 191 }
Chris@0 192 if (!empty($verbose)) {
Chris@0 193 if (isset($status[$type]['severity'])) {
Chris@0 194 if ($status[$type]['severity'] == REQUIREMENT_ERROR) {
Chris@0 195 drupal_set_message($status[$type]['description'], 'error');
Chris@0 196 }
Chris@0 197 elseif ($status[$type]['severity'] == REQUIREMENT_WARNING) {
Chris@0 198 drupal_set_message($status[$type]['description'], 'warning');
Chris@0 199 }
Chris@0 200 }
Chris@0 201 }
Chris@0 202 // Otherwise, if we're on *any* admin page and there's a security
Chris@0 203 // update missing, print an error message about it.
Chris@0 204 else {
Chris@0 205 if (isset($status[$type])
Chris@0 206 && isset($status[$type]['reason'])
Chris@0 207 && $status[$type]['reason'] === UPDATE_NOT_SECURE) {
Chris@0 208 drupal_set_message($status[$type]['description'], 'error');
Chris@0 209 }
Chris@0 210 }
Chris@0 211 }
Chris@0 212 }
Chris@0 213 }
Chris@0 214
Chris@0 215 /**
Chris@0 216 * Resolves if the current user can access updater menu items.
Chris@0 217 *
Chris@0 218 * It both enforces the 'administer software updates' permission and the global
Chris@0 219 * kill switch for the authorize.php script.
Chris@0 220 *
Chris@0 221 * @return
Chris@0 222 * TRUE if the current user can access the updater menu items; FALSE
Chris@0 223 * otherwise.
Chris@0 224 */
Chris@0 225 function _update_manager_access() {
Chris@0 226 return Settings::get('allow_authorize_operations', TRUE) && \Drupal::currentUser()->hasPermission('administer software updates');
Chris@0 227 }
Chris@0 228
Chris@0 229 /**
Chris@0 230 * Implements hook_theme().
Chris@0 231 */
Chris@0 232 function update_theme() {
Chris@0 233 return [
Chris@0 234 'update_last_check' => [
Chris@0 235 'variables' => ['last' => 0],
Chris@0 236 ],
Chris@0 237 'update_report' => [
Chris@0 238 'variables' => ['data' => NULL],
Chris@0 239 'file' => 'update.report.inc',
Chris@0 240 ],
Chris@0 241 'update_project_status' => [
Chris@0 242 'variables' => ['project' => []],
Chris@0 243 'file' => 'update.report.inc',
Chris@0 244 ],
Chris@0 245 // We are using template instead of '#type' => 'table' here to keep markup
Chris@0 246 // out of preprocess and allow for easier changes to markup.
Chris@0 247 'update_version' => [
Chris@0 248 'variables' => ['version' => NULL, 'title' => NULL, 'attributes' => []],
Chris@0 249 'file' => 'update.report.inc',
Chris@0 250 ],
Chris@0 251 ];
Chris@0 252 }
Chris@0 253
Chris@0 254 /**
Chris@0 255 * Implements hook_cron().
Chris@0 256 */
Chris@0 257 function update_cron() {
Chris@0 258 $update_config = \Drupal::config('update.settings');
Chris@0 259 $frequency = $update_config->get('check.interval_days');
Chris@0 260 $interval = 60 * 60 * 24 * $frequency;
Chris@0 261 $last_check = \Drupal::state()->get('update.last_check') ?: 0;
Chris@0 262 if ((REQUEST_TIME - $last_check) > $interval) {
Chris@0 263 // If the configured update interval has elapsed, we want to invalidate
Chris@0 264 // the data for all projects, attempt to re-fetch, and trigger any
Chris@0 265 // configured notifications about the new status.
Chris@0 266 update_refresh();
Chris@0 267 update_fetch_data();
Chris@0 268 }
Chris@0 269 else {
Chris@0 270 // Otherwise, see if any individual projects are now stale or still
Chris@0 271 // missing data, and if so, try to fetch the data.
Chris@0 272 update_get_available(TRUE);
Chris@0 273 }
Chris@0 274 $last_email_notice = \Drupal::state()->get('update.last_email_notification') ?: 0;
Chris@0 275 if ((REQUEST_TIME - $last_email_notice) > $interval) {
Chris@0 276 // If configured time between notifications elapsed, send email about
Chris@0 277 // updates possibly available.
Chris@0 278 module_load_include('inc', 'update', 'update.fetch');
Chris@0 279 _update_cron_notify();
Chris@0 280 }
Chris@0 281
Chris@0 282 // Clear garbage from disk.
Chris@0 283 update_clear_update_disk_cache();
Chris@0 284 }
Chris@0 285
Chris@0 286 /**
Chris@0 287 * Implements hook_themes_installed().
Chris@0 288 *
Chris@0 289 * If themes are installed, we invalidate the information of available updates.
Chris@0 290 */
Chris@0 291 function update_themes_installed($themes) {
Chris@0 292 // Clear all update module data.
Chris@0 293 update_storage_clear();
Chris@0 294 }
Chris@0 295
Chris@0 296 /**
Chris@0 297 * Implements hook_themes_uninstalled().
Chris@0 298 *
Chris@0 299 * If themes are uninstalled, we invalidate the information of available updates.
Chris@0 300 */
Chris@0 301 function update_themes_uninstalled($themes) {
Chris@0 302 // Clear all update module data.
Chris@0 303 update_storage_clear();
Chris@0 304 }
Chris@0 305
Chris@0 306 /**
Chris@0 307 * Implements hook_form_FORM_ID_alter() for system_modules().
Chris@0 308 *
Chris@0 309 * Adds a form submission handler to the system modules form, so that if a site
Chris@0 310 * admin saves the form, we invalidate the information of available updates.
Chris@0 311 *
Chris@0 312 * @see _update_cache_clear()
Chris@0 313 */
Chris@0 314 function update_form_system_modules_alter(&$form, FormStateInterface $form_state) {
Chris@0 315 $form['#submit'][] = 'update_storage_clear_submit';
Chris@0 316 }
Chris@0 317
Chris@0 318 /**
Chris@0 319 * Form submission handler for system_modules().
Chris@0 320 *
Chris@0 321 * @see update_form_system_modules_alter()
Chris@0 322 */
Chris@0 323 function update_storage_clear_submit($form, FormStateInterface $form_state) {
Chris@0 324 // Clear all update module data.
Chris@0 325 update_storage_clear();
Chris@0 326 }
Chris@0 327
Chris@0 328 /**
Chris@0 329 * Returns a warning message when there is no data about available updates.
Chris@0 330 */
Chris@0 331 function _update_no_data() {
Chris@0 332 $destination = \Drupal::destination()->getAsArray();
Chris@0 333 return t('No update information available. <a href=":run_cron">Run cron</a> or <a href=":check_manually">check manually</a>.', [
Chris@0 334 ':run_cron' => \Drupal::url('system.run_cron', [], ['query' => $destination]),
Chris@0 335 ':check_manually' => \Drupal::url('update.manual_status', [], ['query' => $destination]),
Chris@0 336 ]);
Chris@0 337 }
Chris@0 338
Chris@0 339 /**
Chris@0 340 * Tries to get update information and refreshes it when necessary.
Chris@0 341 *
Chris@0 342 * In addition to checking the lifetime, this function also ensures that
Chris@0 343 * there are no .info.yml files for enabled modules or themes that have a newer
Chris@0 344 * modification timestamp than the last time we checked for available update
Chris@0 345 * data. If any .info.yml file was modified, it almost certainly means a new
Chris@0 346 * version of something was installed. Without fresh available update data, the
Chris@0 347 * logic in update_calculate_project_data() will be wrong and produce confusing,
Chris@0 348 * bogus results.
Chris@0 349 *
Chris@0 350 * @param $refresh
Chris@0 351 * (optional) Boolean to indicate if this method should refresh automatically
Chris@0 352 * if there's no data. Defaults to FALSE.
Chris@0 353 *
Chris@0 354 * @return
Chris@0 355 * Array of data about available releases, keyed by project shortname.
Chris@0 356 *
Chris@0 357 * @see update_refresh()
Chris@0 358 * @see \Drupal\Update\UpdateManager::getProjects()
Chris@0 359 */
Chris@0 360 function update_get_available($refresh = FALSE) {
Chris@0 361 module_load_include('inc', 'update', 'update.compare');
Chris@0 362 $needs_refresh = FALSE;
Chris@0 363
Chris@0 364 // Grab whatever data we currently have.
Chris@0 365 $available = \Drupal::keyValueExpirable('update_available_releases')->getAll();
Chris@0 366 $projects = \Drupal::service('update.manager')->getProjects();
Chris@0 367 foreach ($projects as $key => $project) {
Chris@0 368 // If there's no data at all, we clearly need to fetch some.
Chris@0 369 if (empty($available[$key])) {
Chris@0 370 // update_create_fetch_task($project);
Chris@0 371 \Drupal::service('update.processor')->createFetchTask($project);
Chris@0 372 $needs_refresh = TRUE;
Chris@0 373 continue;
Chris@0 374 }
Chris@0 375
Chris@0 376 // See if the .info.yml file is newer than the last time we checked for
Chris@0 377 // data, and if so, mark this project's data as needing to be re-fetched.
Chris@0 378 // Any time an admin upgrades their local installation, the .info.yml file
Chris@0 379 // will be changed, so this is the only way we can be sure we're not showing
Chris@0 380 // bogus information right after they upgrade.
Chris@0 381 if ($project['info']['_info_file_ctime'] > $available[$key]['last_fetch']) {
Chris@0 382 $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
Chris@0 383 }
Chris@0 384
Chris@0 385 // If we have project data but no release data, we need to fetch. This
Chris@0 386 // can be triggered when we fail to contact a release history server.
Chris@0 387 if (empty($available[$key]['releases']) && !$available[$key]['last_fetch']) {
Chris@0 388 $available[$key]['fetch_status'] = UPDATE_FETCH_PENDING;
Chris@0 389 }
Chris@0 390
Chris@0 391 // If we think this project needs to fetch, actually create the task now
Chris@0 392 // and remember that we think we're missing some data.
Chris@0 393 if (!empty($available[$key]['fetch_status']) && $available[$key]['fetch_status'] == UPDATE_FETCH_PENDING) {
Chris@0 394 \Drupal::service('update.processor')->createFetchTask($project);
Chris@0 395 $needs_refresh = TRUE;
Chris@0 396 }
Chris@0 397 }
Chris@0 398
Chris@0 399 if ($needs_refresh && $refresh) {
Chris@0 400 // Attempt to drain the queue of fetch tasks.
Chris@0 401 update_fetch_data();
Chris@0 402 // After processing the queue, we've (hopefully) got better data, so pull
Chris@0 403 // the latest data again and use that directly.
Chris@0 404 $available = \Drupal::keyValueExpirable('update_available_releases')->getAll();
Chris@0 405 }
Chris@0 406
Chris@0 407 return $available;
Chris@0 408 }
Chris@0 409
Chris@0 410 /**
Chris@0 411 * Adds a task to the queue for fetching release history data for a project.
Chris@0 412 *
Chris@0 413 * We only create a new fetch task if there's no task already in the queue for
Chris@0 414 * this particular project (based on 'update_fetch_task' key-value collection).
Chris@0 415 *
Chris@0 416 * @param $project
Chris@0 417 * Associative array of information about a project as created by
Chris@0 418 * \Drupal\Update\UpdateManager::getProjects(), including keys such as 'name'
Chris@0 419 * (short name), and the 'info' array with data from a .info.yml file for the
Chris@0 420 * project.
Chris@0 421 *
Chris@0 422 * @see \Drupal\update\UpdateFetcher::createFetchTask()
Chris@0 423 */
Chris@0 424 function update_create_fetch_task($project) {
Chris@0 425 \Drupal::service('update.processor')->createFetchTask($project);
Chris@0 426 }
Chris@0 427
Chris@0 428 /**
Chris@0 429 * Refreshes the release data after loading the necessary include file.
Chris@0 430 */
Chris@0 431 function update_refresh() {
Chris@0 432 \Drupal::service('update.manager')->refreshUpdateData();
Chris@0 433 }
Chris@0 434
Chris@0 435 /**
Chris@0 436 * Attempts to fetch update data after loading the necessary include file.
Chris@0 437 *
Chris@0 438 * @see \Drupal\update\UpdateProcessor::fetchData()
Chris@0 439 */
Chris@0 440 function update_fetch_data() {
Chris@0 441 \Drupal::service('update.processor')->fetchData();
Chris@0 442 }
Chris@0 443
Chris@0 444 /**
Chris@0 445 * Batch callback: Performs actions when all fetch tasks have been completed.
Chris@0 446 *
Chris@0 447 * @param $success
Chris@0 448 * TRUE if the batch operation was successful; FALSE if there were errors.
Chris@0 449 * @param $results
Chris@0 450 * An associative array of results from the batch operation, including the key
Chris@0 451 * 'updated' which holds the total number of projects we fetched available
Chris@0 452 * update data for.
Chris@0 453 */
Chris@0 454 function update_fetch_data_finished($success, $results) {
Chris@0 455 if ($success) {
Chris@0 456 if (!empty($results)) {
Chris@0 457 if (!empty($results['updated'])) {
Chris@0 458 drupal_set_message(\Drupal::translation()->formatPlural($results['updated'], 'Checked available update data for one project.', 'Checked available update data for @count projects.'));
Chris@0 459 }
Chris@0 460 if (!empty($results['failures'])) {
Chris@0 461 drupal_set_message(\Drupal::translation()->formatPlural($results['failures'], 'Failed to get available update data for one project.', 'Failed to get available update data for @count projects.'), 'error');
Chris@0 462 }
Chris@0 463 }
Chris@0 464 }
Chris@0 465 else {
Chris@0 466 drupal_set_message(t('An error occurred trying to get available update data.'), 'error');
Chris@0 467 }
Chris@0 468 }
Chris@0 469
Chris@0 470 /**
Chris@0 471 * Implements hook_mail().
Chris@0 472 *
Chris@0 473 * Constructs the email notification message when the site is out of date.
Chris@0 474 *
Chris@0 475 * @param $key
Chris@0 476 * Unique key to indicate what message to build, always 'status_notify'.
Chris@0 477 * @param $message
Chris@0 478 * Reference to the message array being built.
Chris@0 479 * @param $params
Chris@0 480 * Array of parameters to indicate what kind of text to include in the message
Chris@0 481 * body. This is a keyed array of message type ('core' or 'contrib') as the
Chris@0 482 * keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for the
Chris@0 483 * values.
Chris@0 484 *
Chris@0 485 * @see \Drupal\Core\Mail\MailManagerInterface::mail()
Chris@0 486 * @see _update_cron_notify()
Chris@0 487 * @see _update_message_text()
Chris@0 488 */
Chris@0 489 function update_mail($key, &$message, $params) {
Chris@0 490 $langcode = $message['langcode'];
Chris@0 491 $language = \Drupal::languageManager()->getLanguage($langcode);
Chris@0 492 $message['subject'] .= t('New release(s) available for @site_name', ['@site_name' => \Drupal::config('system.site')->get('name')], ['langcode' => $langcode]);
Chris@0 493 foreach ($params as $msg_type => $msg_reason) {
Chris@0 494 $message['body'][] = _update_message_text($msg_type, $msg_reason, $langcode);
Chris@0 495 }
Chris@0 496 $message['body'][] = t('See the available updates page for more information:', [], ['langcode' => $langcode]) . "\n" . \Drupal::url('update.status', [], ['absolute' => TRUE, 'language' => $language]);
Chris@0 497 if (_update_manager_access()) {
Chris@0 498 $message['body'][] = t('You can automatically install your missing updates using the Update manager:', [], ['langcode' => $langcode]) . "\n" . \Drupal::url('update.report_update', [], ['absolute' => TRUE, 'language' => $language]);
Chris@0 499 }
Chris@0 500 $settings_url = \Drupal::url('update.settings', [], ['absolute' => TRUE]);
Chris@0 501 if (\Drupal::config('update.settings')->get('notification.threshold') == 'all') {
Chris@0 502 $message['body'][] = t('Your site is currently configured to send these emails when any updates are available. To get notified only for security updates, @url.', ['@url' => $settings_url]);
Chris@0 503 }
Chris@0 504 else {
Chris@0 505 $message['body'][] = t('Your site is currently configured to send these emails only when security updates are available. To get notified for any available updates, @url.', ['@url' => $settings_url]);
Chris@0 506 }
Chris@0 507 }
Chris@0 508
Chris@0 509 /**
Chris@0 510 * Returns the appropriate message text when site is out of date or not secure.
Chris@0 511 *
Chris@0 512 * These error messages are shared by both update_requirements() for the
Chris@0 513 * site-wide status report at admin/reports/status and in the body of the
Chris@0 514 * notification email messages generated by update_cron().
Chris@0 515 *
Chris@0 516 * @param $msg_type
Chris@0 517 * String to indicate what kind of message to generate. Can be either 'core'
Chris@0 518 * or 'contrib'.
Chris@0 519 * @param $msg_reason
Chris@0 520 * Integer constant specifying why message is generated.
Chris@0 521 * @param $langcode
Chris@0 522 * (optional) A language code to use. Defaults to NULL.
Chris@0 523 *
Chris@0 524 * @return
Chris@0 525 * The properly translated error message for the given key.
Chris@0 526 */
Chris@0 527 function _update_message_text($msg_type, $msg_reason, $langcode = NULL) {
Chris@0 528 $text = '';
Chris@0 529 switch ($msg_reason) {
Chris@0 530 case UPDATE_NOT_SECURE:
Chris@0 531 if ($msg_type == 'core') {
Chris@0 532 $text = t('There is a security update available for your version of Drupal. To ensure the security of your server, you should update immediately!', [], ['langcode' => $langcode]);
Chris@0 533 }
Chris@0 534 else {
Chris@0 535 $text = t('There are security updates available for one or more of your modules or themes. To ensure the security of your server, you should update immediately!', [], ['langcode' => $langcode]);
Chris@0 536 }
Chris@0 537 break;
Chris@0 538
Chris@0 539 case UPDATE_REVOKED:
Chris@0 540 if ($msg_type == 'core') {
Chris@0 541 $text = t('Your version of Drupal has been revoked and is no longer available for download. Upgrading is strongly recommended!', [], ['langcode' => $langcode]);
Chris@0 542 }
Chris@0 543 else {
Chris@0 544 $text = t('The installed version of at least one of your modules or themes has been revoked and is no longer available for download. Upgrading or disabling is strongly recommended!', [], ['langcode' => $langcode]);
Chris@0 545 }
Chris@0 546 break;
Chris@0 547
Chris@0 548 case UPDATE_NOT_SUPPORTED:
Chris@0 549 if ($msg_type == 'core') {
Chris@0 550 $text = t('Your version of Drupal is no longer supported. Upgrading is strongly recommended!', [], ['langcode' => $langcode]);
Chris@0 551 }
Chris@0 552 else {
Chris@0 553 $text = t('The installed version of at least one of your modules or themes is no longer supported. Upgrading or disabling is strongly recommended. See the project homepage for more details.', [], ['langcode' => $langcode]);
Chris@0 554 }
Chris@0 555 break;
Chris@0 556
Chris@0 557 case UPDATE_NOT_CURRENT:
Chris@0 558 if ($msg_type == 'core') {
Chris@0 559 $text = t('There are updates available for your version of Drupal. To ensure the proper functioning of your site, you should update as soon as possible.', [], ['langcode' => $langcode]);
Chris@0 560 }
Chris@0 561 else {
Chris@0 562 $text = t('There are updates available for one or more of your modules or themes. To ensure the proper functioning of your site, you should update as soon as possible.', [], ['langcode' => $langcode]);
Chris@0 563 }
Chris@0 564 break;
Chris@0 565
Chris@0 566 case UPDATE_UNKNOWN:
Chris@0 567 case UPDATE_NOT_CHECKED:
Chris@0 568 case UPDATE_NOT_FETCHED:
Chris@0 569 case UPDATE_FETCH_PENDING:
Chris@0 570 if ($msg_type == 'core') {
Chris@0 571 $text = t('There was a problem checking <a href=":update-report">available updates</a> for Drupal.', [':update-report' => \Drupal::url('update.status')], ['langcode' => $langcode]);
Chris@0 572 }
Chris@0 573 else {
Chris@0 574 $text = t('There was a problem checking <a href=":update-report">available updates</a> for your modules or themes.', [':update-report' => \Drupal::url('update.status')], ['langcode' => $langcode]);
Chris@0 575 }
Chris@0 576 break;
Chris@0 577 }
Chris@0 578
Chris@0 579 return $text;
Chris@0 580 }
Chris@0 581
Chris@0 582 /**
Chris@0 583 * Orders projects based on their status.
Chris@0 584 *
Chris@0 585 * Callback for uasort() within update_requirements().
Chris@0 586 */
Chris@0 587 function _update_project_status_sort($a, $b) {
Chris@0 588 // The status constants are numerically in the right order, so we can
Chris@0 589 // usually subtract the two to compare in the order we want. However,
Chris@0 590 // negative status values should be treated as if they are huge, since we
Chris@0 591 // always want them at the bottom of the list.
Chris@0 592 $a_status = $a['status'] > 0 ? $a['status'] : (-10 * $a['status']);
Chris@0 593 $b_status = $b['status'] > 0 ? $b['status'] : (-10 * $b['status']);
Chris@0 594 return $a_status - $b_status;
Chris@0 595 }
Chris@0 596
Chris@0 597 /**
Chris@0 598 * Prepares variables for last time update data was checked templates.
Chris@0 599 *
Chris@0 600 * Default template: update-last-check.html.twig.
Chris@0 601 *
Chris@0 602 * In addition to properly formatting the given timestamp, this function also
Chris@0 603 * provides a "Check manually" link that refreshes the available update and
Chris@0 604 * redirects back to the same page.
Chris@0 605 *
Chris@0 606 * @param $variables
Chris@0 607 * An associative array containing:
Chris@0 608 * - last: The timestamp when the site last checked for available updates.
Chris@0 609 *
Chris@0 610 * @see theme_update_report()
Chris@0 611 */
Chris@0 612 function template_preprocess_update_last_check(&$variables) {
Chris@0 613 $variables['time'] = \Drupal::service('date.formatter')->formatTimeDiffSince($variables['last']);
Chris@0 614 $variables['link'] = \Drupal::l(t('Check manually'), new Url('update.manual_status', [], ['query' => \Drupal::destination()->getAsArray()]));
Chris@0 615 }
Chris@0 616
Chris@0 617 /**
Chris@0 618 * Implements hook_verify_update_archive().
Chris@0 619 *
Chris@0 620 * First, we ensure that the archive isn't a copy of Drupal core, which the
Chris@0 621 * update manager does not yet support. See https://www.drupal.org/node/606592.
Chris@0 622 *
Chris@0 623 * Then, we make sure that at least one module included in the archive file has
Chris@0 624 * an .info.yml file which claims that the code is compatible with the current
Chris@0 625 * version of Drupal core.
Chris@0 626 *
Chris@0 627 * @see \Drupal\Core\Extension\ExtensionDiscovery
Chris@0 628 * @see _system_rebuild_module_data()
Chris@0 629 */
Chris@0 630 function update_verify_update_archive($project, $archive_file, $directory) {
Chris@0 631 $errors = [];
Chris@0 632
Chris@0 633 // Make sure this isn't a tarball of Drupal core.
Chris@0 634 if (
Chris@0 635 file_exists("$directory/$project/index.php")
Chris@0 636 && file_exists("$directory/$project/core/install.php")
Chris@0 637 && file_exists("$directory/$project/core/includes/bootstrap.inc")
Chris@0 638 && file_exists("$directory/$project/core/modules/node/node.module")
Chris@0 639 && file_exists("$directory/$project/core/modules/system/system.module")
Chris@0 640 ) {
Chris@0 641 return [
Chris@0 642 'no-core' => t('Automatic updating of Drupal core is not supported. See the <a href=":upgrade-guide">upgrade guide</a> for information on how to update Drupal core manually.', [':upgrade-guide' => 'https://www.drupal.org/upgrade']),
Chris@0 643 ];
Chris@0 644 }
Chris@0 645
Chris@0 646 // Parse all the .info.yml files and make sure at least one is compatible with
Chris@0 647 // this version of Drupal core. If one is compatible, then the project as a
Chris@0 648 // whole is considered compatible (since, for example, the project may ship
Chris@0 649 // with some out-of-date modules that are not necessary for its overall
Chris@0 650 // functionality).
Chris@0 651 $compatible_project = FALSE;
Chris@0 652 $incompatible = [];
Chris@0 653 $files = file_scan_directory("$directory/$project", '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info.yml$/', ['key' => 'name', 'min_depth' => 0]);
Chris@0 654 foreach ($files as $file) {
Chris@0 655 // Get the .info.yml file for the module or theme this file belongs to.
Chris@0 656 $info = \Drupal::service('info_parser')->parse($file->uri);
Chris@0 657
Chris@0 658 // If the module or theme is incompatible with Drupal core, set an error.
Chris@0 659 if (empty($info['core']) || $info['core'] != \Drupal::CORE_COMPATIBILITY) {
Chris@0 660 $incompatible[] = !empty($info['name']) ? $info['name'] : t('Unknown');
Chris@0 661 }
Chris@0 662 else {
Chris@0 663 $compatible_project = TRUE;
Chris@0 664 break;
Chris@0 665 }
Chris@0 666 }
Chris@0 667
Chris@0 668 if (empty($files)) {
Chris@0 669 $errors[] = t('%archive_file does not contain any .info.yml files.', ['%archive_file' => drupal_basename($archive_file)]);
Chris@0 670 }
Chris@0 671 elseif (!$compatible_project) {
Chris@0 672 $errors[] = \Drupal::translation()->formatPlural(
Chris@0 673 count($incompatible),
Chris@0 674 '%archive_file contains a version of %names that is not compatible with Drupal @version.',
Chris@0 675 '%archive_file contains versions of modules or themes that are not compatible with Drupal @version: %names',
Chris@0 676 ['@version' => \Drupal::CORE_COMPATIBILITY, '%archive_file' => drupal_basename($archive_file), '%names' => implode(', ', $incompatible)]
Chris@0 677 );
Chris@0 678 }
Chris@0 679
Chris@0 680 return $errors;
Chris@0 681 }
Chris@0 682
Chris@0 683 /**
Chris@0 684 * Invalidates stored data relating to update status.
Chris@0 685 */
Chris@0 686 function update_storage_clear() {
Chris@0 687 \Drupal::keyValueExpirable('update')->deleteAll();
Chris@0 688 \Drupal::keyValueExpirable('update_available_release')->deleteAll();
Chris@0 689 }
Chris@0 690
Chris@0 691 /**
Chris@0 692 * Returns a short unique identifier for this Drupal installation.
Chris@0 693 *
Chris@0 694 * @return
Chris@0 695 * An eight character string uniquely identifying this Drupal installation.
Chris@0 696 */
Chris@0 697 function _update_manager_unique_identifier() {
Chris@0 698 $id = &drupal_static(__FUNCTION__, '');
Chris@0 699 if (empty($id)) {
Chris@0 700 $id = substr(hash('sha256', Settings::getHashSalt()), 0, 8);
Chris@0 701 }
Chris@0 702 return $id;
Chris@0 703 }
Chris@0 704
Chris@0 705 /**
Chris@0 706 * Returns the directory where update archive files should be extracted.
Chris@0 707 *
Chris@0 708 * @param $create
Chris@0 709 * (optional) Whether to attempt to create the directory if it does not
Chris@0 710 * already exist. Defaults to TRUE.
Chris@0 711 *
Chris@0 712 * @return
Chris@0 713 * The full path to the temporary directory where update file archives should
Chris@0 714 * be extracted.
Chris@0 715 */
Chris@0 716 function _update_manager_extract_directory($create = TRUE) {
Chris@0 717 $directory = &drupal_static(__FUNCTION__, '');
Chris@0 718 if (empty($directory)) {
Chris@0 719 $directory = 'temporary://update-extraction-' . _update_manager_unique_identifier();
Chris@0 720 if ($create && !file_exists($directory)) {
Chris@0 721 mkdir($directory);
Chris@0 722 }
Chris@0 723 }
Chris@0 724 return $directory;
Chris@0 725 }
Chris@0 726
Chris@0 727 /**
Chris@0 728 * Returns the directory where update archive files should be cached.
Chris@0 729 *
Chris@0 730 * @param $create
Chris@0 731 * (optional) Whether to attempt to create the directory if it does not
Chris@0 732 * already exist. Defaults to TRUE.
Chris@0 733 *
Chris@0 734 * @return
Chris@0 735 * The full path to the temporary directory where update file archives should
Chris@0 736 * be cached.
Chris@0 737 */
Chris@0 738 function _update_manager_cache_directory($create = TRUE) {
Chris@0 739 $directory = &drupal_static(__FUNCTION__, '');
Chris@0 740 if (empty($directory)) {
Chris@0 741 $directory = 'temporary://update-cache-' . _update_manager_unique_identifier();
Chris@0 742 if ($create && !file_exists($directory)) {
Chris@0 743 mkdir($directory);
Chris@0 744 }
Chris@0 745 }
Chris@0 746 return $directory;
Chris@0 747 }
Chris@0 748
Chris@0 749 /**
Chris@0 750 * Clears the temporary files and directories based on file age from disk.
Chris@0 751 */
Chris@0 752 function update_clear_update_disk_cache() {
Chris@0 753 // List of update module cache directories. Do not create the directories if
Chris@0 754 // they do not exist.
Chris@0 755 $directories = [
Chris@0 756 _update_manager_cache_directory(FALSE),
Chris@0 757 _update_manager_extract_directory(FALSE),
Chris@0 758 ];
Chris@0 759
Chris@0 760 // Search for files and directories in base folder only without recursion.
Chris@0 761 foreach ($directories as $directory) {
Chris@0 762 file_scan_directory($directory, '/.*/', ['callback' => 'update_delete_file_if_stale', 'recurse' => FALSE]);
Chris@0 763 }
Chris@0 764 }
Chris@0 765
Chris@0 766 /**
Chris@0 767 * Deletes stale files and directories from the update manager disk cache.
Chris@0 768 *
Chris@0 769 * Files and directories older than 6 hours and development snapshots older than
Chris@0 770 * 5 minutes are considered stale. We only cache development snapshots for 5
Chris@0 771 * minutes since otherwise updated snapshots might not be downloaded as
Chris@0 772 * expected.
Chris@0 773 *
Chris@0 774 * When checking file ages, we need to use the ctime, not the mtime
Chris@0 775 * (modification time) since many (all?) tar implementations go out of their way
Chris@0 776 * to set the mtime on the files they create to the timestamps recorded in the
Chris@0 777 * tarball. We want to see the last time the file was changed on disk, which is
Chris@0 778 * left alone by tar and correctly set to the time the archive file was
Chris@0 779 * unpacked.
Chris@0 780 *
Chris@0 781 * @param $path
Chris@0 782 * A string containing a file path or (streamwrapper) URI.
Chris@0 783 */
Chris@0 784 function update_delete_file_if_stale($path) {
Chris@0 785 if (file_exists($path)) {
Chris@0 786 $filectime = filectime($path);
Chris@0 787 $max_age = \Drupal::config('system.file')->get('temporary_maximum_age');
Chris@0 788
Chris@0 789 if (REQUEST_TIME - $filectime > $max_age || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && REQUEST_TIME - $filectime > 300)) {
Chris@0 790 file_unmanaged_delete_recursive($path);
Chris@0 791 }
Chris@0 792 }
Chris@0 793 }