Changeset 831

Show
Ignore:
Timestamp:
12/05/06 09:17:37 (2 years ago)
Author:
fumiakiy
Message:

Implemented backup progress screen. BugID: 28878

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/wheeljack/lib/MT/App/CMS.pm

    r829 r831  
    148148        'backup_restore' => \&backup_restore, 
    149149        'backup' => \&backup, 
     150        'backup_download' => \&backup_download, 
    150151        'restore' => \&restore, 
    151152        'restore_upload_manifest' => \&restore_upload_manifest, 
     
    1081910820} 
    1082010821 
     10822sub _backup_finisher { 
     10823    my $app = shift; 
     10824    my ($fname, $param) = @_; 
     10825    $param->{filename} = $fname; 
     10826    $param->{backup_success} = 1; 
     10827    require MT::Session; 
     10828    my $sess = MT::Session->new; 
     10829    $sess->id($app->make_magic_token()); 
     10830    $sess->kind('BU'); # BU == Backup 
     10831    $sess->name($fname); 
     10832    $sess->start(time); 
     10833    $sess->save; 
     10834    $app->print($app->build_page('backup_end.tmpl', $param)); 
     10835} 
     10836 
    1082110837sub backup { 
    1082210838    my $app = shift; 
     
    1084110857    my $archive = $q->param('backup_archive_format'); 
    1084210858    my $enc = $app->config('PublishCharset') || 'utf-8'; 
    10843     my $file = ''; 
    1084410859    my @ts = gmtime(time); 
    1084510860    my $ts = sprintf "%04d-%02d-%02d-%02d-%02d-%02d", 
    1084610861        $ts[5]+1900, $ts[4]+1, @ts[3,2,1,0]; 
    10847     $file .= "Movable_Type-$ts" . '-Backup'; 
     10862    my $file = "Movable_Type-$ts" . '-Backup'; 
    1084810863 
    1084910864    if ('1' eq $archive) { 
     
    1085910874    } 
    1086010875 
     10876    my $param = {}; 
     10877    $app->{no_print_body} = 1; 
     10878    $app->add_breadcrumb($app->translate('Backup & Restore'), $app->uri(mode => 'backup_restore')); 
     10879    $app->add_breadcrumb($app->translate('Backup')); 
     10880    $param->{system_overview_nav} = 1; 
     10881    $param->{nav_backup} = 1; 
     10882 
     10883    local $| = 1; 
     10884    my $charset = MT::ConfigMgr->instance->PublishCharset; 
     10885    $app->send_http_header('text/html' . 
     10886        ($charset ? "; charset=$charset" : '')); 
     10887    $app->print($app->build_page('backup_start.tmpl', $param)); 
     10888    require File::Temp; 
     10889    require File::Spec; 
     10890    my $temp_dir = $app->config('TempDir'); 
     10891 
    1086110892    require MT::BackupRestore; 
    1086210893    require MT::Asset; 
     
    1086610897    my $finisher; 
    1086710898    my $fh; 
     10899    my $fname; 
    1086810900    my $arc_buf; 
    1086910901    if (!($number || $num_assets)) { 
    10870         $app->{no_print_body} = 1; 
    1087110902        $splitter = sub {}; 
     10903 
    1087210904        if ('0' eq $archive) { 
    10873             $app->set_header("Content-Disposition" => "attachment; filename=$file.xml"); 
    10874             $app->send_http_header($enc ? "text/xml; charset=$enc" 
    10875                                         : 'text/xml'); 
    10876             $printer = sub { $app->print(@_); }; 
    10877             $finisher = sub { my ($asset_files) = @_; 1; }; 
     10905            ($fh, my $filepath) = File::Temp::tempfile('xml.XXXXXXXX', DIR => $temp_dir); 
     10906            (my $vol, my $dir, $fname) = File::Spec->splitpath($filepath); 
     10907            $printer = sub { my ($data, $message) = @_; print $fh $data; $app->print($message); }; 
     10908            $finisher = sub {  
     10909                my ($asset_files) = @_; 
     10910                close $fh; 
     10911                $app->_backup_finisher($fname, $param); 
     10912            }; 
    1087810913        } elsif ('1' eq $archive) { # tar.gz 
    1087910914            require Archive::Tar; 
    1088010915            require Compress::Zlib; 
    10881             $app->set_header("Content-Disposition" => "attachment; filename=$file.tar.gz"); 
    10882             $app->send_http_header('application/x-tar-gz'); 
    10883             $printer = sub { $arc_buf .= $_[0]; }; 
     10916            $printer = sub { my ($data, $message) = @_; $arc_buf .= $data; $app->print($message); }; 
    1088410917            $finisher = sub { 
    1088510918                my ($asset_files) = @_; 
     10919                ($fh, my $filepath) = File::Temp::tempfile('tar.XXXXXXXX', DIR => $temp_dir); 
     10920                (my $vol, my $dir, $fname) = File::Spec->splitpath($filepath); 
    1088610921                my $arc = Archive::Tar->new; 
    1088710922                $arc->add_data("$file.xml", $arc_buf); 
     
    1089110926                require Compress::Zlib; 
    1089210927                my $dest = Compress::Zlib::memGzip($arc->write); 
    10893                 $app->print($dest); 
     10928                binmode $fh; 
     10929                print $fh $dest; 
     10930                close $fh; 
     10931                $app->_backup_finisher($fname, $param); 
    1089410932            } 
    1089510933        } elsif ('2' eq $archive) { # zip 
    1089610934            require Archive::Zip; 
    10897             $app->set_header("Content-Disposition" => "attachment; filename=$file.zip"); 
    10898             $app->send_http_header('application/zip'); 
    10899             $printer = sub { $arc_buf .= $_[0]; }; 
     10935            $printer = sub { my ($data, $message) = @_; $arc_buf .= $data; $app->print($message); }; 
    1090010936            $finisher = sub { 
    1090110937                my ($asset_files) = @_; 
     10938                ($fh, my $filepath) = File::Temp::tempfile('zip.XXXXXXXX', DIR => $temp_dir); 
     10939                (my $vol, my $dir, $fname) = File::Spec->splitpath($filepath); 
    1090210940                my $arc = Archive::Zip->new; 
    1090310941                $arc->addString($arc_buf, "$file.xml"); 
     
    1090510943                    "<manifest xmlns='" . MT::BackupRestore::NS_MOVABLETYPE() . "'><file type='backup' name='$file.xml' /></manifest>",  
    1090610944                    "$file.manifest"); 
    10907                 my $buf; 
    10908                 require IO::String; 
    10909                 my $ios = IO::String->new(\$buf); 
    10910                 $arc->writeToFileHandle($ios); 
    10911                 $app->print($buf); 
     10945                $arc->writeToFileHandle($fh); 
     10946                close $fh; 
     10947                $app->_backup_finisher($fname, $param); 
    1091210948            }; 
    1091310949        } 
     
    1091910955        open $fh, ">$filename"; 
    1092010956        push @files, { filename => $file . "-1.xml" }; 
    10921         $printer = sub { print $fh $_[0]; }; 
     10957        $printer = sub { my ($data, $message) = @_; print $fh $data; $app->print($message); }; 
    1092210958        $splitter = sub { 
    1092310959            my ($findex) = @_; 
     
    1095610992            push @files, { filename => "$file.manifest" }; 
    1095710993            if ('0' eq $archive) { 
    10958                 my $param = {}; 
    10959                 $app->add_breadcrumb($app->translate('Backup & Restore'), $app->uri(mode => 'backup_restore')); 
    10960                 $app->add_breadcrumb($app->translate('Backup')); 
    10961                 $param->{system_overview_nav} = 1; 
    10962                 $param->{nav_backup} = 1; 
    1096310994                $param->{files_loop} = \@files; 
    10964                 $param->{tempdir} = $temp_dir; 
    10965                 $app->build_page('backup_files.tmpl', $param); 
     10995                $app->_backup_finisher($fname, $param); 
    1096610996            } elsif ('1' eq $archive) { # tar.gz 
    1096710997                require Archive::Tar; 
     
    1097811008                    } 
    1097911009                } 
     11010                my ($fh_arc, $filepath) = File::Temp::tempfile('tar.XXXXXXXX', DIR => $temp_dir); 
     11011                (my $vol, my $dir, $fname) = File::Spec->splitpath($filepath); 
    1098011012                require Compress::Zlib; 
    1098111013                my $dest = Compress::Zlib::memGzip($arc->write); 
    10982                 $app->{no_print_body} = 1
    10983                 $app->set_header("Content-Disposition" => "attachment; filename=$file.tar.gz")
    10984                 $app->send_http_header('application/x-tar-gz')
    10985                 $app->print($dest); 
     11014                binmode $fh_arc
     11015                print $fh_arc $dest
     11016                close $fh_arc
     11017                $app->_backup_finisher($fname, $param); 
    1098611018            } elsif ('2' eq $archive) { # zip 
    1098711019                require Archive::Zip; 
    1098811020                my $arc = Archive::Zip->new; 
    10989                 $app->{no_print_body} = 1; 
    10990                 $app->set_header("Content-Disposition" => "attachment; filename=$file.zip"); 
    10991                 $app->send_http_header('application/zip'); 
    1099211021                for my $f (@files) { 
    1099311022                    if (defined $f->{path}) { 
     
    1099811027                    } 
    1099911028                } 
    11000                 my $buf; 
    11001                 require IO::String; 
    11002                 my $ios = IO::String->new(\$buf); 
    11003                 $arc->writeToFileHandle($ios); 
    11004                 for my $f (@files) { 
    11005                     unlink File::Spec->catfile($temp_dir, $f->{filename}) if !defined($f->{path}); 
    11006                 } 
    11007                 $app->print($buf); 
     11029                my ($fh_arc, $filepath) = File::Temp::tempfile('zip.XXXXXXXX', DIR => $temp_dir); 
     11030                (my $vol, my $dir, $fname) = File::Spec->splitpath($filepath); 
     11031                $arc->writeToFileHandle($fh_arc); 
     11032                close $fh_arc; 
     11033                $app->_backup_finisher($fname, $param); 
    1100811034            } 
    1100911035        }; 
     
    1101211038    MT::BackupRestore->backup( 
    1101311039        \@blog_ids, $printer, $splitter, $finisher, $number, $enc); 
     11040} 
     11041 
     11042sub backup_download { 
     11043    my $app = shift; 
     11044    my $user = $app->user; 
     11045    return $app->errtrans("Permission denied.") if !$user->is_superuser; 
     11046    $app->validate_magic() or return; 
     11047    my $filename = $app->param('filename'); 
     11048    my $temp_dir = $app->config('TempDir'); 
     11049    require File::Spec; 
     11050    my $fname = File::Spec->catfile($temp_dir, $filename); 
     11051     
     11052    my $sess = MT::Session->load( { kind => 'BU', name => $filename } ); 
     11053    if (!defined($sess) || !$sess) { 
     11054        return $app->errtrans("Specified file was not found."); 
     11055    } 
     11056    my @ts = gmtime($sess->start); 
     11057    my $ts = sprintf "%04d-%02d-%02d-%02d-%02d-%02d", 
     11058        $ts[5]+1900, $ts[4]+1, @ts[3,2,1,0]; 
     11059    my $newfilename = "Movable_Type-$ts" . '-Backup'; 
     11060    $sess->remove; 
     11061 
     11062    $app->{no_print_body} = 1; 
     11063    my $contenttype; 
     11064    if ($filename =~ m/^xml\..+$/i) { 
     11065        my $enc = $app->config('PublishCharset') || 'utf-8'; 
     11066        $contenttype = "text/xml; charset=$enc"; 
     11067        $newfilename .= '.xml'; 
     11068    } elsif ($filename =~ m/^tar\..+$/i) { 
     11069        $contenttype = 'application/x-tar-gz'; 
     11070        $newfilename .= '.tar.gz'; 
     11071    } elsif ($filename =~ m/^zip\..+$/i) { 
     11072        $contenttype = 'application/zip'; 
     11073        $newfilename .= '.zip'; 
     11074    } else { 
     11075        $app->send_http_header('application/octet-stream'); 
     11076    } 
     11077 
     11078    $app->set_header("Content-Disposition" => "attachment; filename=$newfilename"); 
     11079    $app->send_http_header($contenttype); 
     11080     
     11081    open my $fh, "<$fname"; 
     11082    my $data; 
     11083    while (read $fh, my($chunk), 8192) { 
     11084        $data .= $chunk; 
     11085    } 
     11086    close $fh; 
     11087    $app->print($data); 
     11088    unlink $fname; 
    1101411089} 
    1101511090 
     
    1107311148            } else { 
    1107411149                my $temp_dir = $app->config('TempDir'); 
    11075                 require File::Temp;; 
     11150                require File::Temp; 
    1107611151                my $tmp = File::Temp::tempdir($uploaded_filename . 'XXXX', DIR => $temp_dir, CLEANUP => 1); 
    1107711152                my $buffer; 
  • branches/wheeljack/lib/MT/BackupRestore.pm

    r823 r831  
    7272    my $header .= "<movabletype xmlns='" . NS_MOVABLETYPE . "'>\n"; 
    7373    $header = "<?xml version='1.0' encoding='$enc'?>\n$header" if $enc !~ m/utf-?8/i; 
    74     $printer->($header); 
     74    $printer->($header, q()); 
    7575 
    7676    my $files = {}; 
     
    7979 
    8080    my $else_xml = MT->run_callbacks('Backup.else'); 
    81     $printer->($else_xml) if $else_xml ne '1'; 
    82  
    83     $printer->('</movabletype>'); 
     81    $printer->($else_xml, q()) if $else_xml ne '1'; 
     82 
     83    $printer->('</movabletype>', q()); 
    8484    $finisher->($files); 
    8585} 
     
    9999        my $err = $@; 
    100100        if ($err) { 
    101             $printer->("$err\n"); 
     101            $printer->("$err\n", "$err\n"); 
    102102            next; 
    103103        } 
     
    117117                    $splitter->(int($counter / $number + 1)); 
    118118                } 
    119                 $printer->($object->to_xml(undef, $args) . "\n") if $object->to_backup; 
     119                $printer->($object->to_xml(undef, $args) . "\n",  
     120                    MT->translate('[_1]#[_2] has been backup.', $class, $object->id) . "\n") 
     121                        if $object->to_backup; 
    120122                if ($class eq 'MT::Author') { 
    121123                    # MT::Author may be duplicated because of how terms and args are created.