开发者

My Drupal 7 batch processing code is repeating the final iteration of the batch process over and over. Can someone help me fix it?

开发者 https://www.devze.com 2023-02-09 11:14 出处:网络
I\'m trying to move data from an old database to a new one.My code doesn\'t currently write anything (which is fine), but the reading repeats over and over on the last record even though $context[\'fi

I'm trying to move data from an old database to a new one. My code doesn't currently write anything (which is fine), but the reading repeats over and over on the last record even though $context['finished'] is set to 1. Any idea what I'm doing wrong?

<?php
function ogamigrate_permission() {
  return array(
    'migrate to oga2' => array(
      'title' => t('OGA 1.x -> OGA 2.0 Data Migration'),
      'description' => t('Migrate data from the old site.'),
    ),
  );
}

function ogamigrate_menu() {
  $items['admin/ogamigrate'] = array(
    'page callback' => '_ogamigrate_batch',
    'access arguments' => array('migrate to oga2'),
  );

  $items['admin/ogamigrate/finished'] = array(
    'page callback' => '_ogamigrate_complete',
    'access arguments' => array('migrate to oga2'),
  );
  return $items;
}

function _ogamigrate_batch() {
  $batch = array(
    'title' => t('Migrating data from OGA 1'),
    'operations' => array(
      array('_ogamigrate_tags', array()),
      #array('my_function_2', array()),
   ),
    'finished' => '_ogamigrate_finished',
  );
  batch_set($batch);
  batch_process('admin/ogamigrate/finished');
}

function _ogamigrate_tags(&$context) {
  db_set_active('old');
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_tid'] = 0;
    $context['sandbox']['max'] = db_query('select max(tid) from {term开发者_运维技巧_data} where vid in (3, 4, 6, 7, 10);')->fetchField();
  }

  error_log("migrating tid {$context['sandbox']['current_tid']} ({$context['finished']}");

  $limit = 5;

  $result = db_select('term_data')
  ->fields('term_data', array('tid', 'name', 'description'))
  ->condition('tid', $context['sandbox']['current_tid'], '>')
  ->condition('vid', array(3, 4, 6, 7, 10), 'in')
  ->orderBy('tid')
  ->range(0, $limit)
  ->execute();

  db_set_active('default');

  foreach ($result as $row) {
    #$node = node_load($row->nid, NULL, TRUE);
   error_log("Processing tid {$row->tid} / {$context['sandbox']['max']} ({$row->name})");
    $context['results'][] = $row->tid . ' : ' . $row->name;
    $context['sandbox']['progress']++;
    $context['sandbox']['current_tid'] = $row->tid;
    $context['message'] = $row->name;
  }

  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}

function _ogamigrate_finished($success, $results, $operations) {
  error_log('finished');
  if ($success) {
    $message = format_plural(count($results), 'One item processed.', '@count item processed.');
  }
  else {
    $message = t('Finished with an error.');
  }
  drupal_set_message($message);
  /*
  // Providing data for the redirected page is done through $_SESSION.
  foreach ($results as $result) {
    $items[] = t('Loaded node %title.', array('%title' => $result));
  }
  $_SESSION['my_batch_results'] = $items;
  */
}

function _ogamigrate_complete() {
  return "<p>Migration complete.</p>";
}


Of course, I spend hours trying to figure this out and the minute I post it I figure out what I was doing wrong.

Here's the deal: I was incrementing 'progress' every time I added a record, but since I wasn't including every single taxonomy vocabulary, it was actually skipping over some records, which means that 'progress' was never quite adding up to 'max', since 'max' was a record ID and not just a total number of records. :p

0

精彩评论

暂无评论...
验证码 换一张
取 消