Skip to content

Instantly share code, notes, and snippets.

@sean3z
Created April 21, 2018 15:28
Show Gist options
  • Save sean3z/1b4f037032183af52c3887a20292ca1b to your computer and use it in GitHub Desktop.
Save sean3z/1b4f037032183af52c3887a20292ca1b to your computer and use it in GitHub Desktop.
Drupal 6 migration example
<?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>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment