batch_test.callbacks.inc 4.4 KB

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