From 89d94c44425559ad14df0363a41e41a7da39999c Mon Sep 17 00:00:00 2001 From: Gordon Heydon Date: Fri, 21 Aug 2009 23:55:22 +1000 Subject: [PATCH 1/2] 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. - Remote the _batch_do_shell(). - Fix up some of the docs. --- includes/batch.inc | 7 ++++++- includes/form.inc | 16 ++++++++++++++-- includes/update.inc | 7 +++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git includes/batch.inc includes/batch.inc index b136ac9..331cfb9 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'); + } } } diff --git includes/form.inc includes/form.inc index 6bde787..8e7e4bb 100644 --- includes/form.inc +++ includes/form.inc @@ -2878,8 +2878,13 @@ 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. By default drupal_goto() will be used to redirect to a + * page which will do the progressive page. Specifying another function will + * allow the progressive processing to be processed differently. */ -function batch_process($redirect = NULL, $url = NULL) { +function batch_process($redirect = NULL, $url = NULL, $redirect_callback = NULL) { $batch =& batch_get(); if (isset($batch)) { @@ -2891,6 +2896,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; @@ -2929,7 +2935,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.3.3