DbUpdateController::triggerBatch

protected DbUpdateController::triggerBatch(Request $request)

Starts the database update batch process.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

File

core/modules/system/src/Controller/DbUpdateController.php, line 568

Class

DbUpdateController
Controller routines for database update routes.

Namespace

Drupal\system\Controller

Code

protected function triggerBatch(Request $request) {
  $maintenance_mode = $this->state->get('system.maintenance_mode', FALSE);
  // Store the current maintenance mode status in the session so that it can
  // be restored at the end of the batch.
  $_SESSION['maintenance_mode'] = $maintenance_mode;
  // During the update, always put the site into maintenance mode so that
  // in-progress schema changes do not affect visiting users.
  if (empty($maintenance_mode)) {
    $this->state->set('system.maintenance_mode', TRUE);
  }

  $operations = array();

  // Resolve any update dependencies to determine the actual updates that will
  // be run and the order they will be run in.
  $start = $this->getModuleUpdates();
  $updates = update_resolve_dependencies($start);

  // Store the dependencies for each update function in an array which the
  // batch API can pass in to the batch operation each time it is called. (We
  // do not store the entire update dependency array here because it is
  // potentially very large.)
  $dependency_map = array();
  foreach ($updates as $function => $update) {
    $dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : array();
  }

  // Determine updates to be performed.
  foreach ($updates as $function => $update) {
    if ($update['allowed']) {
      // Set the installed version of each module so updates will start at the
      // correct place. (The updates are already sorted, so we can simply base
      // this on the first one we come across in the above foreach loop.)
      if (isset($start[$update['module']])) {
        drupal_set_installed_schema_version($update['module'], $update['number'] - 1);
        unset($start[$update['module']]);
      }
      $operations[] = array('update_do_one', array($update['module'], $update['number'], $dependency_map[$function]));
    }
  }

  $post_updates = $this->postUpdateRegistry->getPendingUpdateFunctions();

  if ($post_updates) {
    // Now we rebuild all caches and after that execute the hook_post_update()
    // functions.
    $operations[] = ['drupal_flush_all_caches', []];
    foreach ($post_updates as $function) {
      $operations[] = ['update_invoke_post_update', [$function]];
    }
  }

  $batch['operations'] = $operations;
  $batch += array(
    'title' => $this->t('Updating'),
    'init_message' => $this->t('Starting updates'),
    'error_message' => $this->t('An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.'),
    'finished' => array('\Drupal\system\Controller\DbUpdateController', 'batchFinished'),
  );
  batch_set($batch);

  // @todo Revisit once https://www.drupal.org/node/2548095 is in.
  return batch_process(Url::fromUri('base://results'), Url::fromUri('base://start'));
}
doc_Drupal
2016-10-29 09:01:24
Comments
Leave a Comment

Please login to continue.