#!/usr/bin/perl

use DBI;
use Data::Dumper;
use Time::ParseDate;
use POSIX;
use strict;

# Source DB
my $sdb = DBI->connect("dbi:Pg:dbname=$ARGV[0]") or die;
$sdb->do("SET client_encoding=\'UTF8\'") or die;
#my $sdb = DBI->connect("dbi:mysql:dbname=$ARGV[0]") or die;
#$sdb->do("SET names \'utf8\'"); #character_set_client = utf8");

# Destination DB
my $tdb = DBI->connect("dbi:Pg:dbname=$ARGV[1]") or die;
$tdb->do("SET client_encoding=\'UTF8\'") or die;
#my $tdb = DBI->connect("dbi:mysql:dbname=$ARGV[1]") or die;
#$tdb->do("SET names \'utf8\'"); #character_set_client = utf8");

my $blogid= $ARGV[2] || 1;

# 
# tagid to tag name mapping
my $tagid2tag = $sdb->selectall_hashref("select tag_id,tag_name from mt_tag",'tag_id');
my $catid2cat = $sdb->selectall_hashref("select category_id,category_label,category_basename from mt_category where category_blog_id=?",'category_id',undef,$blogid);

$tdb->do("drop table mt_posts");
$tdb->do(<<EOM);
create table mt_posts
(
        id integer not null primary key,
        title text,
        body text,
        tags text,
        categories text,
        created integer,
        modified integer,
        url text,
        status smallint,
        format smallint
)
EOM
$tdb->do("grant all on mt_posts to public");

my $elist = $sdb->selectall_hashref("select entry_id,entry_allow_comments,entry_basename,entry_convert_breaks,entry_created_on,entry_modified_on,entry_status,entry_text,entry_text_more,entry_title from mt_entry where entry_blog_id=? order by entry_id asc",'entry_id',undef,$blogid);

my $ins = $tdb->prepare("insert into mt_posts (id,title,body,tags,categories,created,modified,url,status,format) values(?,?,?,?,?,?,?,?,?,?)");


$|=1;

for my $id(sort keys %$elist)
  {
    my $item = $elist->{$id};
    $item->{tags} = lookup_tags($id);
    $item->{categories} = lookup_categories($id);
    $item->{created} = parsedate($item->{entry_created_on});
    $item->{modified} = parsedate($item->{entry_modified_on});
    $item->{body}=$item->{entry_text}."\r\n<!--break-->".$item->{entry_text_more};
    $item->{status} = $item->{entry_status}-1;
    $item->{format} = ($item->{convert_breaks} eq 'richtext')?1:3;
    $item->{url} = POSIX::strftime("%Y/%m/%d/",localtime $item->{created}).$item->{entry_basename}.".html";
    $ins->execute($id,$item->{entry_title},$item->{body},$item->{tags},$item->{categories},$item->{created},$item->{modified},$item->{url},$item->{status},$item->{format});
    print "$id ";

  }
#print Dumper $elist;

$sdb->disconnect;
$tdb->disconnect;



sub lookup_tags
  {
    my $id = shift or return undef;
    my $taglist = $sdb->selectall_hashref("select objecttag_tag_id from mt_objecttag where objecttag_object_id=? and objecttag_object_datasource=\'entry\'",'objecttag_tag_id',undef,$id);
    my @l;
    foreach my $t(keys %$taglist)
      {
        push @l,$tagid2tag->{$t}->{tag_name};
      }
    return join(",",@l);
  }
sub lookup_categories
  {
    my $id = shift or return undef;
    my $taglist = $sdb->selectall_hashref("select placement_category_id from mt_placement where placement_entry_id=?",'placement_category_id',undef,$id);
    my @l;
    foreach my $t(keys %$taglist)
      {
        push @l,$catid2cat->{$t}->{category_label};
      }
    return join(",",@l);
  }

