blib/script/oai_pmh.pl untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 82 CC 4 SCAR 18.3
CC
4
Cov
0%
output_record
12.0
debug
6.0
BEGIN
2.0
CRAP 6.2
1 #!/usr/local/bin/perl
2  
3 use HTTP::OAI;
4 use Getopt::Long;
5 use Pod::Usage;
6 use XML::LibXML;
7  
8 =head1 NAME
9  
10 oai_pmh.pl - pipe OAI-PMH to the command-line
11  
12 =head1 SYNOPSIS
13  
14 oai_pmh.pl <options> [baseURL]
15  
16 =head1 OPTIONS
17  
18 =over 8
19  
20 =item --help
21  
22 =item --man
23  
24 =item --verbose
25  
26 Be more verbose (repeatable).
27  
28 =item --force
29  
30 Force a non-conformant OAI request.
31  
32 =item --from <ISO datetime>
33  
34 =item --identifier <identifier>
35  
36 OAI identifier to GetRecord or ListMetadataFormats.
37  
38 =item --metadataPrefix <mdp>
39  
40 Specify format of metadata to retrieve.
41  
42 =item -X/--request <command>
43  
44 Verb to request, defaults to ListRecords.
45  
46 =item --set <oai set>
47  
48 Request only those records in a set.
49  
50 =item --until <ISO datetime>
51  
52 =back
53  
54 =head1 DESCRIPTION
55  
56 Retrieve data from OAI-PMH endpoints. The output format is:
57  
58 <headers>
59  
60 <content>
61 <FORMFEED>
62  
63 Where <headers> are in HTTP header format. Content will be the raw XML as exposed by the repository. Each record is separated by a FORMFEED character.
64  
65 For example:
66  
67 oai_pmh.pl -X GetRecord --metadataPrefix oai_dc \
68 --identifier oai:eprints.soton.ac.uk:20 http://eprints.soton.ac.uk/cgi/oai2
69  
70 =cut
71  
72 my %opts = (
73 verbose => 1,
74 );
75  
76 GetOptions(\%opts,
77 'help',
78 'man',
79 'metadataPrefix=s',
80 'request|X=s',
81 'identifier=s',
82 'verbose+',
83 'force',
84 'from=s',
85 'until=s',
86 ) or pod2usage(2);
87 pod2usage(1) if $opts{help};
88 pod2usage({-verbose => 2}) if $opts{man};
89  
90 my $noise = delete $opts{verbose};
91  
92 if (!exists $opts{request}) {
93 $opts{request} = 'ListRecords';
94 $opts{metadataPrefix} = 'oai_dc';
95 }
96  
97 my $base_url = pop @ARGV;
98 pod2usage(1) if !$base_url;
99  
100 my $ha = HTTP::OAI::Harvester->new(baseURL => $base_url);
101  
102 my $f = delete $opts{request};
103 debug("Requesting $f", 2);
104 my $r = $ha->$f(
105 %opts,
106 onRecord => \&output_record,
107 );
108 if( $f eq "ListMetadataFormats" )
109 {
110 foreach my $mdf ($r->metadataFormat) {
111 print "metadataPrefix: " . $mdf->metadataPrefix . "\n";
112 print "schema: " . $mdf->schema . "\n";
113 print "metadataNamespace: " . $mdf->metadataNamespace . "\n";
114 print "\n";
115 print "\f";
116 }
117 }
118  
119 if( !$r->is_success )
120 {
121 die "Error in response: " . $r->message . "\n";
122 }
123  
124 sub debug
125 {
126 my( $msg, $level ) = @_;
127  
128 warn "$msg\n" if $noise >= $level;
129 }
130  
131 sub output_record
132 {
133 my( $rec ) = @_;
134  
135 my $header = $rec->isa( 'HTTP::OAI::Header' ) ? $rec : $rec->header;
136  
137 print "identifier: " . $header->identifier . "\n";
138 print "datestamp: " . $header->datestamp . "\n";
139 print "status: " . $header->status . "\n";
140 foreach my $set ($header->setSpec) {
141 print "setSpec: " . $set . "\n";
142 }
143 print "\n";
144  
145 if ($rec->can( "metadata" ) && defined(my $metadata = $rec->metadata)) {
146 print $metadata->dom->toString( 1 );
147 }
148  
149 print "\f";
150 }