Skip to content

Instantly share code, notes, and snippets.

@junnama
Created January 22, 2016 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junnama/4feeb23f6fa4e80f2da5 to your computer and use it in GitHub Desktop.
Save junnama/4feeb23f6fa4e80f2da5 to your computer and use it in GitHub Desktop.
Revert to the previous templates.
#!/usr/bin/perl
package MT::Tool::RevertToThePrevious;
use strict;
use warnings;
use File::Spec;
use FindBin;
use lib map File::Spec->catdir( $FindBin::Bin, File::Spec->updir, $_ ),
qw/lib extlib/;
use base qw( MT::Tool );
sub usage { '--debug 1' }
sub help {
return q {
--debug 1
};
}
my ( $debug );
sub options {
return (
'debug=s' => \$debug,
);
}
sub main {
my $class = shift;
my ( $verbose ) = $class->SUPER::main( @_ );
my $iter = MT->model( 'template' )->load_iter( { blog_id => { not => 0 },
type => { not => 'backup' } } );
while (my $template = $iter->()) {
my $current_revision = $template->current_revision;
next unless $current_revision;
$current_revision--;
my $rev_new = _load_revision( $template,
{ rev_number => $current_revision } );
next unless $rev_new;
my $obj_new = $rev_new->[ 0 ];
my $text = $obj_new->text;
if ( $text ) {
$template->text( $text );
$template->save or die $template->errstr;
}
}
1;
}
sub _load_revision {
my ( $obj, $terms, $args ) = @_;
my $datasource = $obj->datasource;
my $rev_class = MT->model( $datasource . ':revision' );
# Only specified a rev_number
if ( defined $terms && ref $terms ne 'HASH' ) {
$terms = { rev_number => $terms };
}
$terms->{ $datasource . '_id' } ||= $obj->id;
if ( wantarray ) {
my @rev = map { _object_from_revision( $obj, $_ ); }
$rev_class->load( $terms, $args );
unless ( @rev ) {
return $obj->error( $rev_class->errstr );
}
return @rev;
} else {
my $rev = $rev_class->load( $terms, $args )
or return $obj->error( $rev_class->errstr );
my $array = _object_from_revision( $obj, $rev );
return $array;
}
}
sub _object_from_revision {
my ( $obj, $rev ) = @_;
my $datasource = $obj->datasource;
my $rev_obj = $obj->clone;
my $serialized_obj = $rev->$datasource;
require MT::Serialize;
my $packed_obj = MT::Serialize->unserialize($serialized_obj);
$rev_obj->unpack_revision($$packed_obj);
$rev_obj->modified_by( $rev->created_by );
$rev_obj->modified_on( $rev->modified_on );
my @changed = split ',', $rev->changed;
return [ $rev_obj, \@changed, $rev->rev_number, $rev ];
}
__PACKAGE__->main() unless caller;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment