- <?php
-  * @file
-  * Hook implementations for the Batch Example module.
-  */
- 
-  * @defgroup batch_example Example: Batch API
-  * @ingroup examples
-  * @{
-  * This example demonstrates how a module can use the Batch API.
-  *
-  * Batches allow heavy processing to be spread out over several page requests,
-  * ensuring that the processing does not get interrupted because of a PHP
-  * timeout, while allowing the user to receive feedback on the progress of the
-  * ongoing operations. It also can prevent out of memory situations.
-  *
-  * The @link batch_example.install .install file @endlink also shows how the
-  * Batch API can be used to handle long-running hook_update_N() functions.
-  *
-  * Two harmless batches are defined:
-  * - batch 1: Load the node with the lowest nid 100 times.
-  * - batch 2: Load all nodes, 20 times and uses a progressive op, loading nodes
-  *   by groups of 5.
-  * @see batch
-  */
- 
-  * Implements hook_menu().
-  */
- function batch_example_menu() {
-   $items = array();
-   $items['examples/batch_example'] = array(
-     'title' => 'Batch example',
-     'description' => 'Example of Backdrop batch processing',
-     'page callback' => 'backdrop_get_form',
-     'page arguments' => array('batch_example_simple_form'),
-     'access callback' => TRUE,
-   );
- 
-   return $items;
- }
- 
-  * Form builder function to allow choice of which batch to run.
-  */
- function batch_example_simple_form() {
-   $form['description'] = array(
-     '#type' => 'markup',
-     '#markup' => t('This example offers two different batches. The first does 1000 identical operations, each completed in one run; the second does 20 operations, but each takes more than one run to operate if there are more than 5 nodes.'),
-   );
-   $form['batch'] = array(
-     '#type' => 'select',
-     '#title' => 'Choose batch',
-     '#options' => array(
-       'batch_1' => t('batch 1 - 1000 operations, each loading the same node'),
-       'batch_2' => t('batch 2 - 20 operations. each one loads all nodes 5 at a time'),
-     ),
-   );
-   $form['submit'] = array(
-     '#type' => 'submit',
-     '#value' => 'Go',
-   );
- 
-   
-   
-   $nid = batch_example_lowest_nid();
-   if (empty($nid)) {
-     backdrop_set_message(t("You don't currently have any nodes, and this example requires a node to work with. As a result, this form is disabled."));
-     $form['submit']['#disabled'] = TRUE;
-   }
-   return $form;
- }
- 
-  * Submit handler.
-  *
-  * @param array $form
-  *   Form API form.
-  * @param array $form_state
-  *   Form API form.
-  */
- function batch_example_simple_form_submit($form, &$form_state) {
-   $function = 'batch_example_' . $form_state['values']['batch'];
- 
-   
-   $_SESSION['http_request_count'] = 0;
- 
-   
-   $batch = $function();
-   batch_set($batch);
- }
- 
- 
-  * Batch 1 definition: Load the node with the lowest nid 1000 times.
-  *
-  * This creates an operations array defining what batch 1 should do, including
-  * what it should do when it's finished. In this case, each operation is the
-  * same and by chance even has the same $nid to operate on, but we could have
-  * a mix of different types of operations in the operations array.
-  */
- function batch_example_batch_1() {
-   $nid = batch_example_lowest_nid();
-   $num_operations = 1000;
-   backdrop_set_message(t('Creating an array of @num operations', array('@num' => $num_operations)));
- 
-   $operations = array();
-   
-   
-   
-   
-   
-   
-   for ($i = 0; $i < $num_operations; $i++) {
-     
-     
-     
-     $operations[] = array(
-       'batch_example_op_1',
-       array(
-         $nid,
-         t('(Operation @operation)', array('@operation' => $i)),
-       ),
-     );
-   }
-   $batch = array(
-     'operations' => $operations,
-     'finished' => 'batch_example_finished',
-   );
-   return $batch;
- }
- 
-  * Batch operation for batch 1: load a node.
-  *
-  * This is the function that is called on each operation in batch 1.
-  */
- function batch_example_op_1($nid, $operation_details, &$context) {
-   $node = node_load($nid, NULL, TRUE);
- 
-   
-   
-   
-   $context['results'][] = $node->nid . ' : ' . check_plain($node->title);
- 
-   
-   
-   
-   $context['message'] = t('Loading node "@title"', array('@title' => $node->title));
-   $context['message'] .= ' ' . $operation_details;
- 
-   _batch_example_update_http_requests();
- }
- 
-  * Batch 2 : Prepare a batch definition that will load all nodes 20 times.
-  */
- function batch_example_batch_2() {
-   $num_operations = 20;
- 
-   
-   $node_count = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
-   backdrop_set_message(
-     t('There are @node_count nodes so each of the @num operations will require @count HTTP requests.',
-       array(
-         '@node_count' => $node_count,
-         '@num' => $num_operations,
-         '@count' => ceil($node_count / 5),
-       )
-     )
-   );
- 
-   $operations = array();
-   
-   for ($i = 0; $i < $num_operations; $i++) {
-     $operations[] = array(
-       'batch_example_op_2',
-       array(t('(Operation @operation)', array('@operation' => $i))),
-     );
-   }
-   $batch = array(
-     'operations' => $operations,
-     'finished' => 'batch_example_finished',
-     
-     
-     
-     
-     
-     
-     
-     
-     
-     'title' => t('Processing batch 2'),
-     'init_message' => t('Batch 2 is starting.'),
-     'progress_message' => t('Processed @current out of @total.'),
-     'error_message' => t('Batch 2 has encountered an error.'),
-   );
-   return $batch;
- }
- 
-  * Batch operation for batch 2 : load all nodes, 5 by five.
-  *
-  * After each group of 5 control is returned to the batch API for later
-  * continuation.
-  */
- function batch_example_op_2($operation_details, &$context) {
-   
-   
-   if (empty($context['sandbox'])) {
-     $context['sandbox'] = array();
-     $context['sandbox']['progress'] = 0;
-     $context['sandbox']['current_node'] = 0;
- 
-     
-     $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
-   }
- 
-   
-   
-   
-   
-   
-   
-   $limit = 5;
- 
-   
-   $result = db_select('node', 'n')
-     ->fields('n', array('nid'))
-     ->orderBy('n.nid', 'ASC')
-     ->where('n.nid > :nid', array(':nid' => $context['sandbox']['current_node']))
-     ->extend('PagerDefault')
-     ->limit($limit)
-     ->execute();
-   foreach ($result as $row) {
-     
-     $node = node_load($row->nid, NULL, TRUE);
- 
-     
-     
-     
-     $context['results'][] = $node->nid . ' : ' . check_plain($node->title) . ' ' . $operation_details;
- 
-     
-     $context['sandbox']['progress']++;
-     $context['sandbox']['current_node'] = $node->nid;
-     $context['message'] = check_plain($node->title);
-   }
- 
-   
-   
-   if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
-     $context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
-   }
-   _batch_example_update_http_requests();
- }
- 
-  * Batch 'finished' callback used by both batch 1 and batch 2.
-  */
- function batch_example_finished($success, $results, $operations) {
-   if ($success) {
-     
-     
-     backdrop_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _batch_example_get_http_requests())));
-     backdrop_set_message(t('The final result was "%final"', array('%final' => end($results))));
-   }
-   else {
-     
-     
-     $error_operation = reset($operations);
-     backdrop_set_message(
-       t('An error occurred while processing @operation with arguments : @args',
-         array(
-           '@operation' => $error_operation[0],
-           '@args' => print_r($error_operation[0], TRUE),
-         )
-       )
-     );
-   }
- }
- 
-  * Utility function - simply queries and loads the lowest nid.
-  *
-  * @return int|NULL
-  *   A nid or NULL if there are no nodes.
-  */
- function batch_example_lowest_nid() {
-   $select = db_select('node', 'n')
-   ->fields('n', array('nid'))
-   ->orderBy('n.nid', 'ASC')
-   ->extend('PagerDefault')
-   ->limit(1);
-   $nid = $select->execute()->fetchField();
-   return $nid;
- }
- 
-  * Utility function to increment HTTP requests in a session variable.
-  */
- function _batch_example_update_http_requests() {
-   $_SESSION['http_request_count']++;
- }
- 
-  * Utility function to count the HTTP requests in a session variable.
-  *
-  * @return int
-  *   Number of requests.
-  */
- function _batch_example_get_http_requests() {
-   return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
- }
-  * @} End of "defgroup batch_example".
-  */