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;
80 $self->{ bad_url } = { };
89 my $biblionumber = shift;
90 my $uagent = $self->{ user_agent };
91 my $host = $self->{ host_default };
92 my $bad_url = $self->{ bad_url };
94 my $record = GetMarcBiblio( $biblionumber );
95 return unless $record->field('856');
98 foreach my $field ( $record->field('856') ) {
99 my $url = $field->subfield('u');
101 $url = "$host/$url" unless $url =~ /^http/;
102 my $check = { url => $url };
103 if ( $bad_url->{ $url } ) {
104 $check->{ is_success } = 1;
105 $check->{ status } = '500 Site already checked';
108 my $req = HTTP::Request->new( GET => $url );
109 my $res = $uagent->request( $req, sub { die }, 1 );
110 if ( $res->is_success ) {
111 $check->{ is_success } = 1;
112 $check->{ status } = 'ok';
115 $check->{ is_success } = 0;
116 $check->{ status } = $res->status_line;
117 $bad_url->{ $url } = 1;
145 my $uriedit = "/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=";
147 'verbose' => \$verbose,
151 'host-pro=s' => \$host_pro,
156 pod2usage( -verbose => 2 );
162 my $biblionumber = shift;
163 my $html = "<a href=\"$host_pro$uriedit$biblionumber\">$biblionumber</a>";
169 # Check all URLs from all current Koha biblio records
172 my $checker = C4::URL::Checker->new();
173 $checker->{ host_default } = $host;
175 my $context = new C4::Context( );
176 my $dbh = $context->dbh;
177 my $sth = $dbh->prepare(
178 "SELECT biblionumber FROM biblioitems WHERE url <> ''" );
187 while ( my ($biblionumber) = $sth->fetchrow ) {
188 my $result = $checker->check_biblio( $biblionumber );
189 next unless $result; # No URL
190 foreach my $url ( @$result ) {
191 if ( ! $url->{ is_success } || $verbose ) {
193 ? "<tr>\n<td>" . bibediturl( $biblionumber ) .
194 "</td>\n<td>" . $url->{url} . "</td>\n<td>" .
195 $url->{status} . "</td>\n</tr>\n\n"
196 : "$biblionumber\t" . $url->{ url } . "\t" .
197 $url->{ status } . "\n";
201 print "</table>\n</body>\n</html>\n" if $html;
209 if ( $html && !$host_pro ) {
214 print "Error: host-pro parameter or host must be provided in html mode\n";
225 check-url.pl - Check URLs from 856$u field.
231 =item check-url.pl [--verbose|--help] [--host=http://default.tld]
233 Scan all URLs found in 856$u of bib records
234 and display if resources are available or not.
242 =item B<--host=http://default.tld>
244 Server host used when URL doesn't have one, ie doesn't begin with 'http:'.
245 For example, if --host=http://www.mylib.com, then when 856$u contains
246 'img/image.jpg', the url checked is: http://www.mylib.com/image.jpg'.
248 =item B<--verbose|-v>
250 Outputs both successful and failed URLs.
254 Formats output in HTML. The result can be redirected to a file
255 accessible by http. This way, it's possible to link directly to biblio
256 record in edit mode. With this parameter B<--host-pro> is required.
258 =item B<--host-pro=http://koha-pro.tld>
260 Server host used to link to biblio record editing page.
264 Print this help page.