Drupal Batch API for hook_update_N()


/ Published in: PHP
Save to your folder(s)

This is the Batch API example shown on http://api.drupal.org/api/drupal/developer--hooks--install.php/function/hook_update_N/6.


Copy this code and paste it in your HTML
  1. <?php
  2. // However, for more complex operations that may take a long time,
  3. // you may hook into Batch API as in the following example.
  4. $ret = array();
  5.  
  6. // Update 3 users at a time to have an exclamation point after their names.
  7. // (They're really happy that we can do batch API in this hook!)
  8. if (!isset($sandbox['progress'])) {
  9. $sandbox['progress'] = 0;
  10. $sandbox['current_uid'] = 0;
  11. // We'll -1 to disregard the uid 0...
  12. $sandbox['max'] = db_result(db_query('SELECT COUNT(DISTINCT uid) FROM {users}')) - 1;
  13. }
  14.  
  15. $users = db_query_range("SELECT uid, name FROM {users} WHERE uid > %d ORDER BY uid ASC", $sandbox['current_uid'], 0, 3);
  16. while ($user = db_fetch_object($users)) {
  17. $user->name .= '!';
  18. $ret[] = update_sql("UPDATE {users} SET name = '$user->name' WHERE uid = $user->uid");
  19.  
  20. $sandbox['progress']++;
  21. $sandbox['current_uid'] = $user->uid;
  22. }
  23.  
  24. $ret['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  25.  
  26. return $ret;
  27. ?>

URL: http://api.drupal.org/api/drupal/developer--hooks--install.php/function/hook_update_N/6

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.