4 # Copyright 2009 Tamil s.a.r.l.
6 # This software is placed under the gnu General Public License, v2
7 # (http://www.gnu.org/licenses/gpl.html)
12 package C4::URL::Checker;
16 C4::URL::Checker - base object for checking URL stored in Koha DB
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";
35 Create a URL Checker. The returned object can be used to set
36 default host variable :
38 my $checker = C4::URL::Checker->new( );
39 $checker->{ host_default } = 'http://mylib.kohalibrary.com';
43 Check all URL from a biblio record. Returns a pointer to an array
44 containing all URLs with checking for each of them.
46 my $checked_urls = $checker->check_biblio( 123 );
48 With 2 URLs, the returned array will look like that:
52 'url' => 'http://mylib.tamil.fr/img/62265_0055B.JPG',
57 'url' => 'http://mylib.tamil.fr//img/62265_0055C.JPG',
59 'status' => '404 - Page not found'
79 $self->{ user_agent } = new LWP::UserAgent;
88 my $biblionumber = shift;
89 my $uagent = $self->{ user_agent };
90 my $host = $self->{ host_default };
92 my $record = GetMarcBiblio( $biblionumber );
93 return unless $record->field('856');
96 foreach my $field ( $record->field('856') ) {
97 my $url = $field->subfield('u');
99 $url = "$host/$url" unless $url =~ /^http/;
100 my $check = { url => $url };
101 my $req = HTTP::Request->new( GET => $url );
102 my $res = $uagent->request( $req, sub { die }, 1 );
103 if ( $res->is_success ) {
104 $check->{ is_success } = 1;
105 $check->{ status } = 'ok';
108 $check->{ is_success } = 0;
109 $check->{ status } = $res->status_line;
111 push( @urls, $check );
136 my $uriedit = "/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=";
138 'verbose' => \$verbose,
142 'host-pro=s' => \$host_pro,
147 pod2usage( -verbose => 2 );
153 my $biblionumber = shift;
154 my $html = "<a href=\"$host_pro$uriedit$biblionumber\">$biblionumber</a>";
160 # Check all URLs from all current Koha biblio records
163 my $checker = C4::URL::Checker->new();
164 $checker->{ host_default } = $host;
166 my $context = new C4::Context( );
167 my $dbh = $context->dbh;
168 my $sth = $dbh->prepare(
169 "SELECT biblionumber FROM biblioitems WHERE url <> ''" );
171 print "<html>\n<body>\n<table>\n" if $html;
172 while ( my ($biblionumber) = $sth->fetchrow ) {
173 my $result = $checker->check_biblio( $biblionumber );
174 next unless $result; # No URL
175 foreach my $url ( @$result ) {
176 if ( ! $url->{ is_success } || $verbose ) {
178 ? "<tr>\n<td>" . bibediturl( $biblionumber ) .
179 "</td>\n<td>" . $url->{url} . "</td>\n<td>" .
180 $url->{status} . "</td>\n</tr>\n\n"
181 : "$biblionumber\t" . $url->{ url } . "\t" .
182 $url->{ status } . "\n";
186 print "</table>\n</body>\n</html>\n" if $html;
194 if ( $html && !$host_pro ) {
199 print "Error: host-pro parameter or host must be provided in html mode\n";
210 check-url.pl - Check URLs from 856$u field.
216 =item check-url.pl [--verbose|--help] [--host=http://default.tld]
218 Scan all URLs found in 856$u of bib records
219 and display if resources are available or not.
227 =item B<--host=http://default.tld>
229 Server host used when URL doesn't have one, ie doesn't begin with 'http:'.
230 For example, if --host=http://www.mylib.com, then when 856$u contains
231 'img/image.jpg', the url checked is: http://www.mylib.com/image.jpg'.
233 =item B<--verbose|-v>
235 Outputs both successful and failed URLs.
239 Formats output in HTML. The result can be redirected to a file
240 accessible by http. This way, it's possible to link directly to biblio
241 record in edit mode. With this parameter B<--host-pro> is required.
243 =item B<--host-pro=http://koha-pro.tld>
245 Server host used to link to biblio record editing page.
249 Print this help page.