2011-07-28 45 views
3

我試圖將我當前的html網站遷移到drupal。我有超過80,000個頁面需要遷移,所以我認爲不用坐在電腦前50年,我會創建一個模塊。我能夠創建一個腳本,從每個目錄中提取html,現在我到了需要創建節點的路障。我正在嘗試使用node_save()來創建一個新節點,但是當執行node_save時,我嘗試的所有事情都會得到一個PDOException錯誤。我傳入$ node,這是一個數組,然後被轉換成一個對象。如何使用node_save創建節點?

PDOException: in field_sql_storage_field_storage_write() (line 424 of /srv/www/htdocs/modules/field/modules/field_sql_storage/field_sql_storage.module).

這是我們目前如何創建節點,但它會產生一個錯誤:

$node= array(
    'uid' => $user->uid, 
    'name' => $user->name, 
    'type' => 'page', 
    'language' => LANGUAGE_NONE, 
    'title' => $html['title'], 
    'status' => 1, 
    'promote' => 0, 
    'sticky' => 0, 
    'created' => (int)REQUEST_TIME, 
    'revision' => 0, 
    'comment' => '1', 
    'menu' => array(
     'enabled' => 0, 
     'mlid' => 0, 
     'module' => 'menu', 
     'hidden' => 0, 
     'has_children' => 0, 
     'customized' => 0, 
     'options' => array(), 
     'expanded' => 0, 
     'parent_depth_limit' => 8, 
     'link_title' => '', 
     'description' => '', 
     'parent' => 'main-menu:0', 
     'weight' => '0', 
     'plid' => '0', 
     'menu_name' => 'main-menu', 
    ), 
    'path' => array(
     'alias' => '', 
     'pid' => null, 
     'source' => null, 
     'language' => LANGUAGE_NONE, 
     'pathauto' => 1, 
    ), 
    'nid' => null, 
    'vid' => null, 
    'changed' => '', 
    'additional_settings__active_tab' => 'edit-menu', 
    'log' => '', 
    'date' => '', 
    'submit' => 'Save', 
    'preview' => 'Preview', 
    'private' => 0, 
    'op' => 'Save', 
    'body' => array(LANGUAGE_NONE => array(
     array(
      'value' => $html['html'], 
      'summary' => $link, 
      'format' => 'full_html', 
     ), 
    )), 
     'validated' => true, 
); 

node_save((object)$node); 

// Small hack to link revisions to our test user. 
db_update('node_revision') 
    ->fields(array('uid' => $node->uid)) 
    ->condition('vid', $node->vid) 
    ->execute(); 

回答

2

通常我在文檔根目錄下創建一個bulkimport.php腳本來完成這種事情。

這裏是我使用爲Drupal 7代碼:

<?php 

define('DRUPAL_ROOT', getcwd()); 

include_once './includes/bootstrap.inc'; 
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 



function _create_node($title="", $body="", $language="und") { 

    $node = (object)array(); 

    $node->uid  = '1'; 
    $node->name  = 'admin'; 

    $node->type  = 'page'; 

    $node->status = 1; 
    $node->promote = 0; 
    $node->sticky = 0; 
    $node->revision = 1; 
    $node->language = $language; 

    $node->title = $title; 
    $node->body[$language][0] = array(
    'value' => $body, 
    'format' => 'full_html', 
    ); 

    $node->teaser = ''; 
    $node->log  = 'Auto Imported Node'; 

    node_submit($node); 
    node_save($node); 

    return $node; 

    } ### function _create_node 

$sith = array(
    'Darth Vader' => 'Master: Darth Sidious', 
    'Darth Sidious' => 'Master: Darth Plagous', 
    'Darth Maul'  => 'Master: Darth Sidious', 
    'Darth Tyranous' => 'Master: Darth Sidious', 
); 

foreach($sith as $title=>$body) { 
    print "Creating Node. Title:[".$title."] \tBody:[".$body."] "; 
    $node = _create_node($title, $body); 
    print "\t... Created Node ID: [".$node->nid."]\n"; 
    #print_r($node); 
    } ### foreach 
0

您使用的一些CMS引擎,或者是定製編寫的網站? 在第一種情況下,看看這裏http://drupal.org/documentation/migrate 我強烈建議你看看給定的模塊,他們應該幫助。另外,作爲選項,可以遷移數據庫。

0

你不需要很多這些空白字段。另外,您可以將節點轉換爲對象而不是轉換爲對象的數組。

你的代碼應該能夠有這麼多的短片斷實現:

$node = new stdClass(); 
    $node->title = $html['title']; 
    $node->type = 'page'; 
    $node->body['und'][0]['value'] = $html['html']; 
    node_save($node); 

此外,還有已發展到大衆進口節點到Drupal的一些方法 - 我的粉絲Feeds模塊(http://drupal.org/project/feeds)。這可能需要編寫一種將現有內容導出爲中間格式(CSV或XML)的方法,但它可以可靠地工作,並且可以在需要時重新導入節點以更新內容。

1

我得到完全相同的錯誤在你原來的職位 - PDOException:在field_sql_storage_field_storage_write() - 等

我已經在使用上面註釋中顯示的stdClass代碼樣式。

問題原來是我分配給Body字段的字符串中的磅符號和重音字符;字符串來自Windows文本文件。

字符串轉換爲Drupal的目標編碼(UTF-8)工作對我來說:

$cleaned_string = mb_convert_encoding($html['html'], "UTF-8", "Windows-1252"); 
$node->body[LANGUAGE_NONE][0]['value'] = $cleaned_string; 
$node->body[LANGUAGE_NONE][0]['format'] = 'plain_text'; 
node_save($node); 

希望這可以幫助別人。

+0

爲我解決了這個問題! –