2011-02-06 6 views
2

私は古いデータベースから新しいデータベースにデータを移動しようとしています。私のコードは現在何も書いていませんが(これは問題ありませんが)、$ context ['finished']が1に設定されていても、最後のレコードで何度も繰り返し読みが繰り返されます。私のDrupal 7バッチ処理コードは、バッチ処理の最後の繰り返しを何度も繰り返しています。誰かが私にそれを解決するのを手伝ってもいいですか

<?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>"; 
} 

答えて

1

もちろん、私はこれを理解しようと時間を費やしています。私が投稿した分は、私が間違っていたことを理解しています。

私はレコードを追加するたびに「進捗状況」を増やしていましたが、すべての単一分類語を含まないため、実際にはいくつかのレコードをスキップしていました。 'max'はレコードIDであり、レコードの総数だけではないため、 'max'に加算されます。 :p

関連する問題