| 1 | package Net::OAI::ListRecords; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 17 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 17 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 17 | ▶ | use base qw( XML::SAX::Base Net::OAI::Base ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 17 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 17 | ▶ | use Net::OAI::Record; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 17 | ▶ | use Net::OAI::Record::Header; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 17 | ▶ | use File::Temp qw( tempfile ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 17 | ▶ | use Storable qw( store_fd fd_retrieve ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 17 | ▶ | use IO::File; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | Net::OAI::ListRecords - Results of the ListRecords OAI-PMH verb. | ||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | =head1 METHODS | ||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | Like all responses to OAI verbs, ListRecords is based on L<Net::OAI::Base> | ||||||||||||||||||||||
| 24 | and inherits its methods. | ||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | =head2 new() | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | You probably don't want to be using this method yourself, since | ||||||||||||||||||||||
| 30 | Net::OAI::Harvester::listRecords() calls it for you. | ||||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | sub new { | ||||||||||||||||||||||
| 35 | 8 | ▶ | my ( $class, %opts ) = @_; | ||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 8 | my $package; | |||||||||||||||||||||
| 38 | 8 | ▶ | if ( $package = $opts{ recordHandler } ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 39 | 2 | ▶ | $opts{ metadataHandler } and croak( "you may pass either a recordHandler or a metadataHandler to getRecord()" ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 40 | 2 | delete $opts { metadataHandler }; | |||||||||||||||||||||
| 41 | } elsif ( $package = $opts{ metadataHandler } ) { | ||||||||||||||||||||||
| 42 | 2 | delete $opts{ recordHandler }; | |||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||
| 44 | 4 | delete $opts{ recordHandler }; | |||||||||||||||||||||
| 45 | 4 | $package = $opts{ metadataHandler } = 'Net::OAI::Record::OAI_DC'; | |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||
| 47 | 8 | Net::OAI::Harvester::_verifyHandler( $package ); | |||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | 8 | ▶ | my $self = bless \%opts, ref( $class ) || $class; | ||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||
| 50 | 8 | my ( $fh, $tempfile ) = tempfile(UNLINK => 1); | |||||||||||||||||||||
| 51 | 8 | binmode( $fh, ':utf8' ); | |||||||||||||||||||||
| 52 | 8 | $self->{ recordsFileHandle } = $fh; | |||||||||||||||||||||
| 53 | 8 | $self->{ recordsFilename } = $tempfile; | |||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | ## so we can store code refs | ||||||||||||||||||||||
| 56 | 8 | $Storable::Deparse = 1; | |||||||||||||||||||||
| 57 | 8 | $Storable::Eval = 1; | |||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | 8 | $self->{ _prefixmap } = {}; | |||||||||||||||||||||
| 60 | 8 | return( $self ); | |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | =head2 next() | ||||||||||||||||||||||
| 64 | |||||||||||||||||||||||
| 65 | Returns the L<Net::OAI::Record> object for the next OAI record in the | ||||||||||||||||||||||
| 66 | response, C<undef> if none remain. resumptionToken handling is performed | ||||||||||||||||||||||
| 67 | automagically if the original request was listAllIdentifiers(). | ||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | sub next { | ||||||||||||||||||||||
| 72 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
next: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | ## if we haven't opened our object store do it now | ||||||||||||||||||||||
| 75 | 0 | ▶ | if ( ! $self->{ recordsFileHandle } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 76 | $self->{ recordsFileHandle } = IO::File->new( $self->{ recordsFilename } ) | ||||||||||||||||||||||
| 77 | 0 | ▶ | or croak "unable to open temp file: ".$self->{ recordsFilename }; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 78 | ## we assume utf8 encoding (perhaps wrongly) | ||||||||||||||||||||||
| 79 | 0 | binmode( $self->{ recordsFileHandle }, ':utf8' ); | |||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | ## no more data to read back from our object store then return undef | ||||||||||||||||||||||
| 83 | 0 | ▶ | if ( $self->{ recordsFileHandle }->eof() ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 84 | $self->{ recordsFileHandle }->close() or croak "Could not close() ".$self->{ recordsFilename } | ||||||||||||||||||||||
| 85 | 0 | ▶ | .". File system full?"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 86 | 0 | return( $self->handleResumptionToken( 'listRecords' ) ); | |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | ## get an object back from the store, thaw and return it | ||||||||||||||||||||||
| 90 | 0 | my $record = fd_retrieve( $self->{ recordsFileHandle } ); | |||||||||||||||||||||
| 91 | 0 | return( $record ); | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | =head2 metadataHandler() | ||||||||||||||||||||||
| 95 | =head2 recordHandler() | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | Returns the name of the package being used to represent the individual metadata | ||||||||||||||||||||||
| 98 | records. If unspecified it defaults to L<Net::OAI::Record::OAI_DC> which | ||||||||||||||||||||||
| 99 | should be ok. | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | sub metadataHandler { | ||||||||||||||||||||||
| 104 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
metadataHandler: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 105 | 0 | return( $self->{ metadataHandler } ); | |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | sub recordHandler { | ||||||||||||||||||||||
| 109 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
recordHandler: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 110 | 0 | return( $self->{ recordHandler } ); | |||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | ## SAX Handlers | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | sub start_prefix_mapping { | ||||||||||||||||||||||
| 116 | 0 | ▶ | my ($self, $mapping) = @_; | ||||||||||||||||||||
|
Subroutine
start_prefix_mapping: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 117 | 0 | ▶ | if ( $self->get_handler() ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 118 | 0 | return $self->SUPER::start_prefix_mapping( $mapping )}; | |||||||||||||||||||||
| 119 | 0 | $self->{ _prefixmap }->{$mapping->{ Prefix }} = $mapping; | |||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | sub end_prefix_mapping { | ||||||||||||||||||||||
| 123 | 0 | ▶ | my ($self, $mapping) = @_; | ||||||||||||||||||||
|
Subroutine
end_prefix_mapping: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 124 | 0 | ▶ | if ( $self->get_handler() ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 125 | 0 | return $self->SUPER::end_prefix_mapping( $mapping )}; | |||||||||||||||||||||
| 126 | 0 | delete $self->{ _prefixmap }->{$mapping->{ Prefix }}; | |||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | sub start_element { | ||||||||||||||||||||||
| 130 | 0 | ▶ | my ( $self, $element ) = @_; | ||||||||||||||||||||
|
Subroutine
start_element: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||
| 131 | 0 | ▶ | return $self->SUPER::start_element( $element ) unless $element->{NamespaceURI} eq Net::OAI::Harvester::XMLNS_OAI; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | ## if we are at the start of a new record then we need an empty | ||||||||||||||||||||||
| 134 | ## metadata object to fill up | ||||||||||||||||||||||
| 135 | 0 | ▶ | if ( $element->{ LocalName } eq 'record' ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 136 | ## we store existing downstream handler so we can replace | ||||||||||||||||||||||
| 137 | ## it after we are done retrieving the metadata record | ||||||||||||||||||||||
| 138 | 0 | $self->{ OLD_Handler } = $self->get_handler(); | |||||||||||||||||||||
| 139 | my $header = $self->{ recordHandler } | ||||||||||||||||||||||
| 140 | ? Net::OAI::Record::Header->new( | ||||||||||||||||||||||
| 141 | Handler => (ref($self->{ recordHandler }) ? $self->{ recordHandler } : $self->{ recordHandler }->new()), | ||||||||||||||||||||||
| 142 | fwdAll => 1, | ||||||||||||||||||||||
| 143 | ) | ||||||||||||||||||||||
| 144 | : Net::OAI::Record::Header->new( | ||||||||||||||||||||||
| 145 | 0 | ▶ | Handler => (ref($self->{ metadataHandler }) ? $self->{ metadataHandler } : $self->{ metadataHandler }->new()), | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 146 | ($Net::OAI::Harvester::OLDmetadataHandler ? (fwdAll => 1) : ()), | ||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||
| 148 | 0 | $self->set_handler( $header ); | |||||||||||||||||||||
| 149 | 0 | foreach my $mapping ( values %{$self->{_prefixmap}} ) { | |||||||||||||||||||||
| 150 | 0 | $self->SUPER::start_prefix_mapping($mapping)}; | |||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||
| 152 | elsif ( $element->{ LocalName } eq 'ListRecords' ) { | ||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||
| 154 | 0 | return $self->SUPER::start_element( $element ); | |||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | sub end_element { | ||||||||||||||||||||||
| 158 | 0 | ▶ | my ( $self, $element ) = @_; | ||||||||||||||||||||
|
Subroutine
end_element: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||
| 159 | |||||||||||||||||||||||
| 160 | 0 | $self->SUPER::end_element( $element ); | |||||||||||||||||||||
| 161 | 0 | ▶ | return unless $element->{NamespaceURI} eq Net::OAI::Harvester::XMLNS_OAI; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 162 | |||||||||||||||||||||||
| 163 | ## if we've got to the end of the record we need to stash | ||||||||||||||||||||||
| 164 | ## away the object in our object store on disk | ||||||||||||||||||||||
| 165 | 0 | ▶ | if ( $element->{ LocalName } eq 'record' ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 166 | |||||||||||||||||||||||
| 167 | ## we need to swap out the existing metadata handler and freeze | ||||||||||||||||||||||
| 168 | ## it on disk | ||||||||||||||||||||||
| 169 | 0 | my $header = $self->get_handler(); | |||||||||||||||||||||
| 170 | 0 | my $data = $header->get_handler(); | |||||||||||||||||||||
| 171 | 0 | $header->set_handler( undef ); ## remove reference to $record | |||||||||||||||||||||
| 172 | |||||||||||||||||||||||
| 173 | ## set handler to what is was before we started processing | ||||||||||||||||||||||
| 174 | ## the record | ||||||||||||||||||||||
| 175 | 0 | $self->set_handler( $self->{ OLD_Handler } ); | |||||||||||||||||||||
| 176 | 0 | my $record; | |||||||||||||||||||||
| 177 | 0 | ▶ | if ( $self->{ recordHandler } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 178 | 0 | $record = Net::OAI::Record->new(header => $header, recorddata => $data) | |||||||||||||||||||||
| 179 | } else { | ||||||||||||||||||||||
| 180 | 0 | $record = Net::OAI::Record->new(header => $header, metadata => $data) | |||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||
| 182 | |||||||||||||||||||||||
| 183 | ## commit the object to disk | ||||||||||||||||||||||
| 184 | 0 | Net::OAI::Harvester::debug( "committing record to object store" ); | |||||||||||||||||||||
| 185 | 0 | store_fd( $record, $self->{ recordsFileHandle } ); | |||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||
| 187 | |||||||||||||||||||||||
| 188 | ## otherwise if we got to the end of our list we can close | ||||||||||||||||||||||
| 189 | ## our object stash on disk | ||||||||||||||||||||||
| 190 | elsif ( $element->{ LocalName } eq 'ListRecords' ) { | ||||||||||||||||||||||
| 191 | 0 | $self->{ recordsFileHandle }->close(); | |||||||||||||||||||||
| 192 | 0 | $self->{ recordsFileHandle } = undef; | |||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||
| 194 | |||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||
| 196 | |||||||||||||||||||||||
| 197 | sub _fatal { | ||||||||||||||||||||||
| 198 | 0 | ▶ | print STDERR "fatal: ", shift, "\n"; | ||||||||||||||||||||
|
Subroutine
_fatal: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 199 | 0 | exit(1); | |||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||
| 201 | |||||||||||||||||||||||
| 202 | 17 | 1; | |||||||||||||||||||||
| 203 | |||||||||||||||||||||||