? file.patch Index: includes/batch.inc =================================================================== RCS file: includes/batch.inc diff -N includes/batch.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/batch.inc 13 Mar 2007 22:46:36 -0000 @@ -0,0 +1,182 @@ + array( + 'errorMessage' => $batch['error_message'], + 'initMessage' => $batch['init_message'], + 'uri' => url('batch'), + ), + ); + drupal_add_js($js_setting, 'setting'); + drupal_add_js('misc/batch.js', 'core', 'header', FALSE, TRUE); + + // TODO : should be themed + $output = '
'; + + return $output; +} + +function _batch_do(&$batch) { + // HTTP Post required + if ($_SERVER['REQUEST_METHOD'] != 'POST') { + drupal_set_message('HTTP Post is required.', 'error'); + drupal_set_title('Error'); + $output = ''; + } + + list($percentage, $message) = _batch_process($batch); + // TODO : should not be needed, but devel_shutdown borks the output otherwise + drupal_set_header('Content-Type: text/plain; charset=utf-8'); + print drupal_to_js(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message)); + exit(); +} + +function _batch_page_nojs(&$batch) { + drupal_set_title($batch['title']); + + $new_op = 'do_nojs'; + + // TODO : sort of is hackish + if (isset($batch['running'])) { +//if ($_SERVER['REQUEST_METHOD'] == 'GET') { + // Error handling: if PHP dies, it will output whatever is in the output + // buffer, followed by the error message. + // TODO : _really_ slows the thing down... + ob_start(); + $fallback = $batch['error_message']; + print theme('page', $fallback, FALSE, TRUE); + + list($percentage, $message) = _batch_process($batch); + if ($percentage == 100) { + $new_op = 'finished'; + } + + // Updates successful; remove fallback + ob_end_clean(); + } + else { + // This is the first page so return some output immediately. + $percentage = 0; + $message = $batch['init_message']; + $batch['running'] = TRUE; + } + $url = url('batch', array('query' => array('op' => $new_op))); + drupal_set_html_head(''); + + $output = theme('progress_bar', $percentage, $message); + return $output; +} + +function _batch_process(&$batch) { + while ($op = reset($batch['operations'])) { + $key = key($batch['operations']); + $op_finished = 1; + if (is_array($op) && isset($op['callback']) && function_exists($op['callback'])) { + $arg = $op['arguments']; + $additions = array(&$batch['results'], &$op_finished); + call_user_func_array($op['callback'], array_merge($arg, $additions)); + } + if ($op_finished == 1) { + unset($batch['operations'][$key]); + $op_finished = 0; + } + if (timer_read('page') > 1000) { + break; + } + } + + $remaining = count($batch['operations']); + $total = $batch['total']; + $current = $total - $remaining + $op_finished; + $percentage = floor($current / $total * 100); + $values = array( + '@current' => floor($current), + '@remaining' => $remaining, + '@total' => $total, + '@percent' => $percentage + ); + $message = strtr($batch['progress_message'], $values); + return array($percentage, $message); +} + +function _batch_finished(&$batch, $status) { + unset($_SESSION['batch']); + + if (isset($batch['finished_callback']) && function_exists($batch['finished_callback'])) { + return $batch['finished_callback']($batch, $status, $batch['results']); + } + else { + // TODO : what then ? return to the calling page ? + drupal_goto(); + } +} \ No newline at end of file Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.619 diff -u -p -r1.619 common.inc --- includes/common.inc 8 Mar 2007 19:33:55 -0000 1.619 +++ includes/common.inc 13 Mar 2007 22:46:37 -0000 @@ -1898,6 +1898,7 @@ function _drupal_bootstrap_full() { require_once './includes/unicode.inc'; require_once './includes/image.inc'; require_once './includes/form.inc'; + require_once './includes/batch.inc'; // Set the Drupal custom error handler. set_error_handler('error_handler'); // Emit the correct charset HTTP header. Index: misc/batch.js =================================================================== RCS file: misc/batch.js diff -N misc/batch.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ misc/batch.js 13 Mar 2007 22:46:37 -0000 @@ -0,0 +1,32 @@ +if (Drupal.jsEnabled) { + $(document).ready(function() { + $('#edit-has-js').each(function() { this.value = 1; }); + $('#progress').each(function () { + var holder = this; + var uri = Drupal.settings.batch.uri; + var initMessage = Drupal.settings.batch.initMessage; + var errorMessage = Drupal.settings.batch.errorMessage; + + // Success: redirect to the summary. + var updateCallback = function (progress, status, pb) { + if (progress == 100) { + pb.stopMonitoring(); + window.location = uri+'&op=finished'; + } + } + + var errorCallback = function (pb) { + var div = document.createElement('p'); + div.className = 'error'; + $(div).html(errorMessage); + $(holder).prepend(div); + $('#wait').hide(); + } + + var progress = new Drupal.progressBar('updateprogress', updateCallback, "POST", errorCallback); + progress.setProgress(-1, initMessage); + $(holder).append(progress.element); + progress.startMonitoring(uri+'&op=do', 10); + }); + }); +} \ No newline at end of file