Advertisement
Guest User

drupal batch_example_in_bed

a guest
Feb 6th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.39 KB | None | 0 0
  1. <?php
  2.  
  3. function batch_example_in_bed_menu() {
  4.   $items['in/bed'] = array(
  5.     'type' => MENU_CALLBACK,
  6.     'page callback' => 'drupal_get_form',
  7.     'page arguments' => array('batch_example_in_bed_form'),
  8.     'access callback' => TRUE,
  9.   );
  10.  
  11.   return $items;
  12. }
  13.  
  14. function batch_example_in_bed_form($form, &$form_state) {
  15.   $form['#submit'] = array('batch_example_in_bed_form_submit');
  16.   $form['warning'] = array(
  17.     '#type' => 'markup',
  18.     '#markup' => t('Clicking the button below will add "in bed" to the end of all your node titles... ALL your node titles.'),
  19.     '#prefix' => '<div class="messages warning">',
  20.     '#suffix' => '</div>',
  21.   );
  22.   $form['submit'] = array(
  23.     '#type' => 'submit',
  24.     '#value' => 'In bed-ify ALL nodes',
  25.   );
  26.  
  27.   return $form;
  28. }
  29.  
  30. /**
  31.  * Defines a batch operation that will migrate Drupal users that do not exist
  32.  * there to IDX.
  33.  */
  34. function batch_example_in_bed_form_submit() {
  35.   $batch = array(
  36.     'title' => t('In Bed...'),
  37.     'operations' => array(
  38.       array('batch_example_in_bed_process', array()),
  39.     ),
  40.     'finished' => 'batch_example_in_bed_finished',
  41.     'init_message' => t('Beginning "In Bed" match process.'),
  42.     'progress_message' => t('Processing&hellip;'),
  43.     'error_message' => t('An error occured while processing "In Bed".'),
  44.   );
  45.  
  46.   batch_set($batch);
  47. }
  48.  
  49. /**
  50.  * Batch operation function.
  51.  */
  52. function batch_example_in_bed_process(&$context) {
  53.   if (!isset($context['sandbox']['progress'])) {
  54.     $context['sandbox']['progress'] = 0;
  55.     $context['sandbox']['max'] = db_select('node', 'n')
  56.       ->fields('n', array('nid'))
  57.       ->countQuery()
  58.       ->execute()
  59.       ->fetchField();
  60.  
  61.     $context['results']['%migrated'] = 0;
  62.     $context['results']['%skipped'] = 0;
  63.   }
  64.  
  65.   // Only migrate this many nodes at a time. If this number is too high, the
  66.   // PHP request may time out or run out of memory.
  67.   $limit = 5;
  68.   $result = db_query_range('SELECT nid FROM {node}', $context['sandbox']['progress'], $limit);
  69.  
  70.   while ($nid = $result->fetchField()) {
  71.     $migrated = _batch_example_in_bed_it($nid);
  72.     // $migrated = TRUE;
  73.  
  74.     if ($migrated) {
  75.       $context['results']['%migrated']++;
  76.     }
  77.     else {
  78.       $context['results']['%skipped']++;
  79.     }
  80.  
  81.     $context['sandbox']['progress']++;
  82.     $context['message'] = t('Migrated @processed out of @total nodes so far.', array('@processed' => $context['sandbox']['progress'], '@total' => $context['sandbox']['max']));
  83.   }
  84.  
  85.   // Inform the batch engine that we are not finished, and provide an estimation
  86.   // of the completion level we reached.
  87.   if ($context['sandbox']['progress'] <= ($context['sandbox']['max'])) {
  88.     $context['finished'] = $context['sandbox']['max'] == 0 ? 1 : $context['sandbox']['progress'] / ($context['sandbox']['max']);
  89.   }
  90.  
  91. }
  92.  
  93. /**
  94.  * Batch finished function.
  95.  */
  96. function batch_example_in_bed_finished($success, $results, $operations) {
  97.   if ($success) {
  98.     drupal_set_message(t('Migration complete: %migrated nodes were in-bed-ified and %skipped nodes were skipped for some reason.', $results));
  99.   }
  100.   else {
  101.     drupal_set_message(t('Migration failed.'), 'error');
  102.   }
  103. }
  104.  
  105. function _batch_example_in_bed_it($nid) {
  106.   $node = node_load($nid);
  107.  
  108.   if (is_object($node)) {
  109.     $node->title = substr($node->title, 0, 12) . t(" in bed");
  110.     node_save($node);
  111.  
  112.     return TRUE;
  113.   }
  114.   else {
  115.     return FALSE;
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement