#!/usr/bin/perl -w # Copyright 2001-2005 Six Apart. This code cannot be redistributed without # permission from www.movabletype.org. # # $Id: mt-db2sql.cgi 12446 2005-05-25 21:32:39Z bchoate $ use strict; my($MT_DIR); BEGIN { if ($0 =~ m!(.*[/\\])!) { $MT_DIR = $1; } else { $MT_DIR = './'; } unshift @INC, $MT_DIR . 'lib'; unshift @INC, $MT_DIR . 'extlib'; } local $| = 1; print "Content-Type: text/html\n\n"; print "
\n\n";



my @CLASSES = qw( MT::Author MT::Blog MT::Category MT::Comment MT::Entry

                  MT::IPBanList MT::Log MT::Notification MT::Permission

                  MT::Placement MT::PluginData MT::Template MT::TemplateMap

                  MT::Trackback MT::TBPing );



use File::Spec;



eval {

    local $SIG{__WARN__} = sub { print "**** WARNING: $_[0]\n" };



    require MT;

    my $mt = MT->new( Config => $MT_DIR . 'mt.cfg', Directory => $MT_DIR )

        or die MT->errstr;



    require MT::Object;

    my($type) = $mt->{cfg}->ObjectDriver =~ /^DBI::(.*)$/;

    MT::Object->set_driver('DBI::' . $type)

        or die MT::ObjectDriver->errstr;

    my $dbh = MT::Object->driver->{dbh};

    my $schema = File::Spec->catfile($MT_DIR, 'schemas', $type . '.dump');

    open FH, $schema or die "Can't open schema file '$schema': $!";

    my $ddl;

    { local $/; $ddl =  }

    close FH;

    my @stmts = split /;/, $ddl;

    print "Loading database schema...\n\n";

    for my $stmt (@stmts) {

        $stmt =~ s!^\s*!!;

        $stmt =~ s!\s*$!!;

        next unless $stmt =~ /\S/;

        $dbh->do($stmt) or die $dbh->errstr if ($ARGV[0]);

    }



    ## %ids will hold the highest IDs of each class.

    my %ids;



    @CLASSES = (split(/,/, $ARGV[1])) if ($ARGV[1]);



    print "Loading data...\n";

    for my $class (@CLASSES) {

        my ($iter, $total);

        my $count = 0;



        print $class, "\n";

        MT::Object->set_driver('DBM');

        eval "use $class";



        if ($class eq 'MT::Entry') {

          my (%args, %terms);

          $args{'direction'} = 'ascend';

          $args{'limit'} = $ARGV[2];

          $args{'offset'} = $ARGV[3];

          $args{'sort'} = 'created_on';

          $iter = $class->load_iter(\%terms, \%args);

          $total = $class->count(\%terms, \%args);

          if ($args{'offset'}) {

            $count = $args{'offset'} - 1;

            $total += $args{'offset'} - 1;

        } else {

          $iter = $class->load_iter();

          $total = $class->count();

        }



        my %names;

        my %cat_parent;



        MT::Object->set_driver('DBI::' . $type);

        while (my $obj = $iter->()) {

            $count++;

            print "    ".$obj->id.' ('.$count.' of '.$total.')'."\n";

            $ids{$class} = $obj->id

                if !$ids{$class} || $obj->id > $ids{$class};

            ## Look for duplicate template, category, and author names,

            ## because we have uniqueness constraints in the DB.

            if ($class eq 'MT::Template') {

                my $key = lc($obj->name) . $obj->blog_id;

                if ($names{$class}{$key}++) {

                    print "        Found duplicate template name '" .

                          $obj->name;

                    $obj->name($obj->name . ' ' . $names{$class}{$key});

                    print "'; renaming to '" . $obj->name . "'\n";

                }

                ## Touch the text column to make sure we read in

                ## any linked templates.

                my $text = $obj->text;

            } elsif ($class eq 'MT::Author') {

                my $key = lc($obj->name);

                if ($names{$class . $obj->type}{$key}++) {

                    print "        Found duplicate author name '" .

                          $obj->name;

                    $obj->name($obj->name . ' ' . $names{$class.$obj->type}{$key});

                    print "'; renaming to '" . $obj->name . "'\n";

                }

                $obj->email('') unless defined $obj->email;

                $obj->set_password('') unless defined $obj->password;

            } elsif ($class eq 'MT::Category') {

                my $key = lc($obj->label) . $obj->blog_id;

                if ($names{$class}{$key}++) {

                    print "        Found duplicate category label '" .

                          $obj->label;

                    $obj->label($obj->label . ' ' . $names{$class}{$key});

                    print "'; renaming to '" . $obj->label . "'\n";

                }

                # save the parent value for assignment at the end

                if ($obj->parent) {

                    $cat_parent{$obj->id} = $obj->parent;

                    $obj->parent(0);

                }

            } elsif ($class eq 'MT::Trackback') {

                $obj->entry_id(0) unless defined $obj->entry_id;

                $obj->category_id(0) unless defined $obj->category_id;

            } elsif ($class eq 'MT::Entry') {

                $obj->allow_pings(0)

                    if defined $obj->allow_pings && $obj->allow_pings eq '';

                $obj->allow_comments(0)

                    if defined $obj->allow_comments && $obj->allow_comments eq '';

            }

            $obj->save

                or die $obj->errstr;

        }



        # fix up the category parents

        foreach my $id (keys %cat_parent) {

            my $cat = MT::Category->load($id);

            $cat->parent( $cat_parent{$id} );

            $cat->save;

        }



        print "\n";

    }



    if ($type eq 'postgres') {

        print "Updating sequences\n";

        my $dbh = MT::Object->driver->{dbh};

        for my $class (keys %ids) {

            print "    $class => $ids{$class}\n";

            my $seq = 'mt_' . $class->datasource . '_' .

                      $class->properties->{primary_key};

            $dbh->do("select setval('$seq', $ids{$class})")

                or die $dbh->errstr;

        }

    }

};

if ($@) {

    print <\n";