Changeset 2829

Show
Ignore:
Timestamp:
07/22/08 21:22:43 (4 months ago)
Author:
arvind
Message:

Bug Fixes:
* Swapped back to MT callbacks (rather than D::OD triggers) to account for any plugins that register pre/post_save callbacks
* Fixed bug with changed_columns to actually store changed_cols
* Validating $diff_method for HTML::Diff
* Revisions are only stored (and current_revision) incremented when a change to a versioned column is made

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/feature-revision-histories/lib/MT/Revisable.pm

    r2826 r2829  
    1515    my ($class) = @_; 
    1616    my $props = $class->properties; 
     17    my $datasource = $class->datasource; 
    1718     
    1819    $props->{column_defs}{current_revision} = { 
     
    2425    $props->{defaults}{current_revision} = 0; 
    2526     
    26     MT->add_callback( 'api_pre_save.entry', 1, undef, 
    27                \&mt_remove_unchanged_cols ); 
    28     MT->add_callback( 'cms_pre_save.entry', 1, undef, 
    29                \&mt_remove_unchanged_cols ); 
     27    # Callbacks: clean list of changed columns to only 
     28    # include versioned columns 
     29    MT->add_callback( 'api_pre_save.' . $datasource, 1, undef, \&mt_presave_obj ); 
     30    MT->add_callback( 'cms_pre_save.' . $datasource, 1, undef, \&mt_presave_obj ); 
    3031                
    31     $class->add_trigger( pre_save => \&increment_revision );            
    32     $class->add_trigger( post_save => \&save_revision ); 
     32    # Callbacks: object-level callbacks could not be  
     33    # prioritized and thus caused problems with plugins 
     34    # registering a post_save and saving      
     35    MT->add_callback( 'api_post_save.' . $datasource, 9, undef, \&mt_postsave_obj); 
     36    MT->add_callback( 'cms_post_save.' . $datasource, 9, undef, \&mt_postsave_obj);                
    3337} 
    3438 
     
    132136} 
    133137 
    134 sub mt_remove_unchanged_cols
     138sub mt_presave_obj
    135139    my ($cb, $app, $obj, $orig) = @_; 
    136     remove_unchanged_cols($obj, $orig); 
    137 
    138  
    139 sub remove_unchanged_cols { 
    140     my ($obj, $orig) = @_; 
    141  
    142     return 1 unless defined $orig; 
    143     return 1 unless $obj->id; 
    144  
     140     
     141    $obj->gather_changed_cols($orig); 
     142     
     143    $obj->increment_revision($orig); # Added here for consistency 
     144
     145 
     146sub mt_postsave_obj { 
     147    my ($cb, $app, $obj, $orig) = @_; 
     148     
     149    $obj->save_revision(); 
     150
     151 
     152sub gather_changed_cols { 
     153    my $obj = shift; 
     154    my ($orig) = @_; 
     155     
     156    my @changed_cols; 
     157    my $revisioned_cols = $obj->revisioned_columns; 
     158     
    145159    my %date_cols = map { $_ => 1 } 
    146160        @{$obj->columns_of_type('datetime', 'timestamp')}; 
    147  
    148     if ( my @changed_cols = $obj->changed_cols ) { 
    149         for my $col ( @changed_cols ) { 
    150             unless($obj->is_revisioned_column($col)) {         
    151                 delete $obj->{changed_cols}->{$col}; 
    152             } 
    153             if ( $obj->$col eq $orig->$col ) { 
    154                 delete $obj->{changed_cols}->{$col}; 
    155             } 
    156             elsif ( exists $date_cols{$col} ) { 
    157                 delete $obj->{changed_cols}->{$col} 
    158                     if $orig->$col eq MT::Object::_db2ts($obj->$col); 
    159             } 
    160         } 
    161     } 
     161     
     162    foreach my $col (@$revisioned_cols) { 
     163        next if $obj->$col eq $orig->$col; 
     164        next if exists $date_cols{$col}  
     165                        && $orig->$col eq MT::Object::_db2ts($obj->$col); 
     166         
     167        push @changed_cols, $col; 
     168    }     
     169     
     170    $obj->{changed_revisioned_cols} = \@changed_cols; 
    162171    1; 
    163172} 
     
    173182 
    174183    my $meta_values = $obj->meta; 
    175     foreach my $key (%$meta_values) { 
     184    foreach my $key (keys %$meta_values) { 
    176185        $values->{$key} = $meta_values->{$key}; 
    177186    } 
     
    190199    my $obj = shift; 
    191200    my ($orig) = @_; 
     201     
     202    my $changed_cols = $obj->{changed_revisioned_cols}; 
     203    return 1 unless scalar @$changed_cols > 0; 
    192204     
    193205    # We default current_revision to 0 so we can always increment 
     
    199211sub save_revision { 
    200212    my $obj = shift; 
    201     my ($orig) = @_; 
    202     return 1 unless $orig->id; 
     213    return 1 unless $obj->id; 
     214     
     215    my $changed_cols = $obj->{changed_revisioned_cols}; 
     216    return 1 unless scalar @$changed_cols > 0; 
    203217     
    204218    my $datasource = $obj->datasource;     
    205219    my $obj_id = $datasource . '_id'; 
    206     my $packed_obj = $orig->pack_revision();  
     220    my $packed_obj = $obj->pack_revision();  
    207221     
    208222    require MT::Serialize; 
     
    210224    my $revision = $rev_class->new; 
    211225    $revision->set_values({ 
    212         $obj_id     => $orig->id, 
     226        $obj_id     => $obj->id, 
    213227        $datasource => MT::Serialize->serialize(\$packed_obj), 
    214         changed     => join ',', $obj->changed_cols 
     228        changed     => join ',', @$changed_cols 
    215229    }); 
    216230    $revision->rev_number($obj->current_revision); 
     
    313327    my $diff_method = $diff_args->{method} || 'html_word_diff'; 
    314328    my $limit_unchanged = $diff_args->{limit_unchanged}; 
    315      
     329 
    316330    require HTML::Diff; 
     331    Carp::croak(MT->translate("Unknown method [_1]", 'HTML::Diff::' . $diff_method)) 
     332        unless HTML::Diff->can($diff_method); 
     333 
    317334    my $diff_result = eval "HTML::Diff::$diff_method(\$str_a, \$str_b)"; 
    318335    my @result;