Upgraded drupal core with security updates
[yaffs-website] / web / core / includes / batch.inc
1 <?php
2
3 /**
4  * @file
5  * Batch processing API for processes to run in multiple HTTP requests.
6  *
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
9  * form.inc.
10  *
11  * @see form.inc
12  * @see batch_set()
13  * @see batch_process()
14  * @see batch_get()
15  */
16
17 use Drupal\Component\Utility\Timer;
18 use Drupal\Component\Utility\UrlHelper;
19 use Drupal\Core\Batch\Percentage;
20 use Drupal\Core\Form\FormState;
21 use Drupal\Core\Url;
22 use Symfony\Component\HttpFoundation\JsonResponse;
23 use Symfony\Component\HttpFoundation\Request;
24 use Symfony\Component\HttpFoundation\RedirectResponse;
25
26 /**
27  * Renders the batch processing page based on the current state of the batch.
28  *
29  * @param \Symfony\Component\HttpFoundation\Request $request
30  *   The current request object.
31  *
32  * @see _batch_shutdown()
33  */
34 function _batch_page(Request $request) {
35   $batch = &batch_get();
36
37   if (!($request_id = $request->query->get('id'))) {
38     return FALSE;
39   }
40
41   // Retrieve the current state of the batch.
42   if (!$batch) {
43     $batch = \Drupal::service('batch.storage')->load($request_id);
44     if (!$batch) {
45       drupal_set_message(t('No active batch.'), 'error');
46       return new RedirectResponse(\Drupal::url('<front>', [], ['absolute' => TRUE]));
47     }
48   }
49
50   // Register database update for the end of processing.
51   drupal_register_shutdown_function('_batch_shutdown');
52
53   $build = [];
54
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;
60       }
61     }
62   }
63
64   $op = $request->query->get('op', '');
65   switch ($op) {
66     case 'start':
67     case 'do_nojs':
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();
73       break;
74
75     case 'do':
76       // JavaScript-based progress page callback.
77       return _batch_do();
78
79     case 'finished':
80       // _batch_finished() returns a RedirectResponse.
81       return _batch_finished();
82   }
83
84   return $build;
85 }
86
87 /**
88  * Does one execution pass with JavaScript and returns progress to the browser.
89  *
90  * @see _batch_progress_page_js()
91  * @see _batch_process()
92  */
93 function _batch_do() {
94   // Perform actual processing.
95   list($percentage, $message, $label) = _batch_process();
96
97   return new JsonResponse(['status' => TRUE, 'percentage' => $percentage, 'message' => $message, 'label' => $label]);
98 }
99
100 /**
101  * Outputs a batch processing page.
102  *
103  * @see _batch_process()
104  */
105 function _batch_progress_page() {
106   $batch = &batch_get();
107
108   $current_set = _batch_current_set();
109
110   $new_op = 'do_nojs';
111
112   if (!isset($batch['running'])) {
113     // This is the first page so we return some output immediately.
114     $percentage       = 0;
115     $message          = $current_set['init_message'];
116     $label            = '';
117     $batch['running'] = TRUE;
118   }
119   else {
120     // This is one of the later requests; do some processing first.
121
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.
125     ob_start();
126     $fallback = $current_set['error_message'] . '<br />' . $batch['error_message'];
127
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,
135     ]);
136
137     // Just use the content of the response.
138     $fallback = $response->getContent();
139
140     list($fallback) = explode('<!--partial-->', $fallback);
141     print $fallback;
142
143     // Perform actual processing.
144     list($percentage, $message, $label) = _batch_process($batch);
145     if ($percentage == 100) {
146       $new_op = 'finished';
147     }
148
149     // PHP did not die; remove the fallback output.
150     ob_end_clean();
151   }
152
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);
159
160   $url = $batch['url']->toString(TRUE)->getGeneratedUrl();
161
162   $build = [
163     '#theme' => 'progress_bar',
164     '#percent' => $percentage,
165     '#message' => ['#markup' => $message],
166     '#label' => $label,
167     '#attached' => [
168       'html_head' => [
169         [
170           [
171             // Redirect through a 'Refresh' meta tag if JavaScript is disabled.
172             '#tag' => 'meta',
173             '#noscript' => TRUE,
174             '#attributes' => [
175               'http-equiv' => 'Refresh',
176               'content' => '0; URL=' . $url,
177             ],
178           ],
179           'batch_progress_meta_refresh',
180         ],
181       ],
182       // Adds JavaScript code and settings for clients where JavaScript is enabled.
183       'drupalSettings' => [
184         'batch' => [
185           'errorMessage' => $current_set['error_message'] . '<br />' . $batch['error_message'],
186           'initMessage' => $current_set['init_message'],
187           'uri' => $url,
188         ],
189       ],
190       'library' => [
191         'core/drupal.batch',
192       ],
193     ],
194   ];
195   return $build;
196 }
197
198 /**
199  * Processes sets in a batch.
200  *
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
204  * the next request.
205  *
206  * @return array
207  *   An array containing a completion value (in percent) and a status message.
208  */
209 function _batch_process() {
210   $batch       = &batch_get();
211   $current_set = &_batch_current_set();
212   // Indicate that this batch set needs to be initialized.
213   $set_changed = TRUE;
214
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');
221   }
222
223   if (empty($current_set['start'])) {
224     $current_set['start'] = microtime(TRUE);
225   }
226
227   $queue = _batch_queue($current_set);
228
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
232     // definitions.
233     if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
234       include_once \Drupal::root() . '/' . $current_set['file'];
235     }
236
237     $task_message = $label = '';
238     // Assume a single pass operation and set the completion level to 1 by
239     // default.
240     $finished = 1;
241
242     if ($item = $queue->claimItem()) {
243       list($callback, $args) = $item->data;
244
245       // Build the 'context' array and execute the function call.
246       $batch_context = [
247         'sandbox'  => &$current_set['sandbox'],
248         'results'  => &$current_set['results'],
249         'finished' => &$finished,
250         'message'  => &$task_message,
251       ];
252       call_user_func_array($callback, array_merge($args, [&$batch_context]));
253
254       if ($finished >= 1) {
255         // Make sure this step is not counted twice when computing $current.
256         $finished = 0;
257         // Remove the processed operation and clear the sandbox.
258         $queue->deleteItem($item);
259         $current_set['count']--;
260         $current_set['sandbox'] = [];
261       }
262     }
263
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);
275       $set_changed = TRUE;
276     }
277
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);
281
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);
286       break;
287     }
288   }
289
290   if ($batch['progressive']) {
291     // Gather progress information.
292
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'];
301       $task_message     = '';
302     }
303     else {
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'];
308     }
309
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;
315     $values     = [
316       '@remaining'  => $remaining,
317       '@total'      => $total,
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) : '-',
323     ];
324     $message = strtr($progress_message, $values);
325     if (!empty($task_message)) {
326       $label = $task_message;
327     }
328
329     return [$percentage, $message, $label];
330   }
331   else {
332     // If we are not in progressive mode, the entire batch has been processed.
333     return _batch_finished();
334   }
335 }
336
337 /**
338  * Formats the percent completion for a batch set.
339  *
340  * @param int $total
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.
347  *
348  * @return string
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
352  *   are meaningful.
353  *
354  * @see _batch_process()
355  */
356 function _batch_api_percentage($total, $current) {
357   return Percentage::format($total, $current);
358 }
359
360 /**
361  * Returns the batch set being currently processed.
362  */
363 function &_batch_current_set() {
364   $batch = &batch_get();
365   return $batch['sets'][$batch['current_set']];
366 }
367
368 /**
369  * Retrieves the next set in a batch.
370  *
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.
374  *
375  * @return true|null
376  *   TRUE if a subsequent set was found in the batch; no value will be returned
377  *   if no subsequent set was found.
378  */
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']]);
389     }
390     return TRUE;
391   }
392 }
393
394 /**
395  * Ends the batch processing.
396  *
397  * Call the 'finished' callback of each batch set to allow custom handling of
398  * the results and resolve page redirection.
399  */
400 function _batch_finished() {
401   $batch = &batch_get();
402   $batch_finished_redirect = NULL;
403
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'];
410       }
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;
420         }
421       }
422     }
423   }
424
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();
431       }
432     }
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']);
438       }
439     }
440   }
441   $_batch = $batch;
442   $batch = NULL;
443
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']);
449     }
450
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;
456     }
457     elseif (!isset($_batch['form_state'])) {
458       $_batch['form_state'] = new FormState();
459     }
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);
467         }
468         else {
469           $redirect = \Drupal::pathValidator()->getUrlIfValid($options['path']);
470           if (!$redirect) {
471             // Stay on the same page if the redirect was invalid.
472             $redirect = Url::fromRoute('<current>');
473           }
474           $redirect->setOptions($options);
475         }
476       }
477       $_batch['form_state']->setRedirectUrl($redirect);
478     }
479
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)) {
484       return $redirect;
485     }
486
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'];
492     }
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'));
497     }
498     elseif ($callback === NULL) {
499       // Default to RedirectResponse objects when nothing specified.
500       return new RedirectResponse($_batch['source_url']->setAbsolute()->toString());
501     }
502   }
503 }
504
505 /**
506  * Shutdown function: Stores the current batch data for the next request.
507  *
508  * @see _batch_page()
509  * @see drupal_register_shutdown_function()
510  */
511 function _batch_shutdown() {
512   if ($batch = batch_get()) {
513     \Drupal::service('batch.storage')->update($batch);
514   }
515 }