Migrating content/node from drupal 4.x to drupal 7.x

Posted on April 11, 2014 by Suresh Kamrushi in PHP

As a project requirement I need to upgrade Drupal project which was built in 4.7 version to latest version i.e. 7.26. The first think, it is my first encounter with Drupal as a developer, yes I developed couple of website with WordPress but Drupal is new to me.

Drupal having a huge support with version 6 > but no support with 4.X and limited support to 5.x.
Initially I install few module to import node/content with CSV/XML but they are not working as expected. I need to import content first which include mainly title, description images(more than one) and attachments (more than 1) for each content. I checked feed, taxanomy csv import, migrate and couple of more but they are able to upload content but not images and attachments.So I thought of writing our own script which will read each node from Drupal 4.x and insert directly into drupal 7.x database.
I am sharing here the same which i have written, it may help someone out their….

Create a PHP file inside drupal4.x and copy below code and run through browser.

include_once './includes/bootstrap.inc';
define('DRUPAL_ROOT', getcwd());
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$mysqli = new mysqli("localhost", "root", "", "target_db");
/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

/* HERE I AM LOOGING BETWEEN SPECIFIC NODES.
 * ALTERNATIVELY YOU CAN FETCH NODES AND THAN LOOPING IS ALSO FINE.
 */
for($nodeLoop=1;$nodeLoop<50;$nodeLoop++) {     
    $node = node_load($nodeLoop);      
    if($node) {                      
        $node->type = "page";
        $node->nid = $nodeLoop;     
        $node->title = addslashes($node->title);
        $node->body = addslashes($node->body);
        $query_node_revision = "INSERT INTO `node_revision` ( `nid` , `uid` , `title` , `log` , `timestamp` , `status` , `comment` , `promote` , `sticky` )
        VALUES (".$node->nid.", ".$node->uid.", '".$node->title."', '".$node->log."', ".$node->created.", ".$node->status.", ".$node->comment.", ".$node->promote.", ".$node->sticky.")";

        if (!$result = $mysqli->query($query_node_revision)) {
            exit("fail to run query.");
        }

        $node->vid = $mysqli->insert_id; 

        $query_node = "INSERT INTO `node` ( `nid` , `vid` , `type` , `language` , `title` , `uid` , `status` , `created` , `changed` , `comment` , `promote` , `sticky` , `tnid` , `translate` )
        VALUES (".$node->nid.",".$node->vid.",'".$node->type."', 'und','".$node->title."',".$node->uid.",".$node->status.",".$node->created.",".$node->changed.",".$node->comment.",".$node->promote.",".$node->sticky.", 0, 0 )" ;
        if (!$result = $mysqli->query($query_node)) {
            echo "fail to run node query";
        }

        $field_data_body = 'INSERT INTO `field_data_body` ( `entity_type` , `bundle` , `deleted` , `entity_id` , `revision_id` , `language` , `delta` , `body_value` , `body_summary` , `body_format` )
        VALUES ("node", "'.$node->type.'", 0, '.$node->nid.', '.$node->vid.', "und", 0, "'.$node->body.'", "", "bbcode")';

        if (!$result = $mysqli->query($field_data_body)) {
            echo "fail to run field_data_body query.";
        }

        $field_revision_body = 'INSERT INTO `field_revision_body` ( `entity_type` , `bundle` , `deleted` , `entity_id` , `revision_id` , `language` , `delta` , `body_value` , `body_summary` , `body_format` )
        VALUES ("node", "'.$node->type.'", 0, '.$node->nid.', '.$node->vid.', "und", 0, "'.$node->body.'", "", "bbcode")';
        if (!$result = $mysqli->query($field_revision_body))
            echo "fail to run field_revision_body query.";

        //IF NODE HAVING ATTACHMENT OR IMAGES UPLOADED.
        if(count($node->files)>0){
            $loop = 0;
            foreach($node->files as $file) {

                $filepath = "public://".$file->filename;
                $file_managed = "INSERT INTO `file_managed` (`uid`, `filename`, `uri`, `filemime`, `filesize`, `status`, `timestamp`) VALUES
                (".$node->uid.", '".$file->filename."','".$filepath."','".$file->filemime."', ".$file->filesize.", ".$node->status.", ".$node->created.")";
                if (!$result = $mysqli->query($file_managed))
                    echo "fail to run file_managed query.";
                $file->fid = $mysqli->insert_id;
                $file_usage = "INSERT INTO `file_usage` (`fid`, `module`, `type`, `id`, `count`) VALUES (".$file->fid.", 'file', 'node', ".$node->nid.", 1)";
                if (!$result = $mysqli->query($file_usage))
                    echo "fail to run file_usage query.";

                if(strstr($file->filemime, 'application')) {
                    #Keep revision_id same as node revision id
                    $file_attachment_revision = "INSERT INTO `field_revision_field_attachment` VALUES('node', '".$node->type."', 0, ".$node->nid.", ".$node->vid.", 'und', ".$loop.", ".$file->fid.", 1, '')";
                    if ($result = $mysqli->query($file_attachment_revision)) {
                        echo "fail to run field_revision_field_attachment query.";
                    }

                    $file_attachment = "INSERT INTO `field_data_field_attachment` (`entity_type`, `bundle`, `deleted`, `entity_id`, `revision_id`, `language`, `delta`, `field_attachment_fid`, `field_attachment_display`, `field_attachment_description`) 
                    VALUES ('node', '".$node->type."', 0, ".$node->nid.", ".$node->vid.", 'und', ".$loop.", ".$file->fid.", 1, '')";
                    if (!$result = $mysqli->query($file_attachment))
                        echo "fail to run field_data_field_attachment query";        
                } else {
                    $revision_field_image = "INSERT INTO `field_revision_field_content_image` (`entity_type`, `bundle`, `deleted`, `entity_id`, `revision_id`, `language`, `delta`, `field_content_image_fid`, `field_content_image_description`) 
                    VALUES ('node', '".$node->type."', 0, ".$node->nid.", ".$node->vid.", 'und', ".$loop.", ".$file->fid.", '".$file->description."')";
                    if (!$result = $mysqli->query($revision_field_image))
                        echo "fail to run field_revision_field_content_image query";

                    $field_image = "INSERT INTO `field_data_field_content_image` (`entity_type`, `bundle`, `deleted`, `entity_id`, `revision_id`, `language`, `delta`, `field_content_image_fid`, `field_content_image_description`) 
                    VALUES ('node', '".$node->type."', 0, ".$node->nid.", ".$node->vid.", 'und', ".$loop.", ".$file->fid.", '".$file->description."')";
                    if (!$result = $mysqli->query($field_image))
                        echo "fail to run query no 98";        
                }
                $loop++;
            }
        }

    } 
}
  Comments or questions are welcome  
Tagged as , ,