WE_Frontend/LinkChecker.pm

filter: Statementstmt 11.5% 21 / 182 Branchbran 0.0% 0 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 28.5% 6 / 21 Podpod 71.4% 5 / 7 totaltotal 11.0% 32 / 290 CC 51 SCAR 41.8
CC
51
Cov
8%
check
156.0
_check
156.0
check_html_header
42.0
CRAP 65.4
« WE_Frontend/Plugin/Linear.pm ↑ Summary WE_Frontend/MainAny.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: LinkChecker.pm,v 1.8 2005/11/04 00:32:48 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2002 Online Office Berlin. All rights reserved.
8 # Copyright (C) 2002,2003 Slaven Rezic.
9 # This is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License, see the file COPYING.
11  
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://we-framework.sourceforge.net
15 #
16  
17 package WE_Frontend::LinkChecker;
18  
19 1 ▶use HTML::LinkExtor;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use vars qw($VERSION $VERBOSE);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 $VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/);
26  
27 1 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 __PACKAGE__->mk_accessors(qw(Restrict Follow Ignore Url Pending SeenOk SeenError Ua));
29  
30 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
31 0 my($class, %args) = @_;
32 0 my $self = {};
33 0 bless $self, $class;
34  
35 0 $self->Follow(undef);
36 0 $self->Restrict(undef);
37 0 $self->Ignore(undef);
38 0 ▶ while(my($k,$v) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 0 0 0
39 0 $self->{ucfirst(substr($k,1))} = $v;
40 }
41 0 $self->SeenOk({});
42 0 $self->SeenError({});
43 0 $self->Pending([]);
44 0 $self;
45 }
46  
47 0 ▶sub get_all_urls {
Subroutine get_all_urls: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my $self = shift;
49 0 ▶ if (ref $self->Url eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->Url eq 'ARRAY') { } 0 0 0
50 0 @{$self->Url};
51 } else {
52 0 $self->Url;
53 }
54 }
55  
56 0 ▶sub check_html {
Subroutine check_html: not called CC=3 SCAR=24.8
Pod documented
57 0 my($self) = @_;
58 0 my $html = "";
59 0 $html .= "<h1>Linkcheck results</h1>";
60 0 $html .= "<h2>Configuration</h2>";
61 0 $html .= $self->check_html_header;
62  
63 0 my %fail_urls = $self->check;
64 0 $html .= "<h2>Link errors</h2>";
65 0 ▶ if (%fail_urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (%fail_urls) { } 0 0 0
66 0 foreach my $caller (sort keys %fail_urls) {
67 0 $html .= $self->output_failed_url_as_html($caller, $fail_urls{$caller});
68 }
69 } else {
70 0 $html .= "None.<p>\n";
71 }
72 0 $html .= "<a href='javascript:history.back()'>Back</a>";
73 0 $html;
74 }
75  
76 0 ▶sub check_tt {
Subroutine check_tt: not called CC=3 SCAR=24.8
Pod documented
77 0 my($self, $tt, $template, $extra_args) = @_;
78 0 my $html;
79 0 ▶ $tt->process($template, { self => $self,
Branch: 2 branches
BranchTrueFalseTotal
$tt->process($template, {"self", $self, "fail_urls", {$self->check}, $extra_args ? %$extra_args : ()}, \$html) or die $tt->error 0 0 0
$extra_args ? : 0 0 0
80 fail_urls => { $self->check },
81 ($extra_args ? %$extra_args : ()),
82 }, \$html)
83 or die $tt->error;
84 0 $html;
85 }
86  
87 0 ▶sub check_html_header {
Subroutine check_html_header: not called CC=6 SCAR=37.4
Pod documented
88 0 my $self = shift;
89 0 my $html = "";
90 0 $html .= "Checked start URLs:<br><ul>\n";
91 0 foreach my $url ($self->get_all_urls) {
92 0 $html .= "<li> <a href=\"$url\">$url</a>\n"; # XXX escape
93 }
94 0 $html .= "</ul>\n";
95  
96 0 $html .= "Restrict to:<br><ul>\n";
97 0 ▶ if (!$self->Restrict) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->Restrict) { } 0 0 0
98 0 $html .= "<li> none\n";
99 } else {
100 0 foreach my $url (@{ $self->Restrict }) {
101 0 $html .= "<li> $url\n"; # XXX escape
102 }
103 }
104 0 $html .= "</ul>\n";
105  
106 0 $html .= "Follow:<br><ul>\n";
107 0 ▶ if (!$self->Follow) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->Follow) { } 0 0 0
108 0 $html .= "<li> all non-restricted\n";
109 } else {
110 0 foreach my $url (@{ $self->Follow }) {
111 0 $html .= "<li> $url\n"; # XXX escape
112 }
113 }
114 0 $html .= "</ul>\n";
115 0 $html;
116 }
117  
118 0 ▶sub output_failed_url_as_html {
Subroutine output_failed_url_as_html: not called CC=2 SCAR=17.9
Pod undocumented
119 0 my($self, $caller, $failures) = @_;
120 0 my $html = "<h2>" . _we_page_link($caller) . "</h2>\n<ul>";
121 0 foreach my $fail_url (sort keys %$failures) {
122 0 $html .= "<li>" . _we_failed_page($fail_url) . " (Error: @{[ $self->SeenError->{$fail_url}->{Code} ]})\n"; # XXX HTML escape
123 }
124 0 $html .= "</ul>\n";
125 0 $html;
126 }
127  
128 0 ▶sub _we_failed_page {
Subroutine _we_failed_page: not called CC=6 SCAR=37.4
129 0 my $url = shift;
130 # XXX lang-dependent strings
131 0 ▶ if ($url =~ m|/images/|) {
Branch: 5 branches
BranchTrueFalseTotal
if ($url =~ m[/images/]) { } 0 0 0
elsif ($url =~ m[/(site_)?photos/]) { } 0 0 0
elsif ($url =~ m[/videos/]) { } 0 0 0
elsif ($url =~ m[/download/]) { } 0 0 0
elsif ($url =~ m[/headlines/]) { } 0 0 0
132 0 "internal image ($url)";
133 } elsif ($url =~ m|/(site_)?photos/|) {
134 0 "embedded photo ($url)";
135 } elsif ($url =~ m|/videos/|) {
136 0 "video link ($url)";
137 } elsif ($url =~ m|/download/|) {
138 0 "download link ($url)";
139 } elsif ($url =~ m|/headlines/|) {
140 0 "headline image ($url)";
141 } else {
142 0 $url;
143 }
144 }
145  
146 # XXX do not hardcode any code or URLs!!!
147 0 ▶sub _we_page_link {
Subroutine _we_page_link: not called CC=3 SCAR=24.8
148 # XXX html escape
149 0 my $url = shift;
150 0 ▶ if ($url =~ m|/html/[^/]+/(\d+)\.html$|) {
Branch: 2 branches
BranchTrueFalseTotal
if ($url =~ m[/html/[^/]+/(\d+)\.html$]) { } 0 0 0
elsif ($url eq 'START') { } 0 0 0
151 0 my $id = $1;
152 0 q{<a target="_blank" href="}.$url.q{">}.$url.q{</a> (<a target="_blank" href="http://$ENV{SERVER_NAME}/cgi-bin/we_redisys.cgi?pageid=}.$id.q{&goto=pageeditorframe">EDIT</a>)};
153 ## XXX opener geht nach dem ersten Mal verloren
154 #q{<a href="#" onclick="opener.top.location.href = 'http://mom.intra.onlineoffice.de/~eserte/sample/wwwroot/cgi-bin/we_redisys.cgi?pageid=}.$id.q{&goto=pageeditorframe'; return false;">}.$url.q{</a>};
155 ## der ursprüngliche Frameaufbau ist nicht mehr da
156 #q{<a href="#" onclick="opener.top.frames['cms_explorer_js'].action('} . $id . q{','cms_explorer_js','','edit','',''); return false;" onmouseover="window.status='edit page with id } . $id . q{'; return true;">} . $url . q{</a>};
157 } elsif ($url eq 'START') {
158 0 $url;
159 } else {
160 0 "<a href=\"$url\">$url</a>";
161 }
162 }
163  
164 0 ▶sub check {
Subroutine check: not called CC=12 SCAR=50.5
Pod documented
165 0 my $self = shift;
166 0 my(%args) = @_;
167  
168 0 my %fail_urls;
169 0 foreach my $url ($self->get_all_urls) {
170 0 push @{ $self->Pending }, {Url => $url,
171 Caller => "START"};
172 }
173  
174 0 ▶ if (!$self->Ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->Ua) 0 0 0
175 0 $self->Ua(LWP::UserAgent->new);
176 0 $self->Ua->timeout(10);
177 0 $self->Ua->env_proxy;
178 }
179 0 ▶ while(@{ $self->Pending }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->Pending;}) 0 0 0
180 0 my $o = shift @{ $self->Pending };
181 0 my $new_url = $o->{Url};
182 0 my $caller = $o->{Caller};
183  
184 # Check whether already checked
185 0 ▶ if ($self->SeenError->{$new_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->SeenError->{$new_url}) 0 0 0
186 0 $fail_urls{$caller}->{$new_url}++;
187 0 next;
188 }
189 0 ▶ next if ($self->SeenOk->{$new_url});
Branch: 1 branch
BranchTrueFalseTotal
if $self->SeenOk->{$new_url} 0 0 0
190  
191 0 ▶ warn "Check $new_url...\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $WE_Frontend::LinkChecker::VERBOSE 0 0 0
192 0 ▶ if ($self->_restricted($new_url)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_restricted($new_url)) 0 0 0
193 0 ▶ warn "$new_url is restricted\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $WE_Frontend::LinkChecker::VERBOSE 0 0 0
194 0 next;
195 }
196 0 ▶ if ($self->_ignored($new_url)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_ignored($new_url)) 0 0 0
197 0 ▶ warn "$new_url is ignored\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $WE_Frontend::LinkChecker::VERBOSE 0 0 0
198 0 next;
199 }
200 0 my $failure = $self->_check($new_url);
201 0 ▶ if ($failure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failure) { } 0 0 0
202 0 $fail_urls{$caller}->{$new_url}++;
203 0 $self->SeenError->{$new_url} = $failure;
204 } else {
205 0 $self->SeenOk->{$new_url}++;
206 }
207 }
208 0 %fail_urls;
209 }
210  
211 0 ▶sub _check {
Subroutine _check: not called CC=12 SCAR=50.5
212 0 my($self, $url) = @_;
213 0 $url = _canonize_url($url);
214  
215 0 ▶ if ($self->_nofollow($url)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_nofollow($url)) 0 0 0
216 0 my $res = $self->Ua->request(HTTP::Request->new(HEAD => $url));
217 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
218 0 ▶ warn "$url returned @{[ $res->code ]}\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $WE_Frontend::LinkChecker::VERBOSE 0 0 0
219 0 return { Code => $res->code,
220 Error => $res->message };
221 }
222 0 ▶ warn "Do not follow $url\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $WE_Frontend::LinkChecker::VERBOSE 0 0 0
223 0 return;
224 }
225  
226 0 my $p = HTML::LinkExtor->new;
227 my $res = $self->Ua->request(HTTP::Request->new(GET => $url),
228 0 ▶ sub {$p->parse($_[0])});
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 ▶ if ($res->content_type ne 'text/html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->content_type ne "text/html") 0 0 0
230 0 ▶ warn "$url is not text/html\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $WE_Frontend::LinkChecker::VERBOSE 0 0 0
231 0 return;
232 }
233 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
234 0 ▶ warn "$url returned @{[ $res->code ]}\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $WE_Frontend::LinkChecker::VERBOSE 0 0 0
235 0 return { Code => $res->code,
236 Error => $res->message };
237 }
238  
239 0 my $base = $res->base;
240  
241 0 my %links;
242 0 foreach my $e ($p->links) {
243 0 ▶ for(my $i=2; $i<=$#$e; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $e->$#*) 0 0 0
244 0 ▶ next if $e->[$i] =~ /^javascript:/;
Branch: 1 branch
BranchTrueFalseTotal
if $e->[$i] =~ /^javascript:/ 0 0 0
245 0 my $checkurl = _canonize_url(URI->new_abs($e->[$i], $base)->as_string);
246 0 $links{$checkurl}++;
247 }
248 }
249 0 push @{ $self->Pending}, map { +{Url => $_, Caller => $url} }
250 sort keys %links;
251 0 undef;
252 }
253  
254 0 ▶sub _canonize_url {
Subroutine _canonize_url: not called CC=1 SCAR=6.9
255 0 my $url = shift;
256 0 $url =~ s/\#.*//; # XXX better way?
257 0 $url;
258 }
259  
260 0 ▶sub _restricted {
Subroutine _restricted: not called CC=4 SCAR=30.0
261 0 my($self, $url) = @_;
262 0 ▶ return 0 if !$self->Restrict;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->Restrict 0 0 0
263 0 foreach my $restr (@{ $self->Restrict }) {
264 0 ▶ return 0 if $url =~ /$restr/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /$restr/ 0 0 0
265 }
266 0 1;
267 }
268  
269 0 ▶sub _ignored {
Subroutine _ignored: not called CC=4 SCAR=30.0
270 0 my($self, $url) = @_;
271 0 ▶ return 0 if !$self->Ignore;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->Ignore 0 0 0
272 0 foreach my $ignore (@{ $self->Ignore }) {
273 0 ▶ return 1 if $url =~ /$ignore/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /$ignore/ 0 0 0
274 }
275 0 0;
276 }
277  
278 0 ▶sub _nofollow {
Subroutine _nofollow: not called CC=4 SCAR=30.0
279 0 my($self, $url) = @_;
280 0 ▶ return 0 if !$self->Follow;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->Follow 0 0 0
281 0 foreach my $restr (@{ $self->Follow }) {
282 0 ▶ return 0 if $url =~ /$restr/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /$restr/ 0 0 0
283 }
284 0 1;
285 }
286  
287 1 1;
288  
289 __END__
« WE_Frontend/Plugin/Linear.pm ↑ Summary WE_Frontend/MainAny.pm »