annotate core/modules/update/update.authorize.inc @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Callbacks and related functions invoked by authorize.php to update projects.
Chris@0 6 *
Chris@0 7 * We use the Batch API to actually update each individual project on the site.
Chris@0 8 * All of the code in this file is run at a low bootstrap level (modules are not
Chris@0 9 * loaded), so these functions cannot assume access to the rest of the code of
Chris@0 10 * the Update Manager module.
Chris@0 11 */
Chris@0 12
Chris@0 13 use Drupal\Core\Updater\UpdaterException;
Chris@0 14 use Drupal\Core\Url;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Updates existing projects when invoked by authorize.php.
Chris@0 18 *
Chris@0 19 * Callback for system_authorized_init() in
Chris@0 20 * update_manager_update_ready_form_submit().
Chris@0 21 *
Chris@0 22 * @param $filetransfer
Chris@0 23 * The FileTransfer object created by authorize.php for use during this
Chris@0 24 * operation.
Chris@0 25 * @param $projects
Chris@0 26 * A nested array of projects to install into the live webroot, keyed by
Chris@0 27 * project name. Each subarray contains the following keys:
Chris@0 28 * - project: The canonical project short name.
Chris@0 29 * - updater_name: The name of the Drupal\Core\Updater\Updater class to use
Chris@0 30 * for this project.
Chris@0 31 * - local_url: The locally installed location of new code to update with.
Chris@0 32 *
Chris@0 33 * @return \Symfony\Component\HttpFoundation\Response|null
Chris@0 34 * The result of processing the batch that updates the projects. If this is
Chris@0 35 * an instance of \Symfony\Component\HttpFoundation\Response the calling code
Chris@0 36 * should use that response for the current page request.
Chris@0 37 */
Chris@0 38 function update_authorize_run_update($filetransfer, $projects) {
Chris@0 39 $operations = [];
Chris@0 40 foreach ($projects as $project_info) {
Chris@0 41 $operations[] = [
Chris@0 42 'update_authorize_batch_copy_project',
Chris@0 43 [
Chris@0 44 $project_info['project'],
Chris@0 45 $project_info['updater_name'],
Chris@0 46 $project_info['local_url'],
Chris@0 47 $filetransfer,
Chris@0 48 ],
Chris@0 49 ];
Chris@0 50 }
Chris@0 51
Chris@0 52 $batch = [
Chris@0 53 'init_message' => t('Preparing to update your site'),
Chris@0 54 'operations' => $operations,
Chris@0 55 'finished' => 'update_authorize_update_batch_finished',
Chris@0 56 'file' => drupal_get_path('module', 'update') . '/update.authorize.inc',
Chris@0 57 ];
Chris@0 58 batch_set($batch);
Chris@0 59
Chris@0 60 // Since authorize.php has its own method for setting the page title, set it
Chris@0 61 // manually here rather than passing it in to batch_set() as would normally
Chris@0 62 // be done.
Chris@0 63 $_SESSION['authorize_page_title'] = t('Installing updates');
Chris@0 64
Chris@0 65 // Invoke the batch via authorize.php.
Chris@0 66 return system_authorized_batch_process();
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Installs a new project when invoked by authorize.php.
Chris@0 71 *
Chris@0 72 * Callback for system_authorized_init() in
Chris@0 73 * update_manager_install_form_submit().
Chris@0 74 *
Chris@0 75 * @param FileTransfer $filetransfer
Chris@0 76 * The FileTransfer object created by authorize.php for use during this
Chris@0 77 * operation.
Chris@0 78 * @param string $project
Chris@0 79 * The canonical project short name; i.e., the name of the module, theme, or
Chris@0 80 * profile.
Chris@0 81 * @param string $updater_name
Chris@0 82 * The name of the Drupal\Core\Updater\Updater class to use for installing
Chris@0 83 * this project.
Chris@0 84 * @param string $local_url
Chris@0 85 * The URL to the locally installed temp directory where the project has
Chris@0 86 * already been downloaded and extracted into.
Chris@0 87 *
Chris@0 88 * @return \Symfony\Component\HttpFoundation\Response|null
Chris@0 89 * The result of processing the batch that installs the project. If this is
Chris@0 90 * an instance of \Symfony\Component\HttpFoundation\Response the calling code
Chris@0 91 * should use that response for the current page request.
Chris@0 92 */
Chris@0 93 function update_authorize_run_install($filetransfer, $project, $updater_name, $local_url) {
Chris@0 94 $operations[] = [
Chris@0 95 'update_authorize_batch_copy_project',
Chris@0 96 [
Chris@0 97 $project,
Chris@0 98 $updater_name,
Chris@0 99 $local_url,
Chris@0 100 $filetransfer,
Chris@0 101 ],
Chris@0 102 ];
Chris@0 103
Chris@0 104 // @todo Instantiate our Updater to set the human-readable title?
Chris@0 105 $batch = [
Chris@0 106 'init_message' => t('Preparing to install'),
Chris@0 107 'operations' => $operations,
Chris@0 108 // @todo Use a different finished callback for different messages?
Chris@0 109 'finished' => 'update_authorize_install_batch_finished',
Chris@0 110 'file' => drupal_get_path('module', 'update') . '/update.authorize.inc',
Chris@0 111 ];
Chris@0 112 batch_set($batch);
Chris@0 113
Chris@0 114 // Since authorize.php has its own method for setting the page title, set it
Chris@0 115 // manually here rather than passing it in to batch_set() as would normally
Chris@0 116 // be done.
Chris@0 117 $_SESSION['authorize_page_title'] = t('Installing %project', ['%project' => $project]);
Chris@0 118
Chris@0 119 // Invoke the batch via authorize.php.
Chris@0 120 return system_authorized_batch_process();
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Implements callback_batch_operation().
Chris@0 125 *
Chris@0 126 * Copies project to its proper place when authorized to do so.
Chris@0 127 *
Chris@0 128 * @param string $project
Chris@0 129 * The canonical short name of the project being installed.
Chris@0 130 * @param string $updater_name
Chris@0 131 * The name of the Drupal\Core\Updater\Updater class to use for installing
Chris@0 132 * this project.
Chris@0 133 * @param string $local_url
Chris@0 134 * The URL to the locally installed temp directory where the project has
Chris@0 135 * already been downloaded and extracted into.
Chris@0 136 * @param FileTransfer $filetransfer
Chris@0 137 * The FileTransfer object to use for performing this operation.
Chris@0 138 * @param array $context
Chris@0 139 * Reference to an array used for Batch API storage.
Chris@0 140 */
Chris@0 141 function update_authorize_batch_copy_project($project, $updater_name, $local_url, $filetransfer, &$context) {
Chris@0 142
Chris@0 143 // Initialize some variables in the Batch API $context array.
Chris@0 144 if (!isset($context['results']['log'])) {
Chris@0 145 $context['results']['log'] = [];
Chris@0 146 }
Chris@0 147 if (!isset($context['results']['log'][$project])) {
Chris@0 148 $context['results']['log'][$project] = [];
Chris@0 149 }
Chris@0 150
Chris@0 151 if (!isset($context['results']['tasks'])) {
Chris@0 152 $context['results']['tasks'] = [];
Chris@0 153 }
Chris@0 154
Chris@0 155 // The batch API uses a session, and since all the arguments are serialized
Chris@0 156 // and unserialized between requests, although the FileTransfer object itself
Chris@0 157 // will be reconstructed, the connection pointer itself will be lost. However,
Chris@0 158 // the FileTransfer object will still have the connection variable, even
Chris@0 159 // though the connection itself is now gone. So, although it's ugly, we have
Chris@0 160 // to unset the connection variable at this point so that the FileTransfer
Chris@0 161 // object will re-initiate the actual connection.
Chris@0 162 unset($filetransfer->connection);
Chris@0 163
Chris@0 164 if (!empty($context['results']['log'][$project]['#abort'])) {
Chris@0 165 $context['finished'] = 1;
Chris@0 166 return;
Chris@0 167 }
Chris@0 168
Chris@0 169 $updater = new $updater_name($local_url, \Drupal::getContainer()->get('update.root'));
Chris@0 170
Chris@0 171 try {
Chris@0 172 if ($updater->isInstalled()) {
Chris@0 173 // This is an update.
Chris@0 174 $tasks = $updater->update($filetransfer);
Chris@0 175 }
Chris@0 176 else {
Chris@0 177 $tasks = $updater->install($filetransfer);
Chris@0 178 }
Chris@0 179 }
Chris@0 180 catch (UpdaterException $e) {
Chris@0 181 _update_batch_create_message($context['results']['log'][$project], t('Error installing / updating'), FALSE);
Chris@0 182 _update_batch_create_message($context['results']['log'][$project], $e->getMessage(), FALSE);
Chris@0 183 $context['results']['log'][$project]['#abort'] = TRUE;
Chris@0 184 return;
Chris@0 185 }
Chris@0 186
Chris@0 187 _update_batch_create_message($context['results']['log'][$project], t('Installed %project_name successfully', ['%project_name' => $project]));
Chris@0 188 if (!empty($tasks)) {
Chris@0 189 $context['results']['tasks'] += $tasks;
Chris@0 190 }
Chris@0 191
Chris@0 192 // This particular operation is now complete, even though the batch might
Chris@0 193 // have other operations to perform.
Chris@0 194 $context['finished'] = 1;
Chris@0 195 }
Chris@0 196
Chris@0 197 /**
Chris@0 198 * Batch callback: Performs actions when the authorized update batch is done.
Chris@0 199 *
Chris@0 200 * This processes the results and stashes them into SESSION such that
Chris@0 201 * authorize.php will render a report. Also responsible for putting the site
Chris@0 202 * back online and clearing the update status storage after a successful update.
Chris@0 203 *
Chris@0 204 * @param $success
Chris@0 205 * TRUE if the batch operation was successful; FALSE if there were errors.
Chris@0 206 * @param $results
Chris@0 207 * An associative array of results from the batch operation.
Chris@0 208 */
Chris@0 209 function update_authorize_update_batch_finished($success, $results) {
Chris@0 210 foreach ($results['log'] as $messages) {
Chris@0 211 if (!empty($messages['#abort'])) {
Chris@0 212 $success = FALSE;
Chris@0 213 }
Chris@0 214 }
Chris@0 215 $offline = \Drupal::state()->get('system.maintenance_mode');
Chris@0 216 if ($success) {
Chris@0 217 // Now that the update completed, we need to clear the available update data
Chris@0 218 // and recompute our status, so prevent show bogus results.
Chris@0 219 _update_authorize_clear_update_status();
Chris@0 220
Chris@0 221 // Take the site out of maintenance mode if it was previously that way.
Chris@0 222 if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
Chris@0 223 \Drupal::state()->set('system.maintenance_mode', FALSE);
Chris@0 224 $page_message = [
Chris@0 225 'message' => t('Update was completed successfully. Your site has been taken out of maintenance mode.'),
Chris@0 226 'type' => 'status',
Chris@0 227 ];
Chris@0 228 }
Chris@0 229 else {
Chris@0 230 $page_message = [
Chris@0 231 'message' => t('Update was completed successfully.'),
Chris@0 232 'type' => 'status',
Chris@0 233 ];
Chris@0 234 }
Chris@0 235 }
Chris@0 236 elseif (!$offline) {
Chris@0 237 $page_message = [
Chris@0 238 'message' => t('Update failed! See the log below for more information.'),
Chris@0 239 'type' => 'error',
Chris@0 240 ];
Chris@0 241 }
Chris@0 242 else {
Chris@0 243 $page_message = [
Chris@0 244 'message' => t('Update failed! See the log below for more information. Your site is still in maintenance mode.'),
Chris@0 245 'type' => 'error',
Chris@0 246 ];
Chris@0 247 }
Chris@0 248 // Since we're doing an update of existing code, always add a task for
Chris@0 249 // running update.php.
Chris@0 250 $url = Url::fromRoute('system.db_update');
Chris@0 251 $results['tasks'][] = t('Your modules have been downloaded and updated.');
Chris@0 252 $results['tasks'][] = [
Chris@0 253 '#type' => 'link',
Chris@0 254 '#url' => $url,
Chris@0 255 '#title' => t('Run database updates'),
Chris@17 256 // Since this is being called outside of the primary front controller,
Chris@0 257 // the base_url needs to be set explicitly to ensure that links are
Chris@0 258 // relative to the site root.
Chris@0 259 // @todo Simplify with https://www.drupal.org/node/2548095
Chris@0 260 '#options' => [
Chris@0 261 'absolute' => TRUE,
Chris@0 262 'base_url' => $GLOBALS['base_url'],
Chris@0 263 ],
Chris@17 264 '#access' => $url->access(\Drupal::currentUser()),
Chris@0 265 ];
Chris@0 266
Chris@0 267 // Unset the variable since it is no longer needed.
Chris@0 268 unset($_SESSION['maintenance_mode']);
Chris@0 269
Chris@0 270 // Set all these values into the SESSION so authorize.php can display them.
Chris@0 271 $_SESSION['authorize_results']['success'] = $success;
Chris@0 272 $_SESSION['authorize_results']['page_message'] = $page_message;
Chris@0 273 $_SESSION['authorize_results']['messages'] = $results['log'];
Chris@0 274 $_SESSION['authorize_results']['tasks'] = $results['tasks'];
Chris@0 275 $_SESSION['authorize_page_title'] = t('Update manager');
Chris@0 276 }
Chris@0 277
Chris@0 278 /**
Chris@0 279 * Implements callback_batch_finished().
Chris@0 280 *
Chris@0 281 * Performs actions when the authorized install batch is done.
Chris@0 282 *
Chris@0 283 * This processes the results and stashes them into SESSION such that
Chris@0 284 * authorize.php will render a report. Also responsible for putting the site
Chris@0 285 * back online after a successful install if necessary.
Chris@0 286 *
Chris@0 287 * @param $success
Chris@0 288 * TRUE if the batch operation was a success; FALSE if there were errors.
Chris@0 289 * @param $results
Chris@0 290 * An associative array of results from the batch operation.
Chris@0 291 */
Chris@0 292 function update_authorize_install_batch_finished($success, $results) {
Chris@0 293 foreach ($results['log'] as $messages) {
Chris@0 294 if (!empty($messages['#abort'])) {
Chris@0 295 $success = FALSE;
Chris@0 296 }
Chris@0 297 }
Chris@0 298 $offline = \Drupal::state()->get('system.maintenance_mode');
Chris@0 299 if ($success) {
Chris@0 300 // Take the site out of maintenance mode if it was previously that way.
Chris@0 301 if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
Chris@0 302 \Drupal::state()->set('system.maintenance_mode', FALSE);
Chris@0 303 $page_message = [
Chris@0 304 'message' => t('Installation was completed successfully. Your site has been taken out of maintenance mode.'),
Chris@0 305 'type' => 'status',
Chris@0 306 ];
Chris@0 307 }
Chris@0 308 else {
Chris@0 309 $page_message = [
Chris@0 310 'message' => t('Installation was completed successfully.'),
Chris@0 311 'type' => 'status',
Chris@0 312 ];
Chris@0 313 }
Chris@0 314 }
Chris@0 315 elseif (!$success && !$offline) {
Chris@0 316 $page_message = [
Chris@0 317 'message' => t('Installation failed! See the log below for more information.'),
Chris@0 318 'type' => 'error',
Chris@0 319 ];
Chris@0 320 }
Chris@0 321 else {
Chris@0 322 $page_message = [
Chris@0 323 'message' => t('Installation failed! See the log below for more information. Your site is still in maintenance mode.'),
Chris@0 324 'type' => 'error',
Chris@0 325 ];
Chris@0 326 }
Chris@0 327
Chris@0 328 // Unset the variable since it is no longer needed.
Chris@0 329 unset($_SESSION['maintenance_mode']);
Chris@0 330
Chris@0 331 // Set all these values into the SESSION so authorize.php can display them.
Chris@0 332 $_SESSION['authorize_results']['success'] = $success;
Chris@0 333 $_SESSION['authorize_results']['page_message'] = $page_message;
Chris@0 334 $_SESSION['authorize_results']['messages'] = $results['log'];
Chris@0 335 $_SESSION['authorize_results']['tasks'] = $results['tasks'];
Chris@0 336 $_SESSION['authorize_page_title'] = t('Update manager');
Chris@0 337 }
Chris@0 338
Chris@0 339 /**
Chris@0 340 * Creates a structure of log messages.
Chris@0 341 *
Chris@0 342 * @param array $project_results
Chris@0 343 * An associative array of results from the batch operation.
Chris@0 344 * @param string $message
Chris@0 345 * A string containing a log message.
Chris@0 346 * @param bool $success
Chris@0 347 * (optional) TRUE if the operation the message is about was a success, FALSE
Chris@0 348 * if there were errors. Defaults to TRUE.
Chris@0 349 */
Chris@0 350 function _update_batch_create_message(&$project_results, $message, $success = TRUE) {
Chris@0 351 $project_results[] = ['message' => $message, 'success' => $success];
Chris@0 352 }
Chris@0 353
Chris@0 354 /**
Chris@0 355 * Clears available update status data.
Chris@0 356 *
Chris@0 357 * Since this function is run at such a low bootstrap level, the Update Manager
Chris@0 358 * module is not loaded. So, we can't just call update_storage_clear(). However,
Chris@0 359 * the key-value backend is available, so we just call that.
Chris@0 360 *
Chris@0 361 * Note that we do not want to delete items related to currently pending fetch
Chris@0 362 * attempts.
Chris@0 363 *
Chris@0 364 * @see update_authorize_update_batch_finished()
Chris@0 365 * @see update_storage_clear()
Chris@0 366 */
Chris@0 367 function _update_authorize_clear_update_status() {
Chris@0 368 \Drupal::keyValueExpirable('update')->deleteAll();
Chris@0 369 \Drupal::keyValueExpirable('update_available_release')->deleteAll();
Chris@0 370 }