batch_test.callbacks.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Batch callbacks for the Batch API tests.
  5. */
  6. /**
  7. * Simple batch operation.
  8. */
  9. function _batch_test_callback_1($id, $sleep, &$context) {
  10. // No-op, but ensure the batch take a couple iterations.
  11. // Batch needs time to run for the test, so sleep a bit.
  12. usleep($sleep);
  13. // Track execution, and store some result for post-processing in the
  14. // 'finished' callback.
  15. batch_test_stack("op 1 id $id");
  16. $context['results'][1][] = $id;
  17. }
  18. /**
  19. * Multistep batch operation.
  20. */
  21. function _batch_test_callback_2($start, $total, $sleep, &$context) {
  22. // Initialize context with progress information.
  23. if (!isset($context['sandbox']['current'])) {
  24. $context['sandbox']['current'] = $start;
  25. $context['sandbox']['count'] = 0;
  26. }
  27. // Process by groups of 5 (arbitrary value).
  28. $limit = 5;
  29. for ($i = 0; $i < $limit && $context['sandbox']['count'] < $total; $i++) {
  30. // No-op, but ensure the batch take a couple iterations.
  31. // Batch needs time to run for the test, so sleep a bit.
  32. usleep($sleep);
  33. // Track execution, and store some result for post-processing in the
  34. // 'finished' callback.
  35. $id = $context['sandbox']['current'] + $i;
  36. batch_test_stack("op 2 id $id");
  37. $context['results'][2][] = $id;
  38. // Update progress information.
  39. $context['sandbox']['count']++;
  40. }
  41. $context['sandbox']['current'] += $i;
  42. // Inform batch engine about progress.
  43. if ($context['sandbox']['count'] != $total) {
  44. $context['finished'] = $context['sandbox']['count'] / $total;
  45. }
  46. }
  47. /**
  48. * Simple batch operation.
  49. */
  50. function _batch_test_callback_5($id, $sleep, &$context) {
  51. // No-op, but ensure the batch take a couple iterations.
  52. // Batch needs time to run for the test, so sleep a bit.
  53. usleep($sleep);
  54. // Track execution, and store some result for post-processing in the
  55. // 'finished' callback.
  56. batch_test_stack("op 5 id $id");
  57. $context['results'][5][] = $id;
  58. // This test is to test finished > 1
  59. $context['finished'] = 3.14;
  60. }
  61. /**
  62. * Batch operation setting up its own batch.
  63. */
  64. function _batch_test_nested_batch_callback() {
  65. batch_test_stack('setting up batch 2');
  66. batch_set(_batch_test_batch_2());
  67. }
  68. /**
  69. * Common 'finished' callbacks for batches 1 to 4.
  70. */
  71. function _batch_test_finished_helper($batch_id, $success, $results, $operations) {
  72. $messages = array("results for batch $batch_id");
  73. if ($results) {
  74. foreach ($results as $op => $op_results) {
  75. $messages[] = 'op '. $op . ': processed ' . count($op_results) . ' elements';
  76. }
  77. }
  78. else {
  79. $messages[] = 'none';
  80. }
  81. if (!$success) {
  82. // A fatal error occurred during the processing.
  83. $error_operation = reset($operations);
  84. $messages[] = t('An error occurred while processing @op with arguments:<br/>@args', array('@op' => $error_operation[0], '@args' => print_r($error_operation[1], TRUE)));
  85. }
  86. drupal_set_message(implode('<br />', $messages));
  87. }
  88. /**
  89. * 'finished' callback for batch 0.
  90. */
  91. function _batch_test_finished_0($success, $results, $operations) {
  92. _batch_test_finished_helper(0, $success, $results, $operations);
  93. }
  94. /**
  95. * 'finished' callback for batch 1.
  96. */
  97. function _batch_test_finished_1($success, $results, $operations) {
  98. _batch_test_finished_helper(1, $success, $results, $operations);
  99. }
  100. /**
  101. * 'finished' callback for batch 2.
  102. */
  103. function _batch_test_finished_2($success, $results, $operations) {
  104. _batch_test_finished_helper(2, $success, $results, $operations);
  105. }
  106. /**
  107. * 'finished' callback for batch 3.
  108. */
  109. function _batch_test_finished_3($success, $results, $operations) {
  110. _batch_test_finished_helper(3, $success, $results, $operations);
  111. }
  112. /**
  113. * 'finished' callback for batch 4.
  114. */
  115. function _batch_test_finished_4($success, $results, $operations) {
  116. _batch_test_finished_helper(4, $success, $results, $operations);
  117. }
  118. /**
  119. * 'finished' callback for batch 5.
  120. */
  121. function _batch_test_finished_5($success, $results, $operations) {
  122. _batch_test_finished_helper(5, $success, $results, $operations);
  123. }