3
votes

I'm trying to migrate my current html site into drupal. I have over 80,000 pages I have to migrate so I thought instead of sitting infront of a computer for 50 years I would create a module. I was able to create a script that extracts the html from each directory and now I got to a road block where I need to create a node. I'm trying to create a new node using node_save(), but when node_save is executed, I get a PDOException error with everything I try. I'm passing in $node, which is an array which is then casted into an object.

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

This is how we are currently creating the node, but it produces an error:

$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();
4

4 Answers

2
votes

Usually I create a bulkimport.php script in the document root to do this kind of thing.

Here is the code I use for 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
1
votes

I was getting exactly the same error as in your original post - PDOException: in field_sql_storage_field_storage_write() - etc.

I was already using the stdClass code style shown in the comments above.

The problem turned out to be the pound signs and accented chars in the string I was assigning to the Body field; the strings were coming from a Windows text file.

Converting the string to the Drupal target encoding (UTF-8) worked for me:

$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);

Hope this helps someone.

0
votes

are you using some CMS engine, or it is custom-written website? in first case, look here http://drupal.org/documentation/migrate I strongly recommend you to look at the given modules, they should help. Also, as an option, to migrate DB.

0
votes

You don't need a lot of those blank fields. Also, you can cast the node as an object rather than an array converted to an object.

Your code should be able to be accomplished with this much shorter snippet:

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

Also, there's a number of methods that have been developed to mass-import nodes into Drupal - I am a fan of the Feeds module (http://drupal.org/project/feeds). This may require writing a method of exporting your existing content to an intermediary format (CSV or XML), but it works reliably and you can re-import nodes to update content when required.