batch_example.install 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 $sandbox['#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. // The count of nodes visited so far.
  31. $sandbox['progress'] = 0;
  32. // Total nodes that must be visited.
  33. $sandbox['max'] = db_query('SELECT COUNT(nid) FROM {node}')->fetchField();
  34. // A place to store messages during the run.
  35. $sandbox['messages'] = array();
  36. // Last node read via the query.
  37. $sandbox['current_node'] = -1;
  38. }
  39. // Process nodes by groups of 10 (arbitrary value).
  40. // When a group is processed, the batch update engine determines
  41. // whether it should continue processing in the same request or provide
  42. // progress feedback to the user and wait for the next request.
  43. $limit = 10;
  44. // Retrieve the next group of nids.
  45. $result = db_select('node', 'n')
  46. ->fields('n', array('nid'))
  47. ->orderBy('n.nid', 'ASC')
  48. ->where('n.nid > :nid', array(':nid' => $sandbox['current_node']))
  49. ->extend('PagerDefault')
  50. ->limit($limit)
  51. ->execute();
  52. foreach ($result as $row) {
  53. // Here we actually perform a dummy 'update' on the current node.
  54. $node = db_query('SELECT nid FROM {node} WHERE nid = :nid', array(':nid' => $row->nid))->fetchField();
  55. // Update our progress information.
  56. $sandbox['progress']++;
  57. $sandbox['current_node'] = $row->nid;
  58. }
  59. // Set the "finished" status, to tell batch engine whether this function
  60. // needs to run again. If you set a float, this will indicate the progress
  61. // of the batch so the progress bar will update.
  62. $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
  63. // Set up a per-run message; Make a copy of $sandbox so we can change it.
  64. // This is simply a debugging stanza to illustrate how to capture status
  65. // from each pass through hook_update_N().
  66. $sandbox_status = $sandbox;
  67. // Don't want them in the output.
  68. unset($sandbox_status['messages']);
  69. $sandbox['messages'][] = t('$sandbox=') . print_r($sandbox_status, TRUE);
  70. if ($sandbox['#finished']) {
  71. // hook_update_N() may optionally return a string which will be displayed
  72. // to the user.
  73. $final_message = '<ul><li>' . implode('</li><li>', $sandbox['messages']) . "</li></ul>";
  74. return t('The batch_example demonstration update did what it was supposed to do: @message', array('@message' => $final_message));
  75. }
  76. }