Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fumiz/3158806 to your computer and use it in GitHub Desktop.
Save fumiz/3158806 to your computer and use it in GitHub Desktop.
adiaryからWordPressへの移行準備ツール
# 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;
@fumiz
Copy link
Author

fumiz commented Jul 22, 2012

ここでのadiaryはこれ: http://adiary.org/

@fumiz
Copy link
Author

fumiz commented Jul 22, 2012

マッチング方法、WordPress -> adiaryじゃなくてadiary -> WordPressの方が良かった

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment