A new script to check URLs in 856$u field
[koha.git] / misc / cronjobs / check-url.pl
1 #!/usr/bin/perl
2
3 #
4 # Copyright 2009 Tamil s.a.r.l.
5 #
6 # This software is placed under the gnu General Public License, v2 
7 # (http://www.gnu.org/licenses/gpl.html)
8 #
9
10 use strict;
11 use warnings;
12 use diagnostics;
13 use Carp;
14 use LWP::Simple;
15 use Pod::Usage;
16 use Getopt::Long;
17 use C4::Context;
18 use C4::Biblio;
19
20
21 my $verbose     = 0;
22 my $help        = 0;
23 my $host        = '';
24 GetOptions( 
25     'verbose'   => \$verbose,
26     'help'      => \$help,
27     'host=s'    => \$host,
28 );
29
30 sub usage {
31     pod2usage( -verbose => 2 );
32     exit;
33
34
35 usage() if $help;          
36
37 my $context = new C4::Context(  );  
38 my $dbh = $context->dbh;
39 my $sth = $dbh->prepare( 
40     "SELECT biblionumber FROM biblioitems WHERE url <> ''" );
41 $sth->execute;
42 while ( my ($biblionumber) = $sth->fetchrow ) { 
43     my $record = GetMarcBiblio( $biblionumber );    
44     next unless $record->field('856');
45     foreach my $field ( $record->field('856') ) {
46         my $url = $field->subfield('u');
47         next unless $url;
48         $url = "$host/$url" unless $url =~ /^http/;
49         if ( head( $url ) ) {
50             print "$biblionumber\t$url\tsucceed\n" if $verbose;
51         }
52         else {
53             print "$biblionumber\t$url\tfailed\n";
54         }
55     }
56 }
57 exit;      
58
59 =head1 NAME
60
61 check-url.pl - Check URLs from 856$u field.
62
63 =head1 USAGE
64
65 =over
66
67 =item check-url.pl [--verbose|--help] [--host=http://default.tld] 
68
69 Scan all URL found in 856$u and display if ressources are available or not.
70
71 =back
72
73 =head1 PARAMETERS
74
75 =over
76
77 =item B<--host=http://default.tld>
78
79 Server host used when URL doesn't have one, ie doesn't begin with 'http:'. 
80 For example, if --host=http://www.mylib.com, then when 856$u contains 
81 'img/image.jpg', the url checked is: http://www.mylib.com/image.jpg'.
82
83 =item B<--verbose|-v>
84
85 Output succeed URL checks with failed ones. 
86
87 =item B<--help|-h>
88
89 Print this help page.
90
91 =back
92
93 =cut
94
95