« 誰どこ:フェニックスの『今ホームショップにいる人』を列挙する | Main | WiiでもWikipediaを見たい! »

データベースを壊してしまってMTを再構築

データベースをいじっていたらついうっかりMovableTypeで使っていたデータベースを削除してしまいました。

バックアップも取ってないのに。

仕方ないのでMovableTypeのstaticなhtmlからXML-RPCを使ってエントリし直すプログラムを書いて対処。なんとかうまく行ってる?

後々また使うことになるかも知れないのでついでに晒しておきます。

※注意:とても適当なプログラムです。もし使おうとする場合は十分テストしてからご利用下さい。


use strict;
use warnings;
use Net::MovableType;
use Jcode;

#staticページからデータを取り出すための正規表現。
my $re_title     = '<h2 id="archive-title">(.+)</h2>';
my $re_entrybody = '<div class="entry-body">';
my $re_entrymore = '<div id="more" class="entry-more">';
my $re_entrydate = 'Posted on (\d+)&#24180;(\d+)&#26376;(\d+)&#26085; (\d+):(\d+)';
my $re_entryend  = '^</div>\n';	# ←これでentrybodyとentrymoreの終了を検知してます。適当すぎ!

#XML-RPCの設定
my $mt = new Net::MovableType('http://tenguyasiki.jp/MT/mt-xmlrpc.cgi');
$mt->username('username');
$mt->password('password');
$mt->blogid(1);

my $dir = 'entries';	# htmlファイルを探索するディレクトリ。

tree($dir, '.');	#探索開始

exit;

# 指定したディレクトリ以下にあるファイルを探索します。
sub tree{
	my $dir = $_[0];
	my $current = $_[1];

	opendir DH, "$current/$dir" or die "$current/$dir:$!";
	my @list = readdir DH;
	close(DH);

	foreach my $file (@list) {
		next if $file =~ /^\.{1,2}$/;	# '.'や'..'も取れるので、スキップする
		next if $file =~ /index\.html/;	# index.htmlもスキップ
		if(-d "$dir/$file"){
			tree($file, "$current/$dir");
		}
		else{
			my $filename = "$current/$dir" . "/" . $file, "\n";
			print "$filename\n";

			my $ent = parseEntry($filename);

			my $entry = {
			    title        => $ent->{title},
			    description  => $ent->{body},
			    mt_text_more => $ent->{more},
			    mt_convert_breaks => '0',
			    dateCreated  => $ent->{date},
			};

			my $new_id = $mt->newPost($entry, 0) or die $mt->errstr;
			$mt->setPostCategories($new_id, "perl") or die $mt->errstr;
			$mt->publishPost($new_id) or die $mt->errstr;
		}
	}
}


sub parseEntry{
	my $filename = $_[0];

	my %text;
	my $mode = '';

	open(IN, "$filename");
	while(<IN>){
		my $line = $_;
		$line =~ s/^\s+//g;

		if($line =~ /$re_title/){
			$text{title} = $1;
		}
		elsif($line =~ /$re_entrydate/){
			$text{date} = "$1-$2-$3T$4:$5:00+0900";
	# ISO-8601 format
	# ex. 2001-08-02T10:45:23+0900
		}
		elsif($line =~ /$re_entrybody/){
			$mode = 'body';
		}
		elsif($line =~ /$re_entrymore/){
			$mode = 'more';
		}
		elsif($line =~ /$re_entryend/){
			$mode = '';
		}
		else{
			if($mode eq 'body'){
				$text{$mode} .= $line;
			}
			elsif($mode eq 'more'){
				$text{$mode} .= $line;
			}
		}
	}
	close(IN);

	return \%text;
}

TrackBack

TrackBack URL for this entry:
http://tenguyasiki.jp/MT/mt-tb.cgi/34

Listed below are links to weblogs that reference データベースを壊してしまってMTを再構築:

» tb+teen from tb+teen
Welcome%21%21%21+tb+teen [Read More]

» caribbean poker on line free from caribbean poker on line free
exponentiation!priest,tolls morose [Read More]

About

This page contains a single entry from the blog posted on 2006年12月21日 17:22.

The previous post in this blog was 誰どこ:フェニックスの『今ホームショップにいる人』を列挙する.

The next post in this blog is WiiでもWikipediaを見たい!.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type 3.31