Community Documentation

Template for MT entry and comment export and Drupal import

Last updated January 12, 2006. Created by Junyor on March 1, 2004.
Edited by sepeck, puregin. Log in to edit this page.

Note: this script was originally written to migrate to Drupal 4.4. It is left for reference.

To do:
  • Automatically create accounts
  • Export MT categories and import to Drupal taxonomy
Limitations:
  • All comments are from the Anonymous user
  • All comments are unthreaded
  • The comment subject is made from the first five words of the comment
  • The import defaults to using uid 1, i.e. the site admin (change the $uid variable to import to another user)
  • All posts are promoted to the front page
  • All comments have a published status
  • MT categories (Drupal taxonomy terms) are not exported
Instructions:
  1. Create a new Movable Type Index template called Drupal Import with import.php specified as the Output File
  2. Cut and paste the following into the Template body textarea
  3. Set the variables below the //set variable defaults comment to the correct values
  4. Save and rebuild the template
  5. Load import.php in your web browser

If the import is successful, the output will be a single sentence listing the number of entries and comments imported.

<html>
<head>
<title>Export</title>
</head>
<body>

<?php
//set variable defaults
$hostname = "";
$username = "";
$password = "";
$db = "";
$uid = 1;

// get next node number from sequences table
$link = mysql_connect($hostname,$username,$password) or die("Could not connect to server");
mysql_select_db($db) or die("Could not select database ".$db);
$result = mysql_query("SELECT nid FROM node");
$node_rows = mysql_num_rows($result)+1;

<
MTEntries lastn="1000" sort_order="ascend">

$node_title = <<<NT
<$MTEntryTitle$>
NT;
$node_title = mysql_escape_string($node_title);

$node_teaser = <<<NE
<$MTEntryBody$>
NE;
$node_teaser = mysql_escape_string($node_teaser);

$node_body = <<<NB
<$MTEntryBody$><$MTEntryMore$>
NB;
$node_body = mysql_escape_string($node_body);

//get post status
$status = strtolower("<$MTEntryStatus$>");

if (
$status == "publish")
{
 
$node_status = 1;   
}
else
{
 
$node_status = 0;
}

$node_insert_query = "INSERT INTO node (type, title, uid, status, comment, promote, users, revisions, created, changed, teaser, body) VALUES ('blog', '$node_title', $uid, $node_status, 2, 1, '', '', UNIX_TIMESTAMP('<$MTEntryDate format="%Y-%m-%d %H:%M:%S"$><$MTBlogTimezone$>'), UNIX_TIMESTAMP('<$MTEntryDate format="%Y-%m-%d %H:%M:%S"$><$MTBlogTimezone$>'), '$node_teaser', '$node_body');";

<
MTComments sort_order="ascend">
$comment_text = <<<CT
<$MTCommentBody$>
CT;
$comment_text = mysql_escape_string($comment_text);
   
// grab the first five words of the comment as the comment subject
$subject = "";
$arr = explode(" ",$comment_text);
   
for(
$i=0; $i<5; $i++) { $subject .= $arr[$i]." "; }

$comments_insert_query = "INSERT INTO comments (cid, pid, nid, uid, subject, comment, hostname, timestamp, score, status, thread, users) VALUES (NULL, 0, $node_rows, 0, '$subject', '$comment_text', '<$MTCommentIP$>', UNIX_TIMESTAMP('<$MTCommentDate format="%Y-%m-%d %H:%M:%S"$><$MTBlogTimezone$>'), 0, 0, '1/', 'a:1:{i:0;i:0;}');";

mysql_query($comments_insert_query);

if (
mysql_errno($link))
{
  echo
mysql_errno($link) . ": " . mysql_error($link) . "\n";
}

$comments_rows++;
</
MTComments>

// increment node_rows counter, so we have the correct nid for the comment insert next time
$node_rows++;

mysql_query($node_insert_query);

if (
mysql_errno($link))
{
  echo
mysql_errno($link) . ": " . mysql_error($link) . "\n";
}

</
MTEntries>

// echo the number of rows added to the nodes table
echo($node_rows." blog entries and ");
mysql_query("INSERT into sequences (name,id) VALUES ('node_nid',$node_rows+1)");
if (
mysql_errno($link))
{
  echo
mysql_errno($link) . ": " . mysql_error($link) . "\n";
}

// echo the number of rows added to the comments table
echo($comments_rows." comments inserted.");
mysql_query("INSERT into sequences (name,id) VALUES ('comments_cid',$comments_rows+1)");
if (
mysql_errno($link))
{
  echo
mysql_errno($link) . ": " . mysql_error($link) . "\n";
}

mysql_close($link);
?>


</body>
</html>

About this page

Drupal version
Drupal 4.5.x or older

Installation guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here