blib/lib/Net/OAI/Harvester.pm

filter: Statementstmt 65.4% 178 / 272 Branchbran 44.7% 43 / 96 Conditioncond 37.0% 10 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 92.1% 35 / 38 Podpod 91.6% 11 / 12 totaltotal 59.9% 277 / 462 SCAR 57.3
CC
59
Cov
58%
listRecords
25.6
listIdentifiers
19.6
_parser
17.9
CRAP 308.1
« blib/lib/Net/OAI/Base.pm ↑ Summary blib/lib/Net/OAI/Record/OAI_DC.pm »
1 package Net::OAI::Harvester;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 ▶use constant XMLNS_OAI => "http://www.openarchives.org/OAI/2.0/";
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use XML::SAX qw( Namespaces Validation );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use Net::OAI::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use Net::OAI::ResumptionToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use Net::OAI::Identify;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use Net::OAI::ListMetadataFormats;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use Net::OAI::ListIdentifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use Net::OAI::ListRecords;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 17 ▶use Net::OAI::GetRecord;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 17 ▶use Net::OAI::ListSets;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use Net::OAI::Record::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use Net::OAI::Record::OAI_DC;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 17 our $VERSION = "1.16_12";
26 17 our $DEBUG = 0;
27 # compatibility mode for metadataHandler
28 17 our $OLDmetadataHandler = 0;
29  
30 =head1 NAME
31  
32 Net::OAI::Harvester - A package for harvesting metadata using OAI-PMH
33  
34 =head1 SYNOPSIS
35  
36 ## create a harvester for the Library of Congress
37 my $harvester = Net::OAI::Harvester->new(
38 'baseURL' => 'http://memory.loc.gov/cgi-bin/oai2_0'
39 );
40  
41 ## list all the records in a repository
42 my $records = $harvester->listRecords(
43 'metadataPrefix' => 'oai_dc'
44 );
45 while ( my $record = $records->next() ) {
46 my $header = $record->header();
47 my $metadata = $record->metadata();
48 print "identifier: ", $header->identifier(), "\n";
49 print "title: ", $metadata->title(), "\n";
50 }
51  
52 ## find out the name for a repository
53 my $identity = $harvester->identify();
54 print "name: ",$identity->repositoryName(),"\n";
55  
56 ## get a list of identifiers
57 my $identifiers = $harvester->listIdentifiers(
58 'metadataPrefix' => 'oai_dc'
59 );
60 while ( my $header = $identifiers->next() ) {
61 print "identifier: ",$header->identifier(), "\n";
62 }
63  
64 ## list all the records in a repository
65 my $records = $harvester->listRecords(
66 'metadataPrefix' => 'oai_dc'
67 );
68 while ( my $record = $records->next() ) {
69 my $header = $record->header();
70 my $metadata = $record->metadata();
71 print "identifier: ", $header->identifier(), "\n";
72 print "title: ", $metadata->title(), "\n";
73 }
74  
75 ## GetRecord, ListSets, ListMetadataFormats also supported
76  
77 =head1 DESCRIPTION
78  
79 Net::OAI::Harvester is a Perl extension for easily querying OAI-PMH
80 repositories. OAI-PMH is the Open Archives Initiative Protocol for Metadata
81 Harvesting. OAI-PMH allows data repositories to share metadata about their
82 digital assets. Net::OAI::Harvester is a OAI-PMH client, so it does for
83 OAI-PMH what LWP::UserAgent does for HTTP.
84  
85 You create a Net::OAI::Harvester object which you can then use to
86 retrieve metadata from a selected repository. Net::OAI::Harvester tries to keep
87 things simple by providing an API to get at the data you want; but it also has
88 a framework which is easy to extend should you need to get more fancy.
89  
90 The guiding principle behind OAI-PMH is to allow metadata about online
91 resources to be shared by data providers, so that the metadata can be harvested
92 by interested parties. The protocol is essentially XML over HTTP (much like
93 XMLRPC or SOAP). Net::OAI::Harvester does XML parsing for you
94 (using XML::SAX internally), but you can get at the raw XML if you want to do
95 your own XML processing, and you can drop in your own XML::SAX handler if you
96 would like to do your own parsing of metadata elements.
97  
98 A OAI-PMH repository supports 6 verbs: GetRecord, Identify, ListIdentifiers,
99 ListMetadataFormats, ListRecords, and ListSets. The verbs translate directly
100 into methods that you can call on a Net::OAI::Harvester object. More details
101 about these methods are supplied below, however for the real story please
102 consult the spec at http://www.openarchives.org.
103  
104 Net::OAI::Harvester has a few features that are worth mentioning:
105  
106 =over 4
107  
108 =item 1
109  
110 Since the OAI-PMH results can be arbitrarily large, a stream based (XML::SAX)
111 parser is used. As the document is parsed corresponding Perl objects are
112 created (records, headers, etc), which are then serialized on disk (using
113 Storable if you are curious). The serialized objects on disk can then be
114 iterated over one at a time. The benefit of this is a lower memory footprint
115 when (for example) a ListRecords verb is exercised on a repository that
116 returns 100,000 records.
117  
118  
119 =item 2
120  
121 XML::SAX filters are used which will allow interested developers to write
122 their own metadata parsing packages, and drop them into place. This is useful
123 because OAI-PMH is itself metadata schema agnostic, so you can use OAI-PMH
124 to distribute all kinds of metadata (Dublin Core, MARC, EAD, or your favorite
125 metadata schema). OAI-PMH does require that a repository at least provides
126 Dublin Core metadata as a baseline. Net::OAI::Harvester has built in support for
127 unqualified Dublin Core, and has a framework for dropping in your own parser
128 for other kinds of metadata. If you create a XML::Handler that you would like
129 to contribute back into the Net::OAI::Harvester project please get in touch!
130  
131 =back
132  
133 =head1 METHODS
134  
135 All the Net::OAI::Harvester methods return other objects. As you would expect
136 new() returns an Net::OAI::Harvester object; similarly getRecord() returns an
137 Net::OAI::Record object, listIdentifiers() returns a Net::OAI::ListIdentifiers
138 object, identify() returns an Net::OAI::Identify object, and so on. So when
139 you use one of these methods you'll probably want to check out the docs for
140 the object that gets returned so you can see what to do with it. Many
141 of these classes inherit from Net::OAI::Base which provides some base
142 functionality for retrieving errors, getting the raw XML, and the
143 temporary file where the XML is stored (see Net::OAI::Base documentation for
144 more details).
145  
146 =head2 new()
147  
148 The constructor which returns an Net::OAI::Harvester object. You must supply the
149 baseURL parameter, to tell Net::OAI::Harvester what data repository you are
150 going to be harvesting. For a list of data providers check out the directory
151 available on the Open Archives Initiative homepage.
152  
153 my $harvester = Net::OAI::Harvester->new(
154 baseURL => 'http://memory.loc.gov/cgi-bin/oai2_0'
155 );
156  
157 If you want to pull down all the XML files and keep them in a directory, rather
158 than having the stored as transient temp files pass in the dumpDir parameter.
159  
160 my $harvester = Net::OAI::Harvester->new(
161 baseUrl => 'http://memory.loc.gov/cgi-bin/oai2_0',
162 dumpDir => 'american-memory'
163 );
164  
165 Also if you would like to fine tune the HTTP client used by Net::OAI::Harvester
166 you can pass in a configured object. For example this can be handy if you
167 want to adjust the client timeout:
168  
169 my $ua = LWP::UserAgent->new();
170 $ua->timeout(20); ## set timeout to 20 seconds
171 my $harvester = Net::OAI::Harvester->new(
172 baseURL => 'http://memory.loc.gov/cgi-bin/oai2_0',
173 userAgent => $ua
174 );
175  
176 =cut
177  
178 sub new {
179 22 ▶ my ( $class, %opts ) = @_;
Subroutine new: called CC=6 SCAR=18.1
Pod documented
180  
181 ## uppercase options
182 22 my %normalOpts = map { ( uc($_), $opts{$_} ) } keys( %opts );
183
184 ## we must be told a baseURL
185 22 ▶ croak( "new() needs the baseUrl parameter" ) if !$normalOpts{ BASEURL };
Branch: 1 branch
BranchTrueFalseTotal
unless $normalOpts{"BASEURL"} 0 22 22
186 22 my $baseURL = URI->new( $normalOpts{ BASEURL } );
187  
188 22 ▶ my $self = bless( { baseURL => $baseURL }, ref( $class ) || $class );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0220
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
189  
190 ## set the user agent
191 22 ▶ if ( $normalOpts{ USERAGENT } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($normalOpts{'USERAGENT'}) { } 1 21 22
192 1 $self->userAgent( $normalOpts{ USERAGENT } );
193 } else {
194 21 my $ua = LWP::UserAgent->new();
195 21 $ua->agent( $class );
196 21 $self->userAgent( $ua );
197 }
198  
199 # set up some stuff if we are dumping xml to a directory
200 22 ▶ if ($normalOpts{ DUMPDIR }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($normalOpts{"DUMPDIR"}) 1 21 22
201 1 my $dir = $normalOpts{ DUMPDIR };
202 1 ▶ croak "no such directory '$dir'" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 1 1
203 1 $self->{ dumpDir } = $dir;
204 1 $self->{ lastDump } = 0;
205 }
206  
207 22 return( $self );
208 }
209  
210 =head2 identify()
211  
212 identify() is the OAI verb that tells a metadata repository to provide a
213 description of itself. A call to identify() returns a Net::OAI::Identify object
214 which you can then call methods on to retrieve the information you are
215 intersted in. For example:
216  
217 my $identity = $harvester->identify();
218 print "repository name: ",$identity->repositoryName(),"\n";
219 print "protocol version: ",$identity->protocolVersion(),"\n";
220 print "earliest date stamp: ",$identity->earliestDatestamp(),"\n";
221 print "admin email(s): ", join( ", ", $identity->adminEmail() ), "\n";
222 ...
223  
224 For more details see the L<Net::OAI::Identify> documentation.
225  
226 =cut
227  
228 sub identify {
229 6 ▶ my $self = shift;
Subroutine identify: called CC=3 SCAR=17.7
Pod documented
230 6 my $uri = $self->{ baseURL }->clone();
231 6 $uri->query_form( 'verb' => 'Identify' );
232  
233 6 my $identity = Net::OAI::Identify->new( $self->_get( $uri ) );
234 6 ▶ return $identity if $identity->{ error };
Branch: 1 branch
BranchTrueFalseTotal
if $identity->{"error"} 6 0 6
235  
236 0 my $error = Net::OAI::Error->new( Handler => $identity );
237 0 my $parser = _parser( $error );
238 0 debug( "parsing Identify response " . $identity->file() );
239 0 eval { $parser->parse_uri( $identity->file() ) };
240 0 ▶ if ( $@ ) { _xmlError( $error ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
241 0 $error->set_handler( undef );
242 0 $identity->{ error } = $error;
243 0 return( $identity );
244 }
245  
246 =head2 listMetadataFormats()
247  
248 listMetadataFormats() asks the repository to return a list of metadata formats
249 that it supports. A call to listMetadataFormats() returns an
250 Net::OAI::ListMetadataFormats object.
251  
252 my $list = $harvester->listMetadataFormats();
253 print "archive supports metadata prefixes: ",
254 join( ',', $list->prefixes() ),"\n";
255  
256 If you are interested in the metadata formats available for
257 a particular resource identifier then you can pass in that identifier.
258
259 my $list = $harvester->listMetadataFormats( identifier => '1234567' );
260 print "record identifier 1234567 can be retrieved as ",
261 join( ',', $list->prefixes() ),"\n";
262  
263 See documentation for L<Net::OAI::ListMetadataFormats> for more details.
264  
265 =cut
266  
267 sub listMetadataFormats {
268 2 ▶ my ( $self, %opts ) = @_;
Subroutine listMetadataFormats: called CC=4 SCAR=19.9
Pod documented
269 2 my $uri = $self->{ baseURL }->clone();
270  
271 $uri->query_form( verb => 'ListMetadataFormats',
272 2 ▶ map { (defined $opts{$_}) ? ($_ => $opts{$_}) : () } qw( identifier )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{$_} ? : 1 1 2
273 );
274  
275 2 my $list = Net::OAI::ListMetadataFormats->new( $self->_get( $uri ) );
276 2 ▶ return $list if $list->{ error };
Branch: 1 branch
BranchTrueFalseTotal
if $list->{"error"} 2 0 2
277  
278 0 my $error = Net::OAI::Error->new( Handler => $list );
279 0 my $parser = _parser( $error );
280 0 debug( "parsing ListMetadataFormats response: ".$list->file() );
281 0 eval { $parser->parse_uri( $list->file() ) };
282 0 ▶ if ( $@ ) { _xmlError( $error ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
283 0 $error->set_handler( undef );
284 0 $list->{ error } = $error;
285 0 return( $list );
286 }
287  
288 =head2 getRecord()
289  
290 getRecord() is used to retrieve a single record from a repository. You must pass
291 in the C<identifier> and an optional C<metadataPrefix> parameters to identify
292 the record, and the flavor of metadata you would like. Net::OAI::Harvester
293 includes a parser for OAI DublinCore, so if you do not specifiy a
294 metadataPrefix 'oai_dc' will be assumed. If you would like to drop in your own
295 XML::Handler for another type of metadata use either the C<metadataHandler>
296 or the C<recordHandler> parameter, either the name of the class as string
297 or an already instantiated object of that class.
298  
299 my $result = $harvester->getRecord(
300 identifier => 'abc123',
301 );
302  
303 ## did something go wrong?
304 if ( my $oops = $result->errorCode() ) { ... };
305  
306 ## get the result as Net::OAI::Record object
307 my $record = $result->record(); # undef if error
308  
309 ## directly get the Net::OAI::Record::Header object
310 my $header = $result->header(); # undef if error
311 ## same as
312 my $header = $result->record()->header(); # undef if error
313  
314 ## get the metadata object
315 my $metadata = $result->metadata(); # undef if error or harvested with recordHandler
316  
317 ## or if you would rather use your own XML::Handler
318 ## pass in the package name for the object you would like to create
319 my $result = $harvester->getRecord(
320 identifier => 'abc123',
321 metadataHandler => 'MyHandler'
322 );
323 my $metadata = $result->metadata();
324
325 my $result = $harvester->getRecord(
326 identifier => 'abc123',
327 recordHandler => 'MyCompleteHandler'
328 );
329 my $complete_record = $result->recorddata(); # undef if error or harvested with metadataHandler
330
331 =cut
332  
333 sub getRecord {
334 9 ▶ my ( $self, %opts ) = @_;
Subroutine getRecord: called CC=8 SCAR=28.6
Pod documented
335  
336 croak( "getRecord(): the 'identifier' parameter is required" )
337 9 ▶ unless defined $opts{ 'identifier' };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"identifier"} 0 9 9
338 croak( "getRecord(): the 'metadataPrefix' parameter is required" )
339 9 ▶ unless exists $opts{ 'metadataPrefix' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"metadataPrefix"} 0 9 9
340 croak( "getRecord(): recordHandler and metadataHandler are mutually exclusive" )
341 9 ▶ if $opts{ recordHandler } and $opts{ metadataHandler };
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"recordHandler"} and $opts{"metadataHandler"} 0 9 9
Condition66%2/3
$opts{"recordHandler"} and $opts{"metadataHandler"}
!ll&&!rl&&r
630
MC/DC0%0/2
$opts{"recordHandler"} and $opts{"metadataHandler"}
$opts{"recordHandler"} $opts{"metadataHandler"}
Truth table66%2/3
A and B
A= $opts{"recordHandler"}B= $opts{"metadataHandler"}
ABresult
0X0
100
111
342  
343 9 my $uri = $self->{ baseURL }->clone();
344  
345 $uri->query_form( verb => 'GetRecord',
346 9 ▶ map { (defined $opts{$_}) ? ($_ => $opts{$_}) : () } qw( identifier metadataPrefix )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{$_} ? : 18 0 18
347 );
348  
349 my $record = Net::OAI::GetRecord->new( $self->_get( $uri ),
350 recordHandler => $opts{ recordHandler },
351 metadataHandler => $opts{ metadataHandler },
352 9 );
353 9 ▶ return $record if $record->{ error };
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"error"} 9 0 9
354  
355 0 my $error = Net::OAI::Error->new( Handler => $record );
356 0 my $parser = _parser( $error );
357 0 debug( "parsing GetRecord response " . $record->file() );
358 0 eval { $parser->parse_uri( $record->file() ) };
359 0 ▶ if ( $@ ) { _xmlError( $error ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
360  
361 0 $error->set_handler( undef );
362 0 $record->{ error } = $error;
363 0 return( $record );
364  
365 }
366  
367  
368 =head2 listRecords()
369  
370 listRecords() allows you to retrieve all the records in a data repository.
371 You must supply the C<metadataPrefix> parameter to tell your Net::OAI::Harvester
372 which type of records you are interested in. listRecords() returns an
373 Net::OAI::ListRecords object. There are four other optional parameters C<from>,
374 C<until>, C<set>, and C<resumptionToken> which are better described in the
375 OAI-PMH spec.
376  
377 my $records = $harvester->listRecords(
378 metadataPrefix => 'oai_dc'
379 );
380  
381 ## iterate through the results with next()
382 while ( my $record = $records->next() ) {
383 my $metadata = $record->metadata();
384 ...
385 }
386  
387 If you would like to use your own metadata handler then you can specify
388 the package name of the handler as the C<metadataHandler> (will be exposed
389 to events below the C<metadata> element) or C<recordHandler> (will be
390 exposed to the C<record> element and all its children) parameter, passing
391 either
392  
393 =over 4
394  
395 =item the name of the class as string, in that case a new instance
396 will be created for any OAI record encountered or
397  
398 =item an already instantiated object of that class which will
399 be reused for all records.
400  
401 =back
402  
403 my $records = $harvester->listRecords(
404 metadataPrefix => 'mods',
405 metadataHandler => 'MODS::Handler'
406 );
407  
408 while ( my $record = $records->next() ) {
409 my $metadata = $record->metadata();
410 # $metadata will be a MODS::Handler object
411 }
412  
413 If you want to automatically handle resumption tokens you can achieve
414 this with the listAllRecords() method. In this case the C<next()>
415 method transparently causes the next response to be fetched from
416 the repository if the current response ran out of records and
417 contained a resumptionToken.
418  
419 If you prefer you can handle resumption tokens yourself with a
420 loop, and the resumptionToken() method. You might want to do this
421 if you are working with a repository that wants you to wait between
422 requests or if connectivity problems become an issue during particulary
423 long harvesting runs and you want to implement a retransmission
424 strategy for failing requests.
425  
426 my $records = $harvester->listRecords( metadataPrefix => 'oai_dc' );
427 my $responseDate = $records->responseDate();
428 my $finished = 0;
429  
430 while ( ! $finished ) {
431  
432 while ( my $record = $records->next() ) { # a Net::OAI::Record object
433 my $metadata = $record->metadata();
434 # do interesting stuff here
435 }
436  
437 my $rToken = $records->resumptionToken();
438 if ( $rToken ) {
439 $records = $harvester->listRecords(
440 resumptionToken => $rToken->token()
441 );
442 } else {
443 $finished = 1;
444 }
445  
446 }
447  
448 Please note: Since C<listRecords()> stashes away the individual
449 records it encounters with C<Storable>, special care has to
450 be taken if the handlers you provided make use of XS modules
451 since these objects cannot be reliably handled. Therefore you will
452 have to provide the special serializing and deserializing methods
453 C<STORABLE_freeze()> and C<STORABLE_thaw()> for the objects
454 used by your filter(s).
455  
456  
457 =cut
458  
459 sub listRecords {
460 8 ▶ my ( $self, %opts ) = @_;
Subroutine listRecords: called CC=9 SCAR=32.4
Pod documented
461  
462 croak( "listRecords(): the 'metadataPrefix' parameter is required" )
463 unless ( exists $opts{ 'metadataPrefix' }
464 8 ▶ or defined $opts{ 'resumptionToken' } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"metadataPrefix"} or defined $opts{"resumptionToken"} 0 8 8
Condition33%1/3
exists $opts{"metadataPrefix"} or defined $opts{"resumptionToken"}
l!l&&r!l&&!r
800
MC/DC0%0/2
exists $opts{"metadataPrefix"} or defined $opts{"resumptionToken"}
exists $opts{"metadataPrefix"} defined $opts{"resumptionToken"}
Truth table33%1/3
A or B
A= exists $opts{"metadataPrefix"}B= defined $opts{"resumptionToken"}
ABresult
1X1
011
000
465 croak( "listRecords(): recordHandler and metadataHandler are mutually exclusive" )
466 8 ▶ if $opts{ recordHandler } and $opts{ metadataHandler };
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"recordHandler"} and $opts{"metadataHandler"} 0 8 8
Condition66%2/3
$opts{"recordHandler"} and $opts{"metadataHandler"}
!ll&&!rl&&r
620
MC/DC0%0/2
$opts{"recordHandler"} and $opts{"metadataHandler"}
$opts{"recordHandler"} $opts{"metadataHandler"}
Truth table66%2/3
A and B
A= $opts{"recordHandler"}B= $opts{"metadataHandler"}
ABresult
0X0
100
111
467  
468 8 my $uri = $self->{ baseURL }->clone();
469  
470 $uri->query_form( verb => 'ListRecords',
471 8 ▶ map { (defined $opts{$_}) ? ($_ => $opts{$_}) : () } qw( metadataPrefix from until set resumptionToken )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{$_} ? : 11 29 40
472 );
473  
474 my $list = Net::OAI::ListRecords->new( $self->_get( $uri ),
475 metadataHandler => $opts{ metadataHandler },
476 recordHandler => $opts{ recordHandler },
477 8 );
478 8 ▶ return $list if $list->{ error };
Branch: 1 branch
BranchTrueFalseTotal
if $list->{"error"} 8 0 8
479  
480 0 my $token = Net::OAI::ResumptionToken->new( Handler => $list );
481 0 my $error = Net::OAI::Error->new( Handler => $token );
482 0 my $parser = _parser( $error );
483 0 debug( "parsing ListRecords response " . $list->file() );
484 0 eval { $parser->parse_uri( $list->file() ) };
485 0 ▶ if ( $@ ) { _xmlError( $error ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
486  
487 0 $token->set_handler( undef );
488 0 ▶ $list->{ token } = $token->token() ? $token : undef;
Branch: 1 branch
BranchTrueFalseTotal
$token->token ? : 0 0 0
489  
490 0 $error->set_handler( undef );
491 0 $list->{ error } = $error;
492  
493 0 return( $list );
494 }
495  
496 =head2 listAllRecords()
497  
498 Does exactly what listRecords() does except the C<next()> method
499 will automatically submit resumption tokens as needed.
500  
501 my $records = $harvester->listAllRecords( metadataPrefix => 'oai_dc' );
502  
503 while ( my $record = $records->next() ) { # a Net::OAI::Record object until undef
504 my $metadata = $record->metadata();
505 # do interesting stuff here
506 }
507  
508  
509 =cut
510  
511 sub listAllRecords {
512 1 ▶ my $self = shift;
Subroutine listAllRecords: called CC=1 SCAR=0.0
Pod documented
513 1 debug( "calling listRecords() as part of listAllRecords request" );
514 1 my $list = listRecords( $self, @_ );
515 1 $list->{ harvester } = $self;
516 1 return( $list );
517 }
518  
519 =head2 listIdentifiers()
520  
521 listIdentifiers() takes the same parameters that listRecords() takes, but it
522 returns only the record headers, allowing you to quickly retrieve all the
523 record identifiers for a particular repository. The object returned is a
524 L<Net::OAI::ListIdentifiers> object.
525  
526 my $headers = $harvester->listIdentifiers(
527 metadataPrefix => 'oai_dc'
528 );
529  
530 ## iterate through the results with next()
531 while ( my $header = $identifiers->next() ) { # a Net::OAI::Record::Header object
532 print "identifier: ", $header->identifier(), "\n";
533 }
534  
535 If you want to automatically handle resumption tokens use listAllIdentifiers().
536 If you are working with a repository that encourages pauses between requests
537 you can handle the tokens yourself using the technique described above
538 in listRecords().
539  
540 =cut
541  
542 sub listIdentifiers {
543 4 ▶ my ( $self, %opts ) = @_;
Subroutine listIdentifiers: called CC=7 SCAR=29.8
Pod documented
544 croak( "listIdentifiers(): the 'metadataPrefix' parameter is required" )
545 unless ( exists $opts{ 'metadataPrefix' }
546 4 ▶ or defined $opts{ 'resumptionToken' } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"metadataPrefix"} or defined $opts{"resumptionToken"} 0 4 4
Condition33%1/3
exists $opts{"metadataPrefix"} or defined $opts{"resumptionToken"}
l!l&&r!l&&!r
400
MC/DC0%0/2
exists $opts{"metadataPrefix"} or defined $opts{"resumptionToken"}
exists $opts{"metadataPrefix"} defined $opts{"resumptionToken"}
Truth table33%1/3
A or B
A= exists $opts{"metadataPrefix"}B= defined $opts{"resumptionToken"}
ABresult
1X1
011
000
547 4 my $uri = $self->{ baseURL }->clone();
548  
549 $uri->query_form( verb => 'ListIdentifiers',
550 4 ▶ map { (defined $opts{$_}) ? ($_ => $opts{$_}) : () } qw( metadataPrefix from until set resumptionToken )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{$_} ? : 7 13 20
551 );
552  
553 4 my $list = Net::OAI::ListIdentifiers->new( $self->_get( $uri ) );
554 4 ▶ return( $list ) if $list->{ error };
Branch: 1 branch
BranchTrueFalseTotal
if $list->{"error"} 4 0 4
555  
556 0 my $token = Net::OAI::ResumptionToken->new( Handler => $list );
557 0 my $error = Net::OAI::Error->new( Handler => $token );
558 0 my $parser = _parser( $error );
559 0 debug( "parsing ListIdentifiers response " . $list->file() );
560 0 eval { $parser->parse_uri( $list->file() ) };
561 0 ▶ if ( $@ ) { _xmlError( $error ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
562  
563 0 $token->set_handler( undef );
564 0 ▶ $list->{ token } = $token->token() ? $token : undef;
Branch: 1 branch
BranchTrueFalseTotal
$token->token ? : 0 0 0
565  
566 0 $error->set_handler( undef );
567 0 $list->{ error } = $error;
568  
569 0 return( $list );
570 }
571  
572 =head2 listAllIdentifiers()
573  
574 Does exactly what listIdentifiers() does except C<next()> will automatically
575 submit resumption tokens as needed.
576  
577 =cut
578  
579 sub listAllIdentifiers {
580 1 ▶ my $self = shift;
Subroutine listAllIdentifiers: called CC=1 SCAR=0.0
Pod documented
581 1 debug( "calling listIdentifiers() as part of listAllIdentifiers() call" );
582 1 my $list = listIdentifiers( $self, @_ );
583 1 $list->{ harvester } = $self;
584 1 return( $list );
585 }
586  
587  
588 =head2 listSets()
589  
590 listSets() takes an optional C<resumptionToken> parameter, and returns a
591 Net::OAI::ListSets object. listSets() allows you to harvest a subset of a
592 particular repository with listRecords(). For more information see the OAI-PMH
593 spec and the Net::OAI::ListSets docs.
594  
595 my $sets = $harvester->listSets();
596 foreach ( $sets->setSpecs() ) {
597 print "set spec: $_ ; set name: ", $sets->setName( $_ ), "\n";
598 }
599  
600 =cut
601  
602 sub listSets {
603 1 ▶ my ( $self, %opts ) = @_;
Subroutine listSets: called CC=5 SCAR=26.2
Pod documented
604  
605 1 my $uri = $self->{ baseURL }->clone();
606  
607 $uri->query_form( verb => 'ListSets',
608 1 ▶ map { (defined $opts{$_}) ? ($_ => $opts{$_}) : () } qw( resumptionToken )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{$_} ? : 0 1 1
609 );
610  
611 1 my $list = Net::OAI::ListSets->new( $self->_get( $uri ) );
612 1 ▶ return( $list ) if $list->{ error };
Branch: 1 branch
BranchTrueFalseTotal
if $list->{"error"} 1 0 1
613  
614 0 my $token = Net::OAI::ResumptionToken->new( Handler => $list );
615 0 my $error = Net::OAI::Error->new( Handler => $token );
616 0 my $parser = _parser( $error );
617 0 debug( "parsing ListSets response " . $list->file() );
618 0 eval { $parser->parse_uri( $list->file() ) };
619 0 ▶ if ( $@ ) { _xmlError( $error ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
620  
621 0 $token->set_handler( undef );
622 0 ▶ $list->{ token } = $token->token() ? $token : undef;
Branch: 1 branch
BranchTrueFalseTotal
$token->token ? : 0 0 0
623 0 $error->set_handler( undef );
624 0 $list->{ error } = $error;
625 0 return( $list );
626 }
627  
628 =head2 baseURL()
629  
630 Gets or sets the base URL for the repository being harvested (as L<URI/>).
631  
632 $harvester->baseURL( 'http://memory.loc.gov/cgi-bin/oai2_0' );
633  
634 Or if you want to know what the current baseURL is
635  
636 $baseURL = $harvester->baseURL();
637  
638 =cut
639  
640 sub baseURL {
641 0 ▶ my ( $self, $url ) = @_;
Subroutine baseURL: not called CC=2 SCAR=17.9
Pod documented
642 0 ▶ if ( $url ) { $self->{ baseURL } = URI->new( $url ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 0 0 0
643 ## The HTTP UserAgent modifies its URI object upon execution,
644 ## therefore we'll always provide a clone <s>have to reconstruct: trim the query part ...</s>
645 # my $c = $self->{ baseURL }; # ->canonical();
646 # if ( $c && ($c =~ /^([^\?]*)\?/) ) { # $c might be undefined
647 # return $1};
648 # return $c;
649 0 return $self->{ baseURL };
650 }
651  
652 =head2 userAgent()
653  
654 Gets or sets the LWP::UserAgent object being used to perform the HTTP
655 transactions. This method could be useful if you wanted to change the
656 agent string, timeout, or some other feature.
657  
658 =cut
659  
660 sub userAgent {
661 23 ▶ my ( $self, $ua ) = @_;
Subroutine userAgent: called CC=3 SCAR=11.0
Pod documented
662 23 ▶ if ( $ua ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua) 22 1 23
663 22 ▶ $ua->isa('LWP::UserAgent') or croak( "userAgent() needs a valid LWP::UserAgent" );
Branch: 1 branch
BranchTrueFalseTotal
$ua->isa("LWP::UserAgent") or croak("userAgent() needs a valid LWP::UserAgent") 0 22 22
664 22 $self->{ userAgent } = $ua;
665 }
666 23 return( $self->{ userAgent } );
667 }
668  
669 ## internal stuff
670  
671 sub _get {
672 30 ▶ my ($self,$uri) = @_;
Subroutine _get: called CC=10 SCAR=25.8
673 30 my $ua = $self->{ userAgent };
674  
675 30 my ($fh, $file);
676 30 ▶ if ( $self->{ dumpDir } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dumpDir'}) { } 1 29 30
677 1 my $filePrefix = $self->{lastDump}++;
678 1 $file = sprintf("%s/%08d.xml", $self->{dumpDir}, $filePrefix);
679 1 $fh = IO::File->new($file, 'w');
680 } else {
681 29 ( $fh, $file ) = tempfile(UNLINK => 1);
682 }
683  
684 30 debug( "fetching ".$uri->as_string() );
685 30 debug( "writing to file: $file" );
686 30 my $request = HTTP::Request->new( GET => $uri->as_string() );
687 30 ▶ my $response = $ua->request( $request, sub { print $fh shift; }, 8192 );
Subroutine __ANON__: called CC=1 SCAR=0.0
688 30 close( $fh );
689  
690 30 ▶ if ( $response->is_error() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 29 1 30
691 # HTTP::Request does not provide a file in case of HTTP level errors,
692 # therefore we do not return the name of the non-existant file but
693 # rather the original HTTP::Response object
694 29 debug( "caught HTTP level error" . $response->message() );
695 29 ▶ my $error = Net::OAI::Error->new(
Condition50%1/2
$response->header('Retry-After') || ''
l!l
029
MC/DC0%0/1
$response->header('Retry-After') || ''
$response->header('Retry-After')
Truth table50%1/2
A
A= $response->header('Retry-After')
Aresult
11
00
696 errorString => 'HTTP Level Error: ' . $response->message(),
697 errorCode => $response->code(),
698 HTTPError => $response,
699 HTTPRetryAfter => $response->header("Retry-After") || "",
700 );
701 return(
702 # file => $file,
703 29 error => $error
704 );
705 }
706 1 ▶ if ( my $ct = $response->header("Content-Type") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ct = $response->header("Content-Type")) 1 0 1
707 1 debug( "Content-type $ct in HTTP response" );
708 1 ▶ unless ( $ct =~ /^text\/xml(;|$)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ct =~ m[^text/xml(;|$)]) 1 0 1
709 1 ▶ return (error => Net::OAI::Error->new(errorCode => 'xmlContentError',
Condition50%1/2
$response->header('Retry-After') || ''
l!l
01
MC/DC0%0/1
$response->header('Retry-After') || ''
$response->header('Retry-After')
Truth table50%1/2
A
A= $response->header('Retry-After')
Aresult
11
00
710 errorString => "Content-Type: text/xml is mandatory (got: $ct)!"),
711 HTTPError => $response,
712 HTTPRetryAfter => $response->header("Retry-After") || "",
713 )
714 };
715 0 ▶ if ( $ct =~ /; charset=(\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /; charset=(\S+)/) 0 0 0
716 0 my $cs = $1;
717 0 ▶ return (error => Net::OAI::Error->new(errorCode => 'xmlContentError',
Branch: 1 branch
BranchTrueFalseTotal
unless $cs =~ /^utf-8/i 0 0 0
Condition0%0/2
$response->header('Retry-After') || ''
l!l
00
MC/DC0%0/1
$response->header('Retry-After') || ''
$response->header('Retry-After')
Truth table0%0/2
A
A= $response->header('Retry-After')
Aresult
11
00
718 errorString => "charset=UTF-8 is mandatory (got: $cs)!"),
719 HTTPError => $response,
720 HTTPRetryAfter => $response->header("Retry-After") || "",
721 ) unless $cs =~ /^utf-8/i;
722 };
723 }
724  
725 return(
726 0 file => $file,
727 );
728  
729 }
730  
731 sub _parser {
732 2 ▶ my $handler = shift;
Subroutine _parser: called CC=7 SCAR=28.9
733 2 my $factory = XML::SAX::ParserFactory->new();
734 2 my $parser;
735 2 $factory->require_feature(Namespaces);
736 2 eval { $parser = $factory->parser( Handler => $handler ) };
737 2 ▶ carp ref($factory)." threw an exception:\n\t$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
738  
739 2 ▶ if ( $parser && ref($parser) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser and ref $parser) 2 0 2
Condition33%1/3
$parser and ref $parser
!ll&&!rl&&r
002
MC/DC0%0/2
$parser and ref $parser
$parser ref $parser
Truth table33%1/3
A and B
A= $parserB= ref $parser
ABresult
0X0
100
111
740 2 debug( "using SAX parser " . ref($parser) . " " . $parser->VERSION );
741 2 return $parser;
742 };
743  
744 0 carp "!!! Please check your setup of XML::SAX, especially ParserDetails.ini !!!\n";
745 0 local($XML::SAX::ParserPackage) = "XML::SAX::PurePerl";
746 0 eval { $parser = $factory->parser( Handler => $handler ) };
747 0 ▶ carp ref($factory)." threw an exception again:\n\t$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
748 0 ▶ if ( $parser && ref($parser) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser and ref $parser) 0 0 0
Condition0%0/3
$parser and ref $parser
!ll&&!rl&&r
000
MC/DC0%0/2
$parser and ref $parser
$parser ref $parser
Truth table0%0/3
A and B
A= $parserB= ref $parser
ABresult
0X0
100
111
749 0 carp "Successfuly forced assignment of a parser: " . ref($parser) . " " . $parser->VERSION ."\n";
750 0 return $parser;
751 };
752  
753 0 croak( ref($factory)." on request did not even give us the default XML::SAX::PurePerl parser.\nGiving up." );
754 }
755  
756 sub _xmlError {
757 0 ▶ my $e = shift;
Subroutine _xmlError: not called CC=1 SCAR=6.9
758 0 carp "caught xml parsing error: $@";
759 0 $e->errorString( "XML parsing error: $@" );
760 0 $e->errorCode( 'xmlParseError' );
761 }
762  
763  
764 sub _verifyHandler {
765 18 ▶ my $package_or_instance = shift;
Subroutine _verifyHandler: called CC=5 SCAR=16.2
766 18 ▶ if ( ref($package_or_instance) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $package_or_instance) { } 6 12 18
767 6 ▶ $package_or_instance->isa('XML::SAX::Base')
Branch: 1 branch
BranchTrueFalseTotal
$package_or_instance->isa("XML::SAX::Base") or _fatal("Handler $package_or_instance must inherit from XML::SAX::Base\n") 0 6 6
768 or _fatal( "Handler $package_or_instance must inherit from XML::SAX::Base\n" )
769 }
770 else {
771 12 ▶ eval( "use $package_or_instance" );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
772 12 ▶ _fatal( "unable to locate Handler $package_or_instance in: " .
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
773 join( "\n\t", @INC ) ) if $@;
774 _fatal( "Handler $package_or_instance must inherit from XML::SAX::Base\n" )
775 12 ▶ if ( ! grep { 'XML::SAX::Base' } eval( '@' . $package_or_instance . '::ISA' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {"XML::SAX::Base";} eval '@' . $package_or_instance . '::ISA' 0 12 12
776 }
777 18 return( 1 );
778 }
779  
780  
781 sub debug {
782 97 ▶ return unless $Net::OAI::Harvester::DEBUG;
Subroutine debug: called CC=2 SCAR=10.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::OAI::Harvester::DEBUG 97 0 97
783 0 my $msg = shift;
784 0 carp "oai-harvester: " . localtime() . ": $msg\n";
785 }
786  
787 sub _fatal {
788 0 ▶ my $msg = shift;
Subroutine _fatal: not called CC=1 SCAR=6.9
789 0 croak "fatal: $msg";
790 }
791  
792 =head1 DIAGNOSTICS
793  
794 If you would like to see diagnostic information when harvesting is running
795 then set Net::OAI::Harvester::DEBUG to a true value.
796  
797 $Net::OAI::Harvester::DEBUG = 1;
798  
799  
800  
801  
802 =head1 PERFORMANCE
803  
804 XML::SAX is used for parsing, but it presents a generalized interface to many
805 parsers. It comes with XML::Parser::PurePerl by default, which is nice since
806 you don't have to worry about getting the right libraries installed. However
807 XML::Parser::PurePerl is rather slow compared to XML::LibXML. If you
808 are a speed freak install XML::LibXML from CPAN today.
809  
810 If you have a particular parser you want to use you can set the
811 $XML::SAX::ParserPackage variable appropriately. See XML::SAX::ParserFactory
812 documentation for details.
813  
814  
815  
816 =head1 ENVIRONMENT
817  
818 The modules use LWP for HTTP operations, thus C<PERL_LWP_ENV_PROXY> controls
819 wether the "_proxy" environment settings shall be honored.
820  
821  
822 =head1 TODO
823  
824 =over 4
825  
826 =item *
827  
828 Allow Net::OAI::ListMetadataFormats to store more than just the metadata
829 prefixes.
830  
831 =item *
832  
833 Implement Net::OAI::Set for iterator access to Net::OAI::ListSets.
834  
835 =item *
836  
837 Implement Net::OAI::Harvester::listAllSets().
838  
839 =item *
840  
841 More documentation of other classes.
842  
843 =item *
844  
845 Document custom XML::Handler creation.
846  
847 =item *
848  
849 Handle optional compression.
850  
851 =item *
852  
853 Create common handlers for other metadata formats (MARC, qualified DC, etc).
854  
855 =item *
856  
857 Or at least provide a generic record handler as fallback, since using
858 L<Net::OAI::Record::OAI_DC> makes absolutely no sense except for ... oai_dc records.
859  
860 =item *
861  
862 Selectively load Net::OAI::* classes as needed, rather than getting all of them
863 at once at the beginning of Net::OAI::Harvester.
864  
865 =back
866  
867 =head1 SEE ALSO
868  
869 =over 4
870  
871 =item *
872  
873 OAI-PMH Specification at L<http://www.openarchives.org>
874  
875 =item *
876  
877 L<Net::OAI::Base>
878  
879 =item *
880  
881 L<Net::OAI::Error>
882  
883 =item *
884  
885 L<Net::OAI::GetRecord>
886  
887 =item *
888  
889 L<Net::OAI::Identify>
890  
891 =item *
892  
893 L<Net::OAI::ListIdentifiers>
894  
895 =item *
896  
897 L<Net::OAI::ListMetadataFormats>
898  
899 =item *
900  
901 L<Net::OAI::ListRecords>
902  
903 =item *
904  
905 L<Net::OAI::ListSets>
906  
907 =item *
908  
909 L<Net::OAI::Record>
910  
911 =item *
912  
913 L<Net::OAI::Record::Header>
914  
915 =item *
916  
917 L<Net::OAI::Record::OAI_DC>
918  
919 =item *
920  
921 L<Net::OAI::Record::DocumentHelper>
922  
923 =item *
924  
925 L<Net::OAI::Record::NamespaceFilter>
926  
927 =item *
928  
929 L<Net::OAI::ResumptionToken>
930  
931 =item *
932  
933 L<Storable>
934  
935 =back
936  
937  
938 =head1 AUTHORS
939  
940 Ed Summers <ehs@pobox.com>
941  
942 Martin Emmerich <Martin.Emmerich@oew.de>
943  
944 Thomas Berger <ThB@gymel.com>
945  
946 =head1 LICENSE
947  
948 This is free software, you may use it and distribute it under the same terms as Perl itself.
949  
950 =cut
951  
952 17 1;
953  
« blib/lib/Net/OAI/Base.pm ↑ Summary blib/lib/Net/OAI/Record/OAI_DC.pm »