Link/Reporter.pm

filter: Statementstmt 32.4% 38 / 117 Branchbran 3.3% 1 / 30 Conditioncond 14.2% 2 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 17.8% 5 / 28 Podpod 57.6% 15 / 26 totaltotal 27.4% 61 / 222 CC 21 SCAR 35.0
CC
21
Cov
25%
examine
132.0
report_broken
6.0
report_okay
6.0
CRAP 33.1
« Link/Selector.pm ↑ Summary Link/Repair/Substitutor.pm »
1 =head1 NAME
2  
3 WWW::Link::Reporter - report information about a link back to a user
4  
5 =head1 SYNOPSIS
6  
7 package WWW::Link::Reporter::somethingorother
8 use WWW::Link::Reporter;
9 sub broken {print "something..."; ... }
10 sub not_found {print "or...; ... }
11 sub redirected {print "other...; ... }
12 sub okay ...
13 sub not_perfect ....
14  
15 =head1 DESCRIPTION
16  
17 This class is really a base class upon which other classes can be
18 built. These classes will allow feedback to users about what the
19 status of various links is.
20  
21 The class provides one facility in that it will gather some statistics
22 on the links that are fed to it.
23  
24 =head1 SUBCLASSES
25  
26 Here is a list of the subclasses which come in the default
27 distribution. Each one should have a more detailed description (but
28 probably doesn't ;-)
29  
30 =over
31  
32 =item Text
33  
34 A simple text output listing which links are broken.
35  
36 =item HTML
37  
38 An HTML page with a list of the broken links.
39  
40 =item RepairForm
41  
42 Generates an HTML page which will drive C<fix-link.cgi> (provided with
43 this package) to fix links.
44  
45 =item LongList
46  
47 Runs C<ls -l> on files which contain broken links giving a format
48 which emacs can interpret for editing (see special emacs mode
49 B<link-report-dired> provided).
50  
51 =item Compile
52  
53 For use whilst checking links in a file online. Generates an emacs
54 compile mode style listing which can be used to go directly to the
55 line needing corrected in the editor.
56  
57 =back
58  
59 Other subclasses can be created by overriding the built in methods
60 (see below).
61  
62 The default class supports storing an index object which could be used
63 for getting information about the link. However it doesn't do
64 anything with it.
65  
66 =cut
67  
68 package WWW::Link::Reporter;
69 2 $REVISION=q$Revision: 1.10 $ ; $VERSION = sprintf ( "%d.%02d", $REVISION =~ /(\d+).(\d+)/ );
70  
71 #default value for verbosity..
72 #$WWW::Link::Reporter::verbose=0xFF;
73 2 $WWW::Link::Reporter::verbose=0x00;
74  
75 2 ▶use WWW::Link;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶use Carp; #or CGI::carp??
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 =head1 METHODS
79  
80 =head2 new WWW::Link::Reporter [$index]
81  
82 New sets up a new reporter. If it is given a suitable index, then it
83 will store this for later use during reporting.
84  
85 =cut
86  
87 1 ▶sub new ($;$) {
Subroutine new: called CC=4 SCAR=15.4
Pod documented
88 1 my $proto = shift;
89 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
90 1 my $self = {};
91 1 $self->{"index"}=shift;
92 1 ▶ ref $index or croak "index must be a reference" if defined $index;
Branch: 1 branch
BranchTrueFalseTotal
if defined $index 0 1 1
Condition20%1/5
defined $index && (ref $index || croak('index must be a reference'))
!ll
100
ref $index or croak("index must be a reference")
l!l
00
MC/DC0%0/2
defined $index && (ref $index || croak('index must be a reference'))
defined $index ref $index
Truth table33%1/3
A && B
A= defined $indexB= ref $index
ABresult
0X0
100
111
93 1 bless ($self, $class);
94 #next come settings
95 1 $self->{"verbose"}=$WWW::Link::Reporter::verbose;
96 1 $self->init() ;
97 1 $self->default_reports();
98 1 return $self;
99 }
100  
101 =head2 $reporter->set_index ( $index )
102  
103 We can set the index that we are using.
104  
105 =cut
106  
107 0 ▶sub set_index {
Subroutine set_index: not called CC=1 SCAR=6.9
Pod documented
108 0 my $self=shift;
109 0 $self->{"index"}=shift;
110 }
111  
112 =head2 $reporter->examine ( $link )
113  
114 The examine class calls appropriate methods of the reporter to give to
115 give information about the link, depending on its status. By
116 overriding the methods in the default class (see below) you can make
117 any kind of report you wish. Individual method calls can be turned on
118 or off using boolean variables in the object (again see below).
119  
120 Normally there's no need to override this.
121  
122 =cut
123  
124 0 ▶sub examine {
Subroutine examine: not called CC=11 SCAR=48.8
Pod documented
125 0 my $self=shift;
126 0 my $link=shift;
127  
128 0 ▶ croak 'usage $reporter->examine($link)' unless ref $link;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $link 0 0 0
129  
130 0 my $url=$link->url();
131  
132 print STDERR "WWW::Link::Reporter::examine looking at $url\n"
133 0 ▶ if $self->{"verbose"} & 8;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} & 8 0 0 0
134  
135 0 $self->{"total"}++ ;
136  
137 CASE: {
138  
139 # no strict refs;
140 0 my $redir=0;
141 0 ▶ $link->is_redirected and $redir=1;
Branch: 1 branch
BranchTrueFalseTotal
$link->is_redirected and $redir = 1 0 0 0
142  
143 0 foreach my $status ( "broken", "okay", "not_checked",
144 "damaged", "disallowed", "unsupported" ) {
145 0 my $testfn = "is_" . $status;
146 0 my $reportvar = "report_" . $status;
147 0 my $showfn = $status;
148  
149 0 ▶ $link->$testfn() && do {
Branch: 1 branch
BranchTrueFalseTotal
$link->$testfn and do { $self->{$reportvar} || $redir && $self->{"report_redirected"} and $self->$showfn($link, $redir); last CASE } 0 0 0
150 ($self->{$reportvar} or $redir && $self->{report_redirected})
151 0 ▶ and $self->$showfn($link, $redir) ;
Branch: 1 branch
BranchTrueFalseTotal
$self->{$reportvar} || $redir && $self->{"report_redirected"} and $self->$showfn($link, $redir) 0 0 0
Condition0%0/6
$self->{$reportvar} or $redir and $self->{"report_redirected"}
l!l&&r!l&&!r
000
$redir and $self->{"report_redirected"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{$reportvar} or $redir and $self->{"report_redirected"}
$self->{$reportvar} $redir $self->{"report_redirected"}
Truth table0%0/4
A or B and C
A= $self->{$reportvar}B= $redirC= $self->{"report_redirected"}
ABCresult
1XX1
0111
00X0
0100
152 0 last CASE;
153 };
154 }
155  
156 0 ▶ $self->{"report_unknown"} && $self->unknown($link);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"report_unknown"} and $self->unknown($link) 0 0 0
157 }
158  
159 print STDERR "WWW::Link::Reporter::examine finished $url\n"
160 0 ▶ if $self->{"verbose"} & 8;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} & 8 0 0 0
161 0 return 0; #we reported nothing..
162 }
163  
164 =head2 init
165  
166 In this class init just re-initialises the statistics the class
167 gathers. It is called automatically by the constructor. Generally it
168 will be over-ridden by a sub class if needed.
169  
170 =cut
171  
172 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
173 1 my $self=shift;
174 1 my $setting=0;
175  
176 1 $self->{"broken_count"} = $setting;
177 1 $self->{"okay_count"} = $setting;
178 1 $self->{"redirected_count"} = $setting;
179 1 $self->{"not_checked_count"} = $setting;
180 1 $self->{"disallowed_count"} = $setting;
181 1 $self->{"unsupported_count"} = $setting;
182 1 $self->{"unknown_count"} = $setting;
183 }
184  
185 =head2 dummy methods
186  
187 $s->okay $s->not_perfect $s->redirected $s->broken
188 $s->unsupported $s->disallowed
189  
190 These methods are designed to be overriden in derived classes. The
191 appropriate function is called by $s->examine depending on the state
192 of the link. These dummy simply increment a count of each kind of
193 link. The one exception is unknown which also issues a warning.
194  
195 =over
196  
197 =item broken
198  
199 Is called when a link was found broken enough times that we consider
200 it permenantly broken. Controlled $self->{"report_broken"}.
201  
202 =item okay
203  
204 Is called when there link has been checked and found to be okay. This
205 is controlled by $self->{"report_okay"}. N.B. this will exclude links
206 which have never been checked. Use not_checked for those.
207  
208 =item damaged
209  
210 Is called when a link was found broken, but not enough times for us to
211 consider it permanently broken. Controlled by
212 $self->{"report_not_perfect"}.
213  
214 This link is exactly the kind of thing which the linkcontroller system
215 was designed to avoid (links which have not been broken for long and
216 will probably soon be repaired), so probably you don't want to use
217 not_perfect unless for some reason you are reporting links which are
218 okay or the user explicitly asks you to.
219  
220 =item redirected
221  
222 Is called when a redirect was returned by the server serving the
223 resource. Controlled $self->{"report_redirected"}.
224  
225 =item unsupported
226  
227 Is called when for some reason a link couldn't be checked at all.
228 This would typically be some unsupported scheme. Controlled
229 $self->{"report_unsupported"}.
230  
231 =item disallowed
232  
233 Is called when checking of the link is disallowed. The status of the
234 link its self cannot be known. $self->{"report_unsupported"}.
235  
236 =item not_checked
237  
238 Is called when for some reason a link hasn't yet been checked.
239  
240 =item unknown
241  
242 Is called when the status of a link is not understood by this module.
243 This should normally be considered an error condition and this module
244 produces a warning.
245  
246 =back
247  
248 =cut
249  
250 0 ▶sub heading {
Subroutine heading: not called CC=1 SCAR=6.9
Pod undocumented
251 0 1;
252 }
253  
254 0 ▶sub footer {
Subroutine footer: not called CC=1 SCAR=6.9
Pod undocumented
255 0 1;
256 }
257  
258 0 ▶sub broken {
Subroutine broken: not called CC=1 SCAR=6.9
Pod documented
259 0 my $self=shift;
260 0 $self->{"broken_count"} ++;
261 }
262  
263 0 ▶sub okay {
Subroutine okay: not called CC=1 SCAR=6.9
Pod documented
264 0 my $self=shift;
265 0 $self->{"okay_count"} ++;
266 }
267  
268 0 ▶sub damaged {
Subroutine damaged: not called CC=1 SCAR=6.9
Pod documented
269 0 my $self=shift;
270 0 $self->{"damaged_count"} ++;
271 }
272  
273 0 ▶sub redirected {
Subroutine redirected: not called CC=1 SCAR=6.9
Pod documented
274 0 my $self=shift;
275 0 $self->{"redirected_count"} ++;
276 }
277  
278 0 ▶sub not_checked {
Subroutine not_checked: not called CC=1 SCAR=6.9
Pod documented
279 0 my $self=shift;
280 0 $self->{"not_checked_count"} ++;
281 }
282  
283 0 ▶sub disallowed {
Subroutine disallowed: not called CC=1 SCAR=6.9
Pod documented
284 0 my $self=shift;
285 0 $self->{"disallowed_count"} ++;
286 }
287  
288 0 ▶sub unsupported {
Subroutine unsupported: not called CC=1 SCAR=6.9
Pod documented
289 0 my $self=shift;
290 0 $self->{"unsupported_count"} ++;
291 }
292  
293 0 ▶sub unknown {
Subroutine unknown: not called CC=1 SCAR=6.9
Pod documented
294 0 my ($self,$link)=@_;
295 0 warn "link found with an unknown status " . $link->url();
296 0 $self->{"unknown_count"} ++;
297 }
298  
299 =head2 not_found
300  
301 This method should be called from outside the module when a link which
302 should be in the links database isn't there.
303  
304 =cut
305  
306 0 ▶sub not_found {
Subroutine not_found: not called CC=1 SCAR=6.9
Pod documented
307 0 carp "link not found in database";
308 }
309  
310 =head2 all_reports
311  
312 This sets the flag about what we will report for every single kind of
313 report and can be used to make a very noisy or a very quiet reporter.
314  
315 =cut
316  
317 0 ▶sub all_reports {
Subroutine all_reports: not called CC=1 SCAR=6.9
Pod documented
318 0 my $self=shift;
319 0 my $setting=shift;
320  
321 0 $self->{"report_broken"} = $setting;
322 0 $self->{"report_okay"} = $setting;
323 0 $self->{"report_damaged"} = $setting;
324 0 $self->{"report_redirected"} = $setting;
325 0 $self->{"report_not_checked"} = $setting;
326 0 $self->{"report_disallowed"} = $setting;
327 0 $self->{"report_unsupported"} = $setting;
328 0 $self->{"report_unknown"} = $setting;
329  
330 }
331  
332 =head2 default_reports
333  
334 This sets a sensible set of default reporting as follows.
335  
336 $self->{"report_broken"} = 1;
337 $self->{"report_okay"} = 0;
338 $self->{"report_damaged"} = 0;
339 $self->{"report_redirected"} = 1;
340 $self->{"report_not_checked"} = 0;
341 $self->{"report_disallowed"} = 1;
342 $self->{"report_unsupported"} = 0;
343 $self->{"report_unknown"} = 1;
344  
345 You can override it no problem.
346  
347 =cut
348  
349 1 ▶sub default_reports {
Subroutine default_reports: called CC=1 SCAR=0.0
Pod documented
350 1 my $self=shift;
351  
352 1 $self->{"report_broken"} = 1;
353 1 $self->{"report_okay"} = 0;
354 1 $self->{"report_damaged"} = 0;
355 1 $self->{"report_redirected"} = 1;
356 1 $self->{"report_not_checked"} = 0;
357 1 $self->{"report_disallowed"} = 1;
358 1 $self->{"report_unsupported"} = 0;
359 1 $self->{"report_unknown"} = 1;
360 }
361  
362 =head1 report_not_perfect()
363  
364 this is a convenience function which turns on reports for all link
365 apart from those which are okay.
366  
367 =cut
368  
369 0 ▶sub report_not_perfect () {
Subroutine report_not_perfect: not called CC=1 SCAR=6.9
Pod undocumented
370 0 my ($self,$value)=@_;
371 0 $self->all_reports(1);
372 0 $self->report_okay(0);
373 }
374  
375 =head1 report_good()
376  
377 This sets reporting which should show all links which are probably not
378 broken. Currently that defininition includes all redirected links and
379 those that are unsupported etc. Excludes are broken links and ones
380 where checking is disallowed.
381  
382 Since this function is designed for automatic link page
383 maintainainance, however, as any other ways of detecting broken links
384 are discovered, those links will be excluded.
385  
386 =cut
387  
388 0 ▶sub report_good () {
Subroutine report_good: not called CC=1 SCAR=6.9
Pod undocumented
389 0 my ($self,$value)=@_;
390 0 $self->all_reports(1);
391 0 $self->report_broken(0);
392 0 $self->report_disallowed(0);
393 }
394  
395 =head1 INDIVIDUAL REPORTS
396  
397 The following functions allow individual reporting functions to be
398 turned on or off if called with a value (1 turns the report on, 0
399 turns it off).
400  
401 If called with no value they simply return the current status of that
402 report.
403  
404 =over 4
405  
406 =item *
407  
408 report_broken
409  
410 =item *
411  
412 report_okay
413  
414 =item *
415  
416 report_damaged
417  
418 =item *
419  
420 report_redirected
421  
422 =item *
423  
424 report_not_checked
425  
426 =item *
427  
428 report_disallowed
429  
430 =item *
431  
432 report_unsupported
433  
434 =back
435  
436 =cut
437  
438 0 ▶sub report_broken (;$) {
Subroutine report_broken: not called CC=2 SCAR=17.9
Pod undocumented
439 0 my ($self,$value)=@_;
440 0 ▶ $self->{"report_broken"} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
441 0 $self->{"report_broken"};
442 }
443  
444 0 ▶sub report_okay (;$) {
Subroutine report_okay: not called CC=2 SCAR=17.9
Pod undocumented
445 0 my ($self,$value)=@_;
446 0 ▶ $self->{"report_okay"} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
447 0 $self->{"report_okay"};
448 }
449  
450 0 ▶sub report_damaged (;$) {
Subroutine report_damaged: not called CC=2 SCAR=17.9
Pod undocumented
451 0 my ($self,$value)=@_;
452 0 ▶ $self->{"report_damaged"} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
453 0 $self->{"report_damaged"};
454 }
455  
456 0 ▶sub report_redirected (;$) {
Subroutine report_redirected: not called CC=2 SCAR=17.9
Pod undocumented
457 0 my ($self,$value)=@_;
458 0 ▶ $self->{"report_redirected"} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
459 0 $self->{"report_redirected"};
460 }
461  
462 0 ▶sub report_not_checked (;$) {
Subroutine report_not_checked: not called CC=2 SCAR=17.9
Pod undocumented
463 0 my ($self,$value)=@_;
464 0 ▶ $self->{"report_not_checked"} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
465 0 $self->{"report_not_checked"};
466 }
467  
468 0 ▶sub report_disallowed (;$) {
Subroutine report_disallowed: not called CC=2 SCAR=17.9
Pod undocumented
469 0 my ($self,$value)=@_;
470 0 ▶ $self->{"report_disallowed"} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
471 0 $self->{"report_disallowed"};
472 }
473  
474 0 ▶sub report_unsupported (;$) {
Subroutine report_unsupported: not called CC=2 SCAR=17.9
Pod undocumented
475 0 my ($self,$value)=@_;
476 0 ▶ $self->{"report_unsupported"} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
477 0 $self->{"report_unsupported"};
478 }
479  
480 2 1;
481  
« Link/Selector.pm ↑ Summary Link/Repair/Substitutor.pm »