From 874314ac9529474cdcf6f266dc89357696b96bc2 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. - Remote the _batch_do_shell(). - Fix up some of the docs. - Remove a blank line. - Fix up PHPWTF with TRUE == 'drupal_goto' - Fix up elseif coding standard - Change is_null() to empty() - Remote checks for null in elseif() - make changes to call drupal_goto directly - change call to be the same as drupal_goto() but use the new query array() - fix up typo in settings default function. - change execution of functions to use $function() instead of $batch['redirect_callback']() - remove duplicate code of isset($url) ? $url : 'batch' - add in drupal_alter() to all the batch api to be altered by other modules. - Fix up typo when I was renaming the batch to function - Change default for $redirect_callback from NULL to 'drupal_goto' - Add a small comment to the drupal_alter(). - change $url to have a default of 'batch' - expand the description of the drupal_alter() intro --- includes/batch.inc | 5 ++++- includes/form.inc | 20 ++++++++++++++++---- includes/update.inc | 7 +++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git includes/batch.inc includes/batch.inc index 5cdcec0..095b9e7 100644 --- includes/batch.inc +++ includes/batch.inc @@ -445,7 +445,10 @@ function _batch_finished() { // If no redirection happened, 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']); + $function = $_batch['redirect_callback']; + if (function_exists($function)) { + $function($_batch['source_url'], array('op' => 'finish', 'id' => $_batch['id'])); + } } } diff --git includes/form.inc includes/form.inc index 3ee8af7..e1da8b0 100644 --- includes/form.inc +++ includes/form.inc @@ -2931,25 +2931,34 @@ 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 = 'batch', $redirect_callback = 'drupal_goto') { $batch =& batch_get(); drupal_theme_initialize(); if (isset($batch)) { // Add process information - $url = isset($url) ? $url : 'batch'; $process_info = array( 'current_set' => 0, 'progressive' => TRUE, - 'url' => isset($url) ? $url : 'batch', + 'url' => $url, 'source_page' => $_GET['q'], 'redirect' => $redirect, 'theme' => $GLOBALS['theme_key'], + 'redirect_callback' => $redirect_callback, ); $batch += $process_info; + // The batch is now completely built. Allow other modules to make changes to the + // batch so that it is easier to reuse batch processes in other enviroments. + drupal_alter('batch', $batch); + if ($batch['progressive']) { // Clear the way for the drupal_goto() redirection to the batch processing // page, by saving and unsetting the 'destination', if there is any. @@ -2984,7 +2993,10 @@ 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'], array('op' => 'start', 'id' => $batch['id'])); + $function = $batch['redirect_callback']; + if (function_exists($function)) { + $function($batch['url'], array('op' => 'start', 'id' => $batch['id'])); + } } else { // Non-progressive execution: bypass the whole progressbar workflow diff --git includes/update.inc includes/update.inc index 7ce6fb3..0b1935b 100644 --- includes/update.inc +++ includes/update.inc @@ -365,8 +365,11 @@ class DrupalUpdateException extends Exception { } * 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 = 'drupal_goto') { // During the update, bring the site offline so that schema changes do not // affect visiting users. $_SESSION['maintenance_mode'] = variable_get('maintenance_mode', FALSE); @@ -397,7 +400,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