Link/Repair/Substitutor.pm

filter: Statementstmt 87.5% 105 / 120 Branchbran 53.7% 43 / 80 Conditioncond 88.2% 15 / 17 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 75.5% 179 / 237 CC 46 SCAR 31.4
CC
46
Cov
75%
__ANON__
31.8
gen_substitutor
24.0
gen_file_substitutor
18.6
CRAP 23.0
« Link/Reporter.pm ↑ Summary Link/Repair.pm »
1 package WWW::Link::Repair::Substitutor;
2 3 $REVISION=q$Revision: 1.12 $ ; $VERSION = sprintf ( "%d.%02d", $REVISION =~ /(\d+).(\d+)/ );
3  
4 =head1 NAME
5  
6 WWW::Link::Repair::Substitutor - repair links by text substitution
7  
8 =head1 SYNOPSIS
9  
10 use WWW::Link::Repair::Substitutor;
11 $dirsubs = WWW::Link::Repair::Substitutor::gen_substitutor
12 ( "http://bounce.bounce.com/frodo/dogo" ,
13 "http://thing.thong/ding/dong",
14 1, 0, ); #directory substitution don't replace subsidiary links
15 &$dirsubs ($line_from_file)
16  
17 =head1 DESCRIPTION
18  
19 A module for substituting one link in a file for another.
20  
21 This link repairer works by going through a file line by line and
22 doing a substitute on each line. It will substitute absolute links
23 all of the time, including within the text of the HTML page. This is
24 useful because it means that things like instructions to people about
25 what to do with URLs will be corrected.
26  
27 =head1 SUBSTITUTORS
28  
29 A substituter is a function which substitutes one url for another in a
30 string. Typically it would be fed a file a line at a time and would
31 substitute it directly. It works on it's argument directly.
32  
33 The two urls should be provided in absolute form.
34  
35 =head2 FILE HANDLERS
36  
37 A file handler goes through files calling a substitutor as needed.
38  
39 =head2 gen_directory_substitutor
40  
41 B<Warning>: I think the logic around here is more than a little dubious
42  
43 =cut
44  
45 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 3 our ($verbose);
51 3 ▶$verbose=0 unless defined $verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $WWW::Link::Repair::Substitutor::verbose 3 0 3
52  
53 =head2 gen_substitutor
54  
55 This function was previously an exported interface and currently
56 remains visible. I think it's interface is likely to change though.
57 Preferably use generate_file_substitutor as an entry point instead.
58  
59 This function generates a function which can be called either on a
60 complete line of text from a file or on a URL and which will update
61 the URL based on the URLs it has been given
62  
63 If the third argument is true then the function will return a
64 substitutor which works on all of the links below a given url and
65 substitutes them all together. Thus if we change
66  
67 http://fred.jim/eating/
68  
69 to
70  
71 http://roger.jemima/food/eating-out/
72  
73 we also change
74  
75  
76 http://fred.jim/eating/hotels.html
77  
78 to
79  
80 http://roger.jemima/food/eating-out/hotels.html
81  
82 This function should handle fragments correctly. This means that we
83 should allow fragments to be substituted to and from normal links, but
84 also when we fix a url to a url all of the internal fragments should
85 follow. Fragments are not relative links. Cases
86  
87 =over 4
88  
89 =item 1
90  
91 substitution of fragment for fragment
92  
93 =item 2
94  
95 substitution of link for link
96  
97 =item 3
98  
99 substitution of link to fragment
100  
101 =item 4
102  
103 substitution of fragment to link
104  
105 =item 5
106  
107 substitution of url base for url base with all relative links
108  
109 =back
110  
111 Note that right now it isn't possible to substitute a tree under a
112 fragment. There is no such thing as a sub-fragment defined in the
113 standards.
114  
115 If we stubstitute a link to a fragment then we should not substitute
116 fragments under that link. that would loose information. Rather we
117 should issue a warning. Maybe there should be an option that lets this
118 happen.
119  
120 =cut
121  
122 #substitution of link for link - including tree mode (never has fragments)
123 #match url without fragment - fragment remains
124  
125 #substitution of fragment for fragment
126 #match whole link
127  
128 #substitution of link to link with fragment
129 #substitute whole link
130 #SHOULD MATCH BUT WARN IF WE FIND AN EXISTING FRAGMENT
131 #will fail to match and leave
132  
133 #substitution of link with fragment to link
134 #match the whole link
135  
136  
137 #substitution of url base for url base with all relative links
138  
139  
140  
141 11 ▶sub gen_substitutor ($$;$$) {
Subroutine gen_substitutor: called CC=23 SCAR=31.8
Pod documented
142 11 my ($orig_url,$new_url,$tree_mode,$baseuri) = @_;
143 11 my ($new_has_fragment) = my ($orig_has_fragment ) =0;
144 11 ▶ $orig_url =~ m/#/ and $orig_has_fragment =1;
Branch: 1 branch
BranchTrueFalseTotal
$orig_url =~ /#/ and $orig_has_fragment = 1 1 10 11
145 11 ▶ $new_url =~ m/#/ and $new_has_fragment =1;
Branch: 1 branch
BranchTrueFalseTotal
$new_url =~ /#/ and $new_has_fragment = 1 2 9 11
146  
147 11 ▶ ( $orig_has_fragment or $new_has_fragment ) and $tree_mode
Branch: 1 branch
BranchTrueFalseTotal
$orig_has_fragment || $new_has_fragment and $tree_mode and die "can't do tree mode substitution with fragments" 1 10 11
Condition100%6/6
$orig_has_fragment || $new_has_fragment and $tree_mode
!ll&&!rl&&r
821
$orig_has_fragment or $new_has_fragment
l!l&&r!l&&!r
128
MC/DC66%2/3
$orig_has_fragment || $new_has_fragment and $tree_mode
$orig_has_fragment $new_has_fragment $tree_mode
Truth table80%4/5
A or B and C
A= $orig_has_fragmentB= $new_has_fragmentC= $tree_mode
ABCresult
00X0
1X00
0100
1X11
0111
148 and die "can't do tree mode substitution with fragments";
149  
150 10 ▶ print STDERR
Branch: 2 branches
BranchTrueFalseTotal
if $WWW::Link::Repair::Substitutor::verbose & 32 0 10 10
defined $baseuri ? : 0 0 0
151 "Generating substitutor from $orig_url to $new_url \n",
152 (defined $baseuri ? "using base $baseuri\n" : "" ) if ($verbose & 32);
153  
154 10 ▶ defined $baseuri and ( not $baseuri =~ m/^[a-z][a-z0-9]*:/ )
Branch: 1 branch
BranchTrueFalseTotal
defined $baseuri and not $baseuri =~ /^[a-z][a-z0-9]*:/ and croak("baseuri must be absolute URI, not $baseuri") 0 10 10
Condition66%2/3
defined $baseuri and not $baseuri =~ /^[a-z][a-z0-9]*:/
!ll&&!rl&&r
910
MC/DC0%0/2
defined $baseuri and not $baseuri =~ /^[a-z][a-z0-9]*:/
defined $baseuri not $baseuri =~ /^[a-z][a-z0-9]*:/
Truth table66%2/3
A and not(B)
A= defined $baseuriB= not $baseuri =~ /^[a-z][a-z0-9]*:/
ABresult
0X0
100
111
155 and croak "baseuri must be absolute URI, not $baseuri";
156  
157 10 my $orig_rel;
158 my $new_rel;
159 10 ▶ defined $baseuri and do {
Branch: 1 branch
BranchTrueFalseTotal
defined $baseuri and do { my $orig_uri = "URI"->new($orig_url); my $new_uri = "URI"->new($new_url); $orig_rel = $orig_uri->rel($baseuri); $new_rel = $new_uri->rel($baseuri) } 1 9 10
160 1 my $orig_uri=URI->new($orig_url);
161 1 my $new_uri=URI->new($new_url);
162 1 $orig_rel=$orig_uri->rel($baseuri);
163 1 $new_rel=$new_uri->rel($baseuri);
164 };
165  
166 10 my $perlcode = <<'EOF';
167 sub {
168 my $substitutions=0;
169 EOF
170  
171 10 ▶ $perlcode .= <<'EOF' if ($verbose & 16);
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Repair::Substitutor::verbose & 16 0 10 10
172 print STDERR "Subs in : $_[0]\n";
173 EOF
174  
175 10 ▶ $perlcode .= <<'EOF' if ($baseuri);
Branch: 1 branch
BranchTrueFalseTotal
if $baseuri 1 9 10
176 EOF
177  
178 10 my $restart = <<'EOF';
179 $substitutions += $_[0] =~ s,( (?:^) #the start of a line
180 |(?:[^A-Za-z0-9]) #or a boundary character..
181 )
182 EOF
183  
184 # $remiddle terminates the url to be replaced... three possibilities
185 #
186 # 1) we are replacing a tree of URLs where the base URL is terminated with a /
187 # => what happens after doesn't matter.. '
188 # 2) we are replacing a tree of URLs where the base URL is unterminated..
189 # => end of the string must be end of the URL or '/' must follow
190 # 3) we only replace the exact URL
191 # => end of the string must be end of the URL
192  
193 10 my $remiddle = '';
194  
195 10 ▶ unless ($orig_url=~ m,/$, and $tree_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($orig_url =~ m[/$] and $tree_mode) 8 2 10
Condition100%3/3
$orig_url =~ m[/$] and $tree_mode
!ll&&!rl&&r
532
MC/DC100%2/2
$orig_url =~ m[/$] and $tree_mode
$orig_url =~ m[/$] $tree_mode
Truth table100%3/3
A and B
A= $orig_url =~ m[/$]B= $tree_mode
ABresult
0X0
100
111
196 8 $remiddle .= <<'EOF';
197  
198 (?=(
199 EOF
200  
201 #end_of_uri - ends at a fragment unless the first url is a fragment
202  
203 8 my $end_of_uri;
204 CASE: {
205 8 ▶ $tree_mode && do {
Branch: 1 branch
BranchTrueFalseTotal
$tree_mode and do { $end_of_uri = " ([#\"'/>]) #\" either end or end of section\n"; last CASE } 2 6 8
206 2 $end_of_uri = <<'EOF' ;
207 ([#"'/>]) #" either end or end of section
208 EOF
209 2 last CASE;
210 };
211 6 ▶ ( not $orig_has_fragment and not $new_has_fragment ) && do {
Branch: 1 branch
BranchTrueFalseTotal
$orig_has_fragment or $new_has_fragment or do { $end_of_uri = " ([#\"'>]) #\" this checks for the end of the url..\n"; last CASE } 2 4 6
Condition100%3/3
$orig_has_fragment or $new_has_fragment
l!l&&r!l&&!r
114
MC/DC100%2/2
$orig_has_fragment or $new_has_fragment
$orig_has_fragment $new_has_fragment
Truth table100%3/3
A or B
A= $orig_has_fragmentB= $new_has_fragment
ABresult
1X1
011
000
212 4 $end_of_uri = <<'EOF' ;
213 ([#"'>]) #" this checks for the end of the url..
214 EOF
215 4 last CASE;
216 };
217 2 do {
218 2 $end_of_uri = <<'EOF' ;
219 (["'>]) #" this checks for the end of the url..
220 EOF
221 2 last CASE;
222 };
223 0 die "not reached";
224 }
225  
226 8 $remiddle .= $end_of_uri;
227  
228 8 ▶ $remiddle .= <<'EOF' unless $orig_url=~ m,/$,;
Branch: 1 branch
BranchTrueFalseTotal
unless $orig_url =~ m[/$] 5 3 8
229 |(\s)
230 |($)
231 EOF
232 8 $remiddle .= <<'EOF';
233 )
234 )
235 EOF
236  
237 }
238  
239  
240 10 $remiddle .= ' ,$1' ;
241  
242 10 my $reend = ",gxo;\n";
243 10 my $relreend = ",gx;\n";
244  
245 #FIXME: url quoting into regex??
246  
247 10 $perlcode .= $restart . $orig_url . $remiddle . $new_url . $reend;
248 10 ▶ if ($baseuri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($baseuri) 1 9 10
249 1 $perlcode .= $restart . $orig_rel . $remiddle . $new_rel . $relreend;
250 }
251  
252 10 ▶ $perlcode .= <<'EOF' if ($verbose & 16);
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Repair::Substitutor::verbose & 16 0 10 10
253 print STDERR "Gives : $_[0]\n";
254 EOF
255  
256 10 $perlcode .= <<'EOF';
257 return $substitutions;
258 }
259 EOF
260 10 ▶ print STDERR "creating substitutor function as follows\n",$perlcode, "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Repair::Substitutor::verbose & 32 0 10 10
261 if ($verbose & 32);
262 10 my $returnme=(eval $perlcode);
263 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
264 0 chomp $@; # to get line no in message
265 0 die "sub creation failed: $@";
266 }
267 10 return $returnme;
268 }
269  
270 =head2 gen_file_substitutor(<original url>, <new url>, [args...])
271  
272 This function returns a function which will act on a text file or
273 other file which can be treated as a text file and will carry out URL
274 substitutions within it.
275  
276 The returned code reference should be called with a filename as an
277 argument, it will then replace all occurrences of original url with
278 new url.
279  
280 There are various options to this which can be set by putting various
281 key value pairs in the call.
282  
283 fakeit - set to create a function which actually does nothing
284  
285 tree_mode - set to true to substitute also URLs which are "beneath"
286 original url
287  
288 keep_orig - set to false to inhibit creation of backup files
289  
290 relative - substitute also relative relative URLs which are equivalent
291 to original url (requires file_to_url)
292  
293 file_to_url - provide a function which can translate a given filename
294 to a URL, so we can work out relative URLs for the current
295 file.
296  
297  
298 so a call like
299  
300 $subs=gen_file_substitutor
301 ("http://www.example.com/friendstuff/old",
302 "http://www.example.com/friendstuff/new",
303 relative => 1, tree_mode => 1;
304 file_to_url =>
305 sub { my $ret=shift;
306 $ret =~ return s,/var/www/me,http://www.example.com/mystuff,;
307 return $ret});
308  
309 &$subs("/var/www/me/index.html");
310 &$subs("/var/www/me/friends.html");
311  
312 should allow you to fix your web pages if your friend renames a whole
313 directory.
314  
315 =head1 BUGS
316  
317 One problem with directory substitutors is treatment of the two different urls
318  
319 http://fred.jim/eating/
320  
321 and
322  
323 http://fred.jim/eating
324  
325 Most of the time, the latter of the pair is really just a mistaken
326 reference to the earlier. This is B<not> always true. What is more,
327 where it is true, a user of LinkController will usually have changed
328 to the correct version. For this reason, if gen_directory_substitutor
329 is passed the first form of a url, it will not substitute the second.
330 If passed the second, it will substitute the first.
331  
332 We have to be fed whole URLs at a time. If a url is split between two
333 different chunks then we may not handle it correctly. Always feeding
334 in a complete line protects us from this because a URL cannot contain
335 an unencoded line break.
336  
337 =cut
338  
339  
340 3 ▶use vars qw($tmpdir $tmpref $tmpname $keeporig_default);
Subroutine BEGIN: called CC=1 SCAR=0.0
341 3 $tmpdir="/tmp/";
342 3 $tmpref="link_repair";
343 3 $tmpname="$tmpdir$tmpref" . "repair.$$";
344 3 $keeporig_default=1;
345  
346 5 ▶sub gen_file_substitutor ($$;%) {
Subroutine gen_file_substitutor: called CC=10 SCAR=29.2
Pod documented
347 5 my $origurl=shift;
348 5 my $finalurl=shift;
349 5 my %settings=@_;
350  
351 5 my $tree_mode=$settings{tree_mode};
352 5 my $relative=$settings{relative};
353 5 my $file_to_url=$settings{file_to_url};
354 5 my $keeporig=$settings{keeporig};
355 5 my $fakeit=$settings{fakeit};
356  
357 5 ▶ $keeporig=$keeporig_default unless defined $keeporig;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keeporig 5 0 5
358  
359 5 ▶ print STDERR "generating a file substitutor\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Repair::Substitutor::verbose 0 5 5
360 5 ▶ $verbose & 32 and do {
Branch: 1 branch
BranchTrueFalseTotal
$WWW::Link::Repair::Substitutor::verbose & 32 and do { print STDERR "From: $origurl\nTo: $finalurl\nSettings:-\n"; print 'keeporig: ' . ($keeporig ? $keeporig : "undef") . ' relative: ' . ($relative ? $relative : "undef") . ' file_to_url: ' . ($file_to_url ? $file_to_url : "undef") . ' fakeit: ' . ($fakeit ? $fakeit : "undef") . " \n" } 0 5 5
361 0 print STDERR <<EOF;
362 From: $origurl
363 To: $finalurl
364 Settings:-
365 EOF
366 0 ▶print "keeporig: " . ( $keeporig ? $keeporig : "undef" )
Branch: 4 branches
BranchTrueFalseTotal
$keeporig ? : 0 0 0
$relative ? : 0 0 0
$file_to_url ? : 0 0 0
$fakeit ? : 0 0 0
367 . " relative: " . ( $relative ? $relative : "undef" )
368 . " file_to_url: " . ( $file_to_url ? $file_to_url : "undef" )
369 . " fakeit: " . ( $fakeit ? $fakeit : "undef" ) . " \n";
370 };
371  
372 5 my $subs;
373 5 ▶ if ($relative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relative) { } 0 5 5
374 0 ▶ $file_to_url
Branch: 1 branch
BranchTrueFalseTotal
$file_to_url or die "relative substitution needs a file_to_url translator" 0 0 0
375 or die "relative substitution needs a file_to_url translator"
376 } else {
377 5 $subs = gen_substitutor($origurl,$finalurl,$tree_mode);
378 }
379  
380 6 ▶ return sub () {
Subroutine __ANON__: called CC=15 SCAR=34.6
381 6 my $filename=shift;
382  
383 6 ▶ print STDERR "file handler called for $filename\n" if $verbose && 8;
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Repair::Substitutor::verbose and 8 0 6 6
Condition50%1/2
$WWW::Link::Repair::Substitutor::verbose and 8
!ll
60
MC/DC0%0/1
$WWW::Link::Repair::Substitutor::verbose and 8
$WWW::Link::Repair::Substitutor::verbose
Truth table50%1/2
A
A= $WWW::Link::Repair::Substitutor::verbose
Aresult
00
11
384  
385 6 ▶ if ($relative) {
Branch: 1 branch
BranchTrueFalseTotal
if ($relative) 0 6 6
386 0 my $baseuri=&$file_to_url($filename);
387 0 ▶ print STDERR "URI for file $filename is $baseuri\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Repair::Substitutor::verbose 0 0 0
388 0 $subs = gen_substitutor($origurl,$finalurl,$tree_mode, $baseuri);
389 }
390  
391 6 my $fixed=0;
392  
393 6 ▶ die "file handler called with undefined values" unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 6 6
394 6 ▶ -d $filename && return 0;
Branch: 1 branch
BranchTrueFalseTotal
-d $filename and return 0 0 6 6
395 0 ▶ -f $filename or do {warn "can't fix special file $filename"; return 0};
Branch: 1 branch
BranchTrueFalseTotal
-f $filename or do { warn "can't fix special file $filename"; return 0 } 6 0 6
396  
397 6 ▶ if ($fakeit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fakeit) { } 0 6 6
398 0 print STDERR "pretending to edit $filename\n";
399 0 ▶ -W $filename or warn "file $filename can't be edited";
Branch: 1 branch
BranchTrueFalseTotal
-W $filename or warn "file $filename can't be edited" 0 0 0
400 } else {
401 open (FIXFILE, "<$filename")
402 0 ▶ or do { die "can't access $filename"; return 0};
Branch: 1 branch
BranchTrueFalseTotal
open FIXFILE, "<$filename" or do { die "can't access $filename"; return 0 } 6 0 6
403 6 ▶ open (TMPFILE, ">$tmpname") or die "can't use tempfile $tmpname";
Branch: 1 branch
BranchTrueFalseTotal
open TMPFILE, ">$WWW::Link::Repair::Substitutor::tmpname" or die "can't use tempfile $WWW::Link::Repair::Substitutor::tmpname" 6 0 6
404 6 ▶ while (<FIXFILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FIXFILE)) 145 6 151
405 145 $fixed += &$subs( $_);
406 145 print TMPFILE $_;
407 }
408 6 close TMPFILE;
409 6 close FIXFILE;
410 #FIXME edit failure?? LOGME
411 6 ▶ print STDERR "Changed links in file $filename\n"
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Link::Repair::verbose & 16 0 6 6
412 if $WWW::Link::Repair::verbose & 16;
413 #I think this is the key bit of the program which needs to be SUID
414 #and could even be separated out for more security.. <<EOSU
415 6 ▶ rename($filename, $filename . ".orig") if $keeporig;
Branch: 1 branch
BranchTrueFalseTotal
if $keeporig 6 0 6
416 6 copy($tmpname, $filename);
417 #EOSU
418 6 unlink $tmpname; #assuming we used it..
419 }
420 6 return $fixed;
421 }
422 5 }
423  
424  
425  
426 3 1;
427  
428  
« Link/Reporter.pm ↑ Summary Link/Repair.pm »