| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @file Batch processing API for processes to run in multiple HTTP requests. 5 */ 6 7 /** 8 * State-based dispatcher for the batch processing page. 9 */ 10 function _batch_page() { 11 $batch =& batch_get(); 12 13 // Retrieve the current state of batch from db. 14 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'])))) { 15 $batch = unserialize($data); 16 } 17 else { 18 return FALSE; 19 } 20 21 // Register database update for end of processing. 22 register_shutdown_function('_batch_shutdown'); 23 24 $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : ''; 25 $output = NULL; 26 switch ($op) { 27 case 'start': 28 $output = _batch_start(); 29 break; 30 31 case 'do': 32 // JS-version AJAX callback. 33 _batch_do(); 34 break; 35 36 case 'do_nojs': 37 // Non-JS progress page. 38 $output = _batch_progress_page_nojs(); 39 break; 40 41 case 'finished': 42 $output = _batch_finished(); 43 break; 44 } 45 46 return $output; 47 } 48 49 /** 50 * Initiate the batch processing 51 */ 52 function _batch_start() { 53 // Choose between the JS and non-JS version. 54 // JS-enabled users are identified through the 'has_js' cookie set in drupal.js. 55 // If the user did not visit any JS enabled page during his browser session, 56 // he gets the non-JS version... 57 if (isset($_COOKIE['has_js']) && $_COOKIE['has_js']) { 58 return _batch_progress_page_js(); 59 } 60 else { 61 return _batch_progress_page_nojs(); 62 } 63 } 64 65 /** 66 * Batch processing page with JavaScript support. 67 */ 68 function _batch_progress_page_js() { 69 $batch = batch_get(); 70 71 // The first batch set gets to set the page title 72 // and the initialization and error messages. 73 $current_set = _batch_current_set(); 74 drupal_set_title($current_set['title']); 75 drupal_add_js('misc/progress.js', 'core', 'header', FALSE, FALSE); 76 77 $url = url($batch['url'], array('query' => array('id' => $batch['id']))); 78 $js_setting = array( 79 'batch' => array( 80 'errorMessage' => $current_set['error_message'] .'<br/>'. $batch['error_message'], 81 'initMessage' => $current_set['init_message'], 82 'uri' => $url, 83 ), 84 ); 85 drupal_add_js($js_setting, 'setting'); 86 drupal_add_js('misc/batch.js', 'core', 'header', FALSE, FALSE); 87 88 $output = '<div id="progress"></div>'; 89 return $output; 90 } 91 92 /** 93 * Do one pass of execution and inform back the browser about progression 94 * (used for JavaScript-mode only). 95 */ 96 function _batch_do() { 97 // HTTP POST required 98 if ($_SERVER['REQUEST_METHOD'] != 'POST') { 99 drupal_set_message(t('HTTP POST is required.'), 'error'); 100 drupal_set_title(t('Error')); 101 return ''; 102 } 103 104 // Perform actual processing. 105 list($percentage, $message) = _batch_process(); 106 107 drupal_json(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message)); 108 } 109 110 /** 111 * Batch processing page without JavaScript support. 112 */ 113 function _batch_progress_page_nojs() { 114 $batch =& batch_get(); 115 $current_set = _batch_current_set(); 116 117 drupal_set_title($current_set['title']); 118 119 $new_op = 'do_nojs'; 120 121 if (!isset($batch['running'])) { 122 // This is the first page so we return some output immediately. 123 $percentage = 0; 124 $message = $current_set['init_message']; 125 $batch['running'] = TRUE; 126 } 127 else { 128 // This is one of the later requests: do some processing first. 129 130 // Error handling: if PHP dies due to a fatal error (e.g. non-existant 131 // function), it will output whatever is in the output buffer, 132 // followed by the error message. 133 ob_start(); 134 $fallback = $current_set['error_message'] .'<br/>'. $batch['error_message']; 135 drupal_maintenance_theme(); 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |