2012-06-29 55 views
2

我有一個粘貼窗口CSV,它有近1個lac行,我想將它保存爲節點,我嘗試了批量API。通過批處理api將窗口csv粘貼到drupal節點

但仍然我得到的PHP超時錯誤..please幫助

function MODULE_aw_batch(){ 
    $operations = array(); 
    $csv = file_directory_path().'/aw/datafeed_134642.csv'; 
    $file = fopen($csv, 'r'); 
    while (($data = fgetcsv($file)) !== FALSE) { 
     $operations[] = array('MODULE_aw_op', array($data)); 
    } 
    $batch = array(
    'title' => t('Generating feeds'), // Title to display while running. 
    'operations' => $operations, 
    'finished' => 'MODULE_aw_finished', // Last function to call. 
    'init_message' => t('Importing...it may take 4-5 hours'), 
    'progress_message' => t('Processed @current out of @total.'), 
    'error_message' => t('Import feeds has encountered an error.'), 
); 
    batch_set($batch); 
    batch_process('admin/content/node/overview'); 
} 

更新(解決)

,而不是一次讀取整個CSV文件,分割CSV閱讀每個進程的5條線路

+1

歡迎堆棧溢出。祝賀您找到自己問題的答案。請將它作爲答案發布(可以回答你自己的問題),這讓其他人清楚這個問題有答案。如果沒有其他答案更有幫助,您也可以將自己的答案標記爲已接受。 – pckabeer

回答

2

找到了解決辦法

批申報

function MODULENAME_batch(){ 
    $operations[] = array('MODULENAME_op', array()); 
    $batch = array(
     'title' => t('Importing events'), 
     'operations' => $operations, 
     'finished' => 'MODULENAME_finished', 
     'init_message' => t('MESSAGE HERE'), 
     'progress_message' => 'MESSAGE HERE', 
     'error_message' => t('MESSAGE HERE'), 
    ); 
    batch_set($batch); 
    batch_process('admin/content/node/overview');// redirect to this page 
} 

function MODULENAME_aw_op(&$context) {  
    if (!isset($context['sandbox']['offset'])) { 
     $context['sandbox']['offset'] = 0; 
     $context['sandbox']['records'] = 0; 
    } 

    $filename =file_directory_path().'/aw/datafeed_134642.csv'; // csv file 

    $fp = fopen($filename, 'r'); 

    if ($fp === FALSE) { 
    // Failed to open file 
     watchdog('MODULENAME', 'Failed to open ' . $filename); 
     $context['finished'] = TRUE; 
     return; 
    } 

    $ret = fseek($fp, $context['sandbox']['offset']); 

    if ($ret != 0) { 
    // Failed to seek 
     watchdog('MODULENAME', 'Failed to seek to ' . $context['sandbox']['offset']); 
     $context['finished'] = TRUE; 
     return; 
    } 

    $limit = 5; // Maximum number of rows to process at a time 
    $done = FALSE; 

    for ($i = 0; $i < $limit; $i++) { 
     $line = fgetcsv($fp); 
     if ($line === FALSE) { 
      $done = TRUE; 
      // No more records to process 
      break; 
     } 
     else {   
      $context['sandbox']['records']++; 
      $context['sandbox']['offset'] = ftell($fp); 
      $record = $context['sandbox']['records']; 
      // Do something with the data 

     } 
    } 

    $eof = feof($fp); 

    if ($eof) { 
     $context['success'] = TRUE; 
    } 
    $context['message'] = "processed " . $context['sandbox']['records'] . " records"; 

    $context['finished'] = ($eof || $done) ? 1 : 0; 
} 
1

您是否嘗試過Drupal提供模塊? CSV導入非常好。 http://drupal.org/project/feeds/

+0

我想處理一些記錄,用飼料這是不可能的,無論如何找到了解決方案:) – Serjas

+0

創建一個答案並分享您的解決方案 – frazras

+0

創建了一個答案 – Serjas