batch_example.install

  1. examples
    1. 6 batch_example/batch_example.install
    2. 7 batch_example/batch_example.install
    3. 8 batch_example/batch_example.install

Install, update, and uninstall functions for the batch_example module.

Functions & methods

NameDescription
batch_example_update_7100Example of batch-driven update function.

File

batch_example/batch_example.install
View source
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the batch_example module.
  5. */
  6. /**
  7. * Example of batch-driven update function.
  8. *
  9. * Because some update functions may require the batch API, the $sandbox
  10. * provides a place to store state. When $standbox['#finished'] == TRUE,
  11. * calls to this update function are completed.
  12. *
  13. * The $sandbox param provides a way to store data during multiple invocations.
  14. * When the $sandbox['#finished'] == 1, execution is complete.
  15. *
  16. * This dummy 'update' function changes no state in the system. It simply
  17. * loads each node.
  18. *
  19. * To make this update function run again and again, execute the query
  20. * "update system set schema_version = 0 where name = 'batch_example';"
  21. * and then run /update.php.
  22. *
  23. * @ingroup batch_example
  24. */
  25. function batch_example_update_7100(&$sandbox) {
  26. $ret = array();
  27. // Use the sandbox at your convenience to store the information needed
  28. // to track progression between successive calls to the function.
  29. if (!isset($sandbox['progress'])) {
  30. $sandbox['progress'] = 0; // The count of nodes visited so far.
  31. // Total nodes that must be visited.
  32. $sandbox['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
  33. // A place to store messages during the run.
  34. $sandbox['messages'] = array();
  35. $sandbox['current_node'] = -1; // Last node read via the query.
  36. }
  37. // Process nodes by groups of 10 (arbitrary value).
  38. // When a group is processed, the batch update engine determines
  39. // whether it should continue processing in the same request or provide
  40. // progress feedback to the user and wait for the next request.
  41. $limit = 10;
  42. // Retrieve the next group of nids.
  43. $result = db_select('node', 'n')
  44. ->fields('n', array('nid'))
  45. ->orderBy('n.nid', 'ASC')
  46. ->where('n.nid > :nid', array(':nid' => $sandbox['current_node']))
  47. ->extend('PagerDefault')
  48. ->limit($limit)
  49. ->execute();
  50. foreach ($result as $row) {
  51. // Here we actually perform a dummy 'update' on the current node.
  52. $node = db_query('SELECT nid FROM {node} WHERE nid = :nid', array(':nid' => $row->nid))->fetchField();
  53. // Update our progress information.
  54. $sandbox['progress']++;
  55. $sandbox['current_node'] = $row->nid;
  56. }
  57. // Set the "finished" status, to tell batch engine whether this function
  58. // needs to run again. If you set a float, this will indicate the progress
  59. // of the batch so the progress bar will update.
  60. $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
  61. // Set up a per-run message; Make a copy of $sandbox so we can change it.
  62. // This is simply a debugging stanza to illustrate how to capture status
  63. // from each pass through hook_update_N().
  64. $sandbox_status = $sandbox;
  65. unset($sandbox_status['messages']); // Don't want them in the output.
  66. $sandbox['messages'][] = t('$sandbox=') . print_r($sandbox_status, TRUE);
  67. if ($sandbox['#finished']) {
  68. // hook_update_N() may optionally return a string which will be displayed
  69. // to the user.
  70. $final_message = '<ul><li>' . implode('</li><li>', $sandbox['messages']) . "</li></ul>";
  71. return t('The batch_example demonstration update did what it was supposed to do: @message', array('@message' => $final_message));
  72. }
  73. }
Login or register to post comments