Albanian po files
[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
11
12 package C4::URL::Checker;
13
14 =head1 NAME 
15
16 C4::URL::Checker - base object for checking URL stored in Koha DB
17
18 =head1 SYNOPSIS
19
20  use C4::URL::Checker;
21
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";
29  }
30  
31 =head1 FUNCTIONS
32
33 =head2 new
34
35 Create a URL Checker. The returned object can be used to set
36 default host variable :
37
38  my $checker = C4::URL::Checker->new( );
39  $checker->{ host_default } = 'http://mylib.kohalibrary.com';
40
41 =head2 check_biblio
42
43 Check all URL from a biblio record. Returns a pointer to an array
44 containing all URLs with checking for each of them.
45
46  my $checked_urls = $checker->check_biblio( 123 );
47
48 With 2 URLs, the returned array will look like that:
49
50   [
51     {
52       'url' => 'http://mylib.tamil.fr/img/62265_0055B.JPG',
53       'is_success' => 1,
54       'status' => 'ok'
55     },
56     {
57       'url' => 'http://mylib.tamil.fr//img/62265_0055C.JPG',
58       'is_success' => 0,
59       'status' => '404 - Page not found'
60     }
61   ],
62   
63
64 =cut
65
66 use strict;
67 use warnings;
68 use LWP::UserAgent;
69 use HTTP::Request;
70 use C4::Biblio;
71
72
73
74 sub new {
75
76     my $self = {};
77     my $class = shift;
78     
79     $self->{ user_agent } = new LWP::UserAgent;
80     
81     bless $self, $class;
82     return $self;
83 }
84
85
86 sub check_biblio {
87     my $self            = shift;
88     my $biblionumber    = shift;
89     my $uagent          = $self->{ user_agent   };
90     my $host            = $self->{ host_default };
91
92     my $record = GetMarcBiblio( $biblionumber ); 
93     return unless $record->field('856');
94
95     my @urls = ();
96     foreach my $field ( $record->field('856') ) {
97         my $url = $field->subfield('u');
98         next unless $url; 
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';
106         }
107         else {
108             $check->{ is_success } = 0;
109             $check->{ status     } = $res->status_line;
110         }
111         push( @urls, $check );       
112     }
113     return \@urls;
114 }
115
116
117
118 package Main;
119
120 use strict;
121 use warnings;
122 use diagnostics;
123 use Carp;
124
125 use Pod::Usage;
126 use Getopt::Long;
127 use C4::Context;
128
129
130
131 my $verbose     = 0;
132 my $help        = 0;
133 my $host        = '';
134 my $host_pro    = '';
135 my $html        = 0;
136 my $uriedit     = "/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=";
137 GetOptions( 
138     'verbose'       => \$verbose,
139     'html'          => \$html,
140     'help'          => \$help,
141     'host=s'        => \$host,
142     'host-pro=s'    => \$host_pro,
143 );
144
145
146 sub usage {
147     pod2usage( -verbose => 2 );
148     exit;
149
150
151
152 sub bibediturl {
153     my $biblionumber = shift;
154     my $html = "<a href=\"$host_pro$uriedit$biblionumber\">$biblionumber</a>";
155     return $html;
156 }
157
158
159
160 # Check all URLs from all current Koha biblio records
161 #
162 sub check_all_url {
163     my $checker = C4::URL::Checker->new();
164     $checker->{ host_default }  = $host;
165     
166     my $context = new C4::Context(  );  
167     my $dbh = $context->dbh;
168     my $sth = $dbh->prepare( 
169         "SELECT biblionumber FROM biblioitems WHERE url <> ''" );
170     $sth->execute;
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 ) {
177                 print $html
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";
183             }
184         }
185     }
186     print "</table>\n</body>\n</html>\n" if $html;
187 }
188
189
190 # BEGIN
191
192 usage() if $help;          
193
194 if ( $html && !$host_pro ) {
195     if ( $host ) {
196         $host_pro = $host;
197     }
198     else {
199         print "Error: host-pro parameter or host must be provided in html mode\n";
200         exit;
201     }
202 }
203
204 check_all_url(); 
205
206
207
208 =head1 NAME
209
210 check-url.pl - Check URLs from 856$u field.
211
212 =head1 USAGE
213
214 =over
215
216 =item check-url.pl [--verbose|--help] [--host=http://default.tld] 
217
218 Scan all URLs found in 856$u of bib records 
219 and display if resources are available or not.
220
221 =back
222
223 =head1 PARAMETERS
224
225 =over
226
227 =item B<--host=http://default.tld>
228
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'.
232
233 =item B<--verbose|-v>
234
235 Outputs both successful and failed URLs.
236
237 =item B<--html>
238
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.
242
243 =item B<--host-pro=http://koha-pro.tld>
244
245 Server host used to link to biblio record editing page.
246
247 =item B<--help|-h>
248
249 Print this help page.
250
251 =back
252
253 =cut
254
255