From 68bb62613128c6128f889be088f762fa4d9ebb46 Mon Sep 17 00:00:00 2001 From: Gordon Heydon Date: Fri, 21 Aug 2009 23:55:22 +1000 Subject: [PATCH] Changes to batch API to allow better user of progressive mode in non HTML enviroments. - Add check of function with drupal_function_exists() - Fix up call to use $redirect_callback instead of the hard coded drupal_goto - change default for $redirect-callback to NULL - Change callback to only ass the batch id if it is not NULL or drupal_goto - Add $op to the callback so that it will be called on start and finish. --- includes/batch.inc | 27 ++++++++++++++++++++++++++- includes/form.inc | 14 ++++++++++++-- includes/update.inc | 7 +++++-- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git includes/batch.inc includes/batch.inc index 42d6159..178fbce 100644 --- includes/batch.inc +++ includes/batch.inc @@ -450,7 +450,12 @@ function _batch_finished() { // multi-step form. We save the final $form_state value to be retrieved // by drupal_get_form(), and redirect to the originating page. $_SESSION['batch_form_state'] = $_batch['form_state']; - drupal_goto($_batch['source_page']); + if (is_null($_batch['redirect_callback']) || $_batch['redirect_callback'] == 'drupal_goto') { + drupal_goto($_batch['source_page']); + } + else if ($_batch['redirect_callback'] && function_exists($_batch['redirect_callback'])) { + call_user_func_array($_batch['redirect_callback'], array($_batch['id']), 'finish'); + } } } @@ -466,3 +471,23 @@ function _batch_shutdown() { } } +/** + * Batch do shell + */ +function _batch_do_shell($batch_id, $op) { + if ($op == 'start') { + $percentage = 0; + while ($percentage !== TRUE) { + // Perform actual processing. + list($percentage, $message) = _batch_process(); + + if ($percentage !== TRUE) { + // Use fwrite() to avoid PHP buffering of output to the console. + fwrite(STDOUT, $message ."\n"); + } + } + } + else if ($op == 'finish') { + return array(TRUE, ''); + } +} diff --git includes/form.inc includes/form.inc index e9030ef..b7e8441 100644 --- includes/form.inc +++ includes/form.inc @@ -2846,8 +2846,11 @@ function batch_set($batch_definition) { * @param $url * (optional - should only be used for separate scripts like update.php) * URL of the batch processing page. + * @param $redirect_callback + * (optional) Specify a function to be called to redirect to the progressive + * processing page. */ -function batch_process($redirect = NULL, $url = NULL) { +function batch_process($redirect = NULL, $url = NULL, $redirect_callback = NULL) { $batch =& batch_get(); if (isset($batch)) { @@ -2859,6 +2862,7 @@ function batch_process($redirect = NULL, $url = NULL) { 'url' => isset($url) ? $url : 'batch', 'source_page' => $_GET['q'], 'redirect' => $redirect, + 'redirect_callback' => $redirect_callback, ); $batch += $process_info; @@ -2897,7 +2901,13 @@ function batch_process($redirect = NULL, $url = NULL) { // Set the batch number in the session to guarantee that it will stay alive. $_SESSION['batches'][$batch['id']] = TRUE; - drupal_goto($batch['url'], 'op=start&id=' . $batch['id']); + if (is_null($redirect_callback) || $redirect_callback == 'drupal_goto') { + drupal_goto($batch['url'], 'op=start&id=' . $batch['id']); + + } + else if ($redirect_callback && function_exists($redirect_callback)) { + call_user_func_array($redirect_callback, array($batch['id']), 'start'); + } } else { // Non-progressive execution: bypass the whole progressbar workflow diff --git includes/update.inc includes/update.inc index c6fa321..75f6d25 100644 --- includes/update.inc +++ includes/update.inc @@ -433,8 +433,11 @@ function update_do_one($module, $number, &$context) { * scripts like update.php). * @param $batch * Optional parameters to pass into the batch API. + * @param $redirect_callback + * (optional) Specify a function to be called to redirect to the progressive + * processing page. */ -function update_batch($start, $redirect = NULL, $url = NULL, $batch = array()) { +function update_batch($start, $redirect = NULL, $url = NULL, $batch = array(), $redirect_callback = NULL) { // During the update, bring the site offline so that schema changes do not // affect visiting users. $_SESSION['maintenance_mode'] = variable_get('maintenance_mode', FALSE); @@ -465,7 +468,7 @@ function update_batch($start, $redirect = NULL, $url = NULL, $batch = array()) { 'file' => 'includes/update.inc', ); batch_set($batch); - batch_process($redirect, $url); + batch_process($redirect, $url, $redirect_callback); } /** -- 1.6.4.rc0.17.gd9eb0