Commit b4ef53ec authored by Dries's avatar Dries

- Patch #127539 by yched: batch processing fixes.

parent 55b4cbad
......@@ -8,11 +8,9 @@
* State based dispatcher for batches.
*/
function _batch_page() {
global $user;
$batch =& batch_get();
if (isset($_REQUEST['id']) && $data = db_result(db_query("SELECT batch FROM {batch} WHERE bid = %d AND sid = %d", $_REQUEST['id'], $user->sid))) {
if (isset($_REQUEST['id']) && $data = db_result(db_query("SELECT batch FROM {batch} WHERE bid = %d AND token = %d", $_REQUEST['id'], drupal_get_token($_REQUEST['id'])))) {
$batch = unserialize($data);
}
else {
......@@ -161,37 +159,42 @@ function _batch_process() {
$current_set =& _batch_current_set();
while (!$current_set['success']) {
$task_message = NULL;
$finished = 1;
$task_message = '';
if ((list($function, $args) = reset($current_set['operations'])) && function_exists($function)) {
// Build the 'batch context' array, execute the function call, and retrieve the user message.
$batch_context = array('sandbox' => &$current_set['sandbox'], 'results' => &$current_set['results'], 'finished' => &$finished, 'message' => '');
$batch_context = array('sandbox' => &$current_set['sandbox'], 'results' => &$current_set['results'], 'finished' => &$finished, 'message' => &$task_message);
call_user_func_array($function, array_merge($args, array(&$batch_context)));
$task_message = $batch_context['message'];
}
if ($finished == 1) {
// Make sure this step isn't counted double.
$finished = 0;
// Remove the operation, and clear the sandbox to reduce the stored data.
array_shift($current_set['operations']);
$current_set['sandbox'] = array();
}
// If the batch set is completed, browse through the remaining sets
// until we find one that acually has operations.
while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
$current_set =& _batch_current_set();
}
// Make sure we display progress information about a batch set that
// actually has operations, and not about a 'control' set (form submit
// handler).
$remaining = count($current_set['operations']);
$progress_message = $current_set['progress_message'];
$total = $current_set['total'];
// If the batch set is completed, browse through the remaining sets
// until we find one that acually has operations.
while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
$current_set =& _batch_current_set();
}
// Progressive mode : stop after 1 second
if ($batch['progressive'] && timer_read('page') > 1000) {
break;
}
}
// TODO : if the last set was a 'form_submit', there is no 'operations', 'total', 'progress message' in $current_set => warnings
if ($batch['progressive']) {
$remaining = count($current_set['operations']);
$total = $current_set['total'];
$current = $total - $remaining + $finished;
$percentage = $total ? floor($current / $total * 100) : 100;
$values = array(
......@@ -200,9 +203,7 @@ function _batch_process() {
'@current' => floor($current),
'@percentage' => $percentage,
);
$progress_message = strtr($current_set['progress_message'], $values);
$message = $progress_message .'<br/>';
$message = strtr($progress_message, $values) .'<br/>';
$message.= $task_message ? $task_message : '&nbsp';
return array($percentage, $message);
......
......@@ -608,7 +608,7 @@ function form_execute_handlers($type, &$form, &$form_state) {
// Some previous _submit handler has set a batch. We store the call
// in a special 'control' batch set, for execution at the correct
// time during the batch processing workflow.
$batch['sets'][] = array('form_submit' => array($function));
$batch['sets'][] = array('form_submit' => $function);
}
else {
$function($form_state['values'], $form, $form_state);
......@@ -1903,7 +1903,6 @@ function batch_set($batch_definition) {
* URL of the batch processing page.
*/
function batch_process($redirect = NULL, $url = NULL) {
global $user;
$batch =& batch_get();
if (isset($batch)) {
......@@ -1931,8 +1930,7 @@ function batch_process($redirect = NULL, $url = NULL) {
$batch['destination'] = $_REQUEST['edit']['destination'];
unset($_REQUEST['edit']['destination']);
}
db_query("INSERT INTO {batch} (bid, sid, timestamp, batch) VALUES (%d, %d, %d, '%s')", $batch['id'], $user->sid, time(), serialize($batch));
db_query("INSERT INTO {batch} (bid, token, timestamp, batch) VALUES (%d, %d, %d, '%s')", $batch['id'], drupal_get_token($batch['id']), time(), serialize($batch));
drupal_goto($batch['url'], 'op=start&id='. $batch['id']);
}
else {
......
......@@ -192,11 +192,11 @@ function system_install() {
db_query("CREATE TABLE {batch} (
bid int(11) NOT NULL,
sid varchar(64) NOT NULL,
token varchar(64) NOT NULL,
timestamp int(11) NOT NULL,
batch longtext,
PRIMARY KEY (bid),
KEY sid (sid)
KEY token (token)
) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
db_query("CREATE TABLE {blocks} (
......@@ -686,13 +686,13 @@ function system_install() {
)");
db_query("CREATE TABLE {batch} (
bid int NOT NULL default '0',
sid varchar(64) NOT NULL default '',
bid serial CHECK (bid >= 0),
token varchar(64) NOT NULL default '',
timestamp int NOT NULL default '0',
batch text,
PRIMARY KEY (bid)
)");
db_query("CREATE INDEX {batch}_sid_idx ON {batch} (sid)");
db_query("CREATE INDEX {batch}_token_idx ON {batch} (token)");
db_query("CREATE TABLE {blocks} (
module varchar(64) DEFAULT '' NOT NULL,
......
......@@ -685,22 +685,22 @@ function update_create_batch_table() {
case 'mysqli':
$ret[] = update_sql("CREATE TABLE {batch} (
bid int(11) NOT NULL,
sid varchar(64) NOT NULL,
token varchar(64) NOT NULL,
timestamp int(11) NOT NULL,
batch longtext,
PRIMARY KEY (bid),
KEY sid (sid)
KEY token (token)
) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
break;
case 'pgsql':
$ret[] = update_sql("CREATE TABLE {batch} (
bid int NOT NULL default '0',
sid varchar(64) NOT NULL default '',
bid serial CHECK (bid >= 0),
token varchar(64) NOT NULL default '',
timestamp int NOT NULL default '0',
batch text,
PRIMARY KEY (bid)
)");
$ret[] = update_sql("CREATE INDEX {batch}_sid_idx ON {batch} (sid)");
$ret[] = update_sql("CREATE INDEX {batch}_token_idx ON {batch} (token)");
break;
}
return $ret;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment