blib/script/oai_browser.pl untested

filter: Statementstmt 0.0% 0 / 205 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 353 CC 34 SCAR 36.7
CC
34
Cov
0%
mainloop
110.0
ListRecords
72.0
GetRecord
42.0
CRAP 39.1
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 oai_browser - Command line OAI repository browser
6  
7 =head1 DESCRIPTION
8  
9 The oai_browser utility provides a command-line tool to browse an OAI-compliant
10 repository.
11  
12 =head1 SYNOPSIS
13  
14 oai_browser.pl B<[options]> I<arguments>
15  
16 =head1 ARGUMENTS
17  
18 =over 4
19  
20 =item I<baseURL>
21  
22 Specify baseURL to connect to.
23  
24 =back
25  
26 =head1 OPTIONS
27  
28 =over 8
29  
30 =item B<--help>
31  
32 Show this page.
33  
34 =item B<--silent>
35  
36 Don't display data harvested from the repository - only shows a record count.
37  
38 =item B<--trace>
39  
40 Turn on trace debugging.
41  
42 =item B<--tracesax>
43  
44 Turn on trace debugging of SAX calls.
45  
46 =item B<--skip-identify>
47  
48 Don't perform an initial Identify to check the repository's baseURL.
49  
50 =back
51  
52 =cut
53  
54 BEGIN {
55 unshift @INC, ".";
56 }
57  
58 use vars qw($VERSION $h);
59  
60 use lib "../lib";
61 use lib "lib";
62  
63 use HTTP::OAI;
64 use Pod::Usage;
65  
66 $VERSION = $HTTP::OAI::VERSION;
67  
68 use vars qw( @ARCHIVES );
69 @ARCHIVES = qw(
70 http://cogprints.soton.ac.uk/perl/oai2
71 http://citebase.eprints.org/cgi-bin/oai2
72 http://arXiv.org/oai2
73 http://www.biomedcentral.com/oai/2.0/
74 );
75  
76 use strict;
77 use warnings;
78 #use sigtrap qw( die INT ); # This is just confusing ...
79  
80 #binmode(STDOUT,":encoding(iso-8859-1)"); # Causes Out of memory! errors :-(
81 binmode(STDOUT,":utf8");
82  
83 use Getopt::Long;
84 eval "use Term::ReadLine";
85 if( $@ )
86 {
87 die "Requires Term::ReadLine perl module\n";
88 }
89 eval "use Term::ReadKey";
90 if( $@ )
91 {
92 die "Requires Term::ReadKey perl module\n";
93 }
94  
95 use HTTP::OAI::Harvester;
96 use HTTP::OAI::Metadata::OAI_DC;
97  
98 my ($opt_silent, $opt_help, $opt_trace, $opt_tracesax, $opt_skip_identify);
99 $opt_silent = 0;
100 GetOptions (
101 'silent' => \$opt_silent,
102 'help' => \$opt_help,
103 'trace' => \$opt_trace,
104 'tracesax' => \$opt_tracesax,
105 'skip-identify' => \$opt_skip_identify,
106 );
107  
108 pod2usage(1) if $opt_help;
109  
110 if( $opt_trace )
111 {
112 HTTP::OAI::Debug::level( '+trace' );
113 }
114 if( $opt_tracesax )
115 {
116 HTTP::OAI::Debug::level( '+sax' );
117 }
118  
119 print <<EOF;
120 Welcome to the Open Archives Browser $VERSION
121  
122 Copyright 2005-2012 Tim Brody <tdb01r\@ecs.soton.ac.uk>
123  
124 Use CTRL+C to quit at any time
125  
126 ---
127  
128 EOF
129  
130 my $DEFAULTID = '';
131  
132 use vars qw($TERM @SETS @PREFIXES);
133 $TERM = Term::ReadLine->new($0);
134 $TERM->addhistory(@ARCHIVES);
135  
136 while(1) {
137 # my $burl = input('Enter the base URL to use [http://cogprints.soton.ac.uk/perl/oai2]: ') || 'http://cogprints.soton.ac.uk/perl/oai2';
138 my $burl = shift || $TERM->readline('OAI Base URL to query>','http://cogprints.soton.ac.uk/perl/oai2') || next;
139 $h = new HTTP::OAI::Harvester(baseURL=>$burl);
140 last if $opt_skip_identify;
141 if( my $id = Identify() ) {
142 last;
143 }
144 }
145  
146 &mainloop();
147  
148 sub mainloop {
149 while(1) {
150 print "\nMenu\n----\n\n",
151 "1. GetRecord\n2. Identify\n3. ListIdentifiers\n4. ListMetadataFormats\n5. ListRecords\n6. ListSets\nq. Quit\n\n>";
152 my $cmd;
153 ReadMode(4);
154 $cmd = ReadKey();
155 ReadMode(0);
156 last unless defined($cmd);
157 print $cmd . "\n";
158 if( $cmd eq 'q' ) {
159 last;
160 } elsif($cmd eq '1') {
161 eval { GetRecord() };
162 } elsif($cmd eq '2') {
163 eval { Identify() };
164 } elsif($cmd eq '3') {
165 eval { ListIdentifiers() };
166 } elsif($cmd eq '4') {
167 eval { ListMetadataFormats() };
168 } elsif($cmd eq '5') {
169 eval { ListRecords() };
170 } elsif($cmd eq '6') {
171 eval { ListSets() };
172 }
173 if( $@ ) {
174 warn "Internal error occurred: $@\n";
175 }
176 }
177 }
178  
179 sub GetRecord {
180 printtitle("GetRecord");
181  
182 my $id = $TERM->readline("Enter the identifier to request>",$DEFAULTID) || $DEFAULTID;
183 $TERM->addhistory(@PREFIXES);
184 my $mdp = $TERM->readline("Enter the metadataPrefix to use>",'oai_dc') || 'oai_dc';
185  
186 my $r = $h->GetRecord(
187 identifier=>$id,
188 metadataPrefix=>$mdp,
189 handlers=>{
190 metadata=>($mdp eq 'oai_dc' ? 'HTTP::OAI::Metadata::OAI_DC' : undef),
191 },
192 );
193  
194 if( defined(my $rec = $r->next) ) {
195 printheader($r);
196 print "identifier => ", $rec->identifier,
197 ($rec->status ? " (".$rec->status.") " : ''), "\n",
198 "datestamp => ", $rec->datestamp, "\n";
199 foreach($rec->header->setSpec) {
200 print "setSpec => ", $_, "\n";
201 }
202 print "\nHeader:\n",
203 $rec->header->toString;
204 print "\nMetadata:\n",
205 $rec->metadata->toString if defined($rec->metadata);
206 print "\nAbout data:\n",
207 join("\n",map { $_->toString } $rec->about) if $rec->about;
208 }
209  
210 iserror($r);
211 }
212  
213 sub Identify {
214 printtitle("Identify");
215  
216 my $r = $h->Identify;
217  
218 return if iserror($r);
219  
220 print map({ "adminEmail => " . $_ . "\n" } $r->adminEmail),
221 "baseURL => ", $r->baseURL, "\n",
222 "protocolVersion => ", $r->protocolVersion, "\n",
223 "repositoryName => ", $r->repositoryName, "\n";
224  
225 foreach my $dom (grep { defined } map { $_->dom } $r->description) {
226 foreach my $md ($dom->firstChild) {
227 foreach my $elem ($md->getElementsByTagNameNS('http://www.openarchives.org/OAI/2.0/oai-identifier','sampleIdentifier')) {
228 $DEFAULTID = $elem->getFirstChild->toString;
229 print "sampleIdentifier => ", $DEFAULTID, "\n";
230 }
231 }
232 }
233  
234 $r;
235 }
236  
237 sub ListIdentifiers {
238 printtitle("ListIdentifiers");
239  
240 my $resumptionToken = $TERM->readline("Enter an optional resumptionToken>");
241 my ($from, $until, $set, $mdp);
242 if( !$resumptionToken ) {
243 $from = $TERM->readline("Enter an optional from period (yyyy-mm-dd)>");
244 $until = $TERM->readline("Enter an optional until period (yyyy-mm-dd)>");
245 $TERM->addhistory(@SETS);
246 $set = $TERM->readline("Enter an optional set ([A-Z0-9_]+)>");
247 $TERM->addhistory(@PREFIXES);
248 $mdp = $TERM->readline("Enter the metadataPrefix to use>",'oai_dc') || 'oai_dc';
249 }
250  
251 my $c = 0;
252 my $cb = $opt_silent ?
253 sub { print STDERR $c++, "\r"; } :
254 sub {
255 my $rec = shift;
256 $c++;
257 print "identifier => ", $rec->identifier,
258 (defined($rec->datestamp) ? " / " . $rec->datestamp : ''),
259 ($rec->status ? " (".$rec->status.") " : ''), "\n";
260 };
261  
262 #printheader($r);
263 my $r = $h->ListIdentifiers(
264 checkargs(resumptionToken=>$resumptionToken,from=>$from,until=>$until,set=>$set,metadataPrefix=>$mdp),
265 onRecord => $cb,
266 );
267  
268 print "\nRead a total of $c records\n";
269  
270 return if iserror($r);
271 }
272  
273 sub ListMetadataFormats {
274 printtitle("ListMetadataFormats");
275  
276 my $id = $TERM->readline("Enter an optional identifier>");
277  
278 my $r = $h->ListMetadataFormats(checkargs(identifier=>$id));
279  
280 return if iserror($r);
281 @PREFIXES = ();
282  
283 printheader($r);
284 while( my $mdf = $r->next ) {
285 push @PREFIXES, $mdf->metadataPrefix;
286 print "metadataPrefix => ", $mdf->metadataPrefix, "\n",
287 "schema => ", $mdf->schema, "\n",
288 "metadataNamespace => ", ($mdf->metadataNamespace || ''), "\n";
289 }
290 }
291  
292 sub ListRecords {
293 printtitle("ListRecords");
294  
295 my $resumptionToken = $TERM->readline("Enter an optional resumptionToken>");
296 my ($from, $until, $set, $mdp);
297 if( !$resumptionToken ) {
298 $from = $TERM->readline("Enter an optional from period (yyyy-mm-dd)>");
299 $until = $TERM->readline("Enter an optional until period (yyyy-mm-dd)>");
300 $TERM->addhistory(@SETS);
301 $set = $TERM->readline("Enter an optional set ([A-Z0-9_]+)>");
302 $TERM->addhistory(@PREFIXES);
303 $mdp = $TERM->readline("Enter the metadataPrefix to use>",'oai_dc') || 'oai_dc';
304 }
305  
306 my $c = 0;
307 my $cb = $opt_silent ?
308 sub { print STDERR $c++, "\r"; } :
309 sub {
310 my $rec = shift;
311 $c++;
312 print "\nidentifier => ", $rec->identifier,
313 ($rec->status ? " (".$rec->status.") " : ''), "\n",
314 "datestamp => ", $rec->datestamp, "\n";
315 foreach($rec->header->setSpec) {
316 print "setSpec => ", $_, "\n";
317 }
318 print "\nMetadata:\n",
319 ($rec->metadata->toString||'(null)') if $rec->metadata;
320 print "\nAbout data:\n",
321 join("\n",map { ($_->toString||'(null)') } $rec->about) if $rec->about;
322 };
323  
324 #printheader($r);
325 my $r = $h->ListRecords(
326 checkargs(resumptionToken=>$resumptionToken,from=>$from,until=>$until,set=>$set,metadataPrefix=>$mdp),
327 handlers=>{
328 metadata=>(($mdp and $mdp eq 'oai_dc') ? 'HTTP::OAI::Metadata::OAI_DC' : undef),
329 },
330 onRecord => $cb,
331 );
332  
333 print "\nRead a total of $c records\n";
334  
335 return if iserror($r);
336 }
337  
338 sub ListSets {
339 printtitle("ListSets");
340  
341 sub cb {
342 my $rec = shift;
343 push @SETS, $rec->setSpec;
344 print "setSpec => ", $rec->setSpec, "\n",
345 "setName => ", ($rec->setName||'(null)'), "\n";
346 };
347  
348 my $r = $h->ListSets(onRecord=>\&cb);
349  
350 return if iserror($r);
351 }
352  
353 sub input {
354 my $q = shift;
355 print $q;
356 my $r = <>;
357 return unless defined($r);
358 chomp($r);
359 return $r;
360 }
361  
362 sub printtitle {
363 my $t = shift;
364 print "\n$t\n";
365 for( my $i = 0; $i < length($t); $i++ ) {
366 print "-";
367 }
368 print "\n";
369 }
370  
371 sub printheader {
372 my $r = shift;
373 print "verb => ", $r->verb, "\n",
374 "responseDate => ", $r->responseDate, "\n",
375 "requestURL => ", $r->requestURL, "\n";
376 }
377  
378 sub checkargs {
379 my %args = @_;
380 foreach my $key (keys %args) {
381 delete $args{$key} if( !defined($args{$key}) || $args{$key} eq '' );
382 }
383 %args;
384 }
385  
386 sub iserror {
387 my $r = shift;
388 if( $r->is_success ) {
389 return undef;
390 } else {
391 print "An error ", $r->code, " occurred while making the request",
392 ($r->request ? " (" . $r->request->uri . ") " : ''),
393 ":\n", $r->message, "\n";
394 return 1;
395 }
396 }