webmaster@1: array('id' => $batch['id']))); webmaster@1: $js_setting = array( webmaster@1: 'batch' => array( webmaster@1: 'errorMessage' => $current_set['error_message'] .'
'. $batch['error_message'], webmaster@1: 'initMessage' => $current_set['init_message'], webmaster@1: 'uri' => $url, webmaster@1: ), webmaster@1: ); webmaster@1: drupal_add_js($js_setting, 'setting'); webmaster@1: drupal_add_js('misc/batch.js', 'core', 'header', FALSE, FALSE); webmaster@1: webmaster@1: $output = '
'; webmaster@1: return $output; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Do one pass of execution and inform back the browser about progression webmaster@1: * (used for JavaScript-mode only). webmaster@1: */ webmaster@1: function _batch_do() { webmaster@1: // HTTP POST required webmaster@1: if ($_SERVER['REQUEST_METHOD'] != 'POST') { webmaster@1: drupal_set_message(t('HTTP POST is required.'), 'error'); webmaster@1: drupal_set_title(t('Error')); webmaster@1: return ''; webmaster@1: } webmaster@1: webmaster@1: // Perform actual processing. webmaster@1: list($percentage, $message) = _batch_process(); webmaster@1: webmaster@1: drupal_json(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message)); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Batch processing page without JavaScript support. webmaster@1: */ webmaster@1: function _batch_progress_page_nojs() { webmaster@1: $batch =& batch_get(); webmaster@1: $current_set = _batch_current_set(); webmaster@1: webmaster@1: drupal_set_title($current_set['title']); webmaster@1: webmaster@1: $new_op = 'do_nojs'; webmaster@1: webmaster@1: if (!isset($batch['running'])) { webmaster@1: // This is the first page so we return some output immediately. webmaster@1: $percentage = 0; webmaster@1: $message = $current_set['init_message']; webmaster@1: $batch['running'] = TRUE; webmaster@1: } webmaster@1: else { webmaster@1: // This is one of the later requests: do some processing first. webmaster@1: webmaster@1: // Error handling: if PHP dies due to a fatal error (e.g. non-existant webmaster@1: // function), it will output whatever is in the output buffer, webmaster@1: // followed by the error message. webmaster@1: ob_start(); webmaster@1: $fallback = $current_set['error_message'] .'
'. $batch['error_message']; webmaster@1: $fallback = theme('maintenance_page', $fallback, FALSE, FALSE); webmaster@1: webmaster@1: // We strip the end of the page using a marker in the template, so any webmaster@1: // additional HTML output by PHP shows up inside the page rather than webmaster@1: // below it. While this causes invalid HTML, the same would be true if webmaster@1: // we didn't, as content is not allowed to appear after anyway. webmaster@1: list($fallback) = explode('', $fallback); webmaster@1: print $fallback; webmaster@1: webmaster@1: // Perform actual processing. webmaster@1: list($percentage, $message) = _batch_process($batch); webmaster@1: if ($percentage == 100) { webmaster@1: $new_op = 'finished'; webmaster@1: } webmaster@1: webmaster@1: // PHP did not die : remove the fallback output. webmaster@1: ob_end_clean(); webmaster@1: } webmaster@1: webmaster@1: $url = url($batch['url'], array('query' => array('id' => $batch['id'], 'op' => $new_op))); webmaster@1: drupal_set_html_head(''); webmaster@1: $output = theme('progress_bar', $percentage, $message); webmaster@1: return $output; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Advance batch processing for 1 second (or process the whole batch if it webmaster@1: * was not set for progressive execution - e.g forms submitted by drupal_execute). webmaster@1: */ webmaster@1: function _batch_process() { webmaster@1: $batch =& batch_get(); webmaster@1: $current_set =& _batch_current_set(); webmaster@1: $set_changed = TRUE; webmaster@1: webmaster@1: if ($batch['progressive']) { webmaster@1: timer_start('batch_processing'); webmaster@1: } webmaster@1: webmaster@1: while (!$current_set['success']) { webmaster@1: // If this is the first time we iterate this batch set in the current webmaster@1: // request, we check if it requires an additional file for functions webmaster@1: // definitions. webmaster@1: if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) { webmaster@1: include_once($current_set['file']); webmaster@1: } webmaster@1: webmaster@1: $finished = 1; webmaster@1: $task_message = ''; webmaster@1: if ((list($function, $args) = reset($current_set['operations'])) && function_exists($function)) { webmaster@1: // Build the 'context' array, execute the function call, webmaster@1: // and retrieve the user message. webmaster@1: $batch_context = array('sandbox' => &$current_set['sandbox'], 'results' => &$current_set['results'], 'finished' => &$finished, 'message' => &$task_message); webmaster@1: // Process the current operation. webmaster@1: call_user_func_array($function, array_merge($args, array(&$batch_context))); webmaster@1: } webmaster@1: webmaster@1: if ($finished == 1) { webmaster@1: // Make sure this step isn't counted double when computing $current. webmaster@1: $finished = 0; webmaster@1: // Remove the operation and clear the sandbox. webmaster@1: array_shift($current_set['operations']); webmaster@1: $current_set['sandbox'] = array(); webmaster@1: } webmaster@1: webmaster@1: // If the batch set is completed, browse through the remaining sets, webmaster@1: // executing 'control sets' (stored form submit handlers) along the way - webmaster@1: // this might in turn insert new batch sets. webmaster@1: // Stop when we find a set that actually has operations. webmaster@1: $set_changed = FALSE; webmaster@1: $old_set = $current_set; webmaster@1: while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) { webmaster@1: $current_set =& _batch_current_set(); webmaster@1: $set_changed = TRUE; webmaster@1: } webmaster@1: // At this point, either $current_set is a 'real' batch set (has operations), webmaster@1: // or all sets have been completed. webmaster@1: webmaster@1: // If we're in progressive mode, stop after 1 second. webmaster@1: if ($batch['progressive'] && timer_read('batch_processing') > 1000) { webmaster@1: break; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: if ($batch['progressive']) { webmaster@1: // Gather progress information. webmaster@1: webmaster@1: // Reporting 100% progress will cause the whole batch to be considered webmaster@1: // processed. If processing was paused right after moving to a new set, webmaster@1: // we have to use the info from the new (unprocessed) one. webmaster@1: if ($set_changed && isset($current_set['operations'])) { webmaster@1: // Processing will continue with a fresh batch set. webmaster@1: $remaining = count($current_set['operations']); webmaster@1: $total = $current_set['total']; webmaster@1: $progress_message = $current_set['init_message']; webmaster@1: $task_message = ''; webmaster@1: } webmaster@1: else { webmaster@1: $remaining = count($old_set['operations']); webmaster@1: $total = $old_set['total']; webmaster@1: $progress_message = $old_set['progress_message']; webmaster@1: } webmaster@1: webmaster@1: $current = $total - $remaining + $finished; webmaster@1: $percentage = $total ? floor($current / $total * 100) : 100; webmaster@1: $values = array( webmaster@1: '@remaining' => $remaining, webmaster@1: '@total' => $total, webmaster@1: '@current' => floor($current), webmaster@1: '@percentage' => $percentage, webmaster@1: ); webmaster@1: $message = strtr($progress_message, $values) .'
'; webmaster@1: $message .= $task_message ? $task_message : ' '; webmaster@1: webmaster@1: return array($percentage, $message); webmaster@1: } webmaster@1: else { webmaster@1: // If we're not in progressive mode, the whole batch has been processed by now. webmaster@1: return _batch_finished(); webmaster@1: } webmaster@1: webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Retrieve the batch set being currently processed. webmaster@1: */ webmaster@1: function &_batch_current_set() { webmaster@1: $batch =& batch_get(); webmaster@1: return $batch['sets'][$batch['current_set']]; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Move execution to the next batch set if any, executing the stored webmaster@1: * form _submit handlers along the way (thus possibly inserting webmaster@1: * additional batch sets). webmaster@1: */ webmaster@1: function _batch_next_set() { webmaster@1: $batch =& batch_get(); webmaster@1: if (isset($batch['sets'][$batch['current_set'] + 1])) { webmaster@1: $batch['current_set']++; webmaster@1: $current_set =& _batch_current_set(); webmaster@1: if (isset($current_set['form_submit']) && ($function = $current_set['form_submit']) && function_exists($function)) { webmaster@1: // We use our stored copies of $form and $form_state, to account for webmaster@1: // possible alteration by the submit handlers. webmaster@1: $function($batch['form'], $batch['form_state']); webmaster@1: } webmaster@1: return TRUE; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * End the batch processing: webmaster@1: * Call the 'finished' callbacks to allow custom handling of results, webmaster@1: * and resolve page redirection. webmaster@1: */ webmaster@1: function _batch_finished() { webmaster@1: $batch =& batch_get(); webmaster@1: webmaster@1: // Execute the 'finished' callbacks for each batch set. webmaster@1: foreach ($batch['sets'] as $key => $batch_set) { webmaster@1: if (isset($batch_set['finished'])) { webmaster@1: // Check if the set requires an additional file for functions definitions. webmaster@1: if (isset($batch_set['file']) && is_file($batch_set['file'])) { webmaster@1: include_once($batch_set['file']); webmaster@1: } webmaster@1: if (function_exists($batch_set['finished'])) { webmaster@1: $batch_set['finished']($batch_set['success'], $batch_set['results'], $batch_set['operations']); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Cleanup the batch table and unset the global $batch variable. webmaster@1: if ($batch['progressive']) { webmaster@1: db_query("DELETE FROM {batch} WHERE bid = %d", $batch['id']); webmaster@1: } webmaster@1: $_batch = $batch; webmaster@1: $batch = NULL; webmaster@1: webmaster@1: // Redirect if needed. webmaster@1: if ($_batch['progressive']) { webmaster@1: // Put back the 'destination' that was saved in batch_process(). webmaster@1: if (isset($_batch['destination'])) { webmaster@1: $_REQUEST['destination'] = $_batch['destination']; webmaster@1: } webmaster@1: webmaster@1: // Use $_batch['form_state']['redirect'], or $_batch['redirect'], webmaster@1: // or $_batch['source_page']. webmaster@1: if (isset($_batch['form_state']['redirect'])) { webmaster@1: $redirect = $_batch['form_state']['redirect']; webmaster@1: } webmaster@1: elseif (isset($_batch['redirect'])) { webmaster@1: $redirect = $_batch['redirect']; webmaster@1: } webmaster@1: else { webmaster@1: $redirect = $_batch['source_page']; webmaster@1: } webmaster@1: webmaster@1: // Let drupal_redirect_form handle redirection logic. webmaster@1: $form = isset($batch['form']) ? $batch['form'] : array(); webmaster@1: if (empty($_batch['form_state']['rebuild']) && empty($_batch['form_state']['storage'])) { webmaster@1: drupal_redirect_form($form, $redirect); webmaster@1: } webmaster@1: webmaster@1: // We get here if $form['#redirect'] was FALSE, or if the form is a webmaster@1: // multi-step form. We save the final $form_state value to be retrieved webmaster@1: // by drupal_get_form, and we redirect to the originating page. webmaster@1: $_SESSION['batch_form_state'] = $_batch['form_state']; webmaster@1: drupal_goto($_batch['source_page']); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Shutdown function: store the batch data for next request, webmaster@1: * or clear the table if the batch is finished. webmaster@1: */ webmaster@1: function _batch_shutdown() { webmaster@1: if ($batch = batch_get()) { webmaster@1: db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']); webmaster@1: } webmaster@1: }