function batch_example_op_2 in Examples for Developers 6
Same name and namespace in other branches
- 8 batch_example/batch_example.module \batch_example_op_2()
- 7 batch_example/batch_example.module \batch_example_op_2()
- 3.x modules/batch_example/batch_example.module \batch_example_op_2()
Batch operation for batch 2 : load all nodes, 5 by five This is a multipart operation, using the
Related topics
1 string reference to 'batch_example_op_2'
- batch_example_batch_2 in batch_example/
batch_example.module - Batch 2 : load all nodes 5 by 5, 20 times (Multipart operation)
File
- batch_example/
batch_example.module, line 235 - This is an example outlining how a module can define batches.
Code
function batch_example_op_2(&$context) {
// Use the $context['sandbox'] at your convenience to store the
// information needed to track progression between successive calls.
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_node'] = 0;
$context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node}'));
}
// Process nodes by groups of 5 (arbitrary value).
// When a group of five is processed, the batch update engine determines
// whether it should continue processing in the same request or provide
// progress feedback to the user and wait for the next request.
$limit = 5;
// Retrieve the next group of nids.
$result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
while ($row = db_fetch_array($result)) {
// Here we actually perform our dummy 'processing' on the current node.
$node = node_load($row['nid'], NULL, TRUE);
// Store some result for post-processing in the finished callback.
$context['results'][] = $node->nid . ' : ' . check_plain($node->title);
// Update our progress information.
$context['sandbox']['progress']++;
$context['sandbox']['current_node'] = $node->nid;
$context['message'] = check_plain($node->title);
}
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] >= $context['sandbox']['max']) {
// We should always check if the current progress is equal or greater to
// the total number of items to process. For example, if a node is added
// while this batch process is running, the progress value will end up being
// one greater than the max value. This will cause an infinite loop. We
// prevent this from happening by always checking if progress is greater
// than or equal to max.
$context['finished'] = 1;
}
else {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}