<?php
function _batch_page() {
$batch =& batch_get();
if (isset($_REQUEST['id']) && $data = db_result(db_query("SELECT batch FROM {batch} WHERE bid = %d AND token = '%s'", $_REQUEST['id'], drupal_get_token($_REQUEST['id'])))) {
$batch = unserialize($data);
}
else {
return FALSE;
}
register_shutdown_function('_batch_shutdown');
$op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
$output = NULL;
switch ($op) {
case 'start':
$output = _batch_start();
break;
case 'do':
_batch_do();
break;
case 'do_nojs':
$output = _batch_progress_page_nojs();
break;
case 'finished':
$output = _batch_finished();
break;
}
return $output;
}
function _batch_start() {
if (isset($_COOKIE['has_js']) && $_COOKIE['has_js']) {
return _batch_progress_page_js();
}
else {
return _batch_progress_page_nojs();
}
}
function _batch_progress_page_js() {
$batch = batch_get();
$current_set = _batch_current_set();
drupal_set_title($current_set['title']);
drupal_add_js('misc/progress.js', 'core', 'header', FALSE, FALSE);
$url = url($batch['url'], array('query' => array('id' => $batch['id'])));
$js_setting = array(
'batch' => array(
'errorMessage' => $current_set['error_message'] .'<br/>'. $batch['error_message'],
'initMessage' => $current_set['init_message'],
'uri' => $url,
),
);
drupal_add_js($js_setting, 'setting');
drupal_add_js('misc/batch.js', 'core', 'header', FALSE, FALSE);
$output = '<div id="progress"></div>';
return $output;
}
function _batch_do() {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
drupal_set_message(t('HTTP POST is required.'), 'error');
drupal_set_title(t('Error'));
return '';
}
list($percentage, $message) = _batch_process();
drupal_json(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message));
}
function _batch_progress_page_nojs() {
$batch =& batch_get();
$current_set = _batch_current_set();
drupal_set_title($current_set['title']);
$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', $fallback, FALSE, FALSE);
list($fallback) = explode('<!--partial-->', $fallback);
print $fallback;
list($percentage, $message) = _batch_process($batch);
if ($percentage == 100) {
$new_op = 'finished';
}
ob_end_clean();
}
$url = url($batch['url'], array('query' => array('id' => $batch['id'], 'op' => $new_op)));
drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL='. $url .'">');
$output = theme('progress_bar', $percentage, $message);
return $output;
}
function _batch_process() {
$batch =& batch_get();
$current_set =& _batch_current_set();
$set_changed = TRUE;
if ($batch['progressive']) {
timer_start('batch_processing');
}
while (!$current_set['success']) {
if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
include_once($current_set['file']);
}
$finished = 1;
$task_message = '';
if ((list($function, $args) = reset($current_set['operations'])) && function_exists($function)) {
$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;
array_shift($current_set['operations']);
$current_set['sandbox'] = array();
}
$set_changed = FALSE;
$old_set = $current_set;
while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
$current_set =& _batch_current_set();
$set_changed = TRUE;
}
if ($batch['progressive'] && timer_read('batch_processing') > 1000) {
break;
}
}
if ($batch['progressive']) {
if ($set_changed && isset($current_set['operations'])) {
$remaining = count($current_set['operations']);
$total = $current_set['total'];
$progress_message = $current_set['init_message'];
$task_message = '';
}
else {
$remaining = count($old_set['operations']);
$total = $old_set['total'];
$progress_message = $old_set['progress_message'];
}
$current = $total - $remaining + $finished;
$percentage = $total ? floor($current / $total * 100) : 100;
$values = array(
'@remaining' => $remaining,
'@total' => $total,
'@current' => floor($current),
'@percentage' => $percentage,
);
$message = strtr($progress_message, $values) .'<br/>';
$message .= $task_message ? $task_message : ' ';
return array($percentage, $message);
}
else {
return _batch_finished();
}
}
function &_batch_current_set() {
$batch =& batch_get();
return $batch['sets'][$batch['current_set']];
}
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'], $batch['form_state']);
}
return TRUE;
}
}
function _batch_finished() {
$batch =& batch_get();
foreach ($batch['sets'] as $key => $batch_set) {
if (isset($batch_set['finished'])) {
if (isset($batch_set['file']) && is_file($batch_set['file'])) {
include_once($batch_set['file']);
}
if (function_exists($batch_set['finished'])) {
$batch_set['finished']($batch_set['success'], $batch_set['results'], $batch_set['operations']);
}
}
}
if ($batch['progressive']) {
db_query("DELETE FROM {batch} WHERE bid = %d", $batch['id']);
}
$_batch = $batch;
$batch = NULL;
if ($_batch['progressive']) {
if (isset($_batch['destination'])) {
$_REQUEST['destination'] = $_batch['destination'];
}
if (isset($_batch['form_state']['redirect'])) {
$redirect = $_batch['form_state']['redirect'];
}
elseif (isset($_batch['redirect'])) {
$redirect = $_batch['redirect'];
}
else {
$redirect = $_batch['source_page'];
}
$form = isset($batch['form']) ? $batch['form'] : array();
if (empty($_batch['form_state']['rebuild']) && empty($_batch['form_state']['storage'])) {
drupal_redirect_form($form, $redirect);
}
$_SESSION['batch_form_state'] = $_batch['form_state'];
drupal_goto($_batch['source_page']);
}
}
function _batch_shutdown() {
if ($batch = batch_get()) {
db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']);
}
}