古いデータベースから新しいデータベースにデータを移動しようとしています。私のコードは現在何も書き込んでいません (これで問題ありません) が、$context['finished'] が 1 に設定されていても、読み取りは最後のレコードで何度も繰り返されます。
<?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>";
}