blib/lib/CGI/Wiki/Plugin/Diff.pm

filter: Statementstmt 21.5% 23 / 107 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 46.6% 7 / 15 Podpod 100% 7 / 7 totaltotal 19.3% 37 / 191 CC 26 SCAR 45.1
CC
26
Cov
15%
intradiff
210.0
differences
72.0
serialise_metadata
20.0
CRAP 90.8
↑ Summary
1 package CGI::Wiki::Plugin::Diff;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.09';
7  
8 1 ▶use base 'CGI::Wiki::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Algorithm::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use VCS::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Params::Validate::Dummy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Module::Optional qw(Params::Validate
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 validate validate_pos SCALAR SCALARREF ARRAYREF HASHREF UNDEF);
14  
15 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
16 0 my $class = shift;
17 0 my %par = validate( @_, {
18 metadata_separator => { type => SCALAR, default => "<br />\n"} ,
19 line_number_format => { type => SCALAR, default => "== Line \$_ ==\n" },
20 word_matcher => { type => SCALARREF, default => qr(
21 &.+?; #HTML special characters e.g. &lt;
22 |<br\s*/> #Line breaks
23 |\w+\s* #Word with trailing spaces
24 |. #Any other single character
25 )xsi },
26 } );
27 0 bless \%par, $class;
28 }
29  
30 0 ▶sub differences {
Subroutine differences: not called CC=8 SCAR=42.8
Pod documented
31 0 my $self = shift;
32 0 my %args = validate( @_, {
33 node => { type => SCALAR},
34 left_version => { type => SCALAR},
35 right_version => { type => SCALAR},
36 meta_include => { type => ARRAYREF, optional => 1 },
37 meta_exclude => { type => ARRAYREF, optional => 1 } });
38  
39 0 my ($node, $v1, $v2) = @args{ qw( node left_version right_version) };
40 0 my $store = $self->datastore;
41 0 my $fmt = $self->formatter;
42
43 0 my %ver1 = $store->retrieve_node( name => $node, version => $v1);
44 0 my %ver2 = $store->retrieve_node( name => $node, version => $v2);
45  
46 0 my $verstring1 = "Version ".$ver1{version};
47 0 my $verstring2 = "Version ".$ver2{version};
48
49 my $el1 = VCS::Lite->new($verstring1,undef,
50 $self->content_escape($ver1{content}).
51 $self->{metadata_separator}.
52 $self->serialise_metadata($ver1{metadata},
53 0 @args{qw(meta_include meta_exclude)}));
54 my $el2 = VCS::Lite->new($verstring2,undef,
55 $self->content_escape($ver2{content}).
56 $self->{metadata_separator}.
57 $self->serialise_metadata($ver2{metadata},
58 0 @args{qw(meta_include meta_exclude)}));
59 0 my %pag = %ver1;
60 0 $pag{left_version} = $verstring1;
61 0 $pag{right_version} = $verstring2;
62 0 $pag{content} = $fmt->format($ver1{content});
63 0 ▶ my $dlt = $el1->delta($el2)
Branch: 1 branch
BranchTrueFalseTotal
my $dlt = $el1->delta($el2) or return %pag 0 0 0
64 or return %pag;
65  
66 0 my @out;
67
68 0 for ($dlt->hunks) {
69 0 my ($lin1,$lin2,$out1,$out2);
70 0 for (@$_) {
71 0 my ($ind,$line,$text) = @$_;
72 0 ▶ if ($ind ne '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind ne "+") 0 0 0
73 0 ▶ $lin1 ||= $line;
Condition0%0/3
$lin1 ||= $line
l!l&&r!l&&!r
000
MC/DC0%0/2
$lin1 ||= $line
$lin1 $line
Truth table0%0/3
A ||= B
A= $lin1B= $line
ABresult
1X1
011
000
74 0 $out1 .= $text;
75 }
76 0 ▶ if ($ind ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind ne "-") 0 0 0
77 0 ▶ $lin2 ||= $line;
Condition0%0/3
$lin2 ||= $line
l!l&&r!l&&!r
000
MC/DC0%0/2
$lin2 ||= $line
$lin2 $line
Truth table0%0/3
A ||= B
A= $lin2B= $line
ABresult
1X1
011
000
78 0 $out2 .= $text;
79 }
80 }
81 0 push @out,{ left => $self->line_number($lin1),
82 right => $self->line_number($lin2) };
83 0 my ($text1,$text2) = $self->intradiff($out1,$out2);
84 0 push @out,{left => $text1,
85 right => $text2};
86 }
87  
88 0 $pag{diff} = \@out;
89 0 %pag;
90 }
91  
92 0 ▶sub line_number {
Subroutine line_number: not called CC=2 SCAR=17.9
Pod documented
93 0 my $self = shift;
94  
95 0 local ($_) = validate_pos(@_, {type => SCALAR | UNDEF, optional => 1} );
96 0 ▶ return '' unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
97  
98 0 my $fmt = '"'. $self->{line_number_format} . '"';
99 0 eval $fmt;
100 }
101  
102 0 ▶sub serialise_metadata {
Subroutine serialise_metadata: not called CC=4 SCAR=30.0
Pod documented
103 0 my $self = shift;
104 0 my ($all_meta,$include,$exclude) = validate_pos ( @_,
105 { type => HASHREF },
106 { type => ARRAYREF | UNDEF, optional => 1 },
107 { type => ARRAYREF | UNDEF, optional => 1 },
108 );
109 0 ▶ $include ||= [keys %$all_meta];
Condition0%0/2
$include ||= [keys %$all_meta]
l!l
00
MC/DC0%0/1
$include ||= [keys %$all_meta]
$include
Truth table0%0/2
A
A= $include
Aresult
11
00
110 0 ▶ $exclude ||= [qw(comment username
Condition0%0/2
$exclude ||= ["comment", "username", "__categories__checksum", "__locales__checksum"]
l!l
00
MC/DC0%0/1
$exclude ||= ["comment", "username", "__categories__checksum", "__locales__checksum"]
$exclude
Truth table0%0/2
A
A= $exclude
Aresult
11
00
111 __categories__checksum __locales__checksum)] ;
112
113 0 my %metadata = map {$_,$all_meta->{$_}} @$include;
114 0 delete $metadata{$_} for @$exclude;
115  
116 join $self->{metadata_separator},
117 0 map {"$_='".join (',',sort @{$metadata{$_}})."'"}
118 sort keys %metadata;
119 }
120  
121 0 ▶sub content_escape {
Subroutine content_escape: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123 0 my ($str) = validate_pos( @_, { type => SCALAR } );
124  
125 0 $str =~ s/&/&amp;/g;
126 0 $str =~ s/</&lt;/g;
127 0 $str =~ s/>/&gt;/g;
128 0 $str =~ s!\s*?\n!<br />\n!gs;
129  
130 0 $str;
131 }
132  
133 0 ▶sub intradiff {
Subroutine intradiff: not called CC=14 SCAR=53.5
Pod documented
134 0 my $self = shift;
135 0 my ($str1,$str2) = validate_pos( @_, {type => SCALAR|UNDEF },
136 {type => SCALAR|UNDEF });
137  
138 0 ▶ return (qq{<span class="diff1">$str1</span>},"") unless $str2;
Branch: 1 branch
BranchTrueFalseTotal
unless $str2 0 0 0
139 0 ▶ return ("",qq{<span class="diff2">$str2</span>}) unless $str1;
Branch: 1 branch
BranchTrueFalseTotal
unless $str1 0 0 0
140 0 my $re_wordmatcher = $self->{word_matcher};
141 my @diffs = Algorithm::Diff::sdiff([$str1 =~ /$re_wordmatcher/sg]
142 0 ▶ ,[$str2 =~ /$re_wordmatcher/sg], sub {$self->get_token(@_)});
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 my $out1 = '';
144 0 my $out2 = '';
145 0 my ($mode1,$mode2);
146  
147 0 for (@diffs) {
148 0 my ($ind,$c1,$c2) = @$_;
149  
150 0 my $newmode1 = $ind =~ /[c\-]/;
151 0 my $newmode2 = $ind =~ /[c+]/;
152 0 ▶ $out1 .= '<span class="diff1">' if $newmode1 && !$mode1;
Branch: 1 branch
BranchTrueFalseTotal
if $newmode1 and not $mode1 0 0 0
Condition0%0/3
$newmode1 and not $mode1
!ll&&!rl&&r
000
MC/DC0%0/2
$newmode1 and not $mode1
$newmode1 not $mode1
Truth table0%0/3
A and not(B)
A= $newmode1B= not $mode1
ABresult
0X0
100
111
153 0 ▶ $out2 .= '<span class="diff2">' if $newmode2 && !$mode2;
Branch: 1 branch
BranchTrueFalseTotal
if $newmode2 and not $mode2 0 0 0
Condition0%0/3
$newmode2 and not $mode2
!ll&&!rl&&r
000
MC/DC0%0/2
$newmode2 and not $mode2
$newmode2 not $mode2
Truth table0%0/3
A and not(B)
A= $newmode2B= not $mode2
ABresult
0X0
100
111
154 0 ▶ $out1 .= '</span>' if !$newmode1 && $mode1;
Branch: 1 branch
BranchTrueFalseTotal
if not $newmode1 and $mode1 0 0 0
Condition0%0/3
not $newmode1 and $mode1
!ll&&!rl&&r
000
MC/DC0%0/2
not $newmode1 and $mode1
not $newmode1 $mode1
Truth table0%0/3
not(A) and B
A= not $newmode1B= $mode1
ABresult
0X0
100
111
155 0 ▶ $out2 .= '</span>' if !$newmode2 && $mode2;
Branch: 1 branch
BranchTrueFalseTotal
if not $newmode2 and $mode2 0 0 0
Condition0%0/3
not $newmode2 and $mode2
!ll&&!rl&&r
000
MC/DC0%0/2
not $newmode2 and $mode2
not $newmode2 $mode2
Truth table0%0/3
not(A) and B
A= not $newmode2B= $mode2
ABresult
0X0
100
111
156 0 ($mode1,$mode2) = ($newmode1,$newmode2);
157 0 $out1 .= $c1;
158 0 $out2 .= $c2;
159 }
160 0 ▶ $out1 .= '</span>' if $mode1;
Branch: 1 branch
BranchTrueFalseTotal
if $mode1 0 0 0
161 0 ▶ $out2 .= '</span>' if $mode2;
Branch: 1 branch
BranchTrueFalseTotal
if $mode2 0 0 0
162  
163 0 ($out1,$out2);
164 }
165  
166 0 ▶sub get_token {
Subroutine get_token: not called CC=2 SCAR=17.9
Pod documented
167 0 my ($self,$str) = @_;
168  
169 0 $str =~ /^(\S*)\s*$/; # Match all but trailing whitespace
170  
171 0 ▶ $1 || $str;
Branch: 1 branch
BranchTrueFalseTotal
$1 or $str 0 0 0
172 }
173  
174 1 1;
175 __END__
↑ Summary