Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2012 Tamil s.a.r.l. |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 2 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use 5.010; |
21 |
use utf8; |
22 |
use strict; |
23 |
use warnings; |
24 |
use Pod::Usage; |
25 |
use Getopt::Long; |
26 |
use C4::Context; |
27 |
use C4::Biblio; |
28 |
use AnyEvent; |
29 |
use AnyEvent::HTTP; |
30 |
|
31 |
my ($verbose, $help, $html) = (0, 0, 0); |
32 |
my ($host, $host_pro) = ('', ''); |
33 |
my ($timeout, $maxconn) = (10, 200); |
34 |
my $uriedit = "/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber="; |
35 |
GetOptions( |
36 |
'verbose' => \$verbose, |
37 |
'html' => \$html, |
38 |
'help' => \$help, |
39 |
'host=s' => \$host, |
40 |
'host-pro=s' => \$host_pro, |
41 |
'timeout=i' => \$timeout, |
42 |
'maxconn=i' => \$maxconn, |
43 |
); |
44 |
|
45 |
|
46 |
sub usage { |
47 |
pod2usage( -verbose => 2 ); |
48 |
exit; |
49 |
} |
50 |
|
51 |
|
52 |
# Check all URLs from all current Koha biblio records |
53 |
|
54 |
sub check_all_url { |
55 |
my $sth = C4::Context->dbh->prepare( |
56 |
"SELECT biblionumber FROM biblioitems WHERE url <> '' LIMIT 20" ); |
57 |
$sth->execute; |
58 |
|
59 |
my $countconn = 0; |
60 |
my $cv = AnyEvent->condvar; |
61 |
say "<html>\n<body>\n<table>" if $html; |
62 |
my $idle = AnyEvent->idle( |
63 |
cb => sub { |
64 |
return if $countconn > $maxconn; |
65 |
while ( my ($biblionumber) = $sth->fetchrow ) { |
66 |
my $record = GetMarcBiblio( $biblionumber ); |
67 |
next unless $record->field('856'); |
68 |
foreach my $field ( $record->field('856') ) { |
69 |
my $url = $field->subfield('u'); |
70 |
next unless $url; |
71 |
$url = "$host/$url" unless $url =~ /^http/; |
72 |
$countconn++; |
73 |
http_request( |
74 |
HEAD => $url, |
75 |
headers => { 'user-agent' => 'Mozilla/5.0 (compatible; U; Koha checkurl)' }, |
76 |
timeout => $timeout, |
77 |
sub { |
78 |
my (undef, $hdr) = @_; |
79 |
$countconn--; |
80 |
if ( $hdr->{Status} !~ /^2/ || $verbose ) { |
81 |
print $html |
82 |
? "<tr>\n<td><a href=\"" . |
83 |
$host_pro . $uriedit . $biblionumber. |
84 |
"\">$biblionumber</a>" . |
85 |
"</td>\n<td>" . $url . "</td>\n<td>" . |
86 |
"$hdr->{Status} $hdr->{Reason}</td>\n</tr>\n\n" |
87 |
: "$biblionumber\t" . $url . "\t" . |
88 |
"$hdr->{Status} $hdr->{Reason} \n"; |
89 |
} |
90 |
}, |
91 |
); |
92 |
} |
93 |
return; |
94 |
} |
95 |
$cv->send; |
96 |
} |
97 |
); |
98 |
$cv->recv; |
99 |
$idle = undef; |
100 |
|
101 |
# Give $timeout to pending requests to send their result |
102 |
$cv = AnyEvent->condvar; |
103 |
my $timer = AnyEvent->timer( after => $timeout + 1, cb => sub { $cv->send } ); |
104 |
$cv->recv; |
105 |
say "</table>\n</body>\n</html>" if $html; |
106 |
} |
107 |
|
108 |
|
109 |
usage() if $help; |
110 |
|
111 |
if ( $html && !$host_pro ) { |
112 |
if ( $host ) { |
113 |
$host_pro = $host; |
114 |
} |
115 |
else { |
116 |
say "Error: host-pro parameter or host must be provided in html mode"; |
117 |
exit; |
118 |
} |
119 |
} |
120 |
|
121 |
check_all_url(); |
122 |
|
123 |
|
124 |
=head1 NAME |
125 |
|
126 |
checkurl - Check URLs from 856$u field. |
127 |
|
128 |
=head1 USAGE |
129 |
|
130 |
=over |
131 |
|
132 |
=item checkurl [--verbose|--help] [--host=http://default.tld] |
133 |
|
134 |
Scan all URLs found in 856$u of bib records and display if resources are |
135 |
available or not. HTTP requests are sent in parallel for efficiency. |
136 |
|
137 |
=back |
138 |
|
139 |
=head1 PARAMETERS |
140 |
|
141 |
=over |
142 |
|
143 |
=item B<--host=http://default.tld> |
144 |
|
145 |
Server host used when URL doesn't have one, ie doesn't begin with 'http:'. |
146 |
For example, if --host=http://www.mylib.com, then when 856$u contains |
147 |
'img/image.jpg', the url checked is: http://www.mylib.com/image.jpg'. |
148 |
|
149 |
=item B<--verbose|-v> |
150 |
|
151 |
Outputs both successful and failed URLs. |
152 |
|
153 |
=item B<--html> |
154 |
|
155 |
Formats output in HTML. The result can be redirected to a file |
156 |
accessible by http. This way, it's possible to link directly to biblio |
157 |
record in edit mode. With this parameter B<--host-pro> is required. |
158 |
|
159 |
=item B<--host-pro=http://koha-pro.tld> |
160 |
|
161 |
Server host used to link to biblio record editing page. |
162 |
|
163 |
=item B<--timeout=10> |
164 |
|
165 |
Timeout for fetching URLs. By default 10 seconds. |
166 |
|
167 |
=item B<--maxconn=1000> |
168 |
|
169 |
Number of simulaneous HTTP requests. By default 200 connexions. |
170 |
|
171 |
=item B<--help|-h> |
172 |
|
173 |
Print this help page. |
174 |
|
175 |
=back |
176 |
|
177 |
=cut |
178 |
|
179 |
|