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
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;
27 C4::URL::Checker - base object for checking URL stored in Koha DB
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";
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';
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',
68 'url' => 'http://mylib.tamil.fr//img/62265_0055C.JPG',
70 'status' => '404 - Page not found'
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 } = { };
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');
111 foreach my $field ( $record->field('856') ) {
112 my $url = $field->subfield('u');
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';
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';
128 $check->{ is_success } = 0;
129 $check->{ status } = $res->status_line;
130 $bad_url->{ $url } = 1;
158 my $uriedit = "/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=";
161 'verbose' => \$verbose,
165 'host-pro=s' => \$host_pro,
166 'timeout=i', => \$timeout,
171 pod2usage( -verbose => 2 );
177 my $biblionumber = shift;
178 my $html = "<a href=\"$host_pro$uriedit$biblionumber\">$biblionumber</a>";
184 # Check all URLs from all current Koha biblio records
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 <> ''" );
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 ) {
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;
224 if ( $html && !$host_pro ) {
229 print "Error: host-pro parameter or host must be provided in html mode\n";
240 check-url.pl - Check URLs from 856$u field.
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.
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.
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.
283 Print this help page.