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