Chris@0: t('Preparing to update your site'), Chris@0: 'operations' => $operations, Chris@0: 'finished' => 'update_authorize_update_batch_finished', Chris@0: 'file' => drupal_get_path('module', 'update') . '/update.authorize.inc', Chris@0: ]; Chris@0: batch_set($batch); Chris@0: Chris@0: // Since authorize.php has its own method for setting the page title, set it Chris@0: // manually here rather than passing it in to batch_set() as would normally Chris@0: // be done. Chris@0: $_SESSION['authorize_page_title'] = t('Installing updates'); Chris@0: Chris@0: // Invoke the batch via authorize.php. Chris@0: return system_authorized_batch_process(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Installs a new project when invoked by authorize.php. Chris@0: * Chris@0: * Callback for system_authorized_init() in Chris@0: * update_manager_install_form_submit(). Chris@0: * Chris@0: * @param FileTransfer $filetransfer Chris@0: * The FileTransfer object created by authorize.php for use during this Chris@0: * operation. Chris@0: * @param string $project Chris@0: * The canonical project short name; i.e., the name of the module, theme, or Chris@0: * profile. Chris@0: * @param string $updater_name Chris@0: * The name of the Drupal\Core\Updater\Updater class to use for installing Chris@0: * this project. Chris@0: * @param string $local_url Chris@0: * The URL to the locally installed temp directory where the project has Chris@0: * already been downloaded and extracted into. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\Response|null Chris@0: * The result of processing the batch that installs the project. If this is Chris@0: * an instance of \Symfony\Component\HttpFoundation\Response the calling code Chris@0: * should use that response for the current page request. Chris@0: */ Chris@0: function update_authorize_run_install($filetransfer, $project, $updater_name, $local_url) { Chris@0: $operations[] = [ Chris@0: 'update_authorize_batch_copy_project', Chris@0: [ Chris@0: $project, Chris@0: $updater_name, Chris@0: $local_url, Chris@0: $filetransfer, Chris@0: ], Chris@0: ]; Chris@0: Chris@0: // @todo Instantiate our Updater to set the human-readable title? Chris@0: $batch = [ Chris@0: 'init_message' => t('Preparing to install'), Chris@0: 'operations' => $operations, Chris@0: // @todo Use a different finished callback for different messages? Chris@0: 'finished' => 'update_authorize_install_batch_finished', Chris@0: 'file' => drupal_get_path('module', 'update') . '/update.authorize.inc', Chris@0: ]; Chris@0: batch_set($batch); Chris@0: Chris@0: // Since authorize.php has its own method for setting the page title, set it Chris@0: // manually here rather than passing it in to batch_set() as would normally Chris@0: // be done. Chris@0: $_SESSION['authorize_page_title'] = t('Installing %project', ['%project' => $project]); Chris@0: Chris@0: // Invoke the batch via authorize.php. Chris@0: return system_authorized_batch_process(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements callback_batch_operation(). Chris@0: * Chris@0: * Copies project to its proper place when authorized to do so. Chris@0: * Chris@0: * @param string $project Chris@0: * The canonical short name of the project being installed. Chris@0: * @param string $updater_name Chris@0: * The name of the Drupal\Core\Updater\Updater class to use for installing Chris@0: * this project. Chris@0: * @param string $local_url Chris@0: * The URL to the locally installed temp directory where the project has Chris@0: * already been downloaded and extracted into. Chris@0: * @param FileTransfer $filetransfer Chris@0: * The FileTransfer object to use for performing this operation. Chris@0: * @param array $context Chris@0: * Reference to an array used for Batch API storage. Chris@0: */ Chris@0: function update_authorize_batch_copy_project($project, $updater_name, $local_url, $filetransfer, &$context) { Chris@0: Chris@0: // Initialize some variables in the Batch API $context array. Chris@0: if (!isset($context['results']['log'])) { Chris@0: $context['results']['log'] = []; Chris@0: } Chris@0: if (!isset($context['results']['log'][$project])) { Chris@0: $context['results']['log'][$project] = []; Chris@0: } Chris@0: Chris@0: if (!isset($context['results']['tasks'])) { Chris@0: $context['results']['tasks'] = []; Chris@0: } Chris@0: Chris@0: // The batch API uses a session, and since all the arguments are serialized Chris@0: // and unserialized between requests, although the FileTransfer object itself Chris@0: // will be reconstructed, the connection pointer itself will be lost. However, Chris@0: // the FileTransfer object will still have the connection variable, even Chris@0: // though the connection itself is now gone. So, although it's ugly, we have Chris@0: // to unset the connection variable at this point so that the FileTransfer Chris@0: // object will re-initiate the actual connection. Chris@0: unset($filetransfer->connection); Chris@0: Chris@0: if (!empty($context['results']['log'][$project]['#abort'])) { Chris@0: $context['finished'] = 1; Chris@0: return; Chris@0: } Chris@0: Chris@0: $updater = new $updater_name($local_url, \Drupal::getContainer()->get('update.root')); Chris@0: Chris@0: try { Chris@0: if ($updater->isInstalled()) { Chris@0: // This is an update. Chris@0: $tasks = $updater->update($filetransfer); Chris@0: } Chris@0: else { Chris@0: $tasks = $updater->install($filetransfer); Chris@0: } Chris@0: } Chris@0: catch (UpdaterException $e) { Chris@0: _update_batch_create_message($context['results']['log'][$project], t('Error installing / updating'), FALSE); Chris@0: _update_batch_create_message($context['results']['log'][$project], $e->getMessage(), FALSE); Chris@0: $context['results']['log'][$project]['#abort'] = TRUE; Chris@0: return; Chris@0: } Chris@0: Chris@0: _update_batch_create_message($context['results']['log'][$project], t('Installed %project_name successfully', ['%project_name' => $project])); Chris@0: if (!empty($tasks)) { Chris@0: $context['results']['tasks'] += $tasks; Chris@0: } Chris@0: Chris@0: // This particular operation is now complete, even though the batch might Chris@0: // have other operations to perform. Chris@0: $context['finished'] = 1; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Batch callback: Performs actions when the authorized update batch is done. Chris@0: * Chris@0: * This processes the results and stashes them into SESSION such that Chris@0: * authorize.php will render a report. Also responsible for putting the site Chris@0: * back online and clearing the update status storage after a successful update. Chris@0: * Chris@0: * @param $success Chris@0: * TRUE if the batch operation was successful; FALSE if there were errors. Chris@0: * @param $results Chris@0: * An associative array of results from the batch operation. Chris@0: */ Chris@0: function update_authorize_update_batch_finished($success, $results) { Chris@0: foreach ($results['log'] as $messages) { Chris@0: if (!empty($messages['#abort'])) { Chris@0: $success = FALSE; Chris@0: } Chris@0: } Chris@0: $offline = \Drupal::state()->get('system.maintenance_mode'); Chris@0: if ($success) { Chris@0: // Now that the update completed, we need to clear the available update data Chris@0: // and recompute our status, so prevent show bogus results. Chris@0: _update_authorize_clear_update_status(); Chris@0: Chris@0: // Take the site out of maintenance mode if it was previously that way. Chris@0: if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) { Chris@0: \Drupal::state()->set('system.maintenance_mode', FALSE); Chris@0: $page_message = [ Chris@0: 'message' => t('Update was completed successfully. Your site has been taken out of maintenance mode.'), Chris@0: 'type' => 'status', Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $page_message = [ Chris@0: 'message' => t('Update was completed successfully.'), Chris@0: 'type' => 'status', Chris@0: ]; Chris@0: } Chris@0: } Chris@0: elseif (!$offline) { Chris@0: $page_message = [ Chris@0: 'message' => t('Update failed! See the log below for more information.'), Chris@0: 'type' => 'error', Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $page_message = [ Chris@0: 'message' => t('Update failed! See the log below for more information. Your site is still in maintenance mode.'), Chris@0: 'type' => 'error', Chris@0: ]; Chris@0: } Chris@0: // Since we're doing an update of existing code, always add a task for Chris@0: // running update.php. Chris@0: $url = Url::fromRoute('system.db_update'); Chris@0: $results['tasks'][] = t('Your modules have been downloaded and updated.'); Chris@0: $results['tasks'][] = [ Chris@0: '#type' => 'link', Chris@0: '#url' => $url, Chris@0: '#title' => t('Run database updates'), Chris@17: // Since this is being called outside of the primary front controller, Chris@0: // the base_url needs to be set explicitly to ensure that links are Chris@0: // relative to the site root. Chris@0: // @todo Simplify with https://www.drupal.org/node/2548095 Chris@0: '#options' => [ Chris@0: 'absolute' => TRUE, Chris@0: 'base_url' => $GLOBALS['base_url'], Chris@0: ], Chris@17: '#access' => $url->access(\Drupal::currentUser()), Chris@0: ]; Chris@0: Chris@0: // Unset the variable since it is no longer needed. Chris@0: unset($_SESSION['maintenance_mode']); Chris@0: Chris@0: // Set all these values into the SESSION so authorize.php can display them. Chris@0: $_SESSION['authorize_results']['success'] = $success; Chris@0: $_SESSION['authorize_results']['page_message'] = $page_message; Chris@0: $_SESSION['authorize_results']['messages'] = $results['log']; Chris@0: $_SESSION['authorize_results']['tasks'] = $results['tasks']; Chris@0: $_SESSION['authorize_page_title'] = t('Update manager'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements callback_batch_finished(). Chris@0: * Chris@0: * Performs actions when the authorized install batch is done. Chris@0: * Chris@0: * This processes the results and stashes them into SESSION such that Chris@0: * authorize.php will render a report. Also responsible for putting the site Chris@0: * back online after a successful install if necessary. Chris@0: * Chris@0: * @param $success Chris@0: * TRUE if the batch operation was a success; FALSE if there were errors. Chris@0: * @param $results Chris@0: * An associative array of results from the batch operation. Chris@0: */ Chris@0: function update_authorize_install_batch_finished($success, $results) { Chris@0: foreach ($results['log'] as $messages) { Chris@0: if (!empty($messages['#abort'])) { Chris@0: $success = FALSE; Chris@0: } Chris@0: } Chris@0: $offline = \Drupal::state()->get('system.maintenance_mode'); Chris@0: if ($success) { Chris@0: // Take the site out of maintenance mode if it was previously that way. Chris@0: if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) { Chris@0: \Drupal::state()->set('system.maintenance_mode', FALSE); Chris@0: $page_message = [ Chris@0: 'message' => t('Installation was completed successfully. Your site has been taken out of maintenance mode.'), Chris@0: 'type' => 'status', Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $page_message = [ Chris@0: 'message' => t('Installation was completed successfully.'), Chris@0: 'type' => 'status', Chris@0: ]; Chris@0: } Chris@0: } Chris@0: elseif (!$success && !$offline) { Chris@0: $page_message = [ Chris@0: 'message' => t('Installation failed! See the log below for more information.'), Chris@0: 'type' => 'error', Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $page_message = [ Chris@0: 'message' => t('Installation failed! See the log below for more information. Your site is still in maintenance mode.'), Chris@0: 'type' => 'error', Chris@0: ]; Chris@0: } Chris@0: Chris@0: // Unset the variable since it is no longer needed. Chris@0: unset($_SESSION['maintenance_mode']); Chris@0: Chris@0: // Set all these values into the SESSION so authorize.php can display them. Chris@0: $_SESSION['authorize_results']['success'] = $success; Chris@0: $_SESSION['authorize_results']['page_message'] = $page_message; Chris@0: $_SESSION['authorize_results']['messages'] = $results['log']; Chris@0: $_SESSION['authorize_results']['tasks'] = $results['tasks']; Chris@0: $_SESSION['authorize_page_title'] = t('Update manager'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a structure of log messages. Chris@0: * Chris@0: * @param array $project_results Chris@0: * An associative array of results from the batch operation. Chris@0: * @param string $message Chris@0: * A string containing a log message. Chris@0: * @param bool $success Chris@0: * (optional) TRUE if the operation the message is about was a success, FALSE Chris@0: * if there were errors. Defaults to TRUE. Chris@0: */ Chris@0: function _update_batch_create_message(&$project_results, $message, $success = TRUE) { Chris@0: $project_results[] = ['message' => $message, 'success' => $success]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Clears available update status data. Chris@0: * Chris@0: * Since this function is run at such a low bootstrap level, the Update Manager Chris@0: * module is not loaded. So, we can't just call update_storage_clear(). However, Chris@0: * the key-value backend is available, so we just call that. Chris@0: * Chris@0: * Note that we do not want to delete items related to currently pending fetch Chris@0: * attempts. Chris@0: * Chris@0: * @see update_authorize_update_batch_finished() Chris@0: * @see update_storage_clear() Chris@0: */ Chris@0: function _update_authorize_clear_update_status() { Chris@0: \Drupal::keyValueExpirable('update')->deleteAll(); Chris@0: \Drupal::keyValueExpirable('update_available_release')->deleteAll(); Chris@0: }