batch.test.inc

  1. 6.x modules/batch/batch.test.inc
  2. 5.x modules/batch/batch.test.inc

This file contains functionality for testing out Batch's processing with a sample form which performs some arbitrary actions.

File

modules/batch/batch.test.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * This file contains functionality for testing out Batch's processing with a sample form which performs
  5. * some arbitrary actions.
  6. */
  7. /**
  8. * This is our test "finished" page which will be displayed upon completion of the batch (if selected in the test form).
  9. *
  10. * Notice that $batch is being automatically supplied.
  11. *
  12. * @param unknown_type $batch
  13. */
  14. function batch_test_finished_page($batch, $items_max) {
  15. $rtn = "";
  16. $rtn .= "<p>" . t("The batch has completed. You processed %max items.", array("%max" => $items_max)) . "</p>";
  17. $rtn .= "<p>" . l(t("Return to form."), "batch-test-form") . "</p>";
  18. return $rtn;
  19. }
  20. /**
  21. * Basic sample form, accessed at site.com/batch-test
  22. *
  23. */
  24. function batch_test_form() {
  25. $form = array();
  26. $form["mark" . $m++] = array(
  27. "value" => "<p>" . t("This is a test form, so you can see how the Batch API system works. When you submit this form,
  28. an arbitrary batch process will begin.") . "</p>",
  29. );
  30. $form["items"] = array(
  31. "label" => t("How many 'items' to process?"),
  32. "type" => "select",
  33. "options" => array(100 => 100, 200 => 200, 300 => 300, 400 => 400, 500 => 500),
  34. "hide_please_select" => TRUE,
  35. );
  36. $form["destination"] = array(
  37. "label" => t("Destination after batch is complete:"),
  38. "type" => "select",
  39. "options" => array("default" => t("None set. By default return to this form page."), "function" => t("Function - go to the function 'batch_test_finished_page'.")),
  40. "hide_please_select" => TRUE,
  41. );
  42. $form["display_percent"] = array(
  43. "type" => "checkbox",
  44. "label" => "Display percentage in progress bar area?",
  45. );
  46. $form["submit_btn"] = array(
  47. "type" => "submit",
  48. "value" => t("Submit and begin batch..."),
  49. );
  50. return $form;
  51. }
  52. function batch_test_form_submit($form, $form_state) {
  53. $items_max = $form_state["values"]["items"];
  54. $destination = $form_state["values"]["destination"];
  55. // Okay, set up the batch....
  56. $batch = array(
  57. "operation" => array("batch_test_perform_batch_operation", array($items_max)),
  58. "title" => t("Test Batch Operation"),
  59. "success_message" => t("The batch operation completed successfully."),
  60. "display_percent" => $form_state["values"]["display_percent"],
  61. "file" => menu_get_module_path("batch") . "/batch.test.inc",
  62. );
  63. // If we should decide to go to a function (and override the default form behavior) then let's record that here.
  64. if ($destination == "function") {
  65. $batch["finished_callback"] = array("batch_test_finished_page", array($items_max));
  66. }
  67. // Set the batch...
  68. batch_set($batch);
  69. // Since this is being called from within a form_submit, there is nothing left for us to do. The batch will be automatically called
  70. // once the submission process is executed.
  71. }
  72. /**
  73. * This is our batch operation function, that gets called each time the batch executes. Notice the first argument
  74. * is passed by reference, and it is our $batch variable. The other arguments are whatever we defined in the batch_set operation.
  75. *
  76. * Some variables in batch to be aware of-- it's up to us to populate:
  77. * $batch["results"]["current"] and $batch["results"]["total"] (current is the record number we are on, total is the current number of records.)
  78. * !!!!! You MUST set $batch["results"]["finished"] = TRUE when the batch has completed.
  79. *
  80. *
  81. * @param unknown_type $batch
  82. * @param unknown_type $items_max
  83. */
  84. function batch_test_perform_batch_operation(&$batch, $items_max) {
  85. // if this is our first time through, let's init our values.
  86. if (!isset($batch["results"]["total"])) {
  87. // Our first time through. Let's start up.
  88. $batch["results"]["total"] = $items_max;
  89. $batch["results"]["current"] = -1; // so that we start with zero below
  90. $batch["results"]["finished"] = FALSE;
  91. }
  92. // Set an arbitrary limit of how many records we are going to examine each time the routine runs.
  93. // This should be whatever we believe we can safely accomplish before the routine times out. Set low for safety.
  94. $limit = rand(5, 15);
  95. $c = $t = 0;
  96. for ($t = $batch["results"]["current"] + 1; $t < $batch["results"]["total"]; $t++) {
  97. // ....
  98. // ....
  99. // Here's where your code would go
  100. // ....
  101. // ....
  102. $c++;
  103. // If we've gone past the limit, then break.
  104. if ($c >= $limit) break;
  105. }
  106. // Update our values.
  107. $batch["results"]["current"] = $t;
  108. // Have we finished?
  109. if ($batch["results"]["current"] >= $batch["results"]["total"]) {
  110. $batch["results"]["finished"] = TRUE;
  111. }
  112. ////////////////////////
  113. ////////////////////////
  114. // !!!!!!
  115. // We are going to artificially slow down this script so we can see
  116. // the batch process operating.
  117. // Obviously, take this out in your own script.
  118. sleep(1); //sleep for 1 second.
  119. }

Functions

Namesort descending Description
batch_test_finished_page This is our test "finished" page which will be displayed upon completion of the batch (if selected in the test form).
batch_test_form Basic sample form, accessed at site.com/batch-test
batch_test_form_submit
batch_test_perform_batch_operation This is our batch operation function, that gets called each time the batch executes. Notice the first argument is passed by reference, and it is our $batch variable. The other arguments are whatever we defined in the batch_set operation.