Created
July 22, 2012 07:46
-
-
Save fumiz/3158806 to your computer and use it in GitHub Desktop.
adiaryからWordPressへの移行準備ツール
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# adiary形式でエクスポートしたadiary.xmlと | |
# WordPress標準のエクスポート機能でエクスポートしたwordpress.xmlを用い | |
# 旧adiaryから新WordPressへ301リダイレクトする.htaccessのルールを作成する | |
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use XML::TreePP; | |
use constant { | |
ADIARY_PATH => 'adiary.xml', | |
WORDPRESS_PATH => 'wordpress.xml' | |
}; | |
my $adiary = adiary_xml_to_title_hash(load_xml(ADIARY_PATH)); | |
my $wp = wordpress_xml_to_title_hash(load_xml(WORDPRESS_PATH)); | |
my @result_maps; | |
for my $title (keys %{$wp}) { | |
unless (exists $adiary->{$title}) { | |
warn 'skip entry: ' . $title; | |
next; | |
} | |
my $wp_item = $wp->{$title}; | |
my $adiary_item = $adiary->{$title}; | |
push(@result_maps, {link_id=>$adiary_item->{adiary_id}, link=>$wp_item->{wp_link}}); | |
} | |
print join("\n", map {'Redirect permanent /diary/' . $_->{link_id} . ' ' . $_->{link}} @result_maps); | |
# adiaryのXMLデータをタイトルをキーにしたハッシュに変換 | |
sub adiary_xml_to_title_hash { | |
my $xml_hash = shift; | |
my $items = {}; | |
for my $item (@{$xml_hash->{diary}->{day}}) { | |
$items->{$item->{-title}} = { | |
adiary_id => $item->{attributes}->{-link_key} | |
}; | |
} | |
return $items; | |
} | |
# WordPressのXMLデータをタイトルをキーにしたハッシュに変換 | |
sub wordpress_xml_to_title_hash { | |
my $xml_hash = shift; | |
my $items = {}; | |
for my $item (@{$xml_hash->{rss}->{channel}->{item}}) { | |
$items->{$item->{title}} = { | |
wp_link => $item->{link} | |
}; | |
} | |
return $items; | |
} | |
sub load_xml { | |
my $path = shift; | |
my $tpp = XML::TreePP->new(); | |
return $tpp->parsefile($path); | |
} | |
exit; |
マッチング方法、WordPress -> adiaryじゃなくてadiary -> WordPressの方が良かった
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ここでのadiaryはこれ: http://adiary.org/