/[drupal]/contributions/docs/developer/examples/batch_example.install
ViewVC logotype

Contents of /contributions/docs/developer/examples/batch_example.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Sun Oct 12 08:52:36 2008 UTC (13 months, 2 weeks ago) by davereid
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +2 -3 lines
File MIME type: text/x-php
- Fix coding standards
1 <?php
2 // $Id: batch_example.install,v 1.3 2007/10/05 16:11:36 drewish Exp $
3
4 /*
5 * Example of multipart update function.
6 * The only difference with pre-Drupal 6 multipart updates is the existence
7 * of the $sandbox param, which provides a cleaner and safer way to store progression
8 * than the previous use of $_SESSION-sored custom values.
9 *
10 * This dummy 'udpate' function does nothing harmful, simply
11 * loads each node...
12 */
13 function batch_test_update_1(&$sandbox) {
14 $ret = array();
15
16 // Use the sandbox at your convenience to store the information needed
17 // to track progression between successive calls to the function.
18 if (!isset($sandbox['progress'])) {
19 $sandbox['progress'] = 0;
20 $sandbox['curr_node'] = 0;
21 $sandbox['max'] = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node}'));
22 }
23
24 // Process nodes by groups of 5 (arbitrary value).
25 // When a group of five is processed, the batch update engine determines
26 // whether it should continue processing in the same request or provide
27 // progress feedback to the user and wait for the next request.
28 $limit = 5;
29
30 // Retrieve the next group of nids.
31 $result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $sandbox['curr_node'], 0, $limit);
32 while ($row = db_fetch_array($result)) {
33 // Here we actually perform our dummy 'update' on the current node.
34 $ret[] = update_sql('SELECT * FROM {node} WHERE nid = ' . $row['nid']);
35
36 // Update our progress information.
37 $sandbox['progress']++;
38 $sandbox['curr_node'] = $row['nid'];
39 }
40
41 // Inform the batch update engine that we are not finished,
42 // and provide an estimation of the completion level we reached.
43 if ($sandbox['progress'] != $sandbox['max']) {
44 $ret['#finished'] = $sandbox['progress'] / $sandbox['max'];
45 }
46 return $ret;
47 }

  ViewVC Help
Powered by ViewVC 1.1.2