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'
77 my ($class, $timeout) = @_;
79 my $uagent = new LWP::UserAgent;
80 $uagent->timeout( $timeout) if $timeout;
81 $self->{ user_agent } = $uagent;
82 $self->{ bad_url } = { };
91 my $biblionumber = shift;
92 my $uagent = $self->{ user_agent };
93 my $host = $self->{ host_default };
94 my $bad_url = $self->{ bad_url };
96 my $record = GetMarcBiblio( $biblionumber );
97 return unless $record->field('856');
100 foreach my $field ( $record->field('856') ) {
101 my $url = $field->subfield('u');
103 $url = "$host/$url" unless $url =~ /^http/;
104 my $check = { url => $url };
105 if ( $bad_url->{ $url } ) {
106 $check->{ is_success } = 1;
107 $check->{ status } = '500 Site already checked';
110 my $req = HTTP::Request->new( GET => $url );
111 my $res = $uagent->request( $req, sub { die }, 1 );
112 if ( $res->is_success ) {
113 $check->{ is_success } = 1;
114 $check->{ status } = 'ok';
117 $check->{ is_success } = 0;
118 $check->{ status } = $res->status_line;
119 $bad_url->{ $url } = 1;
147 my $uriedit = "/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=";
150 'verbose' => \$verbose,
154 'host-pro=s' => \$host_pro,
155 'timeout=i', => \$timeout,
160 pod2usage( -verbose => 2 );
166 my $biblionumber = shift;
167 my $html = "<a href=\"$host_pro$uriedit$biblionumber\">$biblionumber</a>";
173 # Check all URLs from all current Koha biblio records
176 my $checker = C4::URL::Checker->new($timeout);
177 $checker->{ host_default } = $host;
179 my $context = new C4::Context( );
180 my $dbh = $context->dbh;
181 my $sth = $dbh->prepare(
182 "SELECT biblionumber FROM biblioitems WHERE url <> ''" );
191 while ( my ($biblionumber) = $sth->fetchrow ) {
192 my $result = $checker->check_biblio( $biblionumber );
193 next unless $result; # No URL
194 foreach my $url ( @$result ) {
195 if ( ! $url->{ is_success } || $verbose ) {
197 ? "<tr>\n<td>" . bibediturl( $biblionumber ) .
198 "</td>\n<td>" . $url->{url} . "</td>\n<td>" .
199 $url->{status} . "</td>\n</tr>\n\n"
200 : "$biblionumber\t" . $url->{ url } . "\t" .
201 $url->{ status } . "\n";
205 print "</table>\n</body>\n</html>\n" if $html;
213 if ( $html && !$host_pro ) {
218 print "Error: host-pro parameter or host must be provided in html mode\n";
229 check-url.pl - Check URLs from 856$u field.
235 =item check-url.pl [--verbose|--help] [--host=http://default.tld]
237 Scan all URLs found in 856$u of bib records
238 and display if resources are available or not.
246 =item B<--host=http://default.tld>
248 Server host used when URL doesn't have one, ie doesn't begin with 'http:'.
249 For example, if --host=http://www.mylib.com, then when 856$u contains
250 'img/image.jpg', the url checked is: http://www.mylib.com/image.jpg'.
252 =item B<--verbose|-v>
254 Outputs both successful and failed URLs.
258 Formats output in HTML. The result can be redirected to a file
259 accessible by http. This way, it's possible to link directly to biblio
260 record in edit mode. With this parameter B<--host-pro> is required.
262 =item B<--host-pro=http://koha-pro.tld>
264 Server host used to link to biblio record editing page.
266 =item B<--timeout=15>
268 Timeout for fetching URLs. By default 15 seconds.
272 Print this help page.