fix malformed call of XSLTParse4Display
[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) = @_;
89     
90     my $uagent = new LWP::UserAgent;
91     $uagent->timeout( $timeout) if $timeout;
92     $self->{ user_agent } = $uagent;
93     $self->{ bad_url    } = { };
94     
95     bless $self, $class;
96     return $self;
97 }
98
99
100 sub check_biblio {
101     my $self            = shift;
102     my $biblionumber    = shift;
103     my $uagent          = $self->{ user_agent   };
104     my $host            = $self->{ host_default };
105     my $bad_url         = $self->{ bad_url      };
106
107     my $record = GetMarcBiblio( $biblionumber ); 
108     return unless $record->field('856');
109
110     my @urls = ();
111     foreach my $field ( $record->field('856') ) {
112         my $url = $field->subfield('u');
113         next unless $url; 
114         $url = "$host/$url" unless $url =~ /^http/;
115         my $check = { url => $url };
116         if ( $bad_url->{ $url } ) {
117             $check->{ is_success } = 1;
118             $check->{ status     } = '500 Site already checked';
119         }
120         else {
121             my $req = HTTP::Request->new( GET => $url );
122             my $res = $uagent->request( $req, sub { die }, 1 );
123             if ( $res->is_success ) {
124                 $check->{ is_success } = 1;
125                 $check->{ status     } = 'ok';
126             }
127             else {
128                 $check->{ is_success } = 0;
129                 $check->{ status     } = $res->status_line;
130                 $bad_url->{ $url     } = 1;
131             }
132         }
133         push @urls, $check;
134     }
135     return \@urls;
136 }
137
138
139
140 package Main;
141
142 use strict;
143 use warnings;
144 use diagnostics;
145 use Carp;
146
147 use Pod::Usage;
148 use Getopt::Long;
149 use C4::Context;
150
151
152
153 my $verbose     = 0;
154 my $help        = 0;
155 my $host        = '';
156 my $host_pro    = '';
157 my $html        = 0;
158 my $uriedit     = "/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=";
159 my $timeout     = 15;
160 GetOptions( 
161     'verbose'       => \$verbose,
162     'html'          => \$html,
163     'help'          => \$help,
164     'host=s'        => \$host,
165     'host-pro=s'    => \$host_pro,
166     'timeout=i',    => \$timeout,
167 );
168
169
170 sub usage {
171     pod2usage( -verbose => 2 );
172     exit;
173
174
175
176 sub bibediturl {
177     my $biblionumber = shift;
178     my $html = "<a href=\"$host_pro$uriedit$biblionumber\">$biblionumber</a>";
179     return $html;
180 }
181
182
183
184 # Check all URLs from all current Koha biblio records
185 #
186 sub check_all_url {
187     my $checker = C4::URL::Checker->new($timeout);
188     $checker->{ host_default }  = $host;
189     
190     my $context = new C4::Context(  );  
191     my $dbh = $context->dbh;
192     my $sth = $dbh->prepare( 
193         "SELECT biblionumber FROM biblioitems WHERE url <> ''" );
194     $sth->execute;
195     if ( $html ) {
196         print <<EOS;
197 <html>
198 <body>
199 <table>
200 EOS
201     }
202     while ( my ($biblionumber) = $sth->fetchrow ) {
203         my $result = $checker->check_biblio( $biblionumber );  
204         next unless $result;  # No URL
205         foreach my $url ( @$result ) {
206             if ( ! $url->{ is_success } || $verbose ) {
207                 print $html
208                       ? "<tr>\n<td>" . bibediturl( $biblionumber ) . 
209                         "</td>\n<td>" . $url->{url} . "</td>\n<td>" . 
210                         $url->{status} . "</td>\n</tr>\n\n"
211                       : "$biblionumber\t" . $url->{ url } . "\t" .
212                         $url->{ status } . "\n";
213             }
214         }
215     }
216     print "</table>\n</body>\n</html>\n" if $html;
217 }
218
219
220 # BEGIN
221
222 usage() if $help;          
223
224 if ( $html && !$host_pro ) {
225     if ( $host ) {
226         $host_pro = $host;
227     }
228     else {
229         print "Error: host-pro parameter or host must be provided in html mode\n";
230         exit;
231     }
232 }
233
234 check_all_url(); 
235
236
237
238 =head1 NAME
239
240 check-url.pl - Check URLs from 856$u field.
241
242 =head1 USAGE
243
244 =over
245
246 =item check-url.pl [--verbose|--help] [--host=http://default.tld] 
247
248 Scan all URLs found in 856$u of bib records 
249 and display if resources are available or not.
250
251 =back
252
253 =head1 PARAMETERS
254
255 =over
256
257 =item B<--host=http://default.tld>
258
259 Server host used when URL doesn't have one, ie doesn't begin with 'http:'. 
260 For example, if --host=http://www.mylib.com, then when 856$u contains 
261 'img/image.jpg', the url checked is: http://www.mylib.com/image.jpg'.
262
263 =item B<--verbose|-v>
264
265 Outputs both successful and failed URLs.
266
267 =item B<--html>
268
269 Formats output in HTML. The result can be redirected to a file
270 accessible by http. This way, it's possible to link directly to biblio
271 record in edit mode. With this parameter B<--host-pro> is required.
272
273 =item B<--host-pro=http://koha-pro.tld>
274
275 Server host used to link to biblio record editing page.
276
277 =item B<--timeout=15>
278
279 Timeout for fetching URLs. By default 15 seconds.
280
281 =item B<--help|-h>
282
283 Print this help page.
284
285 =back
286
287 =cut
288
289