1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
# Storing revision histories locally in the databse
package MT::Revisable::Local;
use strict;
use base 'MT::ErrorHandler';
sub revision_pkg {
my $driver = shift;
my ($class) = @_;
my $props = $class->properties;
return $props->{revision_pkg} if $props->{revision_pkg};
my $rev = ref $class || $class;
$rev .= '::Revision';
return $props->{revision_pkg} = $rev;
}
sub revision_props {
my $driver = shift;
my ($class) = @_;
my $obj_ds = $class->datasource;
my $obj_id = $obj_ds . '_id';
return {
key => $class->datasource,
column_defs => {
id => 'integer not null auto_increment',
label => 'string(255)',
description => 'string(255)',
$obj_id => 'integer not null',
$obj_ds => 'blob not null',
rev_number => 'integer not null',
changed => 'string(255) not null'
},
indexes => { $obj_id => 1 },
defaults => { rev_number => 0 },
audit => 1,
primary_key => 'id',
datasource => $class->datasource . '_rev'
};
}
sub init_revisioning {
my $driver = shift;
my ($class) = @_;
my $datasource = $class->datasource;
my $subclass = $class->revision_pkg;
return unless $subclass;
my $rev_props = $class->revision_props;
no strict 'refs'; ## no critic
return if defined ${"${subclass}::VERSION"};
## Try to use this subclass first to see if it exists
my $subclass_file = $subclass . '.pm';
$subclass_file =~ s{::}{/}g;
eval "# line " . __LINE__ . " " . __FILE__
. "\nno warnings 'all';require '$subclass_file';$subclass->import();";
if ($@) {
## Die if we get an unexpected error
die $@ unless $@ =~ /Can't locate /;
}
else {
## This class exists. We don't need to do anything.
return 1;
}
my $base_class = 'MT::Object';
my $subclass_src = "
# line " . __LINE__ . " " . __FILE__ . "
package $subclass;
our \$VERSION = 1.0;
use base qw($base_class);
1;
";
## no critic ProhibitStringyEval
eval $subclass_src
or print STDERR "Could not create package $subclass!\n";
$subclass->install_properties($rev_props);
}
sub save_revision {
my $driver = shift;
my ( $obj, $description ) = @_;
my $datasource = $obj->datasource;
my $obj_id = $datasource . '_id';
my $packed_obj = $obj->pack_revision();
my $changed_cols = $obj->{changed_revisioned_cols};
my $current_revision = $obj->current_revision;
require MT::Serialize;
my $rev_class = MT->model( $datasource . ':revision' );
my $revision = $rev_class->new;
$revision->set_values(
{ $obj_id => $obj->id,
$datasource => MT::Serialize->serialize( \$packed_obj ),
changed => join ',',
@$changed_cols,
}
);
$revision->rev_number( ++$current_revision );
$revision->description($description)
if defined($description);
$revision->save or return;
return $current_revision;
}
sub object_from_revision {
my $driver = shift;
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);
# Here we cheat since audit columns aren't revisioned
$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 ];
}
sub load_revision {
my $driver = shift;
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 { $obj->object_from_revision($_); }
$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 = $obj->object_from_revision($rev);
return $array;
}
}
sub handle_max_revisions {
my $driver = shift;
my ( $obj, $max ) = @_;
return unless $max;
my $datasource = $obj->datasource;
my $rev_class = MT->model( $datasource . ':revision' );
my $terms = { $datasource . '_id' => $obj->id };
my $count = $rev_class->count($terms);
if ( $max <= $count ) {
my $rev_iter = $rev_class->load_iter(
$terms,
{ sort => 'created_on',
direction => 'ascend',
limit => $count - $max + 1
}
);
while ( my $rev = $rev_iter->() ) {
$rev->remove;
}
return $max - 1;
}
return $count;
}
sub remove_revisions {
my $driver = shift;
my ($obj) = @_;
my $rpkg = $obj->revision_pkg or return;
my $key = $obj->datasource . '_id';
$rpkg->remove( { $key => $obj->id } );
}
1;
|