- <?php
- * @file
- * Batch processing API for processes to run in multiple HTTP requests.
- *
- * Note that batches are usually invoked by form submissions, which is
- * why the core interaction functions of the batch processing API live in
- * form.inc.
- *
- * @see form.inc
- * @see batch_set()
- * @see batch_process()
- * @see batch_get()
- */
-
- * Loads a batch from the database.
- *
- * @param $id
- * The ID of the batch to load. When a progressive batch is being processed,
- * the relevant ID is found in $_REQUEST['id'].
- *
- * @return
- * An array representing the batch, or FALSE if no batch was found.
- */
- function batch_load($id) {
- $batch = db_query("SELECT batch FROM {batch} WHERE bid = :bid AND token = :token", array(
- ':bid' => $id,
- ':token' => backdrop_get_token($id),
- ))->fetchField();
- if ($batch) {
- return unserialize($batch);
- }
- return FALSE;
- }
-
- * Renders the batch processing page based on the current state of the batch.
- */
- function _batch_page() {
- $batch = &batch_get();
-
- if (!isset($_REQUEST['id'])) {
- return FALSE;
- }
-
-
- if (!$batch) {
- $batch = batch_load($_REQUEST['id']);
- if (!$batch) {
- backdrop_set_message(t('No active batch.'), 'error');
- backdrop_goto();
- }
- }
-
-
- foreach ($batch['sets'] as $batch_set) {
- if (isset($batch_set['css'])) {
- foreach ($batch_set['css'] as $css) {
- backdrop_add_css($css);
- }
- }
- }
-
-
- backdrop_show_messages(FALSE);
-
- $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
- $output = NULL;
- switch ($op) {
- case 'start':
-
-
- $output = _batch_progress_page();
- break;
-
- case 'do':
-
- _batch_do();
- break;
-
- case 'do_nojs':
-
- $output = _batch_progress_page();
- break;
-
- case 'finished':
-
- backdrop_show_messages(TRUE);
- _batch_finished();
- break;
- }
-
-
- db_update('batch')
- ->fields(array('batch' => serialize($batch)))
- ->condition('bid', $batch['id'])
- ->execute();
-
- return $output;
- }
-
- * Does one execution pass with JavaScript and returns progress to the browser.
- *
- * @see _batch_progress_page_js()
- * @see _batch_process()
- */
- function _batch_do() {
-
- list($percentage, $message) = _batch_process();
-
- backdrop_json_output(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message));
- }
-
- * Outputs a batch processing page.
- *
- * @see _batch_process()
- */
- function _batch_progress_page() {
- $batch = &batch_get();
-
- $current_set = _batch_current_set();
- backdrop_set_title($current_set['title'], PASS_THROUGH);
-
- $new_op = 'do_nojs';
-
- if (!isset($batch['running'])) {
-
- $percentage = 0;
- $message = $current_set['init_message'];
- $batch['running'] = TRUE;
- }
- else {
-
-
-
-
-
- ob_start();
- $fallback = $current_set['error_message'] . '<br />' . $batch['error_message'];
- $fallback = theme('maintenance_page', array('content' => $fallback, 'show_messages' => FALSE));
-
-
-
-
-
- list($fallback) = explode('<!--partial-->', $fallback);
- print $fallback;
-
-
- list($percentage, $message) = _batch_process();
- if ($percentage == 100) {
- $new_op = 'finished';
- }
-
-
- ob_end_clean();
- }
-
-
-
- $batch['url_options']['query']['id'] = $batch['id'];
- $batch['url_options']['query']['op'] = $new_op;
-
- $url = url($batch['url'], $batch['url_options']);
- $element = array(
-
- '#prefix' => '<noscript>',
- '#suffix' => '</noscript>',
- '#tag' => 'meta',
- '#attributes' => array(
- 'http-equiv' => 'Refresh',
- 'content' => '0; URL=' . $url,
- ),
- );
- backdrop_add_html_head($element, 'batch_progress_meta_refresh');
-
-
- $js_setting = array(
- 'batch' => array(
- 'errorMessage' => $current_set['error_message'] . '<br />' . $batch['error_message'],
- 'initMessage' => $current_set['init_message'],
- 'uri' => $url,
- ),
- );
- backdrop_add_js($js_setting, 'setting');
- backdrop_add_library('system', 'backdrop.batch');
-
- return theme('progress_bar', array('percent' => $percentage, 'message' => $message));
- }
-
- * Processes sets in a batch.
- *
- * If the batch was marked for progressive execution (default), this executes as
- * many operations in batch sets until an execution time of 1 second has been
- * exceeded. It will continue with the next operation of the same batch set in
- * the next request.
- *
- * @return array
- * An array containing a completion value (in percent) and a status message.
- */
- function _batch_process() {
- $batch = &batch_get();
- $current_set = &_batch_current_set();
- $old_set = array();
-
-
- $set_changed = TRUE;
-
-
-
-
-
- if ($batch['progressive']) {
- timer_start('batch_processing');
- }
-
- if (empty($current_set['start'])) {
- $current_set['start'] = microtime(TRUE);
- }
-
- $queue = _batch_queue($current_set);
- $finished = 0;
-
- while (!$current_set['success']) {
-
-
-
- if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
- include_once BACKDROP_ROOT . '/' . $current_set['file'];
- }
-
- $task_message = '';
-
-
- $finished = 1;
-
- if ($item = $queue->claimItem()) {
- list($function, $args) = $item->data;
-
-
- $batch_context = array(
- 'sandbox' => &$current_set['sandbox'],
- 'results' => &$current_set['results'],
- 'finished' => &$finished,
- 'message' => &$task_message,
- );
- call_user_func_array($function, array_merge($args, array(&$batch_context)));
-
- if ($finished >= 1) {
-
- $finished = 0;
-
- $queue->deleteItem($item);
- $current_set['count']--;
- $current_set['sandbox'] = array();
- }
- }
-
-
-
-
-
-
-
- $set_changed = FALSE;
- $old_set = $current_set;
- while (empty($current_set['count']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
- $current_set = &_batch_current_set();
- $current_set['start'] = microtime(TRUE);
- $set_changed = TRUE;
- }
-
-
-
- $queue = _batch_queue($current_set);
-
-
- if ($batch['progressive'] && timer_read('batch_processing') > 1000) {
-
- $current_set['elapsed'] = round((microtime(TRUE) - $current_set['start']) * 1000, 2);
- break;
- }
- }
-
- if ($batch['progressive']) {
-
-
-
-
-
- if ($set_changed && isset($current_set['queue'])) {
-
- $remaining = $current_set['count'];
- $total = $current_set['total'];
- $progress_message = $current_set['init_message'];
- $task_message = '';
- }
- else {
-
- $remaining = $old_set['count'];
- $total = $old_set['total'];
- $progress_message = $old_set['progress_message'];
- }
-
-
-
- $current = $total - $remaining + $finished;
- $percentage = _batch_api_percentage($total, $current);
- $elapsed = isset($current_set['elapsed']) ? $current_set['elapsed'] : 0;
- $values = array(
- '@remaining' => $remaining,
- '@total' => $total,
- '@current' => floor($current),
- '@percentage' => $percentage,
- '@elapsed' => format_interval($elapsed / 1000),
-
- '@estimate' => ($current > 0) ? format_interval(($elapsed * ($total - $current) / $current) / 1000) : '-',
- );
- $message = strtr($progress_message, $values);
- if (!empty($message)) {
- $message .= '<br />';
- }
- if (!empty($task_message)) {
- $message .= $task_message;
- }
-
- return array($percentage, $message);
- }
- else {
-
- _batch_finished();
- return array('100', '');
- }
- }
-
- * Formats the percent completion for a batch set.
- *
- * @param $total
- * The total number of operations.
- * @param $current
- * The number of the current operation. This may be a floating point number
- * rather than an integer in the case of a multi-step operation that is not
- * yet complete; in that case, the fractional part of $current represents the
- * fraction of the operation that has been completed.
- *
- * @return string
- * The properly formatted percentage, as a string. We output percentages
- * using the correct number of decimal places so that we never print "100%"
- * until we are finished, but we also never print more decimal places than
- * are meaningful.
- *
- * @see _batch_process()
- */
- function _batch_api_percentage($total, $current) {
- if (!$total || $total == $current) {
-
-
- $percentage = "100";
- }
- else {
-
-
- $decimal_places = max(0, floor(log10($total / 2.0)) - 1);
- do {
-
- $percentage = sprintf('%01.' . $decimal_places . 'f', round($current / $total * 100, $decimal_places));
-
-
-
-
-
- $decimal_places++;
- } while ($percentage == '100');
- }
- return $percentage;
- }
-
- * Returns the batch set being currently processed.
- */
- function &_batch_current_set() {
- $batch = &batch_get();
- return $batch['sets'][$batch['current_set']];
- }
-
- * Retrieves the next set in a batch.
- *
- * If there is a subsequent set in this batch, assign it as the new set to
- * process and execute its form submit handler (if defined), which may add
- * further sets to this batch.
- *
- * @return bool
- * TRUE if a subsequent set was found in the batch, FALSE if not found.
- */
- function _batch_next_set() {
- $batch = &batch_get();
- if (isset($batch['sets'][$batch['current_set'] + 1])) {
- $batch['current_set']++;
- $current_set = &_batch_current_set();
- if (isset($current_set['form_submit']) && ($function = $current_set['form_submit']) && function_exists($function)) {
-
-
- $function($batch['form_state']['complete_form'], $batch['form_state']);
- }
- return TRUE;
- }
- return FALSE;
- }
-
- * Ends the batch processing.
- *
- * Call the 'finished' callback of each batch set to allow custom handling of
- * the results and resolve page redirection.
- */
- function _batch_finished() {
- $batch = &batch_get();
-
-
- foreach ($batch['sets'] as $batch_set) {
- if (isset($batch_set['finished'])) {
-
- if (isset($batch_set['file']) && is_file($batch_set['file'])) {
- include_once BACKDROP_ROOT . '/' . $batch_set['file'];
- }
- if (is_callable($batch_set['finished'])) {
- $queue = _batch_queue($batch_set);
- $operations = $queue->getAllItems();
- call_user_func($batch_set['finished'], $batch_set['success'], $batch_set['results'], $operations, format_interval($batch_set['elapsed'] / 1000));
- }
- }
- }
-
-
- if ($batch['progressive']) {
- db_delete('batch')
- ->condition('bid', $batch['id'])
- ->execute();
- foreach ($batch['sets'] as $batch_set) {
- if ($queue = _batch_queue($batch_set)) {
- $queue->deleteQueue();
- }
- }
-
- if (isset($_SESSION)) {
- unset($_SESSION['batches'][$batch['id']]);
- if (empty($_SESSION['batches'])) {
- unset($_SESSION['batches']);
- }
- }
- }
- $_batch = $batch;
- $batch = NULL;
-
-
- if ($_batch['progressive']) {
-
- if (isset($_batch['destination'])) {
- $_GET['destination'] = $_batch['destination'];
- }
-
-
- if (!isset($_batch['form_state']['redirect'])) {
- if (isset($_batch['redirect'])) {
- $_batch['form_state']['redirect'] = $_batch['redirect'];
- }
- else {
- $_batch['form_state']['redirect'] = $_batch['source_url'];
- }
- }
-
-
- backdrop_redirect_form($_batch['form_state']);
-
-
-
-
- if (!empty($_batch['form_state']['rebuild'])) {
- $_SESSION['batch_form_state'] = $_batch['form_state'];
- }
- $function = $_batch['redirect_callback'];
- if (function_exists($function)) {
- $function($_batch['source_url'], array('query' => array('op' => 'finish', 'id' => $_batch['id'])));
- }
- }
- }