package CGI::Wiki::Plugin::Diff; use strict; use warnings; our $VERSION = '0.09'; use base 'CGI::Wiki::Plugin'; use Algorithm::Diff; use VCS::Lite; use Params::Validate::Dummy (); use Module::Optional qw(Params::Validate validate validate_pos SCALAR SCALARREF ARRAYREF HASHREF UNDEF); sub new { my $class = shift; my %par = validate( @_, { metadata_separator => { type => SCALAR, default => "
\n"} , line_number_format => { type => SCALAR, default => "== Line \$_ ==\n" }, word_matcher => { type => SCALARREF, default => qr( &.+?; #HTML special characters e.g. < | #Line breaks |\w+\s* #Word with trailing spaces |. #Any other single character )xsi }, } ); bless \%par, $class; } sub differences { my $self = shift; my %args = validate( @_, { node => { type => SCALAR}, left_version => { type => SCALAR}, right_version => { type => SCALAR}, meta_include => { type => ARRAYREF, optional => 1 }, meta_exclude => { type => ARRAYREF, optional => 1 } }); my ($node, $v1, $v2) = @args{ qw( node left_version right_version) }; my $store = $self->datastore; my $fmt = $self->formatter; my %ver1 = $store->retrieve_node( name => $node, version => $v1); my %ver2 = $store->retrieve_node( name => $node, version => $v2); my $verstring1 = "Version ".$ver1{version}; my $verstring2 = "Version ".$ver2{version}; my $el1 = VCS::Lite->new($verstring1,undef, $self->content_escape($ver1{content}). $self->{metadata_separator}. $self->serialise_metadata($ver1{metadata}, @args{qw(meta_include meta_exclude)})); my $el2 = VCS::Lite->new($verstring2,undef, $self->content_escape($ver2{content}). $self->{metadata_separator}. $self->serialise_metadata($ver2{metadata}, @args{qw(meta_include meta_exclude)})); my %pag = %ver1; $pag{left_version} = $verstring1; $pag{right_version} = $verstring2; $pag{content} = $fmt->format($ver1{content}); my $dlt = $el1->delta($el2) or return %pag; my @out; for ($dlt->hunks) { my ($lin1,$lin2,$out1,$out2); for (@$_) { my ($ind,$line,$text) = @$_; if ($ind ne '+') { $lin1 ||= $line; $out1 .= $text; } if ($ind ne '-') { $lin2 ||= $line; $out2 .= $text; } } push @out,{ left => $self->line_number($lin1), right => $self->line_number($lin2) }; my ($text1,$text2) = $self->intradiff($out1,$out2); push @out,{left => $text1, right => $text2}; } $pag{diff} = \@out; %pag; } sub line_number { my $self = shift; local ($_) = validate_pos(@_, {type => SCALAR | UNDEF, optional => 1} ); return '' unless defined $_; my $fmt = '"'. $self->{line_number_format} . '"'; eval $fmt; } sub serialise_metadata { my $self = shift; my ($all_meta,$include,$exclude) = validate_pos ( @_, { type => HASHREF }, { type => ARRAYREF | UNDEF, optional => 1 }, { type => ARRAYREF | UNDEF, optional => 1 }, ); $include ||= [keys %$all_meta]; $exclude ||= [qw(comment username __categories__checksum __locales__checksum)] ; my %metadata = map {$_,$all_meta->{$_}} @$include; delete $metadata{$_} for @$exclude; join $self->{metadata_separator}, map {"$_='".join (',',sort @{$metadata{$_}})."'"} sort keys %metadata; } sub content_escape { my $self = shift; my ($str) = validate_pos( @_, { type => SCALAR } ); $str =~ s/&/&/g; $str =~ s//>/g; $str =~ s!\s*?\n!
\n!gs; $str; } sub intradiff { my $self = shift; my ($str1,$str2) = validate_pos( @_, {type => SCALAR|UNDEF }, {type => SCALAR|UNDEF }); return (qq{$str1},"") unless $str2; return ("",qq{$str2}) unless $str1; my $re_wordmatcher = $self->{word_matcher}; my @diffs = Algorithm::Diff::sdiff([$str1 =~ /$re_wordmatcher/sg] ,[$str2 =~ /$re_wordmatcher/sg], sub {$self->get_token(@_)}); my $out1 = ''; my $out2 = ''; my ($mode1,$mode2); for (@diffs) { my ($ind,$c1,$c2) = @$_; my $newmode1 = $ind =~ /[c\-]/; my $newmode2 = $ind =~ /[c+]/; $out1 .= '' if $newmode1 && !$mode1; $out2 .= '' if $newmode2 && !$mode2; $out1 .= '' if !$newmode1 && $mode1; $out2 .= '' if !$newmode2 && $mode2; ($mode1,$mode2) = ($newmode1,$newmode2); $out1 .= $c1; $out2 .= $c2; } $out1 .= '' if $mode1; $out2 .= '' if $mode2; ($out1,$out2); } sub get_token { my ($self,$str) = @_; $str =~ /^(\S*)\s*$/; # Match all but trailing whitespace $1 || $str; } 1; __END__ =head1 NAME CGI::Wiki::Plugin::Diff - format differences between two CGI::Wiki pages =head1 SYNOPSIS use CGI::Wiki::Plugin::Diff; my $plugin = CGI::Wiki::Plugin::Diff->new; $wiki->register_plugin( plugin => $plugin ); # called before any node reads my %diff = $plugin->differences( node => 'Imperial College', left_version => 3, right_version => 5); =head1 DESCRIPTION A plug-in for CGI::Wiki sites, which provides a nice extract of differences between two versions of a node. =head1 BASIC USAGE B my %diff_vars = $plugin->differences( node => "Home Page", left_version => 3, right_version => 5 ); Takes a series of key/value pairs: =over 4 =item * B The node version whose content we're considering canonical. =item * B The node version that we're showing the differences from. =item * B Filter the list of metadata fields to only include a certain list in the diff output. The default is to include all metadata fields. =item * B Filter the list of metadata fields to exclude certain fields from the diff output. The default is the following list, to match previous version (OpenGuides) behaviour: C Agreed this list is hopelessly inadequate, especially for L. Hopefully, future wiki designers will use the meta_include parameter to specify exactly what metadata they want to appear on the diff. =back The differences method returns a list of key/value pairs, which can be assigned to a hash: =over 4 =item B The node version whose content we're considering canonical. =item B The node version that we're showing the differences from. =item B The (formatted) contents of the I version of the node. =item B An array of hashrefs of C of differences between the versions. It is assumed that the display will be rendered in HTML, and SPAN tags are inserted with a class of diff1 or diff2, to highlight which individual words have actually changed. Display the contents of diff using a EtableE, with each member of the array corresponding to a row ETRE, and keys {left} and {right} being two columns ETDE. Usually you will want to feed this through a templating system, such as Template Toolkit, which makes iterating the AoH very easy. =back =head1 ADVANCED CGI::Wiki::Plugin::Diff allows for a more flexible approach than HTML only rendering of pages. In particular, there are optional parameters to the constructor which control fine detail of the resultant output. If this is not sufficient, the module is also subclassable, and the programmer can supply alternative methods. =head2 METHODS Most of these are called internally by the plugin, but provide hooks for alternative code if the module is subclassed. =over 4 =item B my $plugin = CGI::Wiki::Plugin::Diff->new( option => value, option => value...); Here, I