You are here

protected function DbUpdateController::triggerBatch in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/src/Controller/DbUpdateController.php \Drupal\system\Controller\DbUpdateController::triggerBatch()
  2. 10 core/modules/system/src/Controller/DbUpdateController.php \Drupal\system\Controller\DbUpdateController::triggerBatch()

Starts the database update batch process.

Parameters

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

1 call to DbUpdateController::triggerBatch()
DbUpdateController::handle in core/modules/system/src/Controller/DbUpdateController.php
Returns a database update page.

File

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

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 = [];

  // 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 = [];
  foreach ($updates as $function => $update) {
    $dependency_map[$function] = !empty($update['reverse_paths']) ? array_keys($update['reverse_paths']) : [];
  }

  // 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[] = [
        'update_do_one',
        [
          $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 += [
    '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' => [
      '\\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'));
}