Migration script from Drupal 5 to Drupal 7 programmatically

This post show you how you can migrate data from a Drupal 5 database to a Drupal 7. It might be some easier ways using some modules for export and import, but this is how you can make it the programmatically way. This is NOT a ready to go script this is more or less a copy of a script I just made, you have to code a bit to make it work.

  • Export the tables node, node_revisions, term_node, url_alias (if exists)
  • Import them to the new database and change table name like adding a prefix "old_"
  • Add the vocabulary and terms manually in UI (sure you can let the script do that, but I had only 4 so I added them manually)
  • Make a conversion rule for tids
  • Add a LIMIT to the script and filter away node types you don't want and make a test print before going full scale.

<?php
define
('DRUPAL_ROOT', getcwd());

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


$result = db_query("SELECT n.*, nr.*, oua.dst, GROUP_CONCAT(DISTINCT tid SEPARATOR ',') AS tids FROM old_node AS n LEFT JOIN old_node_revisions AS nr ON(n.nid=nr.nid) LEFT JOIN old_term_node AS otn ON (n.nid=otn.nid)  LEFT JOIN old_url_alias AS oua ON(oua.src = CONCAT('node/', n.nid)  ) WHERE 1 GROUP BY n.nid ORDER BY n.created LIMIT 5");

foreach (
$result as $record) {
 
$matches[] = $record;
}


?>

<b>Do a test print first</b><pre>
<?php
    print_r
($matches);
    print
__FILE__. " ".__LINE__;
?>

</pre>
<?php  exit;

foreach (
$matches as $key => $old) {
   
   
make_node($old);
    echo
"<br>$old->nid => $old->title";
}



function
make_node($old){
   
   
$node = new stdClass();
   
$node->type = 'bblogg';
   
$node->status = 1;
   
$node->uid = 1;
   
$node->nid = NULL;
   
$node->vid = NULL;
   
   
node_object_prepare($node);
   
   
$node->name = "siteadmin";
   
$node->title = $old->title;
   
$node->promote = 0;
   
$node->created = $old->created;
   
$node->changed = NULL;
   
$node->timestamp = $old->created;
   
$node->sticky = 0;
   
$node->format = 3;
   
$node->language = 'und';
   
$node->body['und'][0]['value'] = $old->body;
   
$node->body['und'][0]['summary'] = $old->teaser;
   
$node->body['und'][0]['format'] = "full_html";
   
$node->revision = 0;
   
$node->comment = 2;
   
$node->path['language'] = $node->language;
   
$node->path['pathauto'] = 1;
   
    if (
strlen($old->tids) > 0) {
       
$old->tids = explode(",", $old->tids);
        foreach (
$old->tids as $key => $tid) {
           
$node->field_kategori['und'][] = array('tid' => ($tid - 4));
           
//7,8,9,10 old
            //3,4,5,6 new
       
}
    }
   
   
$node->field_old_blog['und'][0]['tid'] = 2; //2 = old
   
   
node_save($node);

   
db_update('url_alias')->fields(array('alias' => $old->dst))->condition('source', 'node/'.$node->nid)->execute();

   
}

?>
Knowledge keywords: