Bug 2505 - Add commented use warnings where missing in the misc/ directory
[koha.git] / misc / cronjobs / check-url.pl
blob063eb7e7e8b5c4206bb729aef5d876b1d4e7c846
1 #!/usr/bin/perl
4 # Copyright 2009 Tamil s.a.r.l.
6 # This file is part of Koha.
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.
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.
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.
23 package C4::URL::Checker;
25 =head1 NAME
27 C4::URL::Checker - base object for checking URL stored in Koha DB
29 =head1 SYNOPSIS
31 use C4::URL::Checker;
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";
42 =head1 FUNCTIONS
44 =head2 new
46 Create a URL Checker. The returned object can be used to set
47 default host variable :
49 my $checker = C4::URL::Checker->new( );
50 $checker->{ host_default } = 'http://mylib.kohalibrary.com';
52 =head2 check_biblio
54 Check all URL from a biblio record. Returns a pointer to an array
55 containing all URLs with checking for each of them.
57 my $checked_urls = $checker->check_biblio( 123 );
59 With 2 URLs, the returned array will look like that:
63 'url' => 'http://mylib.tamil.fr/img/62265_0055B.JPG',
64 'is_success' => 1,
65 'status' => 'ok'
68 'url' => 'http://mylib.tamil.fr//img/62265_0055C.JPG',
69 'is_success' => 0,
70 'status' => '404 - Page not found'
75 =cut
77 use strict;
78 use warnings;
79 use LWP::UserAgent;
80 use HTTP::Request;
81 use C4::Biblio;
85 sub new {
87 my $self = {};
88 my ($class, $timeout) = @_;
90 my $uagent = new LWP::UserAgent;
91 $uagent->timeout( $timeout) if $timeout;
92 $self->{ user_agent } = $uagent;
93 $self->{ bad_url } = { };
95 bless $self, $class;
96 return $self;
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 };
107 my $record = GetMarcBiblio( $biblionumber );
108 return unless $record->field('856');
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';
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';
127 else {
128 $check->{ is_success } = 0;
129 $check->{ status } = $res->status_line;
130 $bad_url->{ $url } = 1;
133 push @urls, $check;
135 return \@urls;
140 package Main;
142 use strict;
143 use warnings;
144 use diagnostics;
145 use Carp;
147 use Pod::Usage;
148 use Getopt::Long;
149 use C4::Context;
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,
170 sub usage {
171 pod2usage( -verbose => 2 );
172 exit;
176 sub bibediturl {
177 my $biblionumber = shift;
178 my $html = "<a href=\"$host_pro$uriedit$biblionumber\">$biblionumber</a>";
179 return $html;
184 # Check all URLs from all current Koha biblio records
186 sub check_all_url {
187 my $checker = C4::URL::Checker->new($timeout);
188 $checker->{ host_default } = $host;
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>
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";
216 print "</table>\n</body>\n</html>\n" if $html;
220 # BEGIN
222 usage() if $help;
224 if ( $html && !$host_pro ) {
225 if ( $host ) {
226 $host_pro = $host;
228 else {
229 print "Error: host-pro parameter or host must be provided in html mode\n";
230 exit;
234 check_all_url();
238 =head1 NAME
240 check-url.pl - Check URLs from 856$u field.
242 =head1 USAGE
244 =over
246 =item check-url.pl [--verbose|--help] [--host=http://default.tld]
248 Scan all URLs found in 856$u of bib records
249 and display if resources are available or not.
251 =back
253 =head1 PARAMETERS
255 =over
257 =item B<--host=http://default.tld>
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'.
263 =item B<--verbose|-v>
265 Outputs both successful and failed URLs.
267 =item B<--html>
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.
273 =item B<--host-pro=http://koha-pro.tld>
275 Server host used to link to biblio record editing page.
277 =item B<--timeout=15>
279 Timeout for fetching URLs. By default 15 seconds.
281 =item B<--help|-h>
283 Print this help page.
285 =back
287 =cut