| 1 |
#!/usr/bin/perl -w
|
| 2 |
# $Id: mt-to-drupal.cgi,v 1.20 2004/07/10 05:29:16 jseng Exp $
|
| 3 |
|
| 4 |
use strict;
|
| 5 |
|
| 6 |
###
|
| 7 |
# Please configure these
|
| 8 |
###
|
| 9 |
|
| 10 |
# MT blog id you want to export from
|
| 11 |
my $blog_id = 1;
|
| 12 |
|
| 13 |
# Import mode :-
|
| 14 |
# 0 - Drupal 4 Blogger
|
| 15 |
# 1 - Drupal 4.4.1
|
| 16 |
# 2 - Drupal CVS
|
| 17 |
my $mode = 0;
|
| 18 |
|
| 19 |
# your timezone -12 to +12
|
| 20 |
my $timezone = 0;
|
| 21 |
|
| 22 |
# your mysql settings
|
| 23 |
my $db_host = "localhost";
|
| 24 |
my $db_database = "drupal";
|
| 25 |
my $db_user = "drupal";
|
| 26 |
my $db_pass = "drupal";
|
| 27 |
|
| 28 |
# set this only if you use db_prefix
|
| 29 |
# e.g. my $db_prefix = "drupal_";
|
| 30 |
my $db_prefix = "";
|
| 31 |
|
| 32 |
# your MT RSS feed
|
| 33 |
my $rss1_0 = "index.rdf";
|
| 34 |
my $rss2_0 = "index.xml";
|
| 35 |
|
| 36 |
###
|
| 37 |
# Modify these only if you know what you are doing
|
| 38 |
###
|
| 39 |
|
| 40 |
# preserve entries and comments id? 0=no, 1=yes
|
| 41 |
# If set to 1, ie yes, this script *cannot* import into
|
| 42 |
my $preserve_id = 0;
|
| 43 |
|
| 44 |
# debug message level - 0=basic, 1=some, 2=detail
|
| 45 |
my $debug = 1;
|
| 46 |
|
| 47 |
###
|
| 48 |
# Do not modify from here
|
| 49 |
###
|
| 50 |
my($MT_DIR);
|
| 51 |
BEGIN {
|
| 52 |
if ($0 =~ m!(.*[/\\])!) {
|
| 53 |
$MT_DIR = $1;
|
| 54 |
} else {
|
| 55 |
$MT_DIR = './';
|
| 56 |
}
|
| 57 |
unshift @INC, $MT_DIR . 'lib';
|
| 58 |
unshift @INC, $MT_DIR . 'extlib';
|
| 59 |
}
|
| 60 |
|
| 61 |
use Mysql;
|
| 62 |
|
| 63 |
use MT;
|
| 64 |
use MT::Blog;
|
| 65 |
use MT::Author;
|
| 66 |
use MT::Permission;
|
| 67 |
use MT::Category;
|
| 68 |
use MT::Entry;
|
| 69 |
use MT::Comment;
|
| 70 |
use MT::Util qw( archive_file_for );
|
| 71 |
use MT::TemplateMap;
|
| 72 |
use MT::Trackback;
|
| 73 |
use MT::TBPing;
|
| 74 |
use POSIX;
|
| 75 |
|
| 76 |
local $| = 1;
|
| 77 |
|
| 78 |
sub db_title {
|
| 79 |
print "\n--\n";
|
| 80 |
print "-- ".$_[0]."\n";
|
| 81 |
print "--\n\n";
|
| 82 |
}
|
| 83 |
|
| 84 |
sub db_query {
|
| 85 |
my $dbh = shift;
|
| 86 |
my $sql = shift;
|
| 87 |
|
| 88 |
my $statement = sprintf($sql,@_);
|
| 89 |
$dbh->Query($statement) || print "-- error! ".$Mysql::db_errstr. " - " . $statement;
|
| 90 |
print $statement."\n" if ($debug>=2);
|
| 91 |
}
|
| 92 |
|
| 93 |
sub timeformat {
|
| 94 |
my $mttime = shift; $mttime =~ s/\D//g;
|
| 95 |
my($y, $mo, $d, $h, $m, $s) = unpack 'A4A2A2A2A2A2', $mttime;
|
| 96 |
my $time = POSIX::mktime($s, $m, $h, $d, $mo - 1, $y - 1900) + $timezone;
|
| 97 |
return $time;
|
| 98 |
}
|
| 99 |
|
| 100 |
my $dbh = Mysql->Connect($db_host, $db_database, $db_user, $db_pass);
|
| 101 |
|
| 102 |
print "Content-Type: text/html\n\n";
|
| 103 |
print "<html><pre>\n";
|
| 104 |
|
| 105 |
my $mt = MT->new(Config => $MT_DIR . 'mt.cfg',
|
| 106 |
Directory => $MT_DIR) || die MT->errstr;
|
| 107 |
|
| 108 |
my $sth; my @return;
|
| 109 |
my $drupal_uid = 0;
|
| 110 |
my $drupal_category = 0;
|
| 111 |
my $drupal_term = 0; # number of terms in drupal
|
| 112 |
my $drupal_node = 0; # number of nodes in drupal
|
| 113 |
my $drupal_comment = 0; # number of comments in drupal
|
| 114 |
|
| 115 |
$timezone = $timezone*60*60;
|
| 116 |
|
| 117 |
# find blog
|
| 118 |
my $blog = MT::Blog->load($blog_id);
|
| 119 |
if (!$blog) {
|
| 120 |
print "-- Cannot find MT blog $blog_id!\n";
|
| 121 |
print "</pre></html>";
|
| 122 |
exit;
|
| 123 |
}
|
| 124 |
|
| 125 |
# find default user name
|
| 126 |
my %drupal_users;
|
| 127 |
$sth = $dbh->Query("SELECT uid, name FROM ${db_prefix}users");
|
| 128 |
while (@return = $sth->FetchRow) {
|
| 129 |
$drupal_users{$return[1]} = $return[0];
|
| 130 |
$drupal_uid = ($return[0] > $drupal_uid ? $return[0] : $drupal_uid);
|
| 131 |
}
|
| 132 |
if ($drupal_uid == 0) {
|
| 133 |
print "-- You have not created your first user on drupal yet!\n";
|
| 134 |
print "</pre></html>";
|
| 135 |
exit;
|
| 136 |
}
|
| 137 |
|
| 138 |
$sth = $dbh->Query("SELECT vid FROM ${db_prefix}vocabulary_node_types WHERE type LIKE '%blog%'");
|
| 139 |
while (@return = $sth->FetchRow) {
|
| 140 |
$drupal_category = $return[0];
|
| 141 |
last;
|
| 142 |
}
|
| 143 |
if ($drupal_category == 0) {
|
| 144 |
print "-- You have not created an vocabulary attached to blog yet!\n";
|
| 145 |
print "</pre></html>";
|
| 146 |
exit;
|
| 147 |
}
|
| 148 |
|
| 149 |
my %drupal_categories;
|
| 150 |
$sth = $dbh->Query(sprintf("SELECT name,tid FROM ${db_prefix}term_data WHERE vid=%d", $drupal_category));
|
| 151 |
while (@return = $sth->FetchRow) {
|
| 152 |
$drupal_categories{$return[0]} = $return[1];
|
| 153 |
}
|
| 154 |
|
| 155 |
$sth = $dbh->Query("SELECT name, id FROM ${db_prefix}sequences");
|
| 156 |
while (@return = $sth->FetchRow) {
|
| 157 |
$drupal_term = $return[1] if ($return[0] =~ "${db_prefix}term_data_tid");
|
| 158 |
$drupal_node = $return[1] if ($return[0] =~ "${db_prefix}node_nid");
|
| 159 |
$drupal_comment = $return[1] if ($return[0] =~ "${db_prefix}comments_cid");
|
| 160 |
}
|
| 161 |
|
| 162 |
my $iter; my $count; my @userid; my @catsid;
|
| 163 |
|
| 164 |
db_title('Creating additional bloggers');
|
| 165 |
my @authors = MT::Author->load(); $count = $drupal_uid;
|
| 166 |
for my $author (@authors) {
|
| 167 |
my $perms = MT::Permission->load({author_id => $author->id,
|
| 168 |
blog_id => $blog_id});
|
| 169 |
next if ($perms && !$perms->can_post);
|
| 170 |
|
| 171 |
if ($drupal_users{$author->name}) {
|
| 172 |
$userid[$author->id] = $drupal_users{$author->name};
|
| 173 |
next;
|
| 174 |
}
|
| 175 |
|
| 176 |
$count++;
|
| 177 |
$userid[$author->id] = $count;
|
| 178 |
|
| 179 |
if ($mode == 0 || $mode == 1) {
|
| 180 |
db_query($dbh,"INSERT INTO ${db_prefix}users (uid, rid, name, pass, mail, timestamp) VALUES (%d, %d, %s, %s, %s, %d)", $count, 2, $dbh->quote($author->name), $dbh->quote($author->password), $dbh->quote($author->email), time());
|
| 181 |
} else {
|
| 182 |
db_query($dbh,"INSERT INTO ${db_prefix}users (uid, name, pass, mail, created) VALUES (%d, %s, %s, %s, %d)", $count, $dbh->quote($author->name), $dbh->quote($author->password), $dbh->quote($author->email), time());
|
| 183 |
}
|
| 184 |
}
|
| 185 |
db_query($dbh,"REPLACE INTO ${db_prefix}sequences VALUES (%s, %d)", $dbh->quote("${db_prefix}users_uid"), $count);
|
| 186 |
db_title(sprintf("%s bloggers imported",$count - $drupal_uid));
|
| 187 |
|
| 188 |
db_title("Creating categories");
|
| 189 |
my @categories = MT::Category->load({blog_id => $blog_id}); $count = $drupal_term;
|
| 190 |
for my $cat (@categories) {
|
| 191 |
my $path = $cat->label;
|
| 192 |
$path =~ tr/A-Z/a-z/;
|
| 193 |
$path =~ s/\s/_/g;
|
| 194 |
$path = "archives/cat_".$path.".html";
|
| 195 |
if ($drupal_categories{$cat->label}) {
|
| 196 |
$catsid[$cat->id] = $drupal_categories{$cat->label};
|
| 197 |
db_query($dbh, "REPLACE INTO ${db_prefix}url_alias (src, dst) VALUES (%s, %s)", $dbh->quote(sprintf("taxonomy/page/or/%d",$catsid[$cat->id])),$dbh->quote($path));
|
| 198 |
next;
|
| 199 |
}
|
| 200 |
$count++;
|
| 201 |
$catsid[$cat->id] = $count;
|
| 202 |
db_query($dbh, "REPLACE INTO ${db_prefix}url_alias (src, dst) VALUES (%s, %s)", $dbh->quote(sprintf("taxonomy/page/or/%d",$count)),$dbh->quote($path));
|
| 203 |
db_query($dbh, "INSERT INTO ${db_prefix}term_data (vid, tid, name, description) VALUES (%d, %d, %s, %s)", $drupal_category, $count, $dbh->quote($cat->label), $dbh->quote($cat->description));
|
| 204 |
db_query($dbh, "INSERT INTO ${db_prefix}term_hierarchy (tid, parent) VALUES (%d, %d)", $count, 0);
|
| 205 |
|
| 206 |
if ($cat->allow_pings) {
|
| 207 |
# unable to ping to taxonomy's terms yet.
|
| 208 |
}
|
| 209 |
}
|
| 210 |
db_query($dbh, "REPLACE INTO ${db_prefix}sequences VALUES (%s, %d)", $dbh->quote("${db_prefix}term_data_tid"), $count);
|
| 211 |
db_title(sprintf("%s categories imported",$count - $drupal_term));
|
| 212 |
|
| 213 |
db_title("Creating entries");
|
| 214 |
my $total_entries = MT::Entry->count({blog_id => $blog_id});
|
| 215 |
my $cid = $drupal_comment; $count = $drupal_node;
|
| 216 |
my $lnid = 0;
|
| 217 |
for (my $offset = 0; $offset < $total_entries; $offset += 50) {
|
| 218 |
db_title(sprintf("importing entry %d to %d",$offset,($offset+50>$total_entries ? $total_entries : $offset+50))) if ($debug>=1);
|
| 219 |
my @entries = MT::Entry->load({blog_id => $blog_id}, {limit => 50, offset => $offset});
|
| 220 |
for my $entry (@entries) {
|
| 221 |
$count++; $lnid = $entry->id if ($entry->id > $lnid);
|
| 222 |
my $entry_status = 1;
|
| 223 |
$entry_status = 0 if ($entry->status != 2);
|
| 224 |
if ($mode == 0) {
|
| 225 |
db_query($dbh,"INSERT INTO ${db_prefix}node (nid, type, title, uid, status, created, changed, comment, promote, body, teaser) VALUES (%d, %s, %s, %d, %d, %d, %d, %d, %d, %s, %s)", ($preserve_id ? $entry->id : $count), $dbh->quote('blog'), $dbh->quote($entry->title), ($userid[$entry->author_id] ? $userid[$entry->author_id] : 1), $entry_status, timeformat($entry->created_on), timeformat($entry->modified_on), ($entry->allow_comments ? 2 : 0), 1, $dbh->quote(($entry->text ? $entry->text : '')), $dbh->quote(($entry->text ? $entry->text : '')));
|
| 226 |
db_query($dbh,"INSERT INTO ${db_prefix}blog (nid, extended, excerpt, format) VALUES (%d, %s, %s, %d)", ($preserve_id ? $entry->id : $count), $dbh->quote(($entry->text_more ? $entry->text_more: '')), $dbh->quote(($entry->excerpt ? $entry->excerpt : '')), ($entry->convert_breaks ? 0 : -1));
|
| 227 |
db_query($dbh,"INSERT INTO ${db_prefix}trackback (nid, toping, pinged) VALUES (%d, %s, %s)", ($preserve_id ? $entry->id : $count), $dbh->quote($entry->to_ping_urls), $dbh->quote($entry->pinged_urls));
|
| 228 |
} else {
|
| 229 |
my $body = '';
|
| 230 |
if ($entry->text) {
|
| 231 |
$body = $entry->text;
|
| 232 |
$body .= "<!--break-->" . $entry->text_more if ($entry->text_more);
|
| 233 |
}
|
| 234 |
db_query($dbh,"INSERT INTO ${db_prefix}node (nid, type, title, uid, status, created, changed, comment, promote, body, teaser) VALUES (%d, %s, %s, %d, %d, %d, %d, %d, %d, %s, %s)", ($preserve_id ? $entry->id : $count), $dbh->quote('blog'), $dbh->quote($entry->title), ($userid[$entry->author_id] ? $userid[$entry->author_id] : 1), $entry_status, timeformat($entry->created_on), timeformat($entry->modified_on), ($entry->allow_comments ? 2 : 0), 1, $dbh->quote($body), $dbh->quote(($entry->text ? $entry->text : '')));
|
| 235 |
}
|
| 236 |
|
| 237 |
my $cats = $entry->categories;
|
| 238 |
for my $cat (@$cats) {
|
| 239 |
next if (!$catsid[$cat->id]);
|
| 240 |
db_query($dbh,"INSERT INTO ${db_prefix}term_node (nid, tid) VALUES (%d, %d)", ($preserve_id ? $entry->id : $count), $catsid[$cat->id]);
|
| 241 |
}
|
| 242 |
|
| 243 |
my @comments = MT::Comment->load({blog_id => $blog_id, entry_id => $entry->id});
|
| 244 |
my $ccid = 0; my $thread;
|
| 245 |
for my $comment (@comments) {
|
| 246 |
$cid++; $ccid++; $thread = "";
|
| 247 |
for (my $i = 0; $i < int $ccid/10; $i++) {
|
| 248 |
$thread .= "9";
|
| 249 |
}
|
| 250 |
$thread .= "" . $ccid%10 . "/";
|
| 251 |
# db_query($dbh, "UPDATE ${db_prefix}comments SET thread = %s WHERE cid = %d", $dbh->quote($thread), $comment[0]);
|
| 252 |
if ($mode == 0 || $mode == 2) {
|
| 253 |
db_query($dbh,"INSERT INTO ${db_prefix}comments (cid, pid, nid, uid, subject, comment, hostname, timestamp, status, name, mail, homepage, thread) VALUES (%d, %d, %d, %d, %s, %s, %s, %d, %d, %s, %s, %s, %s)", $cid, 0, ($preserve_id ? $entry->id : $count), 0, $dbh->quote(''), $dbh->quote($comment->text), $dbh->quote($comment->ip), timeformat($comment->created_on), 0, $dbh->quote($comment->author), $dbh->quote($comment->email), $dbh->quote($comment->url), $dbh->quote($thread));
|
| 254 |
} else {
|
| 255 |
db_query($dbh,"INSERT INTO ${db_prefix}comments (cid, pid, nid, uid, subject, comment, hostname, timestamp, status, thread) VALUES (%d, %d, %d, %d, %s, %s, %s, %d, %d, %s)", $cid, 0, ($preserve_id ? $entry->id : $count), 0, $dbh->quote(''), $dbh->quote($comment->text), $dbh->quote($comment->ip), timeformat($comment->created_on), 0, $dbh->quote($thread));
|
| 256 |
}
|
| 257 |
}
|
| 258 |
|
| 259 |
my $tbping = MT::Trackback->load({blog_id => $blog_id, entry_id => $entry->id});
|
| 260 |
if ($tbping) {
|
| 261 |
my @trackbacks = MT::TBPing->load({blog_id => $blog_id, tb_id => $tbping->id});
|
| 262 |
for my $tb (@trackbacks) {
|
| 263 |
$cid++; $ccid++; $thread = "";
|
| 264 |
for (my $i = 0; $i < int $ccid/10; $i++) {
|
| 265 |
$ thread .= "9";
|
| 266 |
}
|
| 267 |
$thread .= "" . $ccid%10 . "/";
|
| 268 |
my $text = sprintf("<strong>Trackback from <a href=\"%s\">%s</a>:</strong><br /><blockquote>%s...</blockquote>", $tb->source_url, $tb->blog_name, $tb->excerpt);
|
| 269 |
if ($mode == 0 || $mode == 2) {
|
| 270 |
db_query($dbh,"INSERT INTO ${db_prefix}comments (cid, pid, nid, uid, subject, comment, hostname, timestamp, status, name, mail, homepage, thread) VALUES (%d, %d, %d, %d, %s, %s, %s, %d, %d, %s, %s, %s, %s)", $cid, ($preserve_id ? $entry->id : $count), ($preserve_id ? $entry->id : $count), 0, $dbh->quote(($tb->title ? $tb->title : '')), $dbh->quote($text), $dbh->quote($tb->ip), timeformat($tb->created_on), 0, $dbh->quote(($tb->blog_name ? $tb->blog_name : '')), $dbh->quote(''), $dbh->quote($tb->source_url), $dbh->quote($thread));
|
| 271 |
} else {
|
| 272 |
db_query($dbh,"INSERT INTO ${db_prefix}comments (cid, pid, nid, uid, subject, comment, hostname, timestamp, status, thread) VALUES (%d, %d, %d, %d, %s, %s, %s, %d, %d, %s)", ($preserve_id ? $entry->id : $count), $cid, ($preserve_id ? $entry->id :$count), 0, $dbh->quote(($tb->title ? $tb->title : '')), $dbh->quote($text), $dbh->quote($tb->ip), timeformat($tb->created_on), 0, $dbh->quote($thread));
|
| 273 |
}
|
| 274 |
db_query($dbh,"REPLACE INTO ${db_prefix}trackback_pingme (nid, cid) VALUES (%d, %d)", ($preserve_id ? $entry->id : $count), $cid) if ($mode == 0);
|
| 275 |
}
|
| 276 |
}
|
| 277 |
|
| 278 |
my $file = $blog->archive_url . sprintf("%06d", $entry->id) . ".html";
|
| 279 |
my $site_url = $blog->site_url;
|
| 280 |
$file =~ s/$site_url//;
|
| 281 |
db_query($dbh,"REPLACE INTO ${db_prefix}url_alias (src, dst) VALUES (%s, %s)", $dbh->quote(sprintf("node/view/%d", ($preserve_id ? $entry->id : $count))), $dbh->quote($file));
|
| 282 |
}
|
| 283 |
}
|
| 284 |
db_query($dbh, "REPLACE INTO ${db_prefix}sequences VALUES (%s, %d)", $dbh->quote("${db_prefix}comments_cid"), $cid);
|
| 285 |
db_query($dbh, "REPLACE INTO ${db_prefix}sequences VALUES (%s, %d)", $dbh->quote("${db_prefix}node_nid"), ($preserve_id ? $lnid : $count));
|
| 286 |
db_title(sprintf("%s entries imported",$count - $drupal_node));
|
| 287 |
db_title(sprintf("%s comments/trackbacks imported",$cid - $drupal_comment));
|
| 288 |
|
| 289 |
db_title("Alias'ing MT RSS feed to Drupal RSS feed");
|
| 290 |
db_query($dbh, "REPLACE INTO ${db_prefix}url_alias (src, dst) VALUES (%s, %s)", $dbh->quote("node/feed/rss1"), $dbh->quote($rss1_0));
|
| 291 |
db_query($dbh, "REPLACE INTO ${db_prefix}url_alias (src, dst) VALUES (%s, %s)", $dbh->quote("node/feed/rss2"), $dbh->quote($rss2_0));
|
| 292 |
|
| 293 |
db_title("Autosubscribe all users to their own post");
|
| 294 |
db_query($dbh, "INSERT INTO ${db_prefix}subscriptions (sid, uid, stype) SELECT nid, uid, 'node' FROM ${db_prefix}node");
|
| 295 |
|
| 296 |
print "\n</pre></html>\n\n";
|