5 * Batch processing API for processes to run in multiple HTTP requests.
7 * Note that batches are usually invoked by form submissions, which is
8 * why the core interaction functions of the batch processing API live in
13 * @see batch_process()
17 use Drupal\Component\Utility\Timer;
18 use Drupal\Component\Utility\UrlHelper;
19 use Drupal\Core\Batch\Percentage;
20 use Drupal\Core\Form\FormState;
22 use Symfony\Component\HttpFoundation\JsonResponse;
23 use Symfony\Component\HttpFoundation\Request;
24 use Symfony\Component\HttpFoundation\RedirectResponse;
27 * Renders the batch processing page based on the current state of the batch.
29 * @param \Symfony\Component\HttpFoundation\Request $request
30 * The current request object.
32 * @see _batch_shutdown()
34 function _batch_page(Request $request) {
35 $batch = &batch_get();
37 if (!($request_id = $request->query->get('id'))) {
41 // Retrieve the current state of the batch.
43 $batch = \Drupal::service('batch.storage')->load($request_id);
45 drupal_set_message(t('No active batch.'), 'error');
46 return new RedirectResponse(\Drupal::url('<front>', [], ['absolute' => TRUE]));
50 // Register database update for the end of processing.
51 drupal_register_shutdown_function('_batch_shutdown');
55 // Add batch-specific libraries.
56 foreach ($batch['sets'] as $batch_set) {
57 if (isset($batch_set['library'])) {
58 foreach ($batch_set['library'] as $library) {
59 $build['#attached']['library'][] = $library;
64 $op = $request->query->get('op', '');
68 // Display the full progress page on startup and on each additional
69 // non-JavaScript iteration.
70 $current_set = _batch_current_set();
71 $build['#title'] = $current_set['title'];
72 $build['content'] = _batch_progress_page();
76 // JavaScript-based progress page callback.
80 // _batch_finished() returns a RedirectResponse.
81 return _batch_finished();
88 * Does one execution pass with JavaScript and returns progress to the browser.
90 * @see _batch_progress_page_js()
91 * @see _batch_process()
93 function _batch_do() {
94 // Perform actual processing.
95 list($percentage, $message, $label) = _batch_process();
97 return new JsonResponse(['status' => TRUE, 'percentage' => $percentage, 'message' => $message, 'label' => $label]);
101 * Outputs a batch processing page.
103 * @see _batch_process()
105 function _batch_progress_page() {
106 $batch = &batch_get();
108 $current_set = _batch_current_set();
112 if (!isset($batch['running'])) {
113 // This is the first page so we return some output immediately.
115 $message = $current_set['init_message'];
117 $batch['running'] = TRUE;
120 // This is one of the later requests; do some processing first.
122 // Error handling: if PHP dies due to a fatal error (e.g. a nonexistent
123 // function), it will output whatever is in the output buffer, followed by
124 // the error message.
126 $fallback = $current_set['error_message'] . '<br />' . $batch['error_message'];
128 // We strip the end of the page using a marker in the template, so any
129 // additional HTML output by PHP shows up inside the page rather than below
130 // it. While this causes invalid HTML, the same would be true if we didn't,
131 // as content is not allowed to appear after </html> anyway.
132 $bare_html_page_renderer = \Drupal::service('bare_html_page_renderer');
133 $response = $bare_html_page_renderer->renderBarePage(['#markup' => $fallback], $current_set['title'], 'maintenance_page', [
134 '#show_messages' => FALSE,
137 // Just use the content of the response.
138 $fallback = $response->getContent();
140 list($fallback) = explode('<!--partial-->', $fallback);
143 // Perform actual processing.
144 list($percentage, $message, $label) = _batch_process($batch);
145 if ($percentage == 100) {
146 $new_op = 'finished';
149 // PHP did not die; remove the fallback output.
153 // Merge required query parameters for batch processing into those provided by
154 // batch_set() or hook_batch_alter().
155 $query_options = $batch['url']->getOption('query');
156 $query_options['id'] = $batch['id'];
157 $query_options['op'] = $new_op;
158 $batch['url']->setOption('query', $query_options);
160 $url = $batch['url']->toString(TRUE)->getGeneratedUrl();
163 '#theme' => 'progress_bar',
164 '#percent' => $percentage,
165 '#message' => ['#markup' => $message],
171 // Redirect through a 'Refresh' meta tag if JavaScript is disabled.
175 'http-equiv' => 'Refresh',
176 'content' => '0; URL=' . $url,
179 'batch_progress_meta_refresh',
182 // Adds JavaScript code and settings for clients where JavaScript is enabled.
183 'drupalSettings' => [
185 'errorMessage' => $current_set['error_message'] . '<br />' . $batch['error_message'],
186 'initMessage' => $current_set['init_message'],
199 * Processes sets in a batch.
201 * If the batch was marked for progressive execution (default), this executes as
202 * many operations in batch sets until an execution time of 1 second has been
203 * exceeded. It will continue with the next operation of the same batch set in
207 * An array containing a completion value (in percent) and a status message.
209 function _batch_process() {
210 $batch = &batch_get();
211 $current_set = &_batch_current_set();
212 // Indicate that this batch set needs to be initialized.
215 // If this batch was marked for progressive execution (e.g. forms submitted by
216 // \Drupal::formBuilder()->submitForm(), initialize a timer to determine
217 // whether we need to proceed with the same batch phase when a processing time
218 // of 1 second has been exceeded.
219 if ($batch['progressive']) {
220 Timer::start('batch_processing');
223 if (empty($current_set['start'])) {
224 $current_set['start'] = microtime(TRUE);
227 $queue = _batch_queue($current_set);
229 while (!$current_set['success']) {
230 // If this is the first time we iterate this batch set in the current
231 // request, we check if it requires an additional file for functions
233 if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
234 include_once \Drupal::root() . '/' . $current_set['file'];
237 $task_message = $label = '';
238 // Assume a single pass operation and set the completion level to 1 by
242 if ($item = $queue->claimItem()) {
243 list($callback, $args) = $item->data;
245 // Build the 'context' array and execute the function call.
247 'sandbox' => &$current_set['sandbox'],
248 'results' => &$current_set['results'],
249 'finished' => &$finished,
250 'message' => &$task_message,
252 call_user_func_array($callback, array_merge($args, [&$batch_context]));
254 if ($finished >= 1) {
255 // Make sure this step is not counted twice when computing $current.
257 // Remove the processed operation and clear the sandbox.
258 $queue->deleteItem($item);
259 $current_set['count']--;
260 $current_set['sandbox'] = [];
264 // When all operations in the current batch set are completed, browse
265 // through the remaining sets, marking them 'successfully processed'
266 // along the way, until we find a set that contains operations.
267 // _batch_next_set() executes form submit handlers stored in 'control'
268 // sets (see \Drupal::service('form_submitter')), which can in turn add new
269 // sets to the batch.
270 $set_changed = FALSE;
271 $old_set = $current_set;
272 while (empty($current_set['count']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
273 $current_set = &_batch_current_set();
274 $current_set['start'] = microtime(TRUE);
278 // At this point, either $current_set contains operations that need to be
279 // processed or all sets have been completed.
280 $queue = _batch_queue($current_set);
282 // If we are in progressive mode, break processing after 1 second.
283 if ($batch['progressive'] && Timer::read('batch_processing') > 1000) {
284 // Record elapsed wall clock time.
285 $current_set['elapsed'] = round((microtime(TRUE) - $current_set['start']) * 1000, 2);
290 if ($batch['progressive']) {
291 // Gather progress information.
293 // Reporting 100% progress will cause the whole batch to be considered
294 // processed. If processing was paused right after moving to a new set,
295 // we have to use the info from the new (unprocessed) set.
296 if ($set_changed && isset($current_set['queue'])) {
297 // Processing will continue with a fresh batch set.
298 $remaining = $current_set['count'];
299 $total = $current_set['total'];
300 $progress_message = $current_set['init_message'];
304 // Processing will continue with the current batch set.
305 $remaining = $old_set['count'];
306 $total = $old_set['total'];
307 $progress_message = $old_set['progress_message'];
310 // Total progress is the number of operations that have fully run plus the
311 // completion level of the current operation.
312 $current = $total - $remaining + $finished;
313 $percentage = _batch_api_percentage($total, $current);
314 $elapsed = isset($current_set['elapsed']) ? $current_set['elapsed'] : 0;
316 '@remaining' => $remaining,
318 '@current' => floor($current),
319 '@percentage' => $percentage,
320 '@elapsed' => \Drupal::service('date.formatter')->formatInterval($elapsed / 1000),
321 // If possible, estimate remaining processing time.
322 '@estimate' => ($current > 0) ? \Drupal::service('date.formatter')->formatInterval(($elapsed * ($total - $current) / $current) / 1000) : '-',
324 $message = strtr($progress_message, $values);
325 if (!empty($task_message)) {
326 $label = $task_message;
329 return [$percentage, $message, $label];
332 // If we are not in progressive mode, the entire batch has been processed.
333 return _batch_finished();
338 * Formats the percent completion for a batch set.
341 * The total number of operations.
342 * @param int|float $current
343 * The number of the current operation. This may be a floating point number
344 * rather than an integer in the case of a multi-step operation that is not
345 * yet complete; in that case, the fractional part of $current represents the
346 * fraction of the operation that has been completed.
349 * The properly formatted percentage, as a string. We output percentages
350 * using the correct number of decimal places so that we never print "100%"
351 * until we are finished, but we also never print more decimal places than
354 * @see _batch_process()
356 function _batch_api_percentage($total, $current) {
357 return Percentage::format($total, $current);
361 * Returns the batch set being currently processed.
363 function &_batch_current_set() {
364 $batch = &batch_get();
365 return $batch['sets'][$batch['current_set']];
369 * Retrieves the next set in a batch.
371 * If there is a subsequent set in this batch, assign it as the new set to
372 * process and execute its form submit handler (if defined), which may add
373 * further sets to this batch.
376 * TRUE if a subsequent set was found in the batch; no value will be returned
377 * if no subsequent set was found.
379 function _batch_next_set() {
380 $batch = &batch_get();
381 if (isset($batch['sets'][$batch['current_set'] + 1])) {
382 $batch['current_set']++;
383 $current_set = &_batch_current_set();
384 if (isset($current_set['form_submit']) && ($callback = $current_set['form_submit']) && is_callable($callback)) {
385 // We use our stored copies of $form and $form_state to account for
386 // possible alterations by previous form submit handlers.
387 $complete_form = &$batch['form_state']->getCompleteForm();
388 call_user_func_array($callback, [&$complete_form, &$batch['form_state']]);
395 * Ends the batch processing.
397 * Call the 'finished' callback of each batch set to allow custom handling of
398 * the results and resolve page redirection.
400 function _batch_finished() {
401 $batch = &batch_get();
402 $batch_finished_redirect = NULL;
404 // Execute the 'finished' callbacks for each batch set, if defined.
405 foreach ($batch['sets'] as $batch_set) {
406 if (isset($batch_set['finished'])) {
407 // Check if the set requires an additional file for function definitions.
408 if (isset($batch_set['file']) && is_file($batch_set['file'])) {
409 include_once \Drupal::root() . '/' . $batch_set['file'];
411 if (is_callable($batch_set['finished'])) {
412 $queue = _batch_queue($batch_set);
413 $operations = $queue->getAllItems();
414 $batch_set_result = call_user_func_array($batch_set['finished'], [$batch_set['success'], $batch_set['results'], $operations, \Drupal::service('date.formatter')->formatInterval($batch_set['elapsed'] / 1000)]);
415 // If a batch 'finished' callback requested a redirect after the batch
416 // is complete, save that for later use. If more than one batch set
417 // returned a redirect, the last one is used.
418 if ($batch_set_result instanceof RedirectResponse) {
419 $batch_finished_redirect = $batch_set_result;
425 // Clean up the batch table and unset the static $batch variable.
426 if ($batch['progressive']) {
427 \Drupal::service('batch.storage')->delete($batch['id']);
428 foreach ($batch['sets'] as $batch_set) {
429 if ($queue = _batch_queue($batch_set)) {
430 $queue->deleteQueue();
433 // Clean-up the session. Not needed for CLI updates.
434 if (isset($_SESSION)) {
435 unset($_SESSION['batches'][$batch['id']]);
436 if (empty($_SESSION['batches'])) {
437 unset($_SESSION['batches']);
444 // Redirect if needed.
445 if ($_batch['progressive']) {
446 // Revert the 'destination' that was saved in batch_process().
447 if (isset($_batch['destination'])) {
448 \Drupal::request()->query->set('destination', $_batch['destination']);
451 // Determine the target path to redirect to. If a batch 'finished' callback
452 // returned a redirect response object, use that. Otherwise, fall back on
453 // the form redirection.
454 if (isset($batch_finished_redirect)) {
455 return $batch_finished_redirect;
457 elseif (!isset($_batch['form_state'])) {
458 $_batch['form_state'] = new FormState();
460 if ($_batch['form_state']->getRedirect() === NULL) {
461 $redirect = $_batch['batch_redirect'] ?: $_batch['source_url'];
462 // Any path with a scheme does not correspond to a route.
463 if (!$redirect instanceof Url) {
464 $options = UrlHelper::parse($redirect);
465 if (parse_url($options['path'], PHP_URL_SCHEME)) {
466 $redirect = Url::fromUri($options['path'], $options);
469 $redirect = \Drupal::pathValidator()->getUrlIfValid($options['path']);
471 // Stay on the same page if the redirect was invalid.
472 $redirect = Url::fromRoute('<current>');
474 $redirect->setOptions($options);
477 $_batch['form_state']->setRedirectUrl($redirect);
480 // Use \Drupal\Core\Form\FormSubmitterInterface::redirectForm() to handle
481 // the redirection logic.
482 $redirect = \Drupal::service('form_submitter')->redirectForm($_batch['form_state']);
483 if (is_object($redirect)) {
487 // If no redirection happened, redirect to the originating page. In case the
488 // form needs to be rebuilt, save the final $form_state for
489 // \Drupal\Core\Form\FormBuilderInterface::buildForm().
490 if ($_batch['form_state']->isRebuilding()) {
491 $_SESSION['batch_form_state'] = $_batch['form_state'];
493 $callback = $_batch['redirect_callback'];
494 $_batch['source_url']->mergeOptions(['query' => ['op' => 'finish', 'id' => $_batch['id']]]);
495 if (is_callable($callback)) {
496 $callback($_batch['source_url'], $_batch['source_url']->getOption('query'));
498 elseif ($callback === NULL) {
499 // Default to RedirectResponse objects when nothing specified.
500 return new RedirectResponse($_batch['source_url']->setAbsolute()->toString());
506 * Shutdown function: Stores the current batch data for the next request.
509 * @see drupal_register_shutdown_function()
511 function _batch_shutdown() {
512 if ($batch = batch_get()) {
513 \Drupal::service('batch.storage')->update($batch);