Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function batch_example_in_bed_menu() {
- $items['in/bed'] = array(
- 'type' => MENU_CALLBACK,
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('batch_example_in_bed_form'),
- 'access callback' => TRUE,
- );
- return $items;
- }
- function batch_example_in_bed_form($form, &$form_state) {
- $form['#submit'] = array('batch_example_in_bed_form_submit');
- $form['warning'] = array(
- '#type' => 'markup',
- '#markup' => t('Clicking the button below will add "in bed" to the end of all your node titles... ALL your node titles.'),
- '#prefix' => '<div class="messages warning">',
- '#suffix' => '</div>',
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => 'In bed-ify ALL nodes',
- );
- return $form;
- }
- /**
- * Defines a batch operation that will migrate Drupal users that do not exist
- * there to IDX.
- */
- function batch_example_in_bed_form_submit() {
- $batch = array(
- 'title' => t('In Bed...'),
- 'operations' => array(
- array('batch_example_in_bed_process', array()),
- ),
- 'finished' => 'batch_example_in_bed_finished',
- 'init_message' => t('Beginning "In Bed" match process.'),
- 'progress_message' => t('Processing…'),
- 'error_message' => t('An error occured while processing "In Bed".'),
- );
- batch_set($batch);
- }
- /**
- * Batch operation function.
- */
- function batch_example_in_bed_process(&$context) {
- if (!isset($context['sandbox']['progress'])) {
- $context['sandbox']['progress'] = 0;
- $context['sandbox']['max'] = db_select('node', 'n')
- ->fields('n', array('nid'))
- ->countQuery()
- ->execute()
- ->fetchField();
- $context['results']['%migrated'] = 0;
- $context['results']['%skipped'] = 0;
- }
- // Only migrate this many nodes at a time. If this number is too high, the
- // PHP request may time out or run out of memory.
- $limit = 5;
- $result = db_query_range('SELECT nid FROM {node}', $context['sandbox']['progress'], $limit);
- while ($nid = $result->fetchField()) {
- $migrated = _batch_example_in_bed_it($nid);
- // $migrated = TRUE;
- if ($migrated) {
- $context['results']['%migrated']++;
- }
- else {
- $context['results']['%skipped']++;
- }
- $context['sandbox']['progress']++;
- $context['message'] = t('Migrated @processed out of @total nodes so far.', array('@processed' => $context['sandbox']['progress'], '@total' => $context['sandbox']['max']));
- }
- // 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'])) {
- $context['finished'] = $context['sandbox']['max'] == 0 ? 1 : $context['sandbox']['progress'] / ($context['sandbox']['max']);
- }
- }
- /**
- * Batch finished function.
- */
- function batch_example_in_bed_finished($success, $results, $operations) {
- if ($success) {
- drupal_set_message(t('Migration complete: %migrated nodes were in-bed-ified and %skipped nodes were skipped for some reason.', $results));
- }
- else {
- drupal_set_message(t('Migration failed.'), 'error');
- }
- }
- function _batch_example_in_bed_it($nid) {
- $node = node_load($nid);
- if (is_object($node)) {
- $node->title = substr($node->title, 0, 12) . t(" in bed");
- node_save($node);
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement