Mercurial > defr > drupal > core
comparison includes/batch.inc @ 1:c1f4ac30525a 6.0
Drupal 6.0
| author | Franck Deroche <webmaster@defr.org> |
|---|---|
| date | Tue, 23 Dec 2008 14:28:28 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:5a113a1c4740 | 1:c1f4ac30525a |
|---|---|
| 1 <?php | |
| 2 // $Id: batch.inc,v 1.14 2007/12/20 11:57:20 goba Exp $ | |
| 3 | |
| 4 /** | |
| 5 * @file Batch processing API for processes to run in multiple HTTP requests. | |
| 6 */ | |
| 7 | |
| 8 /** | |
| 9 * State-based dispatcher for the batch processing page. | |
| 10 */ | |
| 11 function _batch_page() { | |
| 12 $batch =& batch_get(); | |
| 13 | |
| 14 // Retrieve the current state of batch from db. | |
| 15 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'])))) { | |
| 16 $batch = unserialize($data); | |
| 17 } | |
| 18 else { | |
| 19 return FALSE; | |
| 20 } | |
| 21 | |
| 22 // Register database update for end of processing. | |
| 23 register_shutdown_function('_batch_shutdown'); | |
| 24 | |
| 25 $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : ''; | |
| 26 $output = NULL; | |
| 27 switch ($op) { | |
| 28 case 'start': | |
| 29 $output = _batch_start(); | |
| 30 break; | |
| 31 | |
| 32 case 'do': | |
| 33 // JS-version AJAX callback. | |
| 34 _batch_do(); | |
| 35 break; | |
| 36 | |
| 37 case 'do_nojs': | |
| 38 // Non-JS progress page. | |
| 39 $output = _batch_progress_page_nojs(); | |
| 40 break; | |
| 41 | |
| 42 case 'finished': | |
| 43 $output = _batch_finished(); | |
| 44 break; | |
| 45 } | |
| 46 | |
| 47 return $output; | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Initiate the batch processing | |
| 52 */ | |
| 53 function _batch_start() { | |
| 54 // Choose between the JS and non-JS version. | |
| 55 // JS-enabled users are identified through the 'has_js' cookie set in drupal.js. | |
| 56 // If the user did not visit any JS enabled page during his browser session, | |
| 57 // he gets the non-JS version... | |
| 58 if (isset($_COOKIE['has_js']) && $_COOKIE['has_js']) { | |
| 59 return _batch_progress_page_js(); | |
| 60 } | |
| 61 else { | |
| 62 return _batch_progress_page_nojs(); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 /** | |
| 67 * Batch processing page with JavaScript support. | |
| 68 */ | |
| 69 function _batch_progress_page_js() { | |
| 70 $batch = batch_get(); | |
| 71 | |
| 72 // The first batch set gets to set the page title | |
| 73 // and the initialization and error messages. | |
| 74 $current_set = _batch_current_set(); | |
| 75 drupal_set_title($current_set['title']); | |
| 76 drupal_add_js('misc/progress.js', 'core', 'header', FALSE, FALSE); | |
| 77 | |
| 78 $url = url($batch['url'], array('query' => array('id' => $batch['id']))); | |
| 79 $js_setting = array( | |
| 80 'batch' => array( | |
| 81 'errorMessage' => $current_set['error_message'] .'<br/>'. $batch['error_message'], | |
| 82 'initMessage' => $current_set['init_message'], | |
| 83 'uri' => $url, | |
| 84 ), | |
| 85 ); | |
| 86 drupal_add_js($js_setting, 'setting'); | |
| 87 drupal_add_js('misc/batch.js', 'core', 'header', FALSE, FALSE); | |
| 88 | |
| 89 $output = '<div id="progress"></div>'; | |
| 90 return $output; | |
| 91 } | |
| 92 | |
| 93 /** | |
| 94 * Do one pass of execution and inform back the browser about progression | |
| 95 * (used for JavaScript-mode only). | |
| 96 */ | |
| 97 function _batch_do() { | |
| 98 // HTTP POST required | |
| 99 if ($_SERVER['REQUEST_METHOD'] != 'POST') { | |
| 100 drupal_set_message(t('HTTP POST is required.'), 'error'); | |
| 101 drupal_set_title(t('Error')); | |
| 102 return ''; | |
| 103 } | |
| 104 | |
| 105 // Perform actual processing. | |
| 106 list($percentage, $message) = _batch_process(); | |
| 107 | |
| 108 drupal_json(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message)); | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Batch processing page without JavaScript support. | |
| 113 */ | |
| 114 function _batch_progress_page_nojs() { | |
| 115 $batch =& batch_get(); | |
| 116 $current_set = _batch_current_set(); | |
| 117 | |
| 118 drupal_set_title($current_set['title']); | |
| 119 | |
| 120 $new_op = 'do_nojs'; | |
| 121 | |
| 122 if (!isset($batch['running'])) { | |
| 123 // This is the first page so we return some output immediately. | |
| 124 $percentage = 0; | |
| 125 $message = $current_set['init_message']; | |
| 126 $batch['running'] = TRUE; | |
| 127 } | |
| 128 else { | |
| 129 // This is one of the later requests: do some processing first. | |
| 130 | |
| 131 // Error handling: if PHP dies due to a fatal error (e.g. non-existant | |
| 132 // function), it will output whatever is in the output buffer, | |
| 133 // followed by the error message. | |
| 134 ob_start(); | |
| 135 $fallback = $current_set['error_message'] .'<br/>'. $batch['error_message']; | |
| 136 $fallback = theme('maintenance_page', $fallback, FALSE, FALSE); | |
| 137 | |
| 138 // We strip the end of the page using a marker in the template, so any | |
| 139 // additional HTML output by PHP shows up inside the page rather than | |
| 140 // below it. While this causes invalid HTML, the same would be true if | |
| 141 // we didn't, as content is not allowed to appear after </html> anyway. | |
| 142 list($fallback) = explode('<!--partial-->', $fallback); | |
| 143 print $fallback; | |
| 144 | |
| 145 // Perform actual processing. | |
| 146 list($percentage, $message) = _batch_process($batch); | |
| 147 if ($percentage == 100) { | |
| 148 $new_op = 'finished'; | |
| 149 } | |
| 150 | |
| 151 // PHP did not die : remove the fallback output. | |
| 152 ob_end_clean(); | |
| 153 } | |
| 154 | |
| 155 $url = url($batch['url'], array('query' => array('id' => $batch['id'], 'op' => $new_op))); | |
| 156 drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL='. $url .'">'); | |
| 157 $output = theme('progress_bar', $percentage, $message); | |
| 158 return $output; | |
| 159 } | |
| 160 | |
| 161 /** | |
| 162 * Advance batch processing for 1 second (or process the whole batch if it | |
| 163 * was not set for progressive execution - e.g forms submitted by drupal_execute). | |
| 164 */ | |
| 165 function _batch_process() { | |
| 166 $batch =& batch_get(); | |
| 167 $current_set =& _batch_current_set(); | |
| 168 $set_changed = TRUE; | |
| 169 | |
| 170 if ($batch['progressive']) { | |
| 171 timer_start('batch_processing'); | |
| 172 } | |
| 173 | |
| 174 while (!$current_set['success']) { | |
| 175 // If this is the first time we iterate this batch set in the current | |
| 176 // request, we check if it requires an additional file for functions | |
| 177 // definitions. | |
| 178 if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) { | |
| 179 include_once($current_set['file']); | |
| 180 } | |
| 181 | |
| 182 $finished = 1; | |
| 183 $task_message = ''; | |
| 184 if ((list($function, $args) = reset($current_set['operations'])) && function_exists($function)) { | |
| 185 // Build the 'context' array, execute the function call, | |
| 186 // and retrieve the user message. | |
| 187 $batch_context = array('sandbox' => &$current_set['sandbox'], 'results' => &$current_set['results'], 'finished' => &$finished, 'message' => &$task_message); | |
| 188 // Process the current operation. | |
| 189 call_user_func_array($function, array_merge($args, array(&$batch_context))); | |
| 190 } | |
| 191 | |
| 192 if ($finished == 1) { | |
| 193 // Make sure this step isn't counted double when computing $current. | |
| 194 $finished = 0; | |
| 195 // Remove the operation and clear the sandbox. | |
| 196 array_shift($current_set['operations']); | |
| 197 $current_set['sandbox'] = array(); | |
| 198 } | |
| 199 | |
| 200 // If the batch set is completed, browse through the remaining sets, | |
| 201 // executing 'control sets' (stored form submit handlers) along the way - | |
| 202 // this might in turn insert new batch sets. | |
| 203 // Stop when we find a set that actually has operations. | |
| 204 $set_changed = FALSE; | |
| 205 $old_set = $current_set; | |
| 206 while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) { | |
| 207 $current_set =& _batch_current_set(); | |
| 208 $set_changed = TRUE; | |
| 209 } | |
| 210 // At this point, either $current_set is a 'real' batch set (has operations), | |
| 211 // or all sets have been completed. | |
| 212 | |
| 213 // If we're in progressive mode, stop after 1 second. | |
| 214 if ($batch['progressive'] && timer_read('batch_processing') > 1000) { | |
| 215 break; | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 if ($batch['progressive']) { | |
| 220 // Gather progress information. | |
| 221 | |
| 222 // Reporting 100% progress will cause the whole batch to be considered | |
| 223 // processed. If processing was paused right after moving to a new set, | |
| 224 // we have to use the info from the new (unprocessed) one. | |
| 225 if ($set_changed && isset($current_set['operations'])) { | |
| 226 // Processing will continue with a fresh batch set. | |
| 227 $remaining = count($current_set['operations']); | |
| 228 $total = $current_set['total']; | |
| 229 $progress_message = $current_set['init_message']; | |
| 230 $task_message = ''; | |
| 231 } | |
| 232 else { | |
| 233 $remaining = count($old_set['operations']); | |
| 234 $total = $old_set['total']; | |
| 235 $progress_message = $old_set['progress_message']; | |
| 236 } | |
| 237 | |
| 238 $current = $total - $remaining + $finished; | |
| 239 $percentage = $total ? floor($current / $total * 100) : 100; | |
| 240 $values = array( | |
| 241 '@remaining' => $remaining, | |
| 242 '@total' => $total, | |
| 243 '@current' => floor($current), | |
| 244 '@percentage' => $percentage, | |
| 245 ); | |
| 246 $message = strtr($progress_message, $values) .'<br/>'; | |
| 247 $message .= $task_message ? $task_message : ' '; | |
| 248 | |
| 249 return array($percentage, $message); | |
| 250 } | |
| 251 else { | |
| 252 // If we're not in progressive mode, the whole batch has been processed by now. | |
| 253 return _batch_finished(); | |
| 254 } | |
| 255 | |
| 256 } | |
| 257 | |
| 258 /** | |
| 259 * Retrieve the batch set being currently processed. | |
| 260 */ | |
| 261 function &_batch_current_set() { | |
| 262 $batch =& batch_get(); | |
| 263 return $batch['sets'][$batch['current_set']]; | |
| 264 } | |
| 265 | |
| 266 /** | |
| 267 * Move execution to the next batch set if any, executing the stored | |
| 268 * form _submit handlers along the way (thus possibly inserting | |
| 269 * additional batch sets). | |
| 270 */ | |
| 271 function _batch_next_set() { | |
| 272 $batch =& batch_get(); | |
| 273 if (isset($batch['sets'][$batch['current_set'] + 1])) { | |
| 274 $batch['current_set']++; | |
| 275 $current_set =& _batch_current_set(); | |
| 276 if (isset($current_set['form_submit']) && ($function = $current_set['form_submit']) && function_exists($function)) { | |
| 277 // We use our stored copies of $form and $form_state, to account for | |
| 278 // possible alteration by the submit handlers. | |
| 279 $function($batch['form'], $batch['form_state']); | |
| 280 } | |
| 281 return TRUE; | |
| 282 } | |
| 283 } | |
| 284 | |
| 285 /** | |
| 286 * End the batch processing: | |
| 287 * Call the 'finished' callbacks to allow custom handling of results, | |
| 288 * and resolve page redirection. | |
| 289 */ | |
| 290 function _batch_finished() { | |
| 291 $batch =& batch_get(); | |
| 292 | |
| 293 // Execute the 'finished' callbacks for each batch set. | |
| 294 foreach ($batch['sets'] as $key => $batch_set) { | |
| 295 if (isset($batch_set['finished'])) { | |
| 296 // Check if the set requires an additional file for functions definitions. | |
| 297 if (isset($batch_set['file']) && is_file($batch_set['file'])) { | |
| 298 include_once($batch_set['file']); | |
| 299 } | |
| 300 if (function_exists($batch_set['finished'])) { | |
| 301 $batch_set['finished']($batch_set['success'], $batch_set['results'], $batch_set['operations']); | |
| 302 } | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 // Cleanup the batch table and unset the global $batch variable. | |
| 307 if ($batch['progressive']) { | |
| 308 db_query("DELETE FROM {batch} WHERE bid = %d", $batch['id']); | |
| 309 } | |
| 310 $_batch = $batch; | |
| 311 $batch = NULL; | |
| 312 | |
| 313 // Redirect if needed. | |
| 314 if ($_batch['progressive']) { | |
| 315 // Put back the 'destination' that was saved in batch_process(). | |
| 316 if (isset($_batch['destination'])) { | |
| 317 $_REQUEST['destination'] = $_batch['destination']; | |
| 318 } | |
| 319 | |
| 320 // Use $_batch['form_state']['redirect'], or $_batch['redirect'], | |
| 321 // or $_batch['source_page']. | |
| 322 if (isset($_batch['form_state']['redirect'])) { | |
| 323 $redirect = $_batch['form_state']['redirect']; | |
| 324 } | |
| 325 elseif (isset($_batch['redirect'])) { | |
| 326 $redirect = $_batch['redirect']; | |
| 327 } | |
| 328 else { | |
| 329 $redirect = $_batch['source_page']; | |
| 330 } | |
| 331 | |
| 332 // Let drupal_redirect_form handle redirection logic. | |
| 333 $form = isset($batch['form']) ? $batch['form'] : array(); | |
| 334 if (empty($_batch['form_state']['rebuild']) && empty($_batch['form_state']['storage'])) { | |
| 335 drupal_redirect_form($form, $redirect); | |
| 336 } | |
| 337 | |
| 338 // We get here if $form['#redirect'] was FALSE, or if the form is a | |
| 339 // multi-step form. We save the final $form_state value to be retrieved | |
| 340 // by drupal_get_form, and we redirect to the originating page. | |
| 341 $_SESSION['batch_form_state'] = $_batch['form_state']; | |
| 342 drupal_goto($_batch['source_page']); | |
| 343 } | |
| 344 } | |
| 345 | |
| 346 /** | |
| 347 * Shutdown function: store the batch data for next request, | |
| 348 * or clear the table if the batch is finished. | |
| 349 */ | |
| 350 function _batch_shutdown() { | |
| 351 if ($batch = batch_get()) { | |
| 352 db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']); | |
| 353 } | |
| 354 } |
