Bug 7963 Parallel HTTP requests when checking URLs
[koha.git] / misc / cronjobs / check-url.pl
1 #!/usr/bin/perl
2
3 #
4 # Copyright 2009 Tamil s.a.r.l.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21
22
23 package C4::URL::Checker;
24
25 =head1 NAME 
26
27 C4::URL::Checker - base object for checking URL stored in Koha DB
28
29 =head1 SYNOPSIS
30
31  use C4::URL::Checker;
32
33  my $checker = C4::URL::Checker->new( );
34  $checker->{ host_default } = 'http://mylib.kohalibrary.com';
35  my $checked_urls = $checker->check_biblio( 123 );
36  foreach my $url ( @$checked_urls ) {
37      print "url:        ", $url->{ url        }, "\n",
38            "is_success: ", $url->{ is_success }, "\n",
39            "status:     ", $url->{ status     }, "\n";
40  }
41
42 =head1 FUNCTIONS
43
44 =head2 new
45
46 Create a URL Checker. The returned object can be used to set
47 default host variable :
48
49  my $checker = C4::URL::Checker->new( );
50  $checker->{ host_default } = 'http://mylib.kohalibrary.com';
51
52 =head2 check_biblio
53
54 Check all URL from a biblio record. Returns a pointer to an array
55 containing all URLs with checking for each of them.
56
57  my $checked_urls = $checker->check_biblio( 123 );
58
59 With 2 URLs, the returned array will look like that:
60
61   [
62     {
63       'url' => 'http://mylib.tamil.fr/img/62265_0055B.JPG',
64       'is_success' => 1,
65       'status' => 'ok'
66     },
67     {
68       'url' => 'http://mylib.tamil.fr//img/62265_0055C.JPG',
69       'is_success' => 0,
70       'status' => '404 - Page not found'
71     }
72   ],
73   
74
75 =cut
76
77 use strict;
78 use warnings;
79 use LWP::UserAgent;
80 use HTTP::Request;
81 use C4::Biblio;
82
83
84
85 sub new {
86
87     my $self = {};
88     my ($class, $timeout, $agent) = @_;
89     
90     my $uagent = new LWP::UserAgent;
91     $uagent->agent( $agent ) if $agent;
92     $uagent->timeout( $timeout) if $timeout;
93     $self->{ user_agent } = $uagent;
94     $self->{ bad_url    } = { };
95     
96     bless $self, $class;
97     return $self;
98 }
99
100
101 sub check_biblio {
102     my $self            = shift;
103     my $biblionumber    = shift;
104     my $uagent          = $self->{ user_agent   };
105     my $host            = $self->{ host_default };
106     my $bad_url         = $self->{ bad_url      };
107
108     my $record = GetMarcBiblio( $biblionumber ); 
109     return unless $record->field('856');
110
111     my @urls = ();
112     foreach my $field ( $record->field('856') ) {
113         my $url = $field->subfield('u');
114         next unless $url; 
115         $url = "$host/$url" unless $url =~ /^http/;
116         my $check = { url => $url };
117         if ( $bad_url->{ $url } ) {
118             $check->{ is_success } = 1;
119             $check->{ status     } = '500 Site already checked';
120         }
121         else {
122             my $req = HTTP::Request->new( GET => $url );
123             my $res = $uagent->request( $req, sub { die }, 1 );
124             if ( $res->is_success ) {
125                 $check->{ is_success } = 1;
126                 $check->{ status     } = 'ok';
127             }
128             else {
129                 $check->{ is_success } = 0;
130                 $check->{ status     } = $res->status_line;
131                 $bad_url->{ $url     } = 1;
132             }
133         }
134         push @urls, $check;
135     }
136     return \@urls;
137 }
138
139
140
141 package Main;
142
143 use strict;
144 use warnings;
145 use diagnostics;
146 use Carp;
147
148 use Pod::Usage;
149 use Getopt::Long;
150 use C4::Context;
151
152
153
154 my $verbose     = 0;
155 my $help        = 0;
156 my $host        = '';
157 my $host_pro    = '';
158 my $html        = 0;
159 my $uriedit     = "/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=";
160 my $agent       = '';
161 my $timeout     = 15;
162 GetOptions( 
163     'verbose'       => \$verbose,
164     'html'          => \$html,
165     'help'          => \$help,
166     'host=s'        => \$host,
167     'host-pro=s'    => \$host_pro,
168     'agent=s'       => \$agent,
169     'timeout=i',    => \$timeout,
170 );
171
172
173 sub usage {
174     pod2usage( -verbose => 2 );
175     exit;
176
177
178
179 sub bibediturl {
180     my $biblionumber = shift;
181     my $html = "<a href=\"$host_pro$uriedit$biblionumber\">$biblionumber</a>";
182     return $html;
183 }
184
185
186
187 # Check all URLs from all current Koha biblio records
188 #
189 sub check_all_url {
190     my $checker = C4::URL::Checker->new($timeout,$agent);
191     $checker->{ host_default }  = $host;
192     
193     my $context = new C4::Context(  );  
194     my $dbh = $context->dbh;
195     my $sth = $dbh->prepare( 
196         "SELECT biblionumber FROM biblioitems WHERE url <> ''" );
197     $sth->execute;
198     if ( $html ) {
199         print <<EOS;
200 <html>
201 <body>
202 <table>
203 EOS
204     }
205     while ( my ($biblionumber) = $sth->fetchrow ) {
206         my $result = $checker->check_biblio( $biblionumber );  
207         next unless $result;  # No URL
208         foreach my $url ( @$result ) {
209             if ( ! $url->{ is_success } || $verbose ) {
210                 print $html
211                       ? "<tr>\n<td>" . bibediturl( $biblionumber ) . 
212                         "</td>\n<td>" . $url->{url} . "</td>\n<td>" . 
213                         $url->{status} . "</td>\n</tr>\n\n"
214                       : "$biblionumber\t" . $url->{ url } . "\t" .
215                         $url->{ status } . "\n";
216             }
217         }
218     }
219     print "</table>\n</body>\n</html>\n" if $html;
220 }
221
222
223 # BEGIN
224
225 usage() if $help;          
226
227 if ( $html && !$host_pro ) {
228     if ( $host ) {
229         $host_pro = $host;
230     }
231     else {
232         print "Error: host-pro parameter or host must be provided in html mode\n";
233         exit;
234     }
235 }
236
237 check_all_url(); 
238
239
240
241 =head1 NAME
242
243 check-url.pl - Check URLs from 856$u field.
244
245 =head1 USAGE
246
247 =over
248
249 =item check-url.pl [--verbose|--help] [--agent=agent-string] [--host=http://default.tld]
250
251 Scan all URLs found in 856$u of bib records 
252 and display if resources are available or not.
253 This script is deprecated. You should rather use check-url-quick.pl.
254
255 =back
256
257 =head1 PARAMETERS
258
259 =over
260
261 =item B<--host=http://default.tld>
262
263 Server host used when URL doesn't have one, ie doesn't begin with 'http:'. 
264 For example, if --host=http://www.mylib.com, then when 856$u contains 
265 'img/image.jpg', the url checked is: http://www.mylib.com/image.jpg'.
266
267 =item B<--verbose|-v>
268
269 Outputs both successful and failed URLs.
270
271 =item B<--html>
272
273 Formats output in HTML. The result can be redirected to a file
274 accessible by http. This way, it's possible to link directly to biblio
275 record in edit mode. With this parameter B<--host-pro> is required.
276
277 =item B<--host-pro=http://koha-pro.tld>
278
279 Server host used to link to biblio record editing page.
280
281 =item B<--agent=agent-string>
282
283 Change default libwww user-agent string to custom.  Some sites do
284 not like libwww user-agent and return false 40x failure codes,
285 so this allows Koha to report itself as Koha, or a browser.
286
287 =item B<--timeout=15>
288
289 Timeout for fetching URLs. By default 15 seconds.
290
291 =item B<--help|-h>
292
293 Print this help page.
294
295 =back
296
297 =cut
298
299