2007-05-04 09:41:37 +00:00
< ? php
2007-07-03 19:49:07 +00:00
// $Id$
2007-05-04 09:41:37 +00:00
/**
* @ file Batch processing API for processes to run in multiple HTTP requests .
*/
/**
2007-10-15 15:18:39 +00:00
* State - based dispatcher for the batch processing page .
2007-05-04 09:41:37 +00:00
*/
function _batch_page () {
$batch =& batch_get ();
2007-10-15 15:18:39 +00:00
// Retrieve the current state of batch from db.
2007-06-05 20:59:02 +00:00
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' ])))) {
2007-05-04 09:41:37 +00:00
$batch = unserialize ( $data );
}
else {
return FALSE ;
}
// Register database update for end of processing.
register_shutdown_function ( '_batch_shutdown' );
2008-06-24 21:51:03 +00:00
// Add batch-specific css.
foreach ( $batch [ 'sets' ] as $batch_set ) {
foreach ( $batch_set [ 'css' ] as $css ) {
drupal_add_css ( $css );
}
}
2007-05-04 09:41:37 +00:00
$op = isset ( $_REQUEST [ 'op' ]) ? $_REQUEST [ 'op' ] : '' ;
2007-07-20 05:44:13 +00:00
$output = NULL ;
2007-05-04 09:41:37 +00:00
switch ( $op ) {
case 'start' :
$output = _batch_start ();
break ;
case 'do' :
2007-10-15 15:18:39 +00:00
// JS-version AJAX callback.
2007-07-20 05:44:13 +00:00
_batch_do ();
2007-05-04 09:41:37 +00:00
break ;
case 'do_nojs' :
2007-10-15 15:18:39 +00:00
// Non-JS progress page.
2007-05-04 09:41:37 +00:00
$output = _batch_progress_page_nojs ();
break ;
case 'finished' :
$output = _batch_finished ();
break ;
}
return $output ;
}
/**
* Initiate the batch processing
*/
function _batch_start () {
// Choose between the JS and non-JS version.
// JS-enabled users are identified through the 'has_js' cookie set in drupal.js.
// If the user did not visit any JS enabled page during his browser session,
// he gets the non-JS version...
if ( isset ( $_COOKIE [ 'has_js' ]) && $_COOKIE [ 'has_js' ]) {
return _batch_progress_page_js ();
}
else {
return _batch_progress_page_nojs ();
}
}
/**
* Batch processing page with JavaScript support .
*/
function _batch_progress_page_js () {
$batch = batch_get ();
2007-10-15 15:18:39 +00:00
// The first batch set gets to set the page title
// and the initialization and error messages.
$current_set = _batch_current_set ();
2007-05-04 09:41:37 +00:00
drupal_set_title ( $current_set [ 'title' ]);
2007-06-01 09:05:45 +00:00
drupal_add_js ( 'misc/progress.js' , 'core' , 'header' , FALSE , FALSE );
2007-05-04 09:41:37 +00:00
$url = url ( $batch [ 'url' ], array ( 'query' => array ( 'id' => $batch [ 'id' ])));
$js_setting = array (
'batch' => array (
2008-04-14 17:48:46 +00:00
'errorMessage' => $current_set [ 'error_message' ] . '<br/>' . $batch [ 'error_message' ],
2007-05-04 09:41:37 +00:00
'initMessage' => $current_set [ 'init_message' ],
'uri' => $url ,
),
);
drupal_add_js ( $js_setting , 'setting' );
2007-06-01 09:05:45 +00:00
drupal_add_js ( 'misc/batch.js' , 'core' , 'header' , FALSE , FALSE );
2007-05-04 09:41:37 +00:00
$output = '<div id="progress"></div>' ;
return $output ;
}
/**
2007-10-15 15:18:39 +00:00
* Do one pass of execution and inform back the browser about progression
* ( used for JavaScript - mode only ) .
2007-05-04 09:41:37 +00:00
*/
function _batch_do () {
// HTTP POST required
if ( $_SERVER [ 'REQUEST_METHOD' ] != 'POST' ) {
drupal_set_message ( t ( 'HTTP POST is required.' ), 'error' );
drupal_set_title ( t ( 'Error' ));
return '' ;
}
2007-10-15 15:18:39 +00:00
// Perform actual processing.
2007-05-04 09:41:37 +00:00
list ( $percentage , $message ) = _batch_process ();
2007-07-20 05:44:13 +00:00
drupal_json ( array ( 'status' => TRUE , 'percentage' => $percentage , 'message' => $message ));
2007-05-04 09:41:37 +00:00
}
/**
* Batch processing page without JavaScript support .
*/
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' ])) {
2007-10-15 15:18:39 +00:00
// This is the first page so we return some output immediately.
2007-05-04 09:41:37 +00:00
$percentage = 0 ;
$message = $current_set [ 'init_message' ];
$batch [ 'running' ] = TRUE ;
}
else {
// This is one of the later requests: do some processing first.
// Error handling: if PHP dies due to a fatal error (e.g. non-existant
// function), it will output whatever is in the output buffer,
// followed by the error message.
ob_start ();
2008-04-14 17:48:46 +00:00
$fallback = $current_set [ 'error_message' ] . '<br/>' . $batch [ 'error_message' ];
2007-12-20 11:57:20 +00:00
$fallback = theme ( 'maintenance_page' , $fallback , FALSE , FALSE );
2007-05-04 09:41:37 +00:00
// We strip the end of the page using a marker in the template, so any
// additional HTML output by PHP shows up inside the page rather than
// below it. While this causes invalid HTML, the same would be true if
// we didn't, as content is not allowed to appear after </html> anyway.
list ( $fallback ) = explode ( '<!--partial-->' , $fallback );
print $fallback ;
2007-10-15 15:18:39 +00:00
// Perform actual processing.
2007-05-04 09:41:37 +00:00
list ( $percentage , $message ) = _batch_process ( $batch );
if ( $percentage == 100 ) {
$new_op = 'finished' ;
}
2007-10-15 15:18:39 +00:00
// PHP did not die : remove the fallback output.
2007-05-04 09:41:37 +00:00
ob_end_clean ();
}
$url = url ( $batch [ 'url' ], array ( 'query' => array ( 'id' => $batch [ 'id' ], 'op' => $new_op )));
2008-04-14 17:48:46 +00:00
drupal_set_html_head ( '<meta http-equiv="Refresh" content="0; URL=' . $url . '">' );
2007-05-04 09:41:37 +00:00
$output = theme ( 'progress_bar' , $percentage , $message );
return $output ;
}
/**
* Advance batch processing for 1 second ( or process the whole batch if it
2007-10-15 15:18:39 +00:00
* was not set for progressive execution - e . g forms submitted by drupal_execute ) .
2007-05-04 09:41:37 +00:00
*/
function _batch_process () {
$batch =& batch_get ();
$current_set =& _batch_current_set ();
2007-10-15 15:18:39 +00:00
$set_changed = TRUE ;
2007-05-04 09:41:37 +00:00
2007-07-20 05:44:13 +00:00
if ( $batch [ 'progressive' ]) {
timer_start ( 'batch_processing' );
}
2007-05-04 09:41:37 +00:00
while ( ! $current_set [ 'success' ]) {
2007-10-15 15:18:39 +00:00
// If this is the first time we iterate this batch set in the current
// request, we check if it requires an additional file for functions
// definitions.
if ( $set_changed && isset ( $current_set [ 'file' ]) && is_file ( $current_set [ 'file' ])) {
include_once ( $current_set [ 'file' ]);
}
2007-05-16 07:56:19 +00:00
$task_message = '' ;
2008-06-21 18:24:20 +00:00
// We assume a single pass operation and set the completion level
// to 1 by default.
$finished = 1 ;
2007-05-04 09:41:37 +00:00
if (( list ( $function , $args ) = reset ( $current_set [ 'operations' ])) && function_exists ( $function )) {
2007-10-15 15:18:39 +00:00
// Build the 'context' array, execute the function call,
// and retrieve the user message.
2007-05-16 07:56:19 +00:00
$batch_context = array ( 'sandbox' => & $current_set [ 'sandbox' ], 'results' => & $current_set [ 'results' ], 'finished' => & $finished , 'message' => & $task_message );
2007-10-15 15:18:39 +00:00
// Process the current operation.
2007-05-04 09:41:37 +00:00
call_user_func_array ( $function , array_merge ( $args , array ( & $batch_context )));
}
2007-05-16 07:56:19 +00:00
2008-06-21 18:24:20 +00:00
if ( $finished == 1 ) {
2007-07-20 05:44:13 +00:00
// Make sure this step isn't counted double when computing $current.
2008-06-21 18:24:20 +00:00
$finished = 0 ;
2007-07-20 05:44:13 +00:00
// Remove the operation and clear the sandbox.
2007-05-04 09:41:37 +00:00
array_shift ( $current_set [ 'operations' ]);
$current_set [ 'sandbox' ] = array ();
2007-05-16 07:56:19 +00:00
}
2007-05-04 09:41:37 +00:00
2007-07-20 05:44:13 +00:00
// If the batch set is completed, browse through the remaining sets,
2007-10-15 15:18:39 +00:00
// executing 'control sets' (stored form submit handlers) along the way -
// this might in turn insert new batch sets.
// Stop when we find a set that actually has operations.
2007-07-20 05:44:13 +00:00
$set_changed = FALSE ;
$old_set = $current_set ;
2007-05-16 07:56:19 +00:00
while ( empty ( $current_set [ 'operations' ]) && ( $current_set [ 'success' ] = TRUE ) && _batch_next_set ()) {
$current_set =& _batch_current_set ();
2007-07-20 05:44:13 +00:00
$set_changed = TRUE ;
2007-05-04 09:41:37 +00:00
}
2007-07-20 05:44:13 +00:00
// At this point, either $current_set is a 'real' batch set (has operations),
// or all sets have been completed.
2007-05-16 07:56:19 +00:00
2007-10-15 15:18:39 +00:00
// If we're in progressive mode, stop after 1 second.
2007-07-20 05:44:13 +00:00
if ( $batch [ 'progressive' ] && timer_read ( 'batch_processing' ) > 1000 ) {
2007-05-04 09:41:37 +00:00
break ;
}
}
if ( $batch [ 'progressive' ]) {
2007-07-20 05:44:13 +00:00
// Gather progress information.
// Reporting 100% progress will cause the whole batch to be considered
// processed. If processing was paused right after moving to a new set,
2007-10-15 15:18:39 +00:00
// we have to use the info from the new (unprocessed) one.
2007-07-20 05:44:13 +00:00
if ( $set_changed && isset ( $current_set [ 'operations' ])) {
// Processing will continue with a fresh batch set.
$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' ];
}
2007-05-04 09:41:37 +00:00
$current = $total - $remaining + $finished ;
2008-06-09 06:59:41 +00:00
$percentage = $total ? floor ( $current / $total * 100 ) : 100 ;
2007-05-04 09:41:37 +00:00
$values = array (
'@remaining' => $remaining ,
'@total' => $total ,
'@current' => floor ( $current ),
'@percentage' => $percentage ,
);
2008-04-14 17:48:46 +00:00
$message = strtr ( $progress_message , $values ) . '<br/>' ;
2007-09-04 21:10:45 +00:00
$message .= $task_message ? $task_message : ' ' ;
2007-05-04 09:41:37 +00:00
return array ( $percentage , $message );
}
else {
2007-10-15 15:18:39 +00:00
// If we're not in progressive mode, the whole batch has been processed by now.
2007-05-04 09:41:37 +00:00
return _batch_finished ();
}
}
/**
* Retrieve the batch set being currently processed .
*/
function & _batch_current_set () {
$batch =& batch_get ();
return $batch [ 'sets' ][ $batch [ 'current_set' ]];
}
/**
* Move execution to the next batch set if any , executing the stored
2007-05-14 13:43:38 +00:00
* form _submit handlers along the way ( thus possibly inserting
2007-10-15 15:18:39 +00:00
* additional batch sets ) .
2007-05-04 09:41:37 +00:00
*/
function _batch_next_set () {
$batch =& batch_get ();
2007-05-14 13:43:38 +00:00
if ( isset ( $batch [ 'sets' ][ $batch [ 'current_set' ] + 1 ])) {
2007-05-04 09:41:37 +00:00
$batch [ 'current_set' ] ++ ;
$current_set =& _batch_current_set ();
2007-05-14 13:43:38 +00:00
if ( isset ( $current_set [ 'form_submit' ]) && ( $function = $current_set [ 'form_submit' ]) && function_exists ( $function )) {
// We use our stored copies of $form and $form_state, to account for
// possible alteration by the submit handlers.
2007-06-04 13:11:34 +00:00
$function ( $batch [ 'form' ], $batch [ 'form_state' ]);
2007-05-04 09:41:37 +00:00
}
return TRUE ;
}
}
/**
2007-10-15 15:18:39 +00:00
* End the batch processing :
2007-05-04 09:41:37 +00:00
* Call the 'finished' callbacks to allow custom handling of results ,
* and resolve page redirection .
*/
function _batch_finished () {
$batch =& batch_get ();
2007-10-15 15:18:39 +00:00
// Execute the 'finished' callbacks for each batch set.
2007-12-08 14:06:23 +00:00
foreach ( $batch [ 'sets' ] as $key => $batch_set ) {
2007-10-15 15:18:39 +00:00
if ( isset ( $batch_set [ 'finished' ])) {
// Check if the set requires an additional file for functions definitions.
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' ]);
}
2007-05-04 09:41:37 +00:00
}
}
// Cleanup the batch table and unset the global $batch variable.
2007-07-20 05:44:13 +00:00
if ( $batch [ 'progressive' ]) {
db_query ( " DELETE FROM { batch} WHERE bid = %d " , $batch [ 'id' ]);
}
2007-05-04 09:41:37 +00:00
$_batch = $batch ;
$batch = NULL ;
// Redirect if needed.
if ( $_batch [ 'progressive' ]) {
2007-10-15 15:18:39 +00:00
// Put back the 'destination' that was saved in batch_process().
2007-05-04 09:41:37 +00:00
if ( isset ( $_batch [ 'destination' ])) {
$_REQUEST [ 'destination' ] = $_batch [ 'destination' ];
}
2007-05-14 13:43:38 +00:00
2007-10-15 15:18:39 +00:00
// Use $_batch['form_state']['redirect'], or $_batch['redirect'],
// or $_batch['source_page'].
2007-05-14 13:43:38 +00:00
if ( isset ( $_batch [ 'form_state' ][ 'redirect' ])) {
$redirect = $_batch [ 'form_state' ][ 'redirect' ];
}
elseif ( isset ( $_batch [ 'redirect' ])) {
$redirect = $_batch [ 'redirect' ];
}
else {
$redirect = $_batch [ 'source_page' ];
}
// Let drupal_redirect_form handle redirection logic.
$form = isset ( $batch [ 'form' ]) ? $batch [ 'form' ] : array ();
if ( empty ( $_batch [ 'form_state' ][ 'rebuild' ]) && empty ( $_batch [ 'form_state' ][ 'storage' ])) {
drupal_redirect_form ( $form , $redirect );
}
2007-10-15 15:18:39 +00:00
// We get here if $form['#redirect'] was FALSE, or if the form is a
// multi-step form. We save the final $form_state value to be retrieved
// by drupal_get_form, and we redirect to the originating page.
2007-05-14 13:43:38 +00:00
$_SESSION [ 'batch_form_state' ] = $_batch [ 'form_state' ];
2007-05-04 09:41:37 +00:00
drupal_goto ( $_batch [ 'source_page' ]);
}
}
/**
2007-10-15 15:18:39 +00:00
* Shutdown function : store the batch data for next request ,
* or clear the table if the batch is finished .
2007-05-04 09:41:37 +00:00
*/
function _batch_shutdown () {
if ( $batch = batch_get ()) {
db_query ( " UPDATE { batch} SET batch = '%s' WHERE bid = %d " , serialize ( $batch ), $batch [ 'id' ]);
}
}