Migrating nodes from Drupal 6 to 7

(attow) Sadly, there doesn’t seem to be a straight forward method for importing content nodes from Drupal 6 to Drupal 7 without installing a module. Albeit, this isn’t the “best” method but hopefully, this snippet will at least assist in filling the void. There are a few caveats however.

Forewarning:

  • This snippet requires the use of Node 1 but, can be replaced/removed after import.
  • This snippet will reorder your Node IDs (nids) but, will keep your URL aliases intact.
  • This snippet only imports “page” content types but, can easily be modified for others.
  1. First, you’ll need to add your Drupal 6 database to the Druapl 7 $databases array. We accomplish this by modifying then appending the following code to our Drupal 7 settings.php. Naturally, you’ll need to replace the provided information with your connection information.
$databases['drupal6']['default'] = array(
'driver' => 'mysql',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'host' => 'hostname',
'prefix' => '',
);
  • Enable the PHP Filter module. (if not already enabled)
  • Clear Drupal 7 Cache
  • Replace Node 1 (yoursite.com/node/1) or Create your first Basic Page node. Title the node as you wish, select the Text format as “PHP Code”, then paste the following code within the body.
<?php  
function import_nodes() {
db_query('DELETE FROM {node} WHERE `nid` > 1');
db_query('ALTER TABLE {node} AUTO_INCREMENT = 2');
db_query('DELETE FROM {node_revision} WHERE `nid` > 1');
db_query('ALTER TABLE {node_revision} AUTO_INCREMENT = 2');
db_query('DELETE FROM {field_revision_body} WHERE `entity_id` > 1');
db_query('DELETE FROM {field_data_body} WHERE `entity_id` > 1');
db_query('DELETE FROM {node_comment_statistics} WHERE `nid` > 1');
db_query('TRUNCATE TABLE {url_alias}');

db_set_active('drupal6');
$result = db_query('SELECT n.`nid`, n.`title`, n.`created`, u.`dst` as alias, n.`type`,
(SELECT `body` FROM {node_revisions} WHERE `nid` = n.`nid` ORDER BY `vid` DESC LIMIT 1) as body,
(SELECT IF(`format`=3, "php_code", "full_html") FROM {node_revisions} WHERE `nid` = n.`nid` ORDER BY `vid` DESC LIMIT 1) as format
FROM {node} n
LEFT JOIN {url_alias} u ON u.`src` = CONCAT("node/", n.`nid`)
WHERE n.`type` = "page"')->fetchAll();
db_set_active();
foreach($result as $key => $node) {
$n = new stdClass();
$n->title = $node->title;
$n->type = $node->type;
$n->language = 'und';
$n->created = $node->created;
$n->body[$n->language][0]['value'] = $node->body;
$n->body[$n->language][0]['format'] = $node->format;
$n->path = array('alias' => $node->alias);
node_save($n);
unset($n);
}
return 'Import Complete';
}

if (isset($_GET['go'])) echo import_nodes();
else echo '<a href="?go">Import Nodes</a>';
?>
  • Visit yoursite.com/node/1 and click “Import Nodes”. It may take a few minutes depending on how many nodes being imported.

Troubleshooting:

  • If you receive the error “PDOException: SQLSTATE[42000] [1049] Unknown database…” double check your database connection in settings.php then try clearing the Drupal 7 cache.

Originally published at seanwragg.com on January 31, 2011.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.