Bug 16699: Move Swagger-related files to api/v1/swagger
[koha.git] / misc / cronjobs / check-url.pl
blob71885ab234d6ddd5e8d6a43c60e08352edea6a70
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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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, $agent) = @_;
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 } = { };
96 bless $self, $class;
97 return $self;
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 };
108 my $record = GetMarcBiblio( $biblionumber );
109 return unless $record->field('856');
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';
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';
128 else {
129 $check->{ is_success } = 0;
130 $check->{ status } = $res->status_line;
131 $bad_url->{ $url } = 1;
134 push @urls, $check;
136 return \@urls;
141 package Main;
143 use strict;
144 use warnings;
145 use diagnostics;
146 use Carp;
148 use Pod::Usage;
149 use Getopt::Long;
150 use C4::Context;
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,
173 sub usage {
174 pod2usage( -verbose => 2 );
175 exit;
179 sub bibediturl {
180 my $biblionumber = shift;
181 my $html = "<a href=\"$host_pro$uriedit$biblionumber\">$biblionumber</a>";
182 return $html;
187 # Check all URLs from all current Koha biblio records
189 sub check_all_url {
190 my $checker = C4::URL::Checker->new($timeout,$agent);
191 $checker->{ host_default } = $host;
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>
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";
219 print "</table>\n</body>\n</html>\n" if $html;
223 # BEGIN
225 usage() if $help;
227 if ( $html && !$host_pro ) {
228 if ( $host ) {
229 $host_pro = $host;
231 else {
232 print "Error: host-pro parameter or host must be provided in html mode\n";
233 exit;
237 check_all_url();
241 =head1 NAME
243 check-url.pl - Check URLs from 856$u field.
245 =head1 USAGE
247 =over
249 =item check-url.pl [--verbose|--help] [--agent=agent-string] [--host=http://default.tld]
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.
255 =back
257 =head1 PARAMETERS
259 =over
261 =item B<--host=http://default.tld>
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'.
267 =item B<--verbose|-v>
269 Outputs both successful and failed URLs.
271 =item B<--html>
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.
277 =item B<--host-pro=http://koha-pro.tld>
279 Server host used to link to biblio record editing page.
281 =item B<--agent=agent-string>
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.
287 =item B<--timeout=15>
289 Timeout for fetching URLs. By default 15 seconds.
291 =item B<--help|-h>
293 Print this help page.
295 =back
297 =cut