batch.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. /**
  3. * @file
  4. * Batch processing API for processes to run in multiple HTTP requests.
  5. *
  6. * Note that batches are usually invoked by form submissions, which is
  7. * why the core interaction functions of the batch processing API live in
  8. * form.inc.
  9. *
  10. * @see form.inc
  11. * @see batch_set()
  12. * @see batch_process()
  13. * @see batch_get()
  14. */
  15. /**
  16. * Loads a batch from the database.
  17. *
  18. * @param $id
  19. * The ID of the batch to load. When a progressive batch is being processed,
  20. * the relevant ID is found in $_REQUEST['id'].
  21. *
  22. * @return
  23. * An array representing the batch, or FALSE if no batch was found.
  24. */
  25. function batch_load($id) {
  26. $batch = db_query("SELECT batch FROM {batch} WHERE bid = :bid AND token = :token", array(
  27. ':bid' => $id,
  28. ':token' => drupal_get_token($id),
  29. ))->fetchField();
  30. if ($batch) {
  31. return unserialize($batch);
  32. }
  33. return FALSE;
  34. }
  35. /**
  36. * Renders the batch processing page based on the current state of the batch.
  37. *
  38. * @see _batch_shutdown()
  39. */
  40. function _batch_page() {
  41. $batch = &batch_get();
  42. if (!isset($_REQUEST['id'])) {
  43. return FALSE;
  44. }
  45. // Retrieve the current state of the batch.
  46. if (!$batch) {
  47. $batch = batch_load($_REQUEST['id']);
  48. if (!$batch) {
  49. drupal_set_message(t('No active batch.'), 'error');
  50. drupal_goto();
  51. }
  52. }
  53. // Register database update for the end of processing.
  54. drupal_register_shutdown_function('_batch_shutdown');
  55. // Add batch-specific CSS.
  56. foreach ($batch['sets'] as $batch_set) {
  57. if (isset($batch_set['css'])) {
  58. foreach ($batch_set['css'] as $css) {
  59. drupal_add_css($css);
  60. }
  61. }
  62. }
  63. $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
  64. $output = NULL;
  65. switch ($op) {
  66. case 'start':
  67. // Display the full progress page on startup and on each additional
  68. // non-JavaScript iteration.
  69. $output = _batch_progress_page();
  70. break;
  71. case 'do':
  72. // JavaScript-based progress page callback.
  73. _batch_do();
  74. break;
  75. case 'do_nojs':
  76. // Non-JavaScript-based progress page.
  77. $output = _batch_progress_page();
  78. break;
  79. case 'finished':
  80. $output = _batch_finished();
  81. break;
  82. }
  83. return $output;
  84. }
  85. /**
  86. * Does one execution pass with JavaScript and returns progress to the browser.
  87. *
  88. * @see _batch_process()
  89. */
  90. function _batch_do() {
  91. // Perform actual processing.
  92. list($percentage, $message) = _batch_process();
  93. drupal_json_output(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message));
  94. }
  95. /**
  96. * Outputs a batch processing page.
  97. *
  98. * @see _batch_process()
  99. */
  100. function _batch_progress_page() {
  101. $batch = &batch_get();
  102. $current_set = _batch_current_set();
  103. drupal_set_title($current_set['title'], PASS_THROUGH);
  104. $new_op = 'do_nojs';
  105. if (!isset($batch['running'])) {
  106. // This is the first page so we return some output immediately.
  107. $percentage = 0;
  108. $message = $current_set['init_message'];
  109. $batch['running'] = TRUE;
  110. }
  111. else {
  112. // This is one of the later requests; do some processing first.
  113. // Error handling: if PHP dies due to a fatal error (e.g. a nonexistent
  114. // function), it will output whatever is in the output buffer, followed by
  115. // the error message.
  116. ob_start();
  117. $fallback = $current_set['error_message'] . '<br />' . $batch['error_message'];
  118. $fallback = theme('maintenance_page', array('content' => $fallback, 'show_messages' => FALSE));
  119. // We strip the end of the page using a marker in the template, so any
  120. // additional HTML output by PHP shows up inside the page rather than below
  121. // it. While this causes invalid HTML, the same would be true if we didn't,
  122. // as content is not allowed to appear after </html> anyway.
  123. list($fallback) = explode('<!--partial-->', $fallback);
  124. print $fallback;
  125. // Perform actual processing.
  126. list($percentage, $message) = _batch_process($batch);
  127. if ($percentage == 100) {
  128. $new_op = 'finished';
  129. }
  130. // PHP did not die; remove the fallback output.
  131. ob_end_clean();
  132. }
  133. // Merge required query parameters for batch processing into those provided by
  134. // batch_set() or hook_batch_alter().
  135. $batch['url_options']['query']['id'] = $batch['id'];
  136. $batch['url_options']['query']['op'] = $new_op;
  137. $url = url($batch['url'], $batch['url_options']);
  138. $element = array(
  139. // Redirect through a 'Refresh' meta tag if JavaScript is disabled.
  140. '#prefix' => '<noscript>',
  141. '#suffix' => '</noscript>',
  142. '#tag' => 'meta',
  143. '#attributes' => array(
  144. 'http-equiv' => 'Refresh',
  145. 'content' => '0; URL=' . $url,
  146. ),
  147. );
  148. drupal_add_html_head($element, 'batch_progress_meta_refresh');
  149. // Adds JavaScript code and settings for clients where JavaScript is enabled.
  150. $js_setting = array(
  151. 'batch' => array(
  152. 'errorMessage' => $current_set['error_message'] . '<br />' . $batch['error_message'],
  153. 'initMessage' => $current_set['init_message'],
  154. 'uri' => $url,
  155. ),
  156. );
  157. drupal_add_js($js_setting, 'setting');
  158. drupal_add_library('system', 'drupal.batch');
  159. return theme('progress_bar', array('percent' => $percentage, 'message' => $message));
  160. }
  161. /**
  162. * Processes sets in a batch.
  163. *
  164. * If the batch was marked for progressive execution (default), this executes as
  165. * many operations in batch sets until an execution time of 1 second has been
  166. * exceeded. It will continue with the next operation of the same batch set in
  167. * the next request.
  168. *
  169. * @return
  170. * An array containing a completion value (in percent) and a status message.
  171. */
  172. function _batch_process() {
  173. $batch = &batch_get();
  174. $current_set = &_batch_current_set();
  175. // Indicate that this batch set needs to be initialized.
  176. $set_changed = TRUE;
  177. // If this batch was marked for progressive execution (e.g. forms submitted by
  178. // drupal_form_submit()), initialize a timer to determine whether we need to
  179. // proceed with the same batch phase when a processing time of 1 second has
  180. // been exceeded.
  181. if ($batch['progressive']) {
  182. timer_start('batch_processing');
  183. }
  184. if (empty($current_set['start'])) {
  185. $current_set['start'] = microtime(TRUE);
  186. }
  187. $queue = _batch_queue($current_set);
  188. while (!$current_set['success']) {
  189. // If this is the first time we iterate this batch set in the current
  190. // request, we check if it requires an additional file for functions
  191. // definitions.
  192. if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
  193. include_once DRUPAL_ROOT . '/' . $current_set['file'];
  194. }
  195. $task_message = '';
  196. // Assume a single pass operation and set the completion level to 1 by
  197. // default.
  198. $finished = 1;
  199. if ($item = $queue->claimItem()) {
  200. list($function, $args) = $item->data;
  201. // Build the 'context' array and execute the function call.
  202. $batch_context = array(
  203. 'sandbox' => &$current_set['sandbox'],
  204. 'results' => &$current_set['results'],
  205. 'finished' => &$finished,
  206. 'message' => &$task_message,
  207. );
  208. call_user_func_array($function, array_merge($args, array(&$batch_context)));
  209. if ($finished >= 1) {
  210. // Make sure this step is not counted twice when computing $current.
  211. $finished = 0;
  212. // Remove the processed operation and clear the sandbox.
  213. $queue->deleteItem($item);
  214. $current_set['count']--;
  215. $current_set['sandbox'] = array();
  216. }
  217. }
  218. // When all operations in the current batch set are completed, browse
  219. // through the remaining sets, marking them 'successfully processed'
  220. // along the way, until we find a set that contains operations.
  221. // _batch_next_set() executes form submit handlers stored in 'control'
  222. // sets (see form_execute_handlers()), which can in turn add new sets to
  223. // the batch.
  224. $set_changed = FALSE;
  225. $old_set = $current_set;
  226. while (empty($current_set['count']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
  227. $current_set = &_batch_current_set();
  228. $current_set['start'] = microtime(TRUE);
  229. $set_changed = TRUE;
  230. }
  231. // At this point, either $current_set contains operations that need to be
  232. // processed or all sets have been completed.
  233. $queue = _batch_queue($current_set);
  234. // If we are in progressive mode, break processing after 1 second.
  235. if ($batch['progressive'] && timer_read('batch_processing') > 1000) {
  236. // Record elapsed wall clock time.
  237. $current_set['elapsed'] = round((microtime(TRUE) - $current_set['start']) * 1000, 2);
  238. break;
  239. }
  240. }
  241. if ($batch['progressive']) {
  242. // Gather progress information.
  243. // Reporting 100% progress will cause the whole batch to be considered
  244. // processed. If processing was paused right after moving to a new set,
  245. // we have to use the info from the new (unprocessed) set.
  246. if ($set_changed && isset($current_set['queue'])) {
  247. // Processing will continue with a fresh batch set.
  248. $remaining = $current_set['count'];
  249. $total = $current_set['total'];
  250. $progress_message = $current_set['init_message'];
  251. $task_message = '';
  252. }
  253. else {
  254. // Processing will continue with the current batch set.
  255. $remaining = $old_set['count'];
  256. $total = $old_set['total'];
  257. $progress_message = $old_set['progress_message'];
  258. }
  259. // Total progress is the number of operations that have fully run plus the
  260. // completion level of the current operation.
  261. $current = $total - $remaining + $finished;
  262. $percentage = _batch_api_percentage($total, $current);
  263. $elapsed = isset($current_set['elapsed']) ? $current_set['elapsed'] : 0;
  264. $values = array(
  265. '@remaining' => $remaining,
  266. '@total' => $total,
  267. '@current' => floor($current),
  268. '@percentage' => $percentage,
  269. '@elapsed' => format_interval($elapsed / 1000),
  270. // If possible, estimate remaining processing time.
  271. '@estimate' => ($current > 0) ? format_interval(($elapsed * ($total - $current) / $current) / 1000) : '-',
  272. );
  273. $message = strtr($progress_message, $values);
  274. if (!empty($message)) {
  275. $message .= '<br />';
  276. }
  277. if (!empty($task_message)) {
  278. $message .= $task_message;
  279. }
  280. return array($percentage, $message);
  281. }
  282. else {
  283. // If we are not in progressive mode, the entire batch has been processed.
  284. return _batch_finished();
  285. }
  286. }
  287. /**
  288. * Formats the percent completion for a batch set.
  289. *
  290. * @param $total
  291. * The total number of operations.
  292. * @param $current
  293. * The number of the current operation. This may be a floating point number
  294. * rather than an integer in the case of a multi-step operation that is not
  295. * yet complete; in that case, the fractional part of $current represents the
  296. * fraction of the operation that has been completed.
  297. *
  298. * @return
  299. * The properly formatted percentage, as a string. We output percentages
  300. * using the correct number of decimal places so that we never print "100%"
  301. * until we are finished, but we also never print more decimal places than
  302. * are meaningful.
  303. *
  304. * @see _batch_process()
  305. */
  306. function _batch_api_percentage($total, $current) {
  307. if (!$total || $total == $current) {
  308. // If $total doesn't evaluate as true or is equal to the current set, then
  309. // we're finished, and we can return "100".
  310. $percentage = "100";
  311. }
  312. else {
  313. // We add a new digit at 200, 2000, etc. (since, for example, 199/200
  314. // would round up to 100% if we didn't).
  315. $decimal_places = max(0, floor(log10($total / 2.0)) - 1);
  316. do {
  317. // Calculate the percentage to the specified number of decimal places.
  318. $percentage = sprintf('%01.' . $decimal_places . 'f', round($current / $total * 100, $decimal_places));
  319. // When $current is an integer, the above calculation will always be
  320. // correct. However, if $current is a floating point number (in the case
  321. // of a multi-step batch operation that is not yet complete), $percentage
  322. // may be erroneously rounded up to 100%. To prevent that, we add one
  323. // more decimal place and try again.
  324. $decimal_places++;
  325. } while ($percentage == '100');
  326. }
  327. return $percentage;
  328. }
  329. /**
  330. * Returns the batch set being currently processed.
  331. */
  332. function &_batch_current_set() {
  333. $batch = &batch_get();
  334. return $batch['sets'][$batch['current_set']];
  335. }
  336. /**
  337. * Retrieves the next set in a batch.
  338. *
  339. * If there is a subsequent set in this batch, assign it as the new set to
  340. * process and execute its form submit handler (if defined), which may add
  341. * further sets to this batch.
  342. *
  343. * @return
  344. * TRUE if a subsequent set was found in the batch.
  345. */
  346. function _batch_next_set() {
  347. $batch = &batch_get();
  348. if (isset($batch['sets'][$batch['current_set'] + 1])) {
  349. $batch['current_set']++;
  350. $current_set = &_batch_current_set();
  351. if (isset($current_set['form_submit']) && ($function = $current_set['form_submit']) && function_exists($function)) {
  352. // We use our stored copies of $form and $form_state to account for
  353. // possible alterations by previous form submit handlers.
  354. $function($batch['form_state']['complete form'], $batch['form_state']);
  355. }
  356. return TRUE;
  357. }
  358. }
  359. /**
  360. * Ends the batch processing.
  361. *
  362. * Call the 'finished' callback of each batch set to allow custom handling of
  363. * the results and resolve page redirection.
  364. */
  365. function _batch_finished() {
  366. $batch = &batch_get();
  367. // Execute the 'finished' callbacks for each batch set, if defined.
  368. foreach ($batch['sets'] as $batch_set) {
  369. if (isset($batch_set['finished'])) {
  370. // Check if the set requires an additional file for function definitions.
  371. if (isset($batch_set['file']) && is_file($batch_set['file'])) {
  372. include_once DRUPAL_ROOT . '/' . $batch_set['file'];
  373. }
  374. if (is_callable($batch_set['finished'])) {
  375. $queue = _batch_queue($batch_set);
  376. $operations = $queue->getAllItems();
  377. call_user_func($batch_set['finished'], $batch_set['success'], $batch_set['results'], $operations, format_interval($batch_set['elapsed'] / 1000));
  378. }
  379. }
  380. }
  381. // Clean up the batch table and unset the static $batch variable.
  382. if ($batch['progressive']) {
  383. db_delete('batch')
  384. ->condition('bid', $batch['id'])
  385. ->execute();
  386. foreach ($batch['sets'] as $batch_set) {
  387. if ($queue = _batch_queue($batch_set)) {
  388. $queue->deleteQueue();
  389. }
  390. }
  391. // Clean-up the session. Not needed for CLI updates.
  392. if (isset($_SESSION)) {
  393. unset($_SESSION['batches'][$batch['id']]);
  394. if (empty($_SESSION['batches'])) {
  395. unset($_SESSION['batches']);
  396. }
  397. }
  398. }
  399. $_batch = $batch;
  400. $batch = NULL;
  401. // Redirect if needed.
  402. if ($_batch['progressive']) {
  403. // Revert the 'destination' that was saved in batch_process().
  404. if (isset($_batch['destination'])) {
  405. $_GET['destination'] = $_batch['destination'];
  406. }
  407. // Determine the target path to redirect to.
  408. if (!isset($_batch['form_state']['redirect'])) {
  409. if (isset($_batch['redirect'])) {
  410. $_batch['form_state']['redirect'] = $_batch['redirect'];
  411. }
  412. else {
  413. $_batch['form_state']['redirect'] = $_batch['source_url'];
  414. }
  415. }
  416. // Use drupal_redirect_form() to handle the redirection logic.
  417. drupal_redirect_form($_batch['form_state']);
  418. // If no redirection happened, redirect to the originating page. In case the
  419. // form needs to be rebuilt, save the final $form_state for
  420. // drupal_build_form().
  421. if (!empty($_batch['form_state']['rebuild'])) {
  422. $_SESSION['batch_form_state'] = $_batch['form_state'];
  423. }
  424. $function = $_batch['redirect_callback'];
  425. if (function_exists($function)) {
  426. $function($_batch['source_url'], array('query' => array('op' => 'finish', 'id' => $_batch['id'])));
  427. }
  428. }
  429. }
  430. /**
  431. * Shutdown function: Stores the current batch data for the next request.
  432. *
  433. * @see _batch_page()
  434. * @see drupal_register_shutdown_function()
  435. */
  436. function _batch_shutdown() {
  437. if ($batch = batch_get()) {
  438. db_update('batch')
  439. ->fields(array('batch' => serialize($batch)))
  440. ->condition('bid', $batch['id'])
  441. ->execute();
  442. }
  443. }