#!/usr/bin/perl use Mysql; use Getopt::Long; # http://perl.active-venture.com/lib/Getopt/Long.html ################################################## # EXPORT WORDPRESS FROM A MOVABLE TYPE DATABASE WITHOUT A WEB SERVER # # by Brian Doom - Filmmaker, programmer, candy entrepreneur, business maniac. # brain @ ikillspies.com # comment on my blog if you need help, # but for the most part this script is unsupported # ################################################## sub usage () { print "Generates a file from an ancient Movable Type database\n"; print "Completely unsupported.\n"; print "Hardcoded category, blog_id, database login info, timezone\n"; print "Imports comments but not trackbacks.\n"; print "Sets all entries to a single category.\n\n"; print "To use: \n"; print "\t0) read entire script! Set your default category.\n"; print "\t1) Edit the file and set all the database login info. Set Blog Id.\n"; print "\t2) run!\n"; print "\t3) your file is still not done. Make a XML export of your current WordPress blog, this will be the 'real' file.\n"; print "\t4) from the 'real' file, copy the header down to but not including the first tag\n"; print "\t5) prepend to your generated file\n"; print "\t6) add '' to the end of your file \n"; print "\t7) upload to WordPress\n"; print "\t\t7a) may time out. In this case, go to blog admin and wait for number of entries to stop increasing\n"; print "\t\t7b) eliminate successful entries from file and repeat submission!\n"; print "\n"; } # end sub usage # read in which blog we are doing my $blogId = ''; my $help = ''; my $outFile = 'custom-MT-Export.xml'; my $logFile = 'customMTExport.log'; GetOptions ('help|?' => \$help); if ($help) { usage(); exit(0); } GetOptions ('blog|id|b=i' => \$blogId); GetOptions ('out' => \$outFile); GetOptions ('log' => \$logFile); open ( OUT, ">$outFile" ) or die "ERROR: couldn't open $outFile for output\n"; open ( LOG, ">>$logFile" ) or die "ERROR: couldn't open $logFile for logging\n"; # open DB connection # REMEMBER TO SET THESE BEFORE RUNNING!!! $host = 'localhost'; $database = 'blog'; $user = 'mtblogtest'; $password = 'fnord'; # use either MySQL or DBI... not both! # MySQL: # $connection = Mysql->connect($host, $database, $user, $password) # or die "could not connect to $database using $user@$host \n"; # DBI: $dsn = "dbi:mysql:$database:localhost:3306"; $connection = DBI->connect ($dsn, $user, $password) or die "could not connect to $database using $user@$host \n"; # see the "4" here? Set that to your blog key in mt_blog.blog_id # GMT is set to California time because we are the center of the world $query = <prepare($query) or die "Preparing SQL failed! Query was $query \n"; $statement->execute() or die "Executing SQL failed! Query was $query \n"; $statement->bind_columns( \$entry_id, \$entry_status, \$entry_author, \$entry_title, \$entry_excerpt, \$entry_text, \$entry_text_more, \$entry_created_on, \$entry_created_on_gmt); # header: WordPress # this is actually quite extensive: maybe put it in manually? #print OUT '' . "\n"; # DEBUG #$i = 0; while ( $entry = $statement->fetchrow_hashref() ) { # DEBUG: reduce number of iterations for test #$i++; #last if ($i > 30); print OUT "\n"; # TODO: for all fields, do not output if empty # MISSING FIELD: link print OUT "$entry_title\n"; print OUT "$entry_created_on\n"; print OUT "$entry_author\n"; # NOTE: for some reason, we lost all category data in the MT database # default: make them all "looky" print OUT "\n"; # NOTE: if you want to do categories, now would be a good time # MISSING FIELD: guid print OUT "\n$entry_title \n"; print OUT "(At $entry_excerpt)

\n" } elsif ( $entry_excerpt =~ /^\s*$/ ) { # don't print anything if it's blank! #print OUT "DEBUG: length zero! $entry_excerpt \n"; } else { print OUT "\n\n$entry_excerpt\n"; } # end "link-only" print OUT "]]>\n"; # MISSING: #6 #2007-10-24 19:25:25 #2007-10-25 02:25:25 #chicago-day-4-living-maxim-and-geekery print OUT <$entry_created_on $entry_created_on_gmt open open publish 0 0 post BLOCK # fetch entry comments # TODO: SQL $commentDateGMT $commentquery = <prepare($commentquery) or die "Preparing SQL failed! Query was $query \n"; $statement2->execute() or die "Executing SQL failed! Query was $query \n"; $statement2->bind_columns( \$commentAuthor, \$commentDate, \$commentDateGMT, \$commentIP, \$commentEmail, \$commentUrl, \$commentBody ); while ( $comment = $statement2->fetchrow_hashref() ) { print OUT < 1 $commentEmail $commentUrl $commentIP $commentDate $commentDateGMT $commentBody 1 0 BLOCK2 } # end for every comment for this entry print OUT "\n"; $statement2->finish; # that is the end of all the data for a single entry # progress meter print LOG "."; } # end for every entry $statement->finish; $connection->disconnect(); close LOG; close OUT;