This file is indexed.

/usr/share/perl5/Wiki/Toolkit/Plugin/Diff.pm is in libwiki-toolkit-plugin-diff-perl 0.12-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package Wiki::Toolkit::Plugin::Diff;

use strict;
use warnings;

our $VERSION = '0.12';

use base 'Wiki::Toolkit::Plugin';
use Algorithm::Diff;
use VCS::Lite;
use Params::Validate qw(validate validate_pos SCALAR SCALARREF ARRAYREF HASHREF UNDEF);

sub new {
    my $class = shift;
    my %par = validate( @_, {
        metadata_separator => { type => SCALAR, default => "<br />\n"} ,
    	line_number_format => { type => SCALAR, default => "== Line \$_ ==\n" }, 
        word_matcher => { type => SCALARREF, default => qr(
            &.+?;                   #HTML special characters e.g. &lt;
            |<br\s*/>               #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/&/&amp;/g;
    $str =~ s/</&lt;/g;
    $str =~ s/>/&gt;/g;
    $str =~ s!\s*?\n!<br />\n!gs;

    $str;
}

sub intradiff {
    my $self = shift;
    my ($str1,$str2) = validate_pos( @_, {type => SCALAR|UNDEF }, 
                                         {type => SCALAR|UNDEF });

    return (qq{<span class="diff1">$str1</span>},"") unless $str2;
    return ("",qq{<span class="diff2">$str2</span>}) 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 .= '<span class="diff1">' if $newmode1 && !$mode1;
	$out2 .= '<span class="diff2">' if $newmode2 && !$mode2;
	$out1 .= '</span>' if !$newmode1 && $mode1;
	$out2 .= '</span>' if !$newmode2 && $mode2;
	($mode1,$mode2) = ($newmode1,$newmode2);
	$out1 .= $c1;
	$out2 .= $c2;
    }
    $out1 .= '</span>' if $mode1;
    $out2 .= '</span>' if $mode2;

    ($out1,$out2);
}

sub get_token {
    my ($self,$str) = @_;

    $str =~ /^(\S*)\s*$/;	# Match all but trailing whitespace

    $1 || $str;
}

1;
__END__

=head1 NAME

Wiki::Toolkit::Plugin::Diff - format differences between two Wiki::Toolkit pages

=head1 SYNOPSIS

  use Wiki::Toolkit::Plugin::Diff;
  my $plugin = Wiki::Toolkit::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 Wiki::Toolkit sites, which provides a nice extract of differences
between two versions of a node. 

=head1 BASIC USAGE

B<differences>

  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<left_version>

The node version whose content we're considering canonical.

=item *
B<right_version>

The node version that we're showing the differences from.

=item *
B<meta_include>

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<meta_exclude>

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<qw(
   username
   comment
   __categories__checksum
   __locales__checksum )>

Agreed this list is hopelessly inadequate, especially for L<OpenGuides>.
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<left_version>

The node version whose content we're considering canonical.

=item B<right_version>

The node version that we're showing the differences from.

=item B<content> 

The (formatted) contents of the I<Left> version of the node.

=item B<diff> 

An array of hashrefs of C<hunks> 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 E<lt>tableE<gt>, with each member of the array corresponding to a row 
E<lt>TRE<gt>, and keys {left} and {right} being two columns E<lt>TDE<gt>.

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

Wiki::Toolkit::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<new>

  my $plugin = Wiki::Toolkit::Plugin::Diff->new( option => value, option => value...);

Here, I<option> can be one of the following:

=over 4

=item B<metadata_separator>

A string which is inserted between each metadata
field, and also between the contents and the metadata. This defaults to 
"E<lt>br /E<gt>\n" so as to render each metadata field on a new line.

=item B<line_number_format>

Used to lay out the head line number for a 
difference hunk. The string is eval'ed with $_ containing the line number.
Default is "== Line \$_ ==" .

=item B<word_matcher> 

is a regular expression, used to tokenize the input
string. This is the way of grouping together atomic sequences, so as to
give a readable result. The default is the following:

            &.+?;                   #HTML special characters e.g. &lt;
            |<br\s*/>               #Line breaks
            |\w+\s*       	    #Word with trailing spaces 
            |.                      #Any other single character

=back

=item B<differences>

see above.

=item B<line_number>

This method is called to format a line number into a suitable string.
The supplied routine performs the necessary substitution of 
$self->{line_number_format} using eval.

=item B<serialise_metadata>

The purpose of this method is to turn a metadata hash into a string
suitable for diffing.

=item B<content_escape>

This method is used to apply the necessary quoting or escaping of 
characters that could appear in the content body, that could interfere
with the rendering of the diff text.

=item B<intradiff>

This method turns an array of hunks as returned by VCS::Lite::Delta->hunks
into a side by side diff listing, with highlights indicating different 
words and punctuation.

Currently, this is hardcoded to present the differences with HTML tags.

This module is a prime candidate for migration into VCS::Lite, where this
functionality really belongs.

=item B<get_token>

This allows the "false positive" bug discovered by Earle Martin to be solved
(rt.cpan.org #6284).
Effectively, the input strings (diff1 and diff2) are tokenised using the
word_matcher regexp (see above), and turned into arrays of tokens.

The default regexp absorbs trailing whitespace, to give a more readable result.
However, if a word is followed by differing whitespace or no whitespace at
all, this was throwing up a diff.

The get_token method supplied removes trailing whitespace from the key
values before they are compared.

=back

=head1 TODO

Move intradiff functionality into VCS::Lite.

=head1 BUGS AND ENHANCEMENTS

Please use rt.cpan.org to report any bugs in this module. If you have any
ideas for how this module could be enhanced, please email the author, or
post to the Wiki::Toolkit list (cgi (hyphen) wiki (hyphen) dev (at) earth (dot) li).

=head1 AUTHOR

I. P. Williams (ivorw_openguides [at] xemaps {dot} com)
The Wiki::Toolkit team (http://www.wiki-toolkit.org/)

=head1 COPYRIGHT

     Copyright (C) 2003-2004 I. P. Williams (ivorw_openguides [at] xemaps {dot} com).
     Copyright (C) 2006 the Wiki::Toolkit team (http://www.wiki-toolkit.org/)
     All Rights Reserved.

This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=head1 SEE ALSO

L<VCS::Lite>, L<Wiki::Toolkit>, L<Wiki::Toolkit::Plugin>, L<OpenGuides>

=cut