Created
January 11, 2009 02:13
-
-
Save zaphar/45612 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use DB::CouchDB::Schema; | |
use DBI; | |
my $mysql = DBI->connect("DBI:mysql:database=<databaseName>;host=localhost", '<username>', '<password>'); | |
my $schema = DB::CouchDB::Schema->new(host => 'localhost', db => 'metabase'); | |
my $sth = $mysql->prepare("select post_name, | |
post_title, | |
post_date, | |
post_category, | |
post_content, | |
post_status, | |
ID | |
from wp_posts") | |
or die $mysql->errstr; | |
my $rs = $sth->execute() | |
or die $mysql->errstr; | |
while (my $post = $sth->fetchrow_hashref()) { | |
#now process the post; | |
next if !$post->{post_title}; | |
my $response = $schema->get($post->{post_title}); | |
my $postobj = { | |
_id => $post->{post_name}, | |
Title => $post->{post_title}, | |
Body => $post->{post_content}, | |
Meta => { | |
TimeStamp => $post->{post_date}, # this needs to be an epoch form of post_date | |
UpdateHistory => [{timestamp => time(), comment => 'import from wp'}], | |
Category => $post->{post_category}, | |
tags => [$post->{post_category}] | |
}, | |
Type => 'blog' }; | |
if ($post->{post_status} eq 'draft') { | |
$postobj->{Meta}{draft} = 1; } else { | |
delete $postobj->{Meta}{draft}; | |
} | |
if ($response->err) { | |
#post isnt already there | |
my $r = $schema->create_doc(id => $post->{post_name}, doc => $postobj); | |
} else { | |
if ($response->{Body} ne $post->{post_content}) { | |
my $history = $response->{Meta}{UpdateHistory}; | |
push @$history, {timestamp => time(), 'sync with wp'}; | |
$post->{Meta}{UpdateHistory} = $history; | |
} | |
$postobj->{_id} = $response->{_id}; | |
$postobj->{_rev} = $response->{_rev}; | |
my $r = $schema->db->update_doc($response->{_id}, $post); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment