script/oai_pmh.pl untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 86 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 'set=s',
83 'verbose+',
84 'force',
85 'from=s',
86 'until=s',
87 ) or pod2usage(2);
88 pod2usage(1) if $opts{help};
89 pod2usage({-verbose => 2}) if $opts{man};
90  
91 my $noise = delete $opts{verbose};
92  
93 if (!exists $opts{request}) {
94 $opts{request} = 'ListRecords';
95 $opts{metadataPrefix} = 'oai_dc';
96 }
97  
98 my $base_url = pop @ARGV;
99 pod2usage(1) if !$base_url;
100  
101 my $ha = HTTP::OAI::Harvester->new(baseURL => $base_url);
102  
103 my $f = delete $opts{request};
104 debug("Requesting $f", 2);
105 my $r = $ha->$f(
106 %opts,
107 onRecord => \&output_record,
108 );
109 if( $f eq "ListMetadataFormats" )
110 {
111 foreach my $mdf ($r->metadataFormat) {
112 print "metadataPrefix: " . $mdf->metadataPrefix . "\n";
113 print "schema: " . $mdf->schema . "\n";
114 print "metadataNamespace: " . $mdf->metadataNamespace . "\n";
115 print "\n";
116 print "\f";
117 }
118 }
119  
120 if( !$r->is_success )
121 {
122 die "Error in response: " . $r->message . "\n";
123 }
124  
125 sub debug
126 {
127 my( $msg, $level ) = @_;
128  
129 warn "$msg\n" if $noise >= $level;
130 }
131  
132 sub output_record
133 {
134 my( $rec ) = @_;
135  
136 my $header = $rec->isa( 'HTTP::OAI::Header' ) ? $rec : $rec->header;
137  
138 print "identifier: " . $header->identifier . "\n";
139 print "datestamp: " . $header->datestamp . "\n";
140 print "status: " . $header->status . "\n";
141 foreach my $set ($header->setSpec) {
142 print "setSpec: " . $set . "\n";
143 }
144 print "\n";
145  
146 if ($rec->can( "metadata" ) && defined(my $metadata = $rec->metadata)) {
147 print $metadata->dom->toString( 1 );
148 }
149  
150 print "\f";
151 }