type = "blog";
// the vocabulary name is used to create a new taxonomy within
// your new Drupal installation. taxonomies and vocabularies are
// fancy terms for Movable Type categories. you can name the vocab
// here; you can also change it through Drupal at a later date.
$vocabulary['name'] = "Blog Categories";
// newly imported authors are given an entirely random password -
// for them to login, you can instruct users to "Request new
// password" from the login block. If, on the other hand, you'd
// like to set a default password for everyone, do so here.
$author['pass'] = rand(1,66606660666066606660);
// generic settings that probably shouldn't
// be changed unless you have a clue bit.
$timezone = preg_replace('!:!', '', "<$MTBlogTimezone$>");
$node->promote = 1; // all nodes should have had main page status.
$author['status'] = 1; // all author's are enabled within the system.
$comment['pid'] = 0; // no comments have been threaded; no parents.
// change character encoding (e.g. from ISO-8859-1 to UTF-8)
// if either of these is blank then no encoding is done.
$fromencode = '';
$toencode = '';
// Set your default input format ID here.
$inputformatid = '';
// let's begin this bad boy, eh?
echo "Movable Type => Drupal Conversion
";
// ///////////////////////////////////////////////////////////////////////////
// define code in functions to minimize generated size of the import file. //
// ///////////////////////////////////////////////////////////////////////////
function taxonomy_get_vocabulary_by_name($name) { // Stolen from Drupal 4.4.
$db_result = db_query("SELECT * FROM {vocabulary} WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
$result = array(); while ($vocabulary = db_fetch_object($db_result)) { $result[] = $vocabulary; }
return $result; // this function was removed in Drupal 4.6 builds.
}
function add_taxonomy_term($term) {
global $vocabulary; $term['tid'] = NULL; $term['vid'] = $vocabulary['vid'];
$term = taxonomy_save_term($term); // should probably do some error checking.
echo "Created new term '$term[name]' in '$vocabulary[name]'.";
// path loving. yum. stolen from Scott Laird.
$term_link = ereg_replace('^http://[^/]+/', '', $term['permalink']);
echo "Linking taxonomy/term/$term[tid] to $term_link.";
path_set_alias("taxonomy/term/$term[tid]", $term_link);
}
function check_for_author($author) {
global $node; // yum, yum, gimme some.
$user = user_load(array(mail => $author['mail']));
$user_uid = $user->uid; if (!$user_uid) {
$author['rid'] = array(_user_authenticated_id());
$user = user_save($account, $author);
echo "Created new user '$author[name]'.";
} else { echo "Entry has known user '$author[name]'."; }
$node->uid = $user->uid; // node author to uid.
}
function encodetext($string) {
if ($fromencode == '' or $toencode == '') { return $string; }
else { return iconv($fromencode, $toencode, $string); }
}
// for the node validation, it's important NOT to use the return
// $node from _validate, and only check for $error. since we're
// directly adding new entries to the database from previously
// unauthorized users (those MT folks we're importing), node_
// validate will fail the "is this user authorized?" check, and
// return bum data (notably, the entry's ->date and ->created).
function add_node($node) {
$node->body = encodetext($node->body);
$node->teaser = encodetext($node->teaser);
$node->title = encodetext($node->title);
$node->format = $inputformatid;
node_validate($node, $error); // check existence of $error only.
if (!$error) { $nid = node_save($node); } else { print_r($error); exit; }
echo "Created new entry '". $node->title ."'.";
// path loving. yum. stolen from Scott Laird.
$path_link = ereg_replace('^http://[^/]+/', '', $node->permalink);
echo "Linking node/$nid to $path_link.";
path_set_alias("node/$nid", $path_link);
return $nid; // all done. keep on truckin'.
}
function add_term_to_node($term) {
global $vocabulary, $nid;
$matches = taxonomy_get_term_by_name($term);
foreach ($matches as $match) {
if ($match->vid == $vocabulary['vid']) {
taxonomy_node_save($nid, array($match->tid));
echo "Entry is categorized as '$term'.";
}
}
}
function add_comment_to_node($comment) {
global $nid; // wow. I have to pee. huh.
$comment['thread'] = "$comment[thread]/";
$comment['cid'] = db_next_id("{comments}_cid");
$comment['title'] = encodetext($comment['title']);
$comment['comment'] = encodetext($comment['comment']);
// woulda liked to use the API, but the timestamp needed to be
// the original setting, not the time of the import. some other
// issues here and there too (invalid e-mails that had been
// accepted, the check for whether comments were enabled, etc.)
db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, hostname, ".
"timestamp, status, score, users, thread, name, mail, homepage) VALUES ".
"(%d, %d, %d, %d, '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')",
$comment['cid'], $nid, 0, 0, $comment['title'], $comment['comment'], $comment['remote_addr'],
$comment['timestamp'], 0, 0, "a:1:{i:0;i:0;}", $comment['thread'],
$comment['name'], $comment['email'], $comment['URL']);
}
// code stolen from trackback_receive.
function add_ping_to_node($ping) {
global $nid; // wow. I have to pee. huh. that's not cool.
$ping['thread'] = "$ping[thread]/";
$ping['cid'] = db_next_id("{comments}_cid");
$ping['excerpt'] = (strlen($ping['excerpt'] > 255) ? substr($ping['excerpt'], 0, 252) ."..." : $ping['excerpt']);
$ping['name'] = ($ping['blog_name']) ? $ping['blog_name'] : $ping['url'];
$comment = "". t("TrackBack from %url", array("%url" => "$ping[name]")) .":
";
$comment .= "$ping[excerpt]
"; // all errors introduced are my own. beeyotch.
// woulda liked to use the API, but the timestamp needed to be
// the original setting, not time of the import. same with comments.
// biggest change here is the addition of name/url as per CVS HEAD.
db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, hostname, ".
"timestamp, status, score, users, thread, name, mail, homepage) VALUES ".
"(%d, %d, %d, %d, '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')",
$ping['cid'], $nid, 0, 0, NULL, $comment, $ping['remote_addr'],
$ping['timestamp'], 0, 0, "", $ping['thread'], $ping['name'],
"", $ping['url']);
}
// ///////////////////////////////////////////////////////////////////////////
// first thing we do is import the categories of Movable Type (the "terms") //
// into the Drupal taxonomy ($vocabulary_name). if our vocabulary has been //
// added already, we'll move on to entries (assuming multiple import runs). //
// ///////////////////////////////////////////////////////////////////////////
if (!taxonomy_get_vocabulary_by_name($vocabulary['name'])) {
echo "Importing Categories
"; echo "- Created new vocabulary '$vocabulary[name]'.
";
$vocabulary = taxonomy_save_vocabulary(array("name"=> $vocabulary['name'], "nodes" => array($node['type'])));
$term['name'] = "<$MTCategoryLabel$>";
$term['permalink'] = "<$MTCategoryArchiveLink$>";
$term['description'] = <<
CATEGORY_DESCRIPTION;
add_taxonomy_term($term);
// if we're not creating a vocab, load the ID in. ugly hack, love me anyways. seriously.
echo "
"; } else { $v = taxonomy_get_vocabulary_by_name($vocabulary['name']); $vocabulary['vid'] = $v[0]->vid; }
// ///////////////////////////////////////////////////////////////////////////
// now, let's import the entries. it'd be nice if we could import the //
// authors first, but MT has no loopable author equivalent, so instead //
// we have to do people when we learn about 'em during entry loop. //
// ///////////////////////////////////////////////////////////////////////////
echo "Importing Entries
";
// depending on how you've configured
// your movable type entries, you should
// tweak these to be better represented.
//
// if you're running this script multiple
// times for large imports, [MTEntries] is
// where you'd set "lastn" and "offset". see
// your movable type manual for more info.
$node->title = <<
ENTRY_TITLE;
$node->teaser = <<
ENTRY_TEASER;
$node->body = <<<$MTEntryMore$>
ENTRY_BODY;
$author['name'] = "<$MTEntryAuthor$>";
$author['mail'] = '<$MTEntryAuthorEmail$>';
check_for_author($author); // add 'em?
$node->comment = 0; // comment status?
$node->comment = 1;
$node->comment = 2;
$node->permalink = "<$MTEntryPermalink$>";
$node->status = strtolower('<$MTEntryStatus$>') == "publish" ? 1 : 0;
$node->created = strtotime("<$MTEntryDate format="%Y-%m-%d %H:%M:%S"$> $timezone");
$node->changed = strtotime("<$MTEntryModifiedDate format="%Y-%m-%d %H:%M:%S"$> $timezone");
$nid = add_node($node); // does validation.
$term = "<$MTCategoryLabel$>";
add_term_to_node($term);
$comment_count = 1;
$comment['comment'] = <<
COMMENT_BODY;
$comment['name'] = <<
COMMENT_NAME;
$comment['timestamp'] = strtotime("<$MTCommentDate format="%Y-%m-%d %H:%M:%S"$> $timezone");
$comment['remote_addr'] = "<$MTCommentIP$>";
$comment['email'] = "<$MTCommentEmail$>";
$comment['URL'] = "<$MTCommentURL$>";
$comment['thread'] = $comment_count;
$comment['title'] = <<
ENTRY_TITLE;
add_comment_to_node($comment); $comment_count++;
$ping['title'] = <<
PING_TITLE;
$ping['excerpt'] = <<
PING_EXCERPT;
$ping['blog_name'] = <<
PING_BLOGNAME;
$ping['url'] = "<$MTPingURL$>";
$ping['timestamp'] = strtotime("<$MTPingDate format="%Y-%m-%d %H:%M:%S"$> $timezone");
$ping['remote_addr'] = "<$MTPingIP$>";
$ping['thread'] = $comment_count;
add_ping_to_node($ping); $comment_count++;
$comment_count = 0;
echo "
";
?>