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