Changeset 11

Show
Ignore:
Timestamp:
04/06/05 19:20:15 (3 years ago)
Author:
ogawa
Message:

specify the derived version of mt-db2sql.cgi

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • mt-db-convert/trunk/mt-db-convert.cgi

    r10 r11  
    11#!/usr/bin/perl -w 
    2 # This code cannot be redistributed without permissions. 
     2# mt-db-convert.cgi: converting your MT data between multiple db engines 
     3# This is a derived work from the following: 
    34# 
    4 # mt-db-convert.cgi: converting your MT data between multiple data storage 
    5  
     5# Copyright 2001-2004 Six Apart. This code cannot be redistributed without 
     6# permission from www.movabletype.org. 
     7
     8# $Id: mt-db2sql.cgi,v 1.18 2004/09/29 21:33:03 ezra Exp $ 
    69use strict; 
    7 local $| = 1; 
    810 
    911my($MT_DIR); 
    1012BEGIN { 
    11   if ($0 =~ m!(.*[/\\])!) { 
    12     $MT_DIR = $1; 
    13   } else { 
    14     $MT_DIR = './'; 
    15   } 
    16   unshift @INC, $MT_DIR . 'lib'; 
    17   unshift @INC, $MT_DIR . 'extlib'; 
    18 
    19  
    20 use CGI; 
    21  
     13    if ($0 =~ m!(.*[/\\])!) { 
     14        $MT_DIR = $1; 
     15    } else { 
     16        $MT_DIR = './'; 
     17    } 
     18    unshift @INC, $MT_DIR . 'lib'; 
     19    unshift @INC, $MT_DIR . 'extlib'; 
     20
     21 
     22local $| = 1; 
    2223print "Content-Type: text/html\n\n"; 
    2324print <<HTML; 
     
    2526<head><title>MT-DB-CONVERT: Converting your MT data</title></head> 
    2627<body> 
    27 <p><strong>MT-DB-CONVERT: Coverting your MT data</strong></p> 
    28  
     28<p><strong>MT-DB-CONVERT: Coverting your MT data between multiple db engines</strong></p> 
    2929HTML 
    3030 
     
    3434                  MT::TBPing ); 
    3535 
     36use File::Spec; 
     37 
    3638my @DBSPECS = qw( DataSource ObjectDriver Database DBUser DBHost DBPassword ); 
    3739 
    3840require MT; 
    3941my $mt = MT->new(Config => $MT_DIR . 'mt.cfg', Directory => $MT_DIR) 
    40   or die MT->errstr; 
     42    or die MT->errstr; 
    4143my $cfg = $mt->{cfg}; 
    4244 
     45use CGI; 
    4346my $q = CGI->new; 
    4447 
    45 my %src; 
    46 my %dest; 
     48my (%src, %dst); 
    4749 
    4850for my $dbspec (@DBSPECS) { 
    4951  $src{$dbspec} = $q->param('src_' . $dbspec) || ''; 
    50   $dest{$dbspec} = $q->param('dest_' . $dbspec) || ''; 
    51 
    52  
     52  $dst{$dbspec} = $q->param('dst_' . $dbspec) || ''; 
     53
     54 
     55# If src and dst dbspecs not given 
    5356if (!$src{ObjectDriver} || ($src{ObjectDriver} eq 'DBM' && $src{DataSource}) || 
    54     !$dest{ObjectDriver} || ($dest{ObjectDriver} eq 'DBM' && $dest{DataSource})) { 
    55  
    56   for my $dbspec (@DBSPECS) { 
    57     $src{$dbspec} ||= $cfg->$dbspec(); 
    58   } 
    59  
    60   my %src_selected = ('DBM' => '', 'DBI::mysql' => '', 'DBI::postgres' => '', 'DBI::sqlite' => ''); 
    61   my %dest_selected = ('DBM' => '', 'DBI::mysql' => '', 'DBI::postgres' => '', 'DBI::sqlite' => ''); 
    62   $src_selected{$src{ObjectDriver}} = 'selected' if $src{ObjectDriver}; 
    63   $dest_selected{$dest{ObjectDriver}} = 'selected' if $dest{ObjectDriver}; 
    64   print <<HTML; 
     57    !$dst{ObjectDriver} || ($dst{ObjectDriver} eq 'DBM' && $dst{DataSource})) { 
     58 
     59    for my $dbspec (@DBSPECS) { 
     60        $src{$dbspec} ||= $cfg->$dbspec(); 
     61    } 
     62 
     63    my %src_selected = ('DBM' => '', 'DBI::mysql' => '', 'DBI::postgres' => '', 'DBI::sqlite' => ''); 
     64    my %dst_selected = ('DBM' => '', 'DBI::mysql' => '', 'DBI::postgres' => '', 'DBI::sqlite' => ''); 
     65    $src_selected{$src{ObjectDriver}} = 'selected' if $src{ObjectDriver}; 
     66    $dst_selected{$dst{ObjectDriver}} = 'selected' if $dst{ObjectDriver}; 
     67 
     68    print <<HTML; 
    6569<p>Please fill the following:</p> 
    6670 
     
    9498<dl> 
    9599<dt>DataSource: (mandatory if using BerkeleyDB)</dt> 
    96 <dd><input name="dest_DataSource" type="text" value="$dest{DataSource}" /></dd> 
     100<dd><input name="dst_DataSource" type="text" value="$dst{DataSource}" /></dd> 
    97101<dt>ObjectDriver:</dt> 
    98 <dd><select name="dest_ObjectDriver"> 
     102<dd><select name="dst_ObjectDriver"> 
    99103<option value="">Select a driver</option> 
    100 <option value="DBM" $dest_selected{'DBM'}>BerkeleyDB</option> 
    101 <option value="DBI::mysql" $dest_selected{'DBI::mysql'}>MySQL</option> 
    102 <option value="DBI::postgres" $dest_selected{'DBI::postgres'}>PostgreSQL</option> 
    103 <option value="DBI::sqlite" $dest_selected{'DBI::sqlite'}>SQLite</option> 
     104<option value="DBM" $dst_selected{'DBM'}>BerkeleyDB</option> 
     105<option value="DBI::mysql" $dst_selected{'DBI::mysql'}>MySQL</option> 
     106<option value="DBI::postgres" $dst_selected{'DBI::postgres'}>PostgreSQL</option> 
     107<option value="DBI::sqlite" $dst_selected{'DBI::sqlite'}>SQLite</option> 
    104108</select></dd> 
    105109<dt>Database:</dt> 
    106 <dd><input name="dest_Database" type="text" value="$dest{Database}" /></dd> 
     110<dd><input name="dst_Database" type="text" value="$dst{Database}" /></dd> 
    107111<dt>DBUser:</dt> 
    108 <dd><input name="dest_DBUser" type="text" value="$dest{DBUser}" /></dd> 
     112<dd><input name="dst_DBUser" type="text" value="$dst{DBUser}" /></dd> 
    109113<dt>DBHost:</dt> 
    110 <dd><input name="dest_DBHost" type="text" value="$dest{DBHost}" /></dd> 
     114<dd><input name="dst_DBHost" type="text" value="$dst{DBHost}" /></dd> 
    111115<dt>DBPassword:</dt> 
    112 <dd><input name="dest_DBPassword" type="password" value="$dest{DBPassword}" /></dd> 
     116<dd><input name="dst_DBPassword" type="password" value="$dst{DBPassword}" /></dd> 
    113117</dl> 
    114118</fieldset> 
     
    121125} else { 
    122126 
    123   eval { 
     127eval { 
    124128    local $SIG{__WARN__} = sub { print "**** WARNING: $_[0]\n" }; 
    125129 
    126130    require MT::Object; 
    127  
    128     # Preload schema to the dest DB 
    129     my ($type) = $dest{ObjectDriver} =~ /^DBI::(.*)$/; 
     131    my ($type) = $dst{ObjectDriver} =~ /^DBI::(.*)$/; 
    130132    if ($type) { 
    131       # set dest driver 
    132       for my $dbspec (@DBSPECS) { 
    133         $cfg->set($dbspec, $dest{$dbspec}); 
    134       } 
    135       MT::Object->set_driver($dest{ObjectDriver}) 
    136           or die MT::ObjectDriver->errstr; 
    137       my $dbh = MT::Object->driver->{dbh}; 
    138       my $schema = File::Spec->catfile($MT_DIR, 'schemas', $type . '.dump'); 
    139       open FH, $schema or die "Can't open schema file '$schema': $!"; 
    140       my $ddl; 
    141       { local $/; $ddl = <FH> } 
    142       close FH; 
    143       my @stmts = split /;/, $ddl; 
    144       print "Loading database schema...\n\n"; 
    145       for my $stmt (@stmts) { 
     133    # set dst driver 
     134    for my $dbspec (@DBSPECS) { 
     135        $cfg->set($dbspec, $dst{$dbspec}); 
     136    } 
     137    MT::Object->set_driver($dst{ObjectDriver}) 
     138        or die MT::ObjectDriver->errstr; 
     139    my $dbh = MT::Object->driver->{dbh}; 
     140    $dbh->begin_work if $type eq 'sqlite'; 
     141    my $schema = File::Spec->catfile($MT_DIR, 'schemas', $type . '.dump'); 
     142    open FH, $schema or die "Can't open schema file '$schema': $!"; 
     143    my $ddl; 
     144    { local $/; $ddl = <FH> } 
     145    close FH; 
     146    my @stmts = split /;/, $ddl; 
     147    print "<p>Loading database schema...</p>\n\n"; 
     148    for my $stmt (@stmts) { 
    146149        $stmt =~ s!^\s*!!; 
    147150        $stmt =~ s!\s*$!!; 
    148151        next unless $stmt =~ /\S/; 
    149152        $dbh->do($stmt) or die $dbh->errstr; 
    150       } 
     153    } 
     154    $dbh->commit if $type eq 'sqlite'; 
    151155    } 
    152156 
     
    155159 
    156160    for my $class (@CLASSES) { 
    157       print "<p>Dumping $class:<br />\n"; 
    158  
    159       # set source driver 
    160       for my $dbspec (@DBSPECS) { 
    161         $cfg->set($dbspec, $src{$dbspec}); 
    162       } 
    163       MT::Object->set_driver($src{ObjectDriver}); 
    164  
    165       eval "use $class"; 
    166       my $iter = $class->load_iter; 
    167  
    168       my %names; 
    169  
    170       # set dest driver 
    171       for my $dbspec (@DBSPECS) { 
    172         $cfg->set($dbspec, $dest{$dbspec}); 
    173       } 
    174       MT::Object->set_driver($dest{ObjectDriver}); 
    175  
    176       my $i = 0; 
    177       while (my $o = $iter->()) { 
    178         $ids{$class} = $o->id 
    179           if !$ids{$class} || $o->id > $ids{$class}; 
    180         ## Look for duplicate template, category, and author names, 
    181         ## because we have uniqueness constraints in the DB. 
    182         if ($class eq 'MT::Template') { 
    183           my $key = lc($o->name) . $o->blog_id; 
    184           if ($names{$class}{$key}++) { 
    185             print "        Found duplicate template name '" . 
    186               $o->name; 
    187             $o->name($o->name . ' ' . $names{$class}{$key}); 
    188             print "'; renaming to '" . $o->name . "'\n"; 
    189           } 
    190           ## Touch the text column to make sure we read in 
    191           ## any linked templates. 
    192           my $text = $o->text; 
    193         } elsif ($class eq 'MT::Author') { 
    194           my $key = lc($o->name); 
    195           if ($names{$class . $o->type}{$key}++) { 
    196             print "        Found duplicate author name '" . 
    197               $o->name; 
    198             $o->name($o->name . ' ' . $names{$class}{$key}); 
    199             print "'; renaming to '" . $o->name . "'\n"; 
    200           } 
    201           $o->email('') unless defined $o->email; 
    202           $o->set_password('') unless defined $o->password; 
    203         } elsif ($class eq 'MT::Category') { 
    204           my $key = lc($o->label) . $o->blog_id; 
    205           if ($names{$class}{$key}++) { 
    206             print "        Found duplicate category label '" . 
    207               $o->label; 
    208             $o->label($o->label . ' ' . $names{$class}{$key}); 
    209             print "'; renaming to '" . $o->label . "'\n"; 
    210           } 
    211         } elsif ($class eq 'MT::Trackback') { 
    212           $o->entry_id(0) unless defined $o->entry_id; 
    213           $o->category_id(0) unless defined $o->category_id; 
    214         } elsif ($class eq 'MT::Entry') { 
    215           $o->allow_pings(0) 
    216             if defined $o->allow_pings && $o->allow_pings eq ''; 
    217           $o->allow_comments(0) 
    218             if defined $o->allow_comments && $o->allow_comments eq ''; 
     161        print "<p>Dumping $class:<br />\n"; 
     162 
     163        # set source driver 
     164        for my $dbspec (@DBSPECS) { 
     165            $cfg->set($dbspec, $src{$dbspec}); 
    219166        } 
    220  
    221         $i++; 
    222         print $o->save ? "." : "!<br />" . $o->errstr; 
    223         $i % 10 or print " "; 
    224         $i % 100 or print "<br />"; 
    225       } 
    226       print "</p>\n\n"; 
     167        MT::Object->set_driver($src{ObjectDriver}); 
     168 
     169        eval "use $class"; 
     170        my $iter = $class->load_iter; 
     171 
     172        my %names; 
     173 
     174        # set dst driver 
     175        for my $dbspec (@DBSPECS) { 
     176            $cfg->set($dbspec, $dst{$dbspec}); 
     177        } 
     178        MT::Object->set_driver($dst{ObjectDriver}); 
     179        MT::Object->driver->{dbh}->begin_work if $type eq 'sqlite'; 
     180 
     181        my $i = 0; 
     182        while (my $obj = $iter->()) { 
     183            $ids{$class} = $obj->id 
     184                if !$ids{$class} || $obj->id > $ids{$class}; 
     185            ## Look for duplicate template, category, and author names, 
     186            ## because we have uniqueness constraints in the DB. 
     187            if ($class eq 'MT::Template') { 
     188                my $key = lc($obj->name) . $obj->blog_id; 
     189                if ($names{$class}{$key}++) { 
     190                    print "        Found duplicate template name '" . 
     191                          $obj->name; 
     192                    $obj->name($obj->name . ' ' . $names{$class}{$key}); 
     193                    print "'; renaming to '" . $obj->name . "'\n"; 
     194                } 
     195                ## Touch the text column to make sure we read in 
     196                ## any linked templates. 
     197                my $text = $obj->text; 
     198            } elsif ($class eq 'MT::Author') { 
     199                my $key = lc($obj->name); 
     200                if ($names{$class . $obj->type}{$key}++) { 
     201                    print "        Found duplicate author name '" . 
     202                          $obj->name; 
     203                    $obj->name($obj->name . ' ' . $names{$class}{$key}); 
     204                    print "'; renaming to '" . $obj->name . "'\n"; 
     205                } 
     206                $obj->email('') unless defined $obj->email; 
     207                $obj->set_password('') unless defined $obj->password; 
     208            } elsif ($class eq 'MT::Category') { 
     209                my $key = lc($obj->label) . $obj->blog_id; 
     210                if ($names{$class}{$key}++) { 
     211                    print "        Found duplicate category label '" . 
     212                          $obj->label; 
     213                    $obj->label($obj->label . ' ' . $names{$class}{$key}); 
     214                    print "'; renaming to '" . $obj->label . "'\n"; 
     215                } 
     216            } elsif ($class eq 'MT::Trackback') { 
     217                $obj->entry_id(0) unless defined $obj->entry_id; 
     218                $obj->category_id(0) unless defined $obj->category_id; 
     219            } elsif ($class eq 'MT::Entry') { 
     220                $obj->allow_pings(0) 
     221                    if defined $obj->allow_pings && $obj->allow_pings eq ''; 
     222                $obj->allow_comments(0) 
     223                    if defined $obj->allow_comments && $obj->allow_comments eq ''; 
     224            } 
     225 
     226            $i++; 
     227            $obj->save 
     228                or die $obj->errstr; 
     229            print "."; 
     230            $i % 10 or print " "; 
     231            $i % 100 or print "<br />\n"; 
     232        } 
     233        print "</p>\n\n"; 
     234        MT::Object->driver->{dbh}->commit if $type eq 'sqlite'; 
    227235    } 
    228236 
     
    238246        } 
    239247    } 
    240  
    241   }; 
    242  
    243   if ($@) { 
     248}; 
     249if ($@) { 
    244250    print <<HTML; 
    245 <p><strong>An error occurred while loading data: $@</strong></p> 
     251<p>An error occurred while loading data: <br /> 
     252$@</p> 
     253HTML 
     254} else { 
     255    print <<HTML; 
     256<p>Done copying data from $src{ObjectDriver} to $dst{ObjectDriver}! All went well.</p> 
     257HTML 
     258
     259 
     260
     261 
     262print <<HTML; 
    246263</body> 
    247264</html> 
    248265HTML 
    249   } else { 
    250     print <<HTML; 
    251 <p><strong>Done copying data from $src{ObjectDriver} to $dest{ObjectDriver}.</strong></p> 
    252 </body> 
    253 </html> 
    254 HTML 
    255   } 
    256 }