2011-02-06 4 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

물론 나는 이것을 알아 내려고 노력하며 시간은 내가 게시 한 순간에 내가 잘못하고있는 것을 알아 낸다.

레코드를 추가 할 때마다 '진행률'을 증가 시키지만 모든 단일 분류 어휘를 포함하지 않았기 때문에 실제로는 일부 레코드를 건너 뛰었습니다. 즉 '진행률'이 절대로 '최대'는 최대 레코드 수만이 아니라 레코드 ID 였으므로 '최대'까지 추가됩니다. : p