| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: batch.inc,v 1.14.2.1 2010/08/06 11:52:19 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 drupal_maintenance_theme(); 137 $fallback = theme('maintenance_page', $fallback, FALSE, FALSE); 138 139 // We strip the end of the page using a marker in the template, so any 140 // additional HTML output by PHP shows up inside the page rather than 141 // below it. While this causes invalid HTML, the same would be true if 142 // we didn't, as content is not allowed to appear after </html> anyway. 143 list($fallback) = explode('<!--partial-->', $fallback); 144 print $fallback; 145 146 // Perform actual processing. 147 list($percentage, $message) = _batch_process($batch); 148 if ($percentage == 100) { 149 $new_op = 'finished'; 150 } 151 152 // PHP did not die : remove the fallback output. 153 ob_end_clean(); 154 } 155 156 $url = url($batch['url'], array('query' => array('id' => $batch['id'], 'op' => $new_op))); 157 drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL='. $url .'">'); 158 $output = theme('progress_bar', $percentage, $message); 159 return $output; 160 } 161 162 /** 163 * Advance batch processing for 1 second (or process the whole batch if it 164 * was not set for progressive execution - e.g forms submitted by drupal_execute). 165 */ 166 function _batch_process() { 167 $batch =& batch_get(); 168 $current_set =& _batch_current_set(); 169 $set_changed = TRUE; 170 171 if ($batch['progressive']) { 172 timer_start('batch_processing'); 173 } 174 175 while (!$current_set['success']) { 176 // If this is the first time we iterate this batch set in the current 177 // request, we check if it requires an additional file for functions 178 // definitions. 179 if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) { 180 include_once($current_set['file']); 181 } 182 183 $finished = 1; 184 $task_message = ''; 185 if ((list($function, $args) = reset($current_set['operations'])) && function_exists($function)) { 186 // Build the 'context' array, execute the function call, 187 // and retrieve the user message. 188 $batch_context = array('sandbox' => &$current_set['sandbox'], 'results' => &$current_set['results'], 'finished' => &$finished, 'message' => &$task_message); 189 // Process the current operation. 190 call_user_func_array($function, array_merge($args, array(&$batch_context))); 191 } 192 193 if ($finished == 1) { 194 // Make sure this step isn't counted double when computing $current. 195 $finished = 0; 196 // Remove the operation and clear the sandbox. 197 array_shift($current_set['operations']); 198 $current_set['sandbox'] = array(); 199 } 200 201 // If the batch set is completed, browse through the remaining sets, 202 // executing 'control sets' (stored form submit handlers) along the way - 203 // this might in turn insert new batch sets. 204 // Stop when we find a set that actually has operations. 205 $set_changed = FALSE; 206 $old_set = $current_set; 207 while (empty($current_set['operations']) && ($current_set['success'] = TRUE) && _batch_next_set()) { 208 $current_set =& _batch_current_set(); 209 $set_changed = TRUE; 210 } 211 // At this point, either $current_set is a 'real' batch set (has operations), 212 // or all sets have been completed. 213 214 // If we're in progressive mode, stop after 1 second. 215 if ($batch['progressive'] && timer_read('batch_processing') > 1000) { 216 break; 217 } 218 } 219 220 if ($batch['progressive']) { 221 // Gather progress information. 222 223 // Reporting 100% progress will cause the whole batch to be considered 224 // processed. If processing was paused right after moving to a new set, 225 // we have to use the info from the new (unprocessed) one. 226 if ($set_changed && isset($current_set['operations'])) { 227 // Processing will continue with a fresh batch set. 228 $remaining = count($current_set['operations']); 229 $total = $current_set['total']; 230 $progress_message = $current_set['init_message']; 231 $task_message = ''; 232 } 233 else { 234 $remaining = count($old_set['operations']); 235 $total = $old_set['total']; 236 $progress_message = $old_set['progress_message']; 237 } 238 239 $current = $total - $remaining + $finished; 240 $percentage = $total ? floor($current / $total * 100) : 100; 241 $values = array( 242 '@remaining' => $remaining, 243 '@total' => $total, 244 '@current' => floor($current), 245 '@percentage' => $percentage, 246 ); 247 $message = strtr($progress_message, $values) .'<br/>'; 248 $message .= $task_message ? $task_message : ' '; 249 250 return array($percentage, $message); 251 } 252 else { 253 // If we're not in progressive mode, the whole batch has been processed by now. 254 return _batch_finished(); 255 } 256 257 } 258 259 /** 260 * Retrieve the batch set being currently processed. 261 */ 262 function &_batch_current_set() { 263 $batch =& batch_get(); 264 return $batch['sets'][$batch['current_set']]; 265 } 266 267 /** 268 * Move execution to the next batch set if any, executing the stored 269 * form _submit handlers along the way (thus possibly inserting 270 * additional batch sets). 271 */ 272 function _batch_next_set() { 273 $batch =& batch_get(); 274 if (isset($batch['sets'][$batch['current_set'] + 1])) { 275 $batch['current_set']++; 276 $current_set =& _batch_current_set(); 277 if (isset($current_set['form_submit']) && ($function = $current_set['form_submit']) && function_exists($function)) { 278 // We use our stored copies of $form and $form_state, to account for 279 // possible alteration by the submit handlers. 280 $function($batch['form'], $batch['form_state']); 281 } 282 return TRUE; 283 } 284 } 285 286 /** 287 * End the batch processing: 288 * Call the 'finished' callbacks to allow custom handling of results, 289 * and resolve page redirection. 290 */ 291 function _batch_finished() { 292 $batch =& batch_get(); 293 294 // Execute the 'finished' callbacks for each batch set. 295 foreach ($batch['sets'] as $key => $batch_set) { 296 if (isset($batch_set['finished'])) { 297 // Check if the set requires an additional file for functions definitions. 298 if (isset($batch_set['file']) && is_file($batch_set['file'])) { 299 include_once($batch_set['file']); 300 } 301 if (function_exists($batch_set['finished'])) { 302 $batch_set['finished']($batch_set['success'], $batch_set['results'], $batch_set['operations']); 303 } 304 } 305 } 306 307 // Cleanup the batch table and unset the global $batch variable. 308 if ($batch['progressive']) { 309 db_query("DELETE FROM {batch} WHERE bid = %d", $batch['id']); 310 } 311 $_batch = $batch; 312 $batch = NULL; 313 314 // Redirect if needed. 315 if ($_batch['progressive']) { 316 // Put back the 'destination' that was saved in batch_process(). 317 if (isset($_batch['destination'])) { 318 $_REQUEST['destination'] = $_batch['destination']; 319 } 320 321 // Use $_batch['form_state']['redirect'], or $_batch['redirect'], 322 // or $_batch['source_page']. 323 if (isset($_batch['form_state']['redirect'])) { 324 $redirect = $_batch['form_state']['redirect']; 325 } 326 elseif (isset($_batch['redirect'])) { 327 $redirect = $_batch['redirect']; 328 } 329 else { 330 $redirect = $_batch['source_page']; 331 } 332 333 // Let drupal_redirect_form handle redirection logic. 334 $form = isset($batch['form']) ? $batch['form'] : array(); 335 if (empty($_batch['form_state']['rebuild']) && empty($_batch['form_state']['storage'])) { 336 drupal_redirect_form($form, $redirect); 337 } 338 339 // We get here if $form['#redirect'] was FALSE, or if the form is a 340 // multi-step form. We save the final $form_state value to be retrieved 341 // by drupal_get_form, and we redirect to the originating page. 342 $_SESSION['batch_form_state'] = $_batch['form_state']; 343 drupal_goto($_batch['source_page']); 344 } 345 } 346 347 /** 348 * Shutdown function: store the batch data for next request, 349 * or clear the table if the batch is finished. 350 */ 351 function _batch_shutdown() { 352 if ($batch = batch_get()) { 353 db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']); 354 } 355 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |