00001 <?php
00002
00003
00011 function _batch_page() {
00012 $batch =& batch_get();
00013
00014
00015 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'])))) {
00016 $batch = unserialize($data);
00017 }
00018 else {
00019 return FALSE;
00020 }
00021
00022
00023 register_shutdown_function('_batch_shutdown');
00024
00025 $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
00026 $output = NULL;
00027 switch ($op) {
00028 case 'start':
00029 $output = _batch_start();
00030 break;
00031
00032 case 'do':
00033
00034 _batch_do();
00035 break;
00036
00037 case 'do_nojs':
00038
00039 $output = _batch_progress_page_nojs();
00040 break;
00041
00042 case 'finished':
00043 $output = _batch_finished();
00044 break;
00045 }
00046
00047 return $output;
00048 }
00049
00053 function _batch_start() {
00054
00055
00056
00057
00058 if (isset($_COOKIE['has_js']) && $_COOKIE['has_js']) {
00059 return _batch_progress_page_js();
00060 }
00061 else {
00062 return _batch_progress_page_nojs();
00063 }
00064 }
00065
00069 function _batch_progress_page_js() {
00070 $batch = batch_get();
00071
00072
00073
00074 $current_set = _batch_current_set();
00075 drupal_set_title($current_set['title']);
00076 drupal_add_js('misc/progress.js', 'core', 'header', FALSE, FALSE);
00077
00078 $url = url($batch['url'], array('query' => array('id' => $batch['id'])));
00079 $js_setting = array(
00080 'batch' => array(
00081 'errorMessage' => $current_set['error_message'] . '<br/>' . $batch['error_message'],
00082 'initMessage' => $current_set['init_message'],
00083 'uri' => $url,
00084 ),
00085 );
00086 drupal_add_js($js_setting, 'setting');
00087 drupal_add_js('misc/batch.js', 'core', 'header', FALSE, FALSE);
00088
00089 $output = '<div id="progress"></div>';
00090 return $output;
00091 }
00092
00097 function _batch_do() {
00098
00099 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
00100 drupal_set_message(t('HTTP POST is required.'), 'error');
00101 drupal_set_title(t('Error'));
00102 return '';
00103 }
00104
00105
00106 list($percentage, $message) = _batch_process();
00107
00108 drupal_json(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message));
00109 }
00110
00114 function _batch_progress_page_nojs() {
00115 $batch =& batch_get();
00116 $current_set = _batch_current_set();
00117
00118 drupal_set_title($current_set['title']);
00119
00120 $new_op = 'do_nojs';
00121
00122 if (!isset($batch['running'])) {
00123
00124 $percentage = 0;
00125 $message = $current_set['init_message'];
00126 $batch['running'] = TRUE;
00127 }
00128 else {
00129
00130
00131
00132
00133
00134 ob_start();
00135 $fallback = $current_set['error_message'] . '<br/>' . $batch['error_message'];
00136 $fallback = theme('maintenance_page', $fallback, FALSE, FALSE);
00137
00138
00139
00140
00141
00142 list($fallback) = explode('<!--partial-->', $fallback);
00143 print $fallback;
00144
00145
00146 list($percentage, $message) = _batch_process($batch);
00147 if ($percentage == 100) {
00148 $new_op = 'finished';
00149 }
00150
00151
00152 ob_end_clean();
00153 }
00154
00155 $url = url($batch['url'], array('query' => array('id' => $batch['id'], 'op' => $new_op)));
00156 drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL=' . $url . '">');
00157 $output = theme('progress_bar', $percentage, $message);
00158 return $output;
00159 }
00160
00165 function _batch_process() {
00166 $batch =& batch_get();
00167 $current_set =& _batch_current_set();
00168 $set_changed = TRUE;
00169
00170 if ($batch['progressive']) {
00171 timer_start('batch_processing');
00172 }
00173
00174 while (!$current_set['success']) {
00175
00176
00177
00178 if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
00179 include_once($current_set['file']);
00180 }
00181
00182 $finished = 1;
00183 $task_message = '';
00184 if ((list($function, $args) = reset($current_set['operations'])) && function_exists($function)) {
00185
00186
00187 $batch_context = array('sandbox' => &$current_set['sandbox'], 'results' => &$current_set['results'], 'finished' => &$finished, 'message' => &$task_message);
00188
00189 call_user_func_array($function, array_merge($args, array(&$batch_context)));
00190 }
00191
00192 if ($finished == 1) {
00193
00194 $finished = 0;
00195
00196 array_shift($current_set['operations']);
00197 $current_set['sandbox'] = array();
00198 }
00199
00200
00201
00202
00203
00204 $set_changed = FALSE;
00205 $old_set = $current_set;
00206 while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
00207 $current_set =& _batch_current_set();
00208 $set_changed = TRUE;
00209 }
00210
00211
00212
00213
00214 if ($batch['progressive'] && timer_read('batch_processing') > 1000) {
00215 break;
00216 }
00217 }
00218
00219 if ($batch['progressive']) {
00220
00221
00222
00223
00224
00225 if ($set_changed && isset($current_set['operations'])) {
00226
00227 $remaining = count($current_set['operations']);
00228 $total = $current_set['total'];
00229 $progress_message = $current_set['init_message'];
00230 $task_message = '';
00231 }
00232 else {
00233 $remaining = count($old_set['operations']);
00234 $total = $old_set['total'];
00235 $progress_message = $old_set['progress_message'];
00236 }
00237
00238 $current = $total - $remaining + $finished;
00239 $percentage = $total ? floor($current / $total * 100) : 100;
00240 $values = array(
00241 '@remaining' => $remaining,
00242 '@total' => $total,
00243 '@current' => floor($current),
00244 '@percentage' => $percentage,
00245 );
00246 $message = strtr($progress_message, $values) . '<br/>';
00247 $message .= $task_message ? $task_message : ' ';
00248
00249 return array($percentage, $message);
00250 }
00251 else {
00252
00253 return _batch_finished();
00254 }
00255
00256 }
00257
00261 function &_batch_current_set() {
00262 $batch =& batch_get();
00263 return $batch['sets'][$batch['current_set']];
00264 }
00265
00271 function _batch_next_set() {
00272 $batch =& batch_get();
00273 if (isset($batch['sets'][$batch['current_set'] + 1])) {
00274 $batch['current_set']++;
00275 $current_set =& _batch_current_set();
00276 if (isset($current_set['form_submit']) && ($function = $current_set['form_submit']) && function_exists($function)) {
00277
00278
00279 $function($batch['form'], $batch['form_state']);
00280 }
00281 return TRUE;
00282 }
00283 }
00284
00290 function _batch_finished() {
00291 $batch =& batch_get();
00292
00293
00294 foreach ($batch['sets'] as $key => $batch_set) {
00295 if (isset($batch_set['finished'])) {
00296
00297 if (isset($batch_set['file']) && is_file($batch_set['file'])) {
00298 include_once($batch_set['file']);
00299 }
00300 if (function_exists($batch_set['finished'])) {
00301 $batch_set['finished']($batch_set['success'], $batch_set['results'], $batch_set['operations']);
00302 }
00303 }
00304 }
00305
00306
00307 if ($batch['progressive']) {
00308 db_query("DELETE FROM {batch} WHERE bid = %d", $batch['id']);
00309 }
00310 $_batch = $batch;
00311 $batch = NULL;
00312
00313
00314 if ($_batch['progressive']) {
00315
00316 if (isset($_batch['destination'])) {
00317 $_REQUEST['destination'] = $_batch['destination'];
00318 }
00319
00320
00321
00322 if (isset($_batch['form_state']['redirect'])) {
00323 $redirect = $_batch['form_state']['redirect'];
00324 }
00325 elseif (isset($_batch['redirect'])) {
00326 $redirect = $_batch['redirect'];
00327 }
00328 else {
00329 $redirect = $_batch['source_page'];
00330 }
00331
00332
00333 $form = isset($batch['form']) ? $batch['form'] : array();
00334 if (empty($_batch['form_state']['rebuild']) && empty($_batch['form_state']['storage'])) {
00335 drupal_redirect_form($form, $redirect);
00336 }
00337
00338
00339
00340
00341 $_SESSION['batch_form_state'] = $_batch['form_state'];
00342 drupal_goto($_batch['source_page']);
00343 }
00344 }
00345
00350 function _batch_shutdown() {
00351 if ($batch = batch_get()) {
00352 db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']);
00353 }
00354 }