update.authorize.inc 11.7 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5 6 7 8 9 10
 * Callbacks and related functions invoked by authorize.php to update projects.
 *
 * We use the Batch API to actually update each individual project on the site.
 * All of the code in this file is run at a low bootstrap level (modules are not
 * loaded), so these functions cannot assume access to the rest of the code of
 * the Update Manager module.
11 12
 */

13 14
use Drupal\Core\Updater\UpdaterException;

15
/**
16 17 18 19
 * Updates existing projects when invoked by authorize.php.
 *
 * Callback for system_authorized_init() in
 * update_manager_update_ready_form_submit().
20 21 22 23 24 25 26
 *
 * @param $filetransfer
 *   The FileTransfer object created by authorize.php for use during this
 *   operation.
 * @param $projects
 *   A nested array of projects to install into the live webroot, keyed by
 *   project name. Each subarray contains the following keys:
27 28
 *   - project: The canonical project short name.
 *   - updater_name: The name of the Drupal\Core\Updater\Updater class to use
29
 *     for this project.
30
 *   - local_url: The locally installed location of new code to update with.
31 32 33
 */
function update_authorize_run_update($filetransfer, $projects) {
  $operations = array();
34
  foreach ($projects as $project_info) {
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    $operations[] = array(
      'update_authorize_batch_copy_project',
      array(
        $project_info['project'],
        $project_info['updater_name'],
        $project_info['local_url'],
        $filetransfer,
      ),
    );
  }

  $batch = array(
    'title' => t('Installing updates'),
    'init_message' => t('Preparing to update your site'),
    'operations' => $operations,
    'finished' => 'update_authorize_update_batch_finished',
    'file' => drupal_get_path('module', 'update') . '/update.authorize.inc',
  );

  batch_set($batch);
  // Invoke the batch via authorize.php.
56
  return system_authorized_batch_process();
57 58 59
}

/**
60 61 62 63
 * Installs a new project when invoked by authorize.php.
 *
 * Callback for system_authorized_init() in
 * update_manager_install_form_submit().
64 65 66 67 68
 *
 * @param FileTransfer $filetransfer
 *   The FileTransfer object created by authorize.php for use during this
 *   operation.
 * @param string $project
69 70
 *   The canonical project short name; i.e., the name of the module, theme, or
 *   profile.
71
 * @param string $updater_name
72 73
 *   The name of the Drupal\Core\Updater\Updater class to use for installing
 *   this project.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
 * @param string $local_url
 *   The URL to the locally installed temp directory where the project has
 *   already been downloaded and extracted into.
 */
function update_authorize_run_install($filetransfer, $project, $updater_name, $local_url) {
  $operations[] = array(
    'update_authorize_batch_copy_project',
    array(
      $project,
      $updater_name,
      $local_url,
      $filetransfer,
    ),
  );

  // @todo Instantiate our Updater to set the human-readable title?
  $batch = array(
    'title' => t('Installing %project', array('%project' => $project)),
    'init_message' => t('Preparing to install'),
    'operations' => $operations,
    // @todo Use a different finished callback for different messages?
    'finished' => 'update_authorize_install_batch_finished',
    'file' => drupal_get_path('module', 'update') . '/update.authorize.inc',
  );
  batch_set($batch);

  // Invoke the batch via authorize.php.
101
  return system_authorized_batch_process();
102 103 104
}

/**
105
 * Batch callback: Copies project to its proper place when authorized to do so.
106 107
 *
 * @param string $project
108
 *   The canonical short name of the project being installed.
109
 * @param string $updater_name
110 111
 *   The name of the Drupal\Core\Updater\Updater class to use for installing
 *   this project.
112 113 114 115 116
 * @param string $local_url
 *   The URL to the locally installed temp directory where the project has
 *   already been downloaded and extracted into.
 * @param FileTransfer $filetransfer
 *   The FileTransfer object to use for performing this operation.
117
 * @param array $context
118
 *   Reference to an array used for Batch API storage.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
 */
function update_authorize_batch_copy_project($project, $updater_name, $local_url, $filetransfer, &$context) {

  // Initialize some variables in the Batch API $context array.
  if (!isset($context['results']['log'])) {
    $context['results']['log'] = array();
  }
  if (!isset($context['results']['log'][$project])) {
    $context['results']['log'][$project] = array();
  }

  if (!isset($context['results']['tasks'])) {
    $context['results']['tasks'] = array();
  }

134 135 136 137 138 139 140
  // The batch API uses a session, and since all the arguments are serialized
  // and unserialized between requests, although the FileTransfer object itself
  // will be reconstructed, the connection pointer itself will be lost. However,
  // the FileTransfer object will still have the connection variable, even
  // though the connection itself is now gone. So, although it's ugly, we have
  // to unset the connection variable at this point so that the FileTransfer
  // object will re-initiate the actual connection.
141 142 143
  unset($filetransfer->connection);

  if (!empty($context['results']['log'][$project]['#abort'])) {
144
    $context['finished'] = 1;
145 146 147 148 149 150 151 152 153 154 155 156 157 158
    return;
  }

  $updater = new $updater_name($local_url);

  try {
    if ($updater->isInstalled()) {
      // This is an update.
      $tasks = $updater->update($filetransfer);
    }
    else {
      $tasks = $updater->install($filetransfer);
    }
  }
159 160
  catch (UpdaterException $e) {
    _update_batch_create_message($context['results']['log'][$project], t('Error installing / updating'), FALSE);
161 162 163 164 165 166
    _update_batch_create_message($context['results']['log'][$project], $e->getMessage(), FALSE);
    $context['results']['log'][$project]['#abort'] = TRUE;
    return;
  }

  _update_batch_create_message($context['results']['log'][$project], t('Installed %project_name successfully', array('%project_name' => $project)));
167 168 169
  if (!empty($tasks)) {
    $context['results']['tasks'] += $tasks;
  }
170 171 172 173 174 175 176

  // This particular operation is now complete, even though the batch might
  // have other operations to perform.
  $context['finished'] = 1;
}

/**
177
 * Batch callback: Performs actions when the authorized update batch is done.
178 179 180
 *
 * This processes the results and stashes them into SESSION such that
 * authorize.php will render a report. Also responsible for putting the site
181
 * back online and clearing the update status storage after a successful update.
182 183 184 185 186
 *
 * @param $success
 *   TRUE if the batch operation was successful; FALSE if there were errors.
 * @param $results
 *   An associative array of results from the batch operation.
187 188
 */
function update_authorize_update_batch_finished($success, $results) {
189
  foreach ($results['log'] as $messages) {
190 191 192 193
    if (!empty($messages['#abort'])) {
      $success = FALSE;
    }
  }
194
  $offline = \Drupal::state()->get('system.maintenance_mode');
195
  if ($success) {
196 197
    // Now that the update completed, we need to clear the available update data
    // and recompute our status, so prevent show bogus results.
198 199
    _update_authorize_clear_update_status();

200 201
    // Take the site out of maintenance mode if it was previously that way.
    if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
202
      \Drupal::state()->set('system.maintenance_mode', FALSE);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
      $page_message = array(
        'message' => t('Update was completed successfully. Your site has been taken out of maintenance mode.'),
        'type' => 'status',
      );
    }
    else {
      $page_message = array(
        'message' => t('Update was completed successfully.'),
        'type' => 'status',
      );
    }
  }
  elseif (!$offline) {
    $page_message = array(
      'message' => t('Update failed! See the log below for more information.'),
      'type' => 'error',
    );
  }
  else {
    $page_message = array(
      'message' => t('Update failed! See the log below for more information. Your site is still in maintenance mode.'),
      'type' => 'error',
    );
  }
227 228 229
  // Since we're doing an update of existing code, always add a task for
  // running update.php.
  $results['tasks'][] = t('Your modules have been downloaded and updated.');
