script/oai_browser.pl untested

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