230
  $results['tasks'][] = t('<a href="@update">Run database updates</a>', array('@update' => base_path() . 'core/update.php'));
231

232 233 234
  // Unset the variable since it is no longer needed.
  unset($_SESSION['maintenance_mode']);

235 236 237 238 239 240 241 242
  // Set all these values into the SESSION so authorize.php can display them.
  $_SESSION['authorize_results']['success'] = $success;
  $_SESSION['authorize_results']['page_message'] = $page_message;
  $_SESSION['authorize_results']['messages'] = $results['log'];
  $_SESSION['authorize_results']['tasks'] = $results['tasks'];
}

/**
243
 * Batch callback: Performs actions when the authorized install batch is done.
244 245 246 247
 *
 * This processes the results and stashes them into SESSION such that
 * authorize.php will render a report. Also responsible for putting the site
 * back online after a successful install if necessary.
248 249 250 251 252
 *
 * @param $success
 *   TRUE if the batch operation was a success; FALSE if there were errors.
 * @param $results
 *   An associative array of results from the batch operation.
253 254
 */
function update_authorize_install_batch_finished($success, $results) {
255
  foreach ($results['log'] as $messages) {
256 257 258 259
    if (!empty($messages['#abort'])) {
      $success = FALSE;
    }
  }
260
  $offline = \Drupal::state()->get('system.maintenance_mode');
261 262 263
  if ($success) {
    // Take the site out of maintenance mode if it was previously that way.
    if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
264
      \Drupal::state()->set('system.maintenance_mode', FALSE);
265 266 267 268 269 270 271 272 273 274 275
      $page_message = array(
        'message' => t('Installation was completed successfully. Your site has been taken out of maintenance mode.'),
        'type' => 'status',
      );
    }
    else {
      $page_message = array(
        'message' => t('Installation was completed successfully.'),
        'type' => 'status',
      );
    }
276 277 278 279 280 281 282 283 284 285 286 287 288 289
  }
  elseif (!$success && !$offline) {
    $page_message = array(
      'message' => t('Installation failed! See the log below for more information.'),
      'type' => 'error',
    );
  }
  else {
    $page_message = array(
      'message' => t('Installation failed! See the log below for more information. Your site is still in maintenance mode.'),
      'type' => 'error',
    );
  }

290 291 292
  // Unset the variable since it is no longer needed.
  unset($_SESSION['maintenance_mode']);

293 294 295 296 297 298 299 300
  // Set all these values into the SESSION so authorize.php can display them.
  $_SESSION['authorize_results']['success'] = $success;
  $_SESSION['authorize_results']['page_message'] = $page_message;
  $_SESSION['authorize_results']['messages'] = $results['log'];
  $_SESSION['authorize_results']['tasks'] = $results['tasks'];
}

/**
301
 * Creates a structure of log messages.
302 303
 *
 * @param array $project_results
304
 *   An associative array of results from the batch operation.
305
 * @param string $message
306
 *   A string containing a log message.
307
 * @param bool $success
308 309
 *   (optional) TRUE if the operation the message is about was a success, FALSE
 *   if there were errors. Defaults to TRUE.
310 311 312 313 314 315
 */
function _update_batch_create_message(&$project_results, $message, $success = TRUE) {
  $project_results[] = array('message' => $message, 'success' => $success);
}

/**
316
 * Clears available update status data.
317
 *
318
 * Since this function is run at such a low bootstrap level, the Update Manager
319 320
 * module is not loaded. So, we can't just call update_storage_clear(). However,
 * the key-value backend is available, so we just call that.
321
 *
322 323
 * Note that we do not want to delete  items related to currently pending fetch
 * attempts.
324 325
 *
 * @see update_authorize_update_batch_finished()
326
 * @see update_storage_clear()
327 328
 */
function _update_authorize_clear_update_status() {
329 330
  \Drupal::keyValueExpirable('update')->deleteAll();
  \Drupal::keyValueExpirable('update_available_release')->deleteAll();
331
}