blib/lib/Plack/App/Catmandu/OAI.pm

filter: Statementstmt 25.4% 101 / 397 Branchbran 0.0% 0 / 162 Conditioncond 0.0% 0 / 65 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 37.0% 20 / 54 Podpod 33.3% 1 / 3 totaltotal 16.9% 122 / 721 CC 118 SCAR 77.2
CC
118
Cov
16%
__ANON__
5256.0
_new_token
240.0
BUILD
210.0
CRAP 2259.7
↑ Summary
1 package Plack::App::Catmandu::OAI;
2  
3 1 our $VERSION = '0.02';
4  
5 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Catmandu::Util qw(:is io);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Catmandu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Catmandu;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Types::Standard qw(Str ArrayRef HashRef Enum Int CodeRef ScalarRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Types::Common::String qw(NonEmptyStr);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Types::Common::Numeric qw(PositiveInt);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Catmandu::Exporter::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Catmandu::Fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::MessagePack;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use MIME::Base64 qw(encode_base64url decode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use feature qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶no warnings qw(experimental::signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 has store_name => (
27 is => 'ro',
28 isa => Str,
29 init_arg => 'store',
30 );
31  
32 1 has bag_name => (
33 is => 'ro',
34 isa => Str,
35 init_arg => 'bag',
36 );
37  
38 has fix => (
39 is => 'ro',
40 0 ▶ coerce => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
41 0 ▶ if (is_string($_[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_string($_[0])) { } 0 0 0
elsif (is_array_ref($_[0])) { } 0 0 0
42 0 $_[0] = Catmandu::Fix->new(fixes => [$_[0]]);
43 } elsif (is_array_ref($_[0])) {
44 0 $_[0] = Catmandu::Fix->new(fixes => $_[0]);
45 }
46 0 $_[0];
47 },
48 1 );
49  
50 has deleted => (
51 is => 'ro',
52 isa => CodeRef,
53 0 ▶ default => sub { sub {0}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 1 );
55  
56 has set_specs_for => (
57 is => 'ro',
58 isa => CodeRef,
59 0 ▶ default => sub { sub {[];}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 1 );
61  
62 1 has datestamp_field => (
63 is => 'ro',
64 isa => NonEmptyStr,
65 required => 1,
66 );
67  
68 1 has datestamp_index => (
69 is => 'lazy',
70 isa => NonEmptyStr,
71 );
72  
73 1 has repositoryName => (
74 is => 'ro',
75 isa => NonEmptyStr,
76 required => 1,
77 );
78  
79 1 has adminEmail => (
80 is => 'ro',
81 isa => ArrayRef[NonEmptyStr],
82 );
83  
84 1 has compression => (
85 is => 'ro',
86 isa => ArrayRef[NonEmptyStr],
87 );
88  
89 1 has description => (
90 is => 'ro',
91 isa => ArrayRef[NonEmptyStr],
92 );
93  
94 1 has earliestDatestamp => (
95 is => 'ro',
96 isa => NonEmptyStr,
97 );
98  
99 has deletedRecord => (
100 is => 'ro',
101 isa => Enum[qw(no persistent transient)],
102 0 ▶ default => sub { 'no'; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 1 );
104  
105 1 has repositoryIdentifier => (
106 is => 'ro',
107 isa => NonEmptyStr,
108 required => 1,
109 );
110  
111 1 has cql_filter => (
112 is => 'ro',
113 isa => NonEmptyStr,
114 );
115  
116 1 has default_search_params => (
117 is => 'ro',
118 isa => HashRef,
119 );
120  
121 has search_strategy => (
122 is => 'ro',
123 isa => Enum[qw(paginate es.scroll filter)],
124 0 ▶ default => sub { 'paginate'; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 1 );
126  
127 has limit => (
128 is => 'ro',
129 isa => PositiveInt,
130 0 ▶ default => sub { 100; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 1 );
132  
133 has delimiter => (
134 is => 'ro',
135 isa => NonEmptyStr,
136 0 ▶ default => sub { ':'; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 1 );
138  
139 1 has sampleIdentifier => (
140 is => 'ro',
141 isa => NonEmptyStr,
142 required => 1,
143 );
144  
145 1 has xsl_stylesheet => (
146 is => 'ro',
147 isa => NonEmptyStr,
148 );
149  
150 has metadata_formats => (
151 is => 'ro',
152 isa => ArrayRef[HashRef],
153 0 ▶ default => sub { []; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 ▶ coerce => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
155 0 for my $format (@{$_[0]}) {
156 0 ▶ if (my $fix = $format->{fix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fix = $format->{"fix"}) 0 0 0
157 0 $format->{fix} = Catmandu::Fix->new(fixes => $fix);
158 }
159 }
160 0 $_[0];
161 },
162 1 );
163  
164 has sets => (
165 is => 'ro',
166 isa => ArrayRef[HashRef],
167 0 ▶ default => sub { []; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 1 );
169  
170 has template_options => (
171 is => 'ro',
172 isa => HashRef,
173 0 ▶ default => sub { +{}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
174 1 );
175  
176 has granularity => (
177 is => 'ro',
178 isa => NonEmptyStr,
179 0 ▶ default => sub { 'YYYY-MM-DDThh:mm:ssZ' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 1 );
181  
182 1 has collectionIcon => (
183 is => 'ro',
184 isa => HashRef,
185 );
186  
187 1 has get_record_cql_pattern => (
188 is => 'lazy',
189 isa => NonEmptyStr,
190 );
191  
192 1 has datestamp_pattern => (
193 is => 'ro',
194 isa => NonEmptyStr,
195 );
196  
197 # internal methods
198 1 has _bag => (
199 is => 'lazy',
200 init_arg => undef,
201 );
202  
203 1 has _datestamp_formatter => (
204 is => 'lazy',
205 init_arg => undef,
206 );
207  
208 has _message_pack => (
209 is => 'ro',
210 lazy => 1,
211 0 ▶ default => sub { Data::MessagePack->new->utf8; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 1 init_arg => undef,
213 );
214  
215 1 has _templ_error => (
216 is => 'rw',
217 isa => ScalarRef[Str],
218 init_arg => undef,
219 );
220  
221 1 has _templ_get_record => (
222 is => 'rw',
223 isa => ScalarRef[Str],
224 init_arg => undef,
225 );
226  
227 1 has _templ_identify => (
228 is => 'rw',
229 isa => ScalarRef[Str],
230 init_arg => undef,
231 );
232  
233 1 has _templ_list_identifiers => (
234 is => 'rw',
235 isa => ScalarRef[Str],
236 init_arg => undef,
237 );
238  
239 1 has _templ_list_records => (
240 is => 'rw',
241 isa => ScalarRef[Str],
242 init_arg => undef,
243 );
244  
245 1 has _templ_list_metadata_formats => (
246 is => 'rw',
247 isa => ScalarRef[Str],
248 init_arg => undef,
249 );
250  
251 1 has _templ_list_sets => (
252 is => 'rw',
253 isa => ScalarRef[Str],
254 init_arg => undef,
255 );
256  
257 0 ▶sub _serialize_token ($self, $token) {
Subroutine _serialize_token: not called CC=1 SCAR=6.9
258 0 encode_base64url($self->_message_pack->pack($token));
259 }
260  
261 0 ▶sub _deserialize_token ($self, $token) {
Subroutine _deserialize_token: not called CC=1 SCAR=6.9
262 0 $self->_message_pack->unpack(decode_base64url($token));
263 }
264  
265 0 ▶sub _build_datestamp_index ($self) {
Subroutine _build_datestamp_index: not called CC=1 SCAR=6.9
266 0 $self->datestamp_field;
267 }
268  
269 0 ▶sub _get_record_cql_pattern ($self) {
Subroutine _get_record_cql_pattern: not called CC=1 SCAR=6.9
270 0 $self->bag->id_key . ' exact "%s"';
271 }
272  
273 0 ▶sub _build__bag ($self) {
Subroutine _build__bag: not called CC=1 SCAR=6.9
274 0 Catmandu->store($self->store_name)->bag($self->bag_name);
275 }
276  
277 0 ▶sub _build__datestamp_formatter ($self) {
Subroutine _build__datestamp_formatter: not called CC=3 SCAR=24.8
278 0 my $datestamp_parser;
279 0 ▶ if ($self->datestamp_pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->datestamp_pattern) 0 0 0
280 0 $datestamp_parser = DateTime::Format::Strptime->new(
281 pattern => $self->datestamp_pattern,
282 on_error => 'undef',
283 );
284 }
285  
286 0 ▶ $datestamp_parser ? sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
287 0 $datestamp_parser->parse_datetime($_[0])->iso8601 . 'Z';
288 0 ▶ } : sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
289 0 $_[0];
290 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$datestamp_parser ? : 0 0 0
291 }
292  
293 0 ▶sub BUILD {
Subroutine BUILD: not called CC=14 SCAR=53.5
Pod undocumented
294 0 my ($self, $args) = @_;
295  
296 0 ▶ if ($self->search_strategy eq 'es.scroll') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->search_strategy eq "es.scroll") 0 0 0
297 0 ▶ $self->default_search_params->{scroll} //= '10m';
Condition0%0/2
$self->default_search_params->{"scroll"} //= "10m"
l!l
00
MC/DC0%0/1
$self->default_search_params->{"scroll"} //= "10m"
$self->default_search_params->{"scroll"}
Truth table0%0/2
A
A= $self->default_search_params->{"scroll"}
Aresult
11
00
298 }
299  
300  
301 0 my $ns = "oai:".$self->repositoryIdentifier.":";
302  
303 0 my $branding = "";
304 0 ▶ if (my $icon = $self->collectionIcon) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $icon = $self->collectionIcon) 0 0 0
305 0 ▶ if (my $url = $icon->{url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = $icon->{"url"}) 0 0 0
306 0 $branding .= <<TT;
307 <description>
308 <branding xmlns="http://www.openarchives.org/OAI/2.0/branding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/branding/ http://www.openarchives.org/OAI/2.0/branding.xsd">
309 <collectionIcon>
310 <url>$url</url>
311 TT
312 0 for my $tag (qw(link title width height)) {
313 0 ▶ my $val = $icon->{$tag} // next;
Condition0%0/2
$icon->{$tag} // (next)
l!l
00
MC/DC0%0/1
$icon->{$tag} // (next)
$icon->{$tag}
Truth table0%0/2
A
A= $icon->{$tag}
Aresult
11
00
314 0 $branding .= "<$tag>$val</$tag>\n";
315 }
316  
317 0 $branding .= <<TT;
318 </collectionIcon>
319 </branding>
320 </description>
321 TT
322 }
323 }
324  
325 0 my $xsl_stylesheet = "";
326 0 ▶ if (my $xsl_path = $self->xsl_stylesheet) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xsl_path = $self->xsl_stylesheet) 0 0 0
327 0 $xsl_stylesheet
328 = "<?xml-stylesheet type='text/xsl' href='$xsl_path' ?>";
329 }
330  
331 0 my $template_header = <<TT;
332 <?xml version="1.0" encoding="UTF-8"?>
333 $xsl_stylesheet
334 <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
335 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
336 xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
337 <responseDate>[% response_date %]</responseDate>
338 [%- IF params.resumptionToken %]
339 <request verb="[% params.verb %]" resumptionToken="[% params.resumptionToken %]">[% uri_base %]</request>
340 [%- ELSE %]
341 <request[% FOREACH param IN params %] [% param.key %]="[% param.value | xml %]"[% END %]>[% uri_base %]</request>
342 [%- END %]
343 TT
344  
345 0 my $template_footer = <<TT;
346 </OAI-PMH>
347 TT
348  
349 0 my $template_error = <<TT;
350 $template_header
351 [%- FOREACH error IN errors %]
352 <error code="[% error.0 %]">[% error.1 | xml %]</error>
353 [%- END %]
354 $template_footer
355 TT
356 0 $self->_templ_error(\$template_error);
357  
358 0 my $template_record_header = <<TT;
359 <header[% IF deleted %] status="deleted"[% END %]>
360 <identifier>${ns}[% id %]</identifier>
361 <datestamp>[% datestamp %]</datestamp>
362 [%- FOREACH s IN setSpec %]
363 <setSpec>[% s %]</setSpec>
364 [%- END %]
365 </header>
366 TT
367  
368 0 my $template_get_record = <<TT;
369 $template_header
370 <GetRecord>
371 <record>
372 $template_record_header
373 [%- UNLESS deleted %]
374 <metadata>
375 [% metadata %]
376 </metadata>
377 [%- END %]
378 </record>
379 </GetRecord>
380 $template_footer
381 TT
382 0 $self->_templ_get_record(\$template_get_record);
383  
384 0 my @identify_extra_fields;
385 0 for my $field (qw(adminEmail description compression)) {
386 0 my $vals = $self->$field;
387 push @identify_extra_fields,
388 0 join('', map {"<$field>$_</$field>"} @$vals);
389 }
390  
391 0 my $repositoryName = $self->repositoryName;
392 0 my $deletedRecord = $self->deletedRecord;
393 0 my $granularity = $self->granularity;
394 0 my $repositoryIdentifier = $self->repositoryIdentifier;
395 0 my $delimiter = $self->delimiter;
396 0 my $sampleIdentifier = $self->sampleIdentifier;
397  
398 0 my $template_identify = <<TT;
399 $template_header
400 <Identify>
401 <repositoryName>$repositoryName</repositoryName>
402 <baseURL>[% uri_base %]</baseURL>
403 <protocolVersion>2.0</protocolVersion>
404 <earliestDatestamp>[% earliest_datestamp %]</earliestDatestamp>
405 <deletedRecord>$deletedRecord</deletedRecord>
406 <granularity>$granularity</granularity>
407 <description>
408 <oai-identifier xmlns="http://www.openarchives.org/OAI/2.0/oai-identifier"
409 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
410 xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai-identifier http://www.openarchives.org/OAI/2.0/oai-identifier.xsd">
411 <scheme>oai</scheme>
412 <repositoryIdentifier>$repositoryIdentifier</repositoryIdentifier>
413 <delimiter>$delimiter</delimiter>
414 <sampleIdentifier>$sampleIdentifier</sampleIdentifier>
415 </oai-identifier>
416 </description>
417 @identify_extra_fields
418 $branding
419 </Identify>
420 $template_footer
421 TT
422  
423 0 $self->_templ_identify(\$template_identify);
424  
425 0 my $template_list_identifiers = <<TT;
426 $template_header
427 <ListIdentifiers>
428 [%- FOREACH records %]
429 $template_record_header
430 [%- END %]
431 [%- IF resumption_token %]
432 <resumptionToken completeListSize="[% total %]">[% resumption_token %]</resumptionToken>
433 [%- ELSE %]
434 <resumptionToken completeListSize="[% total %]"/>
435 [%- END %]
436 </ListIdentifiers>
437 $template_footer
438 TT
439  
440 0 $self->_templ_list_identifiers(\$template_list_identifiers);
441  
442 0 my $template_list_records = <<TT;
443 $template_header
444 <ListRecords>
445 [%- FOREACH records %]
446 <record>
447 $template_record_header
448 [%- UNLESS deleted %]
449 <metadata>
450 [% metadata %]
451 </metadata>
452 [%- END %]
453 </record>
454 [%- END %]
455 [%- IF resumption_token %]
456 <resumptionToken completeListSize="[% total %]">[% resumption_token %]</resumptionToken>
457 [%- ELSE %]
458 <resumptionToken completeListSize="[% total %]"/>
459 [%- END %]
460 </ListRecords>
461 $template_footer
462 TT
463  
464 0 $self->_templ_list_records(\$template_list_records);
465  
466 0 my $template_list_metadata_formats = <<TT;
467 $template_header
468 <ListMetadataFormats>
469 TT
470 0 for my $format (@{$self->metadata_formats}) {
471 0 $template_list_metadata_formats .= <<TT;
472 <metadataFormat>
473 <metadataPrefix>$format->{metadataPrefix}</metadataPrefix>
474 <schema>$format->{schema}</schema>
475 <metadataNamespace>$format->{metadataNamespace}</metadataNamespace>
476 </metadataFormat>
477 TT
478 }
479 0 $template_list_metadata_formats .= <<TT;
480 </ListMetadataFormats>
481 $template_footer
482 TT
483  
484 0 $self->_templ_list_metadata_formats(\$template_list_metadata_formats);
485  
486 0 my $template_list_sets = <<TT;
487 $template_header
488 <ListSets>
489 TT
490 0 for my $set (@{$self->sets}) {
491 0 $template_list_sets .= <<TT;
492 <set>
493 <setSpec>$set->{setSpec}</setSpec>
494 <setName>$set->{setName}</setName>
495 TT
496  
497 0 ▶ my $set_descriptions = $set->{setDescription} // [];
Condition0%0/2
$set->{"setDescription"} // []
l!l
00
MC/DC0%0/1
$set->{"setDescription"} // []
$set->{"setDescription"}
Truth table0%0/2
A
A= $set->{"setDescription"}
Aresult
11
00
498 0 ▶ $set_descriptions = [$set_descriptions]
Branch: 1 branch
BranchTrueFalseTotal
unless is_array_ref($set_descriptions) 0 0 0
499 unless is_array_ref($set_descriptions);
500 $template_list_sets .= "<setDescription>$_</setDescription>"
501 0 for @$set_descriptions;
502  
503 0 $template_list_sets .= <<TT;
504 </set>
505 TT
506 }
507 0 $template_list_sets .= <<TT;
508 </ListSets>
509 $template_footer
510 TT
511 0 $self->_templ_list_sets(\$template_list_sets);
512 }
513  
514 0 ▶sub _tt_process ($self, $tmpl, $data) {
Subroutine _tt_process: not called CC=1 SCAR=6.9
515 0 my $out = "";
516 0 open my $fh, '>:utf8', \$out;
517 0 my $exporter = Catmandu::Exporter::Template->new(
518 template => $tmpl,
519 fh => $fh,
520 );
521 0 $exporter->add($data);
522 0 $exporter->commit;
523 0 $out;
524 }
525  
526 0 ▶sub _render ($self, $body) {
Subroutine _render: not called CC=1 SCAR=6.9
527 0 [200, ['Content-Type' => 'application/xml; charset=utf-8'], [$body]];
528 }
529  
530 0 ▶sub _render_error ($self, $vars) {
Subroutine _render_error: not called CC=1 SCAR=6.9
531 0 $self->_render($self->_tt_process($self->_templ_error, $vars));
532 }
533  
534 1 my $VERBS = {
535 GetRecord => {
536 valid => {metadataPrefix => 1, identifier => 1},
537 required => [qw(metadataPrefix identifier)],
538 },
539 Identify => {valid => {}, required => []},
540 ListIdentifiers => {
541 valid => {
542 metadataPrefix => 1,
543 from => 1,
544 until => 1,
545 set => 1,
546 resumptionToken => 1
547 },
548 required => [qw(metadataPrefix)],
549 },
550 ListMetadataFormats =>
551 {valid => {identifier => 1, resumptionToken => 1}, required => []},
552 ListRecords => {
553 valid => {
554 metadataPrefix => 1,
555 from => 1,
556 until => 1,
557 set => 1,
558 resumptionToken => 1
559 },
560 required => [qw(metadataPrefix)],
561 },
562 ListSets => {valid => {resumptionToken => 1}, required => []},
563 };
564  
565 0 ▶sub _new_token ($self, $hits, $params, $from, $until, $old_token = undef) {
Subroutine _new_token: not called CC=15 SCAR=54.8
566 0 ▶ my $n = $old_token && $old_token->{_n} ? $old_token->{_n} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$old_token && $old_token->{'_n'} ? : 0 0 0
Condition0%0/3
$old_token and $old_token->{"_n"}
!ll&&!rl&&r
000
MC/DC0%0/2
$old_token and $old_token->{"_n"}
$old_token $old_token->{"_n"}
Truth table0%0/3
A and B
A= $old_tokenB= $old_token->{"_n"}
ABresult
0X0
100
111
567 0 $n += $hits->size;
568  
569 0 ▶ return unless $n < $hits->total;
Branch: 1 branch
BranchTrueFalseTotal
unless $n < $hits->total 0 0 0
570  
571 0 my $strategy = $self->search_strategy;
572  
573 0 my $token;
574  
575 0 ▶ if ($strategy eq 'paginate' && $hits->more) {
Branch: 3 branches
BranchTrueFalseTotal
if ($strategy eq 'paginate' and $hits->more) { } 0 0 0
elsif ($strategy eq 'es.scroll' and exists $hits->{'scroll_id'}) { } 0 0 0
elsif ($strategy eq 'filter' and $hits->more) { } 0 0 0
Condition0%0/3
$strategy eq "paginate" and $hits->more
!ll&&!rl&&r
000
MC/DC0%0/2
$strategy eq "paginate" and $hits->more
$strategy eq "paginate" $hits->more
Truth table0%0/3
A and B
A= $strategy eq "paginate"B= $hits->more
ABresult
0X0
100
111
576 0 $token = {start => $hits->start + $hits->limit};
577 }
578 ▶ elsif ($strategy eq 'es.scroll' && exists $hits->{scroll_id}) {
Condition0%0/3
$strategy eq "es.scroll" and exists $hits->{"scroll_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$strategy eq "es.scroll" and exists $hits->{"scroll_id"}
$strategy eq "es.scroll" exists $hits->{"scroll_id"}
Truth table0%0/3
A and B
A= $strategy eq "es.scroll"B= exists $hits->{"scroll_id"}
ABresult
0X0
100
111
579 0 $token = {scroll_id => $hits->{scroll_id}};
580 }
581 ▶ elsif ($strategy eq 'filter' && $hits->more) {
Condition0%0/3
$strategy eq "filter" and $hits->more
!ll&&!rl&&r
000
MC/DC0%0/2
$strategy eq "filter" and $hits->more
$strategy eq "filter" $hits->more
Truth table0%0/3
A and B
A= $strategy eq "filter"B= $hits->more
ABresult
0X0
100
111
582 0 $token = {_id => $hits->hits->[$hits->size - 1]->{_id}};
583 }
584 else {
585 0 return;
586 }
587  
588 0 $token->{_n} = $n;
589 0 ▶ $token->{_s} = $params->{set} if defined $params->{set};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"set"} 0 0 0
590 $token->{_m} = $params->{metadataPrefix}
591 0 ▶ if defined $params->{metadataPrefix};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"metadataPrefix"} 0 0 0
592 0 ▶ $token->{_f} = $from if defined $from;
Branch: 1 branch
BranchTrueFalseTotal
if defined $from 0 0 0
593 0 ▶ $token->{_u} = $until if defined $until;
Branch: 1 branch
BranchTrueFalseTotal
if defined $until 0 0 0
594 0 $token;
595 }
596  
597 0 ▶sub _search ($self, $q, $token = undef) {
Subroutine _search: not called CC=10 SCAR=47.0
598 0 my $strategy = $self->search_strategy;
599  
600 my %args = (
601 0 %{$self->default_search_params},
602 limit => $self->limit,
603 cql_query => $q,
604 );
605 0 ▶ if ($token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token) 0 0 0
606 0 ▶ if ($strategy eq 'paginate' && exists $token->{start}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($strategy eq 'paginate' and exists $token->{'start'}) { } 0 0 0
elsif ($strategy eq 'es.scroll' and exists $token->{'scroll_id'}) { } 0 0 0
elsif ($strategy eq 'filter' and $token->{'_id'}) { } 0 0 0
Condition0%0/3
$strategy eq "paginate" and exists $token->{"start"}
!ll&&!rl&&r
000
MC/DC0%0/2
$strategy eq "paginate" and exists $token->{"start"}
$strategy eq "paginate" exists $token->{"start"}
Truth table0%0/3
A and B
A= $strategy eq "paginate"B= exists $token->{"start"}
ABresult
0X0
100
111
607 0 $args{start} = $token->{start};
608 }
609 ▶ elsif ($strategy eq 'es.scroll' && exists $token->{scroll_id}) {
Condition0%0/3
$strategy eq "es.scroll" and exists $token->{"scroll_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$strategy eq "es.scroll" and exists $token->{"scroll_id"}
$strategy eq "es.scroll" exists $token->{"scroll_id"}
Truth table0%0/3
A and B
A= $strategy eq "es.scroll"B= exists $token->{"scroll_id"}
ABresult
0X0
100
111
610 0 $args{scroll_id} = $token->{scroll_id};
611 }
612 ▶ elsif ($strategy eq 'filter' && $token->{_id}) {
Condition0%0/3
$strategy eq "filter" and $token->{"_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$strategy eq "filter" and $token->{"_id"}
$strategy eq "filter" $token->{"_id"}
Truth table0%0/3
A and B
A= $strategy eq "filter"B= $token->{"_id"}
ABresult
0X0
100
111
613 0 my $cql_query = $args{cql_query};
614 0 ▶ if (is_string($cql_query)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_string($cql_query)) { } 0 0 0
615 0 $cql_query .= qq( and _id > "$token->{_id}");
616 } else {
617 0 $cql_query = qq(_id > "$token->{_id}");
618 }
619 0 $args{cql_query} = $cql_query;
620 }
621 }
622  
623 0 $self->_bag->search(%args);
624 }
625  
626 0 ▶sub to_app ($self) {
Subroutine to_app: not called CC=1 SCAR=6.9
Pod documented
627 0 ▶ sub {
Subroutine __ANON__: not called CC=72 SCAR=85.7
628 0 my $env = $_[0];
629  
630 0 my $req = Plack::Request->new($env);
631  
632 0 ▶ return not_found() if $req->method() ne "GET";
Branch: 1 branch
BranchTrueFalseTotal
if $req->method ne "GET" 0 0 0
633  
634 0 my $params = $req->query_parameters();
635 0 my $path = $req->path_info();
636 0 my $uri_base = $req->base();
637 0 my $response_date = DateTime->now->iso8601 . 'Z';
638 0 my $errors = [];
639 0 my $format;
640 my $set;
641 0 my $ns = "oai:".$self->repositoryIdentifier.":";
642 0 my $resumptionToken = $params->get('resumptionToken');
643 0 my $verb = $params->get('verb');
644 0 my $vars = {
645 uri_base => $uri_base->as_string,
646 request_uri => $uri_base . $path,
647 response_date => $response_date,
648 errors => $errors,
649 };
650  
651 0 ▶ if ($verb and my $spec = $VERBS->{$verb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb and my $spec = $VERBS->{$verb}) { } 0 0 0
Condition0%0/3
$verb and my $spec = $VERBS->{$verb}
!ll&&!rl&&r
000
MC/DC0%0/2
$verb and my $spec = $VERBS->{$verb}
$verb my $spec = $VERBS->{$verb}
Truth table0%0/3
A and B
A= $verbB= my $spec = $VERBS->{$verb}
ABresult
0X0
100
111
652 0 my $valid = $spec->{valid};
653 0 my $required = $spec->{required};
654  
655 0 ▶ if ($valid->{resumptionToken} && exists $params->{resumptionToken}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid->{'resumptionToken'} and exists $params->{'resumptionToken'}) { } 0 0 0
Condition0%0/3
$valid->{"resumptionToken"} and exists $params->{"resumptionToken"}
!ll&&!rl&&r
000
MC/DC0%0/2
$valid->{"resumptionToken"} and exists $params->{"resumptionToken"}
$valid->{"resumptionToken"} exists $params->{"resumptionToken"}
Truth table0%0/3
A and B
A= $valid->{"resumptionToken"}B= exists $params->{"resumptionToken"}
ABresult
0X0
100
111
656 0 ▶ if (keys(%$params) > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$params > 2) 0 0 0
657 0 push @$errors, [badArgument => "resumptionToken cannot be combined with other parameters"];
658 }
659 } else {
660 0 for my $key (keys %$params) {
661 0 ▶ next if $key eq 'verb';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "verb" 0 0 0
662 0 ▶ unless ($valid->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid->{$key}) 0 0 0
663 0 push @$errors, [badArgument => "parameter $key is illegal"];
664 }
665 }
666 0 for my $key (@$required) {
667 0 ▶ unless (exists $params->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params->{$key}) 0 0 0
668 0 push @$errors, [badArgument => "parameter $key is missing"];
669 }
670 }
671 }
672 } else {
673 0 push @$errors, [badVerb => "illegal OAI verb"];
674 }
675  
676 0 ▶ if (@$errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$errors) 0 0 0
677 0 return $self->_render_error($vars);
678 }
679  
680 0 $vars->{params} = +{$params->flatten};
681  
682 0 ▶ if ($resumptionToken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resumptionToken) 0 0 0
683 0 ▶ unless (is_string($resumptionToken)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_string($resumptionToken)) 0 0 0
684 0 push @$errors,
685 [badResumptionToken =>
686 "resumptionToken is not in the correct format"
687 ];
688 }
689  
690 0 ▶ if ($verb eq 'ListSets') {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb eq 'ListSets') { } 0 0 0
691 0 push @$errors,
692 [badResumptionToken => "resumptionToken isn't necessary"];
693 } else {
694 0 ▶ try {
Subroutine __ANON__: not called CC=5 SCAR=34.0
695 0 my $token = $self->_deserialize_token($resumptionToken);
696 0 ▶ $params->{set} = $token->{_s} if defined $token->{_s};
Branch: 1 branch
BranchTrueFalseTotal
if defined $token->{"_s"} 0 0 0
697 $params->{metadataPrefix} = $token->{_m}
698 0 ▶ if defined $token->{_m};
Branch: 1 branch
BranchTrueFalseTotal
if defined $token->{"_m"} 0 0 0
699 0 ▶ $params->{from} = $token->{_f} if defined $token->{_f};
Branch: 1 branch
BranchTrueFalseTotal
if defined $token->{"_f"} 0 0 0
700 0 ▶ $params->{until} = $token->{_u} if defined $token->{_u};
Branch: 1 branch
BranchTrueFalseTotal
if defined $token->{"_u"} 0 0 0
701 0 $vars->{token} = $token;
702 }
703 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
704 0 push @$errors,
705 [badResumptionToken =>
706 "resumptionToken is not in the correct format"
707 ];
708 0 };
709 }
710 }
711  
712 0 ▶ if (my $setSpec = $params->get('set')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $setSpec = $params->get("set")) 0 0 0
713 0 ▶ if (scalar(@{$self->sets}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->sets;} == 0) { } 0 0 0
714 0 push @$errors, [noSetHierarchy => "sets are not supported"];
715 } else {
716 0 for (@{$self->sets}) {
717 0 ▶ if ($_->{setSpec} eq $setSpec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"setSpec"} eq $setSpec) 0 0 0
718 0 $set = $_;
719 0 last;
720 }
721 }
722 0 ▶ unless ($self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 0 0 0
723 0 push @$errors, [badArgument => "set does not exist"];
724 }
725 }
726 }
727  
728 0 ▶ if (my $metadataPrefix = $params->get('metadataPrefix')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $metadataPrefix = $params->get("metadataPrefix")) 0 0 0
729 0 for (@{$self->metadata_formats}) {
730 0 ▶ if ($metadataPrefix eq $_->{metadataPrefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadataPrefix eq $_->{"metadataPrefix"}) 0 0 0
731 0 $format = $_;
732 0 last;
733 }
734 }
735 0 ▶ unless ($format) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format) 0 0 0
736 0 push @$errors,
737 [cannotDisseminateFormat =>
738 "metadataPrefix $metadataPrefix is not supported"
739 ];
740 }
741 }
742  
743 0 ▶ if (@$errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$errors) 0 0 0
744 0 return $self->_render_error($vars);
745 }
746  
747 0 ▶ if ($verb eq 'GetRecord') {
Branch: 5 branches
BranchTrueFalseTotal
if ($verb eq 'GetRecord') { } 0 0 0
elsif ($verb eq 'Identify') { } 0 0 0
elsif ($verb eq 'ListIdentifiers' or $verb eq 'ListRecords') { } 0 0 0
elsif ($verb eq 'ListMetadataFormats') { } 0 0 0
elsif ($verb eq 'ListSets') { } 0 0 0
748 0 my $id = $params->get('identifier');
749 0 $id =~ s/^$ns//;
750  
751 my $rec = $self->_bag->search(
752 0 %{$self->default_search_params},
753 cql_query => sprintf($self->get_record_cql_pattern, $id),
754 start => 0,
755 limit => 1,
756 )->first;
757  
758 0 ▶ if (defined $rec) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rec) 0 0 0
759 0 ▶ if ($self->fix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fix) 0 0 0
760 0 $rec = $self->fix->fix($rec);
761 }
762  
763 0 $vars->{id} = $id;
764 0 $vars->{datestamp} = $self->_datestamp_formatter->($rec->{$self->datestamp_field()});
765 0 $vars->{deleted} = $self->deleted->($rec);
766 0 $vars->{setSpec} = $self->set_specs_for->($rec);
767 0 my $metadata = "";
768 my $exporter = Catmandu::Exporter::Template->new(
769 0 %{$self->template_options},
770 template => $format->{template},
771 0 file => \$metadata,
772 );
773 0 ▶ if ($format->{fix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format->{"fix"}) 0 0 0
774 0 $rec = $format->{fix}->fix($rec);
775 }
776 0 $exporter->add($rec);
777 0 $exporter->commit;
778 0 $vars->{metadata} = $metadata;
779 0 ▶ unless ($vars->{deleted} && $self->deletedRecord eq 'no') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vars->{"deleted"} and $self->deletedRecord eq "no") 0 0 0
Condition0%0/3
$vars->{"deleted"} and $self->deletedRecord eq "no"
!ll&&!rl&&r
000
MC/DC0%0/2
$vars->{"deleted"} and $self->deletedRecord eq "no"
$vars->{"deleted"} $self->deletedRecord eq "no"
Truth table0%0/3
A and B
A= $vars->{"deleted"}B= $self->deletedRecord eq "no"
ABresult
0X0
100
111
780 0 return $self->_render($self->_tt_process($self->_templ_get_record, $vars));
781 }
782 }
783 0 push @$errors,
784 [idDoesNotExist =>
785 "identifier $params->{identifier} is unknown or illegal"
786 ];
787 0 return $self->_render_error($vars);
788  
789 } elsif ($verb eq 'Identify') {
790  
791 0 ▶ $vars->{earliest_datestamp} = $self->earliestDatestamp || do {
Condition0%0/3
$self->earliestDatestamp || do { my $hits = $self->_bag->search(%{$self->default_search_params;}, 'cql_query', $self->cql_filter || 'cql.allRecords', 'limit', 1, 'sru_sortkeys', $self->datestamp_index . ',,1'); if (my $rec = $hits->first) { $self->_datestamp_formatter->($$rec{$self->datestamp_field}); } else { '1970-01-01T00:00:01Z'; } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->earliestDatestamp || do { my $hits = $self->_bag->search(%{$self->default_search_params;}, 'cql_query', $self->cql_filter || 'cql.allRecords', 'limit', 1, 'sru_sortkeys', $self->datestamp_index . ',,1'); if (my $rec = $hits->first) { $self->_datestamp_formatter->($$rec{$self->datestamp_field}); } else { '1970-01-01T00:00:01Z'; } }
$self->earliestDatestamp do { my $hits = $self->_bag->search(%{$self->default_search_params;}, 'cql_query', $self->cql_filter || 'cql.allRecords', 'limit', 1, 'sru_sortkeys', $self->datestamp_index . ',,1'); if (my $rec = $hits->first) { $self->_datestamp_formatter->($$rec{$self->datestamp_field}); } else { '1970-01-01T00:00:01Z'; } }
Truth table0%0/3
A || B
A= $self->earliestDatestampB= do { my $hits = $self->_bag->search(%{$self->default_search_params;}, 'cql_query', $self->cql_filter || 'cql.allRecords', 'limit', 1, 'sru_sortkeys', $self->datestamp_index . ',,1'); if (my $rec = $hits->first) { $self->_datestamp_formatter->($$rec{$self->datestamp_field}); } else { '1970-01-01T00:00:01Z'; } }
ABresult
1X1
011
000
792 my $hits = $self->_bag->search(
793 0 ▶ %{$self->default_search_params},
Condition0%0/2
$self->cql_filter || 'cql.allRecords'
l!l
00
MC/DC0%0/1
$self->cql_filter || 'cql.allRecords'
$self->cql_filter
Truth table0%0/2
A
A= $self->cql_filter
Aresult
11
00
794 cql_query => $self->cql_filter || 'cql.allRecords',
795 limit => 1,
796 sru_sortkeys => $self->datestamp_index.",,1",
797 );
798 0 ▶ if (my $rec = $hits->first) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rec = $hits->first) { } 0 0 0
799 0 $self->_datestamp_formatter->($rec->{$self->datestamp_field});
800 } else {
801 0 '1970-01-01T00:00:01Z';
802 }
803 };
804 0 return $self->_render($self->_tt_process($self->_templ_identify, $vars));
805  
806 ▶ } elsif ($verb eq 'ListIdentifiers' || $verb eq 'ListRecords') {
Condition0%0/3
$verb eq "ListIdentifiers" or $verb eq "ListRecords"
l!l&&r!l&&!r
000
MC/DC0%0/2
$verb eq "ListIdentifiers" or $verb eq "ListRecords"
$verb eq "ListIdentifiers" $verb eq "ListRecords"
Truth table0%0/3
A or B
A= $verb eq "ListIdentifiers"B= $verb eq "ListRecords"
ABresult
1X1
011
000
807  
808 0 my $from = $params->get('from');
809 0 my $until = $params->get('until');
810  
811 0 for my $datestamp (($from, $until)) {
812 0 ▶ $datestamp || next;
Branch: 1 branch
BranchTrueFalseTotal
$datestamp or next 0 0 0
813 0 ▶ if ($datestamp !~ /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}Z)?$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($datestamp =~ /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}Z)?$/o) 0 0 0
814 0 push @$errors,
815 [badArgument =>
816 "datestamps must have the format YYYY-MM-DD or YYYY-MM-DDThh:mm:ssZ"
817 ];
818 0 return $self->_render_error($vars);
819 }
820 }
821  
822 0 ▶ if ($from && $until && length($from) != length($until)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from and $until and length $from != length $until) 0 0 0
Condition0%0/6
$from and $until and length $from != length $until
!ll&&!rl&&r
000
$from and $until
!ll&&!rl&&r
000
MC/DC0%0/3
$from and $until and length $from != length $until
$from $until length $from != length $until
Truth table0%0/4
A and B and C
A= $fromB= $untilC= length $from != length $until
ABCresult
0XX0
10X0
1100
1111
823 0 push @$errors,
824 [
825 badArgument => "datestamps must have the same granularity"
826 ];
827 0 return $self->_render_error($vars);
828 }
829  
830 0 ▶ if ($from && $until && $from gt $until) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from and $until and $from gt $until) 0 0 0
Condition0%0/6
$from and $until and $from gt $until
!ll&&!rl&&r
000
$from and $until
!ll&&!rl&&r
000
MC/DC0%0/3
$from and $until and $from gt $until
$from $until $from gt $until
Truth table0%0/4
A and B and C
A= $fromB= $untilC= $from gt $until
ABCresult
0XX0
10X0
1100
1111
831 0 push @$errors,
832 [badArgument => "from is more recent than until"];
833 0 return $self->_render_error($vars);
834 }
835  
836 0 ▶ if ($from && length($from) == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from and length $from == 10) 0 0 0
Condition0%0/3
$from and length $from == 10
!ll&&!rl&&r
000
MC/DC0%0/2
$from and length $from == 10
$from length $from == 10
Truth table0%0/3
A and B
A= $fromB= length $from == 10
ABresult
0X0
100
111
837 0 $from = "${from}T00:00:00Z";
838 }
839 0 ▶ if ($until && length($until) == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($until and length $until == 10) 0 0 0
Condition0%0/3
$until and length $until == 10
!ll&&!rl&&r
000
MC/DC0%0/2
$until and length $until == 10
$until length $until == 10
Truth table0%0/3
A and B
A= $untilB= length $until == 10
ABresult
0X0
100
111
840 0 $until = "${until}T23:59:59Z";
841 }
842  
843 0 my @cql;
844 0 my $cql_from = $from;
845 0 my $cql_until = $until;
846 0 ▶ if (my $pattern = $self->datestamp_pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pattern = $self->datestamp_pattern) 0 0 0
847 0 ▶ $cql_from
Branch: 1 branch
BranchTrueFalseTotal
if $cql_from 0 0 0
848 = DateTime::Format::ISO8601->parse_datetime($from)
849 ->strftime($pattern)
850 if $cql_from;
851 0 ▶ $cql_until
Branch: 1 branch
BranchTrueFalseTotal
if $cql_until 0 0 0
852 = DateTime::Format::ISO8601->parse_datetime($until)
853 ->strftime($pattern)
854 if $cql_until;
855 }
856  
857 0 ▶ push @cql, "(".$self->cql_filter.")" if $self->cql_filter;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cql_filter 0 0 0
858 0 ▶ push @cql, qq|($format->{cql})| if $format->{cql};
Branch: 1 branch
BranchTrueFalseTotal
if $format->{"cql"} 0 0 0
859 0 ▶ push @cql, qq|($set->{cql})| if $set && $set->{cql};
Branch: 1 branch
BranchTrueFalseTotal
if $set and $set->{"cql"} 0 0 0
Condition0%0/3
$set and $set->{"cql"}
!ll&&!rl&&r
000
MC/DC0%0/2
$set and $set->{"cql"}
$set $set->{"cql"}
Truth table0%0/3
A and B
A= $setB= $set->{"cql"}
ABresult
0X0
100
111
860 0 ▶ push @cql, "(".$self->datestamp_index ." >= \"$cql_from\")" if $cql_from;
Branch: 1 branch
BranchTrueFalseTotal
if $cql_from 0 0 0
861 0 ▶ push @cql, "(".$self->datestamp_index ." <= \"$cql_until\")" if $cql_until;
Branch: 1 branch
BranchTrueFalseTotal
if $cql_until 0 0 0
862 0 ▶ unless (@cql) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@cql) 0 0 0
863 0 push @cql, "(cql.allRecords)";
864 }
865  
866 0 my $search = $self->_search(join(' and ', @cql), $vars->{token});
867  
868 0 ▶ unless ($search->total) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($search->total) 0 0 0
869 0 push @$errors, [noRecordsMatch => "no records found"];
870 0 return $self->_render_error($vars);
871 }
872  
873 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $new_token = $self->_new_token($search, $params, $from, $until, $vars->{"token"}))) 0 0 0
874 defined(
875 my $new_token = $self->_new_token(
876 $search, $params,
877 $from, $until, $vars->{token}
878 )
879 )
880 )
881 {
882 0 $vars->{resumption_token} = $self->_serialize_token($new_token);
883 }
884  
885 0 $vars->{total} = $search->total;
886  
887 0 ▶ if ($verb eq 'ListIdentifiers') {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb eq 'ListIdentifiers') { } 0 0 0
888 $vars->{records} = [
889 map {
890 0 my $rec = $_;
891 0 my $id = $rec->{$self->_bag->id_key};
892  
893 0 ▶ if ($self->fix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fix) 0 0 0
894 0 $rec = $self->fix->fix($rec);
895 }
896  
897 {
898 id => $id,
899 datestamp => $self->_datestamp_formatter->(
900 0 $rec->{$self->datestamp_field()}
901 ),
902 deleted => $self->deleted->($rec),
903 setSpec => $self->set_specs_for->($rec),
904 };
905 0 } @{$search->hits}
906 ];
907 0 return $self->_render($self->_tt_process($self->_templ_list_identifiers, $vars));
908 } else {
909 $vars->{records} = [
910 map {
911 0 my $rec = $_;
912 0 my $id = $rec->{$self->_bag->id_key};
913  
914 0 ▶ if ($self->fix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fix) 0 0 0
915 0 $rec = $self->fix->fix($rec);
916 }
917  
918 0 my $deleted = $self->deleted->($rec);
919  
920 my $rec_vars = {
921 id => $id,
922 datestamp => $self->_datestamp_formatter->(
923 0 $rec->{$self->datestamp_field()}
924 ),
925 deleted => $deleted,
926 setSpec => $self->set_specs_for->($rec),
927 };
928 0 ▶ unless ($deleted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($deleted) 0 0 0
929 0 my $metadata = "";
930 my $exporter = Catmandu::Exporter::Template->new(
931 0 %{$self->template_options},
932 template => $format->{template},
933 0 file => \$metadata,
934 );
935 0 ▶ if ($format->{fix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format->{"fix"}) 0 0 0
936 0 $rec = $format->{fix}->fix($rec);
937 }
938 0 $exporter->add($rec);
939 0 $exporter->commit;
940 0 $rec_vars->{metadata} = $metadata;
941 }
942 0 $rec_vars;
943 0 } @{$search->hits}
944 ];
945 0 return $self->_render($self->_tt_process($self->_templ_list_records, $vars));
946 }
947  
948 } elsif ($verb eq 'ListMetadataFormats') {
949 0 ▶ if (my $id = $params->get('identifier')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $params->get("identifier")) 0 0 0
950 0 $id =~ s/^$ns//;
951 0 ▶ unless ($self->_bag->get($id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_bag->get($id)) 0 0 0
952 0 push @$errors,
953 [idDoesNotExist =>
954 "identifier $id is unknown or illegal"
955 ];
956 0 return $self->_render_error($vars);
957 }
958 }
959 0 return $self->_render($self->_tt_process($self->_templ_list_metadata_formats, $vars));
960 }
961 elsif ($verb eq 'ListSets') {
962 0 return $self->_render($self->_tt_process($self->_templ_list_sets, $vars));
963 }
964  
965 0 };
966 }
967  
968 0 ▶sub not_found ($self) {
Subroutine not_found: not called CC=1 SCAR=6.9
Pod undocumented
969 0 [404, ['Content-Type' => 'text/plain'], ['not found']];
970 }
971  
972 1 1;
973  
974  
975 =head1 NAME
976  
977 Plack::App::Catmandu::OAI - drop in replacement for Dancer::Plugin::Catmandu::OAI
978  
979 =head1 SYNOPSIS
980  
981 use Plack::Builder;
982 Plack::App::Catmandu::OAI;
983  
984 builder {
985 enable 'ReverseProxy';
986 enable '+Dancer::Middleware::Rebase', base => Catmandu->config->{uri_base}, strip => 1;
987 mount "/oai" => Plack::App::Catmandu::OAI->new(
988 repositoryName => 'my repo',
989 store => 'search',
990 bag => 'publication',
991 cql_filter => 'type = dataset',
992 limit => 100,
993 datestamp_field => 'date_updated',
994 deleted => sub { $_[0]->{deleted}; },
995 set_specs_for => sub {
996 $_[0]->{specs};
997 }
998 )->to_app;
999 };
1000  
1001 =head1 CONSTRUCTOR ARGUMENTS
1002  
1003 =over
1004  
1005 =item store
1006  
1007 Type: string
1008  
1009 Description: Name of Catmandu store in your catmandu store configuration
1010  
1011 Default: C<default>
1012  
1013 =item bag
1014  
1015 Type: string
1016  
1017 Description: Name of Catmandu bag in your catmandu store configuration
1018  
1019 Default: C<data>
1020  
1021 This must be a bag that implements L<Catmandu::CQLSearchable>, and that configures a C<cql_mapping>
1022  
1023 =item fix
1024  
1025 Either name of fix, path to fix file or instance of L<Catmandu::Fix>
1026  
1027 This fix will be applied to every record found, either via GetRecord or ListRecords
1028  
1029 =item deleted
1030  
1031 Type: code reference
1032  
1033 Description: code reference that is supplied a record, and must return 0 or 1 determing if that record is deleted or not.
1034  
1035 Required: false
1036  
1037 =item set_specs_for
1038  
1039 Type: code reference
1040  
1041 Description: code reference that is supplied a record, and must return an array reference of strings, showing to which sets that record belongs
1042  
1043 Required: false
1044  
1045 =item datestamp_field
1046  
1047 Type: string
1048  
1049 Description: name of the field in the record that contains the oai record datestamp ('datestamp' in our example above)
1050  
1051 Required: true
1052  
1053 =item datestamp_index
1054  
1055 Type: string
1056  
1057 Description: Which CQL index should be used to find records within a specified date range. If not specified, the value from the 'datestamp_field' setting is used
1058  
1059 Required: false
1060  
1061 =item repositoryName
1062  
1063 Type: string
1064  
1065 Description: name of the repository
1066  
1067 Required: true
1068  
1069 =item adminEmail
1070  
1071 Type: array reference of non empty strings
1072  
1073 Description: array reference of administrative emails. These will be included in the Identify response.
1074  
1075 Required: false
1076  
1077 =item compression
1078  
1079 Type: array reference of non empty strings
1080  
1081 Description: a list compression encodings supported by the repository. These will be included in the Identify response.
1082  
1083 Required: false
1084  
1085 =item description
1086  
1087 Type: array reference of non empty strings
1088  
1089 Description: a list of XML containers that describe your repository. These will be included in the Identify response. Note that this module will try to validate the XML data.
1090  
1091 Required: false
1092  
1093 =item earliestDatestamp
1094  
1095 Type: string
1096  
1097 Description: The earliest datestamp available in the dataset formatted as YYYY-MM-DDTHH:MM:SSZ. This will be determined dynamically if no static value is given.
1098  
1099 Required: false
1100  
1101 =item deletedRecord
1102  
1103 Type: enumeration, having possible values C<no> (default), C<persistent> or C<transient>
1104  
1105 Description: The policy for deleted records. See also: L<https://www.openarchives.org/OAI/openarchivesprotocol.html#DeletedRecords>
1106  
1107 Required: 1
1108  
1109 =item repositoryIdentifier
1110  
1111 Type: string
1112  
1113 Description: A prefix to use in OAI-PMH identifiers
1114  
1115 Required: true
1116  
1117 =item cql_filter
1118  
1119 A global CQL query that is applied to all search requests (GetRecord, ListRecords and ListIdentifiers).
1120 Use this to determine what records from your underlying catmandu search store should be available to
1121 to the OAI.
1122  
1123 =item default_search_params
1124  
1125 Extra search parameters added during search in your catmandu bag:
1126  
1127 $bag->search(
1128 %{$self->default_search_params},
1129 cql_query => $cql,
1130 sru_sortkeys => $request->sortKeys,
1131 limit => $limit,
1132 start => $first - 1,
1133 );
1134  
1135 Must be a hash reference
1136  
1137 Note that search parameter C<cql_query>, C<sru_sortkeys>, C<limit> and C<start> are overwritten
1138  
1139 =item search_strategy
1140  
1141 Type: enumeration having possible value C<paginate> (default), C<es.scroll> or C<filter>
1142  
1143 Description: strategy to implement paging of oai record results (in ListRecords or ListIdentifiers).
1144 The default search strategy C<paginate> uses C<start> and C<limit> in the underlying search request,
1145 but that may lead to deep paging problems. ElasticSearch for example will refuse to return results after
1146 hit 10.000.
1147  
1148 * C<paginate>: use catmandu store search parameters C<start> and C<limit> in order to advance to the next page.
1149 May result into a deep paging problem, but serves as a convenient default for small repositories.
1150  
1151 * C<es.scroll>: use ElasticSearch scroll api (https://www.elastic.co/guide/en/elasticsearch/reference/current/scroll-api.html#scroll-api-request) to split into pages.
1152 As expected only useful for elasticsearch. Elasticsearch stores a snapshot of the resultset
1153 for a certain amount of time, returns the identifier (scroll_id) for that snapshot, and we use that
1154 scroll_id as the cursor to the next page. Requesting the first page however several times may cause a server downtime
1155 when the resources are exhausted to store an additional snapshot. This option is ONLY included as a deprecated option,
1156 never to be used anymore.
1157  
1158 * C<filter>: use prefix filtering to split into pages. Works by sorting by on the primary identifier,
1159 and using a prefix query like C<"_id > this_page.last_id"> to fetch the next page.
1160 This is the RECOMMENDED approach
1161 cf. https://solr.apache.org/guide/6_6/pagination-of-results.html#using-cursors
1162  
1163 Required: 1
1164  
1165 =item limit
1166  
1167 The number of records to be returned in each OAI list record page
1168  
1169 When not provided in the constructor, it is derived from the default limit of your catmandu bag (see L<Catmandu::Searchable#default_limit>)
1170  
1171 =item delimiter
1172  
1173 Type: string
1174  
1175 Description: delimiter used in prefixing a record identifier with a repositoryIdentifier.
1176  
1177 Default: C<:>
1178  
1179 Required: false
1180  
1181 =item sampleIdentifier
1182  
1183 Type: non empty string
1184  
1185 Description: sample identifier.
1186  
1187 Required: true
1188  
1189 =item xsl_stylesheet
1190  
1191 Type: non empty string
1192  
1193 Description: url to XSLT stylesheet. When given a link to this stylesheet in every request of type ListRecords or ListIdentifiers.
1194 Only useful in browser context where the browser use the stylesheet for dynamic conversion.
1195  
1196 Required: false
1197  
1198 =item metadata_formats
1199  
1200 Type: array reference of hash reference
1201  
1202 Description: An array of metadataFormats that are supported. Every object needs the following attributes:
1203  
1204 * metadataPrefix: a short string for the name of the format
1205 * schema: an URL to the XSD schema of this format
1206 * metadataNamespace: an XML namespace for this format
1207 * template: path to a Template Toolkit file to transform your records into this format
1208 * fix: optionally an array of one or more L<Catmandu::Fix>-es or Fix files
1209  
1210 Required: true
1211  
1212 =item sets
1213  
1214 Type: array reference of hash references
1215  
1216 Description: an array of OAI-PMH sets and the CQL query to retrieve records in this set from the Catmandu::Store. Each object must have the following attributes:
1217  
1218 * setSpec: a short string for the same of the set
1219 * setName: a longer description of the set
1220 * setDescription: an optional and repeatable container that may hold community-specific XML-encoded data about the set. Should be string or array of strings.
1221 * cql: the CQL command to find records in this set in the L<Catmandu::Store>
1222  
1223 Required: false
1224  
1225 =item granularity
1226  
1227 Type: non empty string
1228  
1229 Description: datestamp granularity. Default: YYYY-MM-DDThh:mm:ssZ. This is validated against the returned record timestamps
1230  
1231 Required: false
1232  
1233 =item collectionIcon
1234  
1235 Type: hash reference
1236  
1237 Description: object containing attributes for collectionIcon as used in the Identify response:
1238  
1239 * url (required)
1240 * link
1241 * title
1242 * width
1243 * height
1244  
1245 Required: false
1246  
1247 =item get_record_cql_pattern
1248  
1249 Type: non empty string
1250  
1251 Description: CQL query template to use when fetching a single record. Defaults to C<_id exact "%s">.
1252 Note that the record identifier key as defined by the catmandu bag is taken into account (which is _id by default)
1253  
1254 Required: true
1255  
1256 =item datestamp_pattern
1257  
1258 Type: non empty string
1259  
1260 Description: datestamp pattern for OAI parameters C<from> and C<until>. Example: C<%Y-%m-%dT%H:%M:%SZ>
1261  
1262 Required: true
1263  
1264 =item template_options
1265  
1266 An optional hash of configuration options that will be passed to L<Catmandu::Exporter::Template> or L<Template>
1267  
1268 =back
1269  
1270 As this is meant as a drop in replacement for L<Dancer::Plugin::Catmandu::OAI> all arguments should be the same.
1271  
1272 So all arguments can be taken from your previous dancer plugin configuration, if necessary:
1273  
1274 use Dancer;
1275 use Catmandu;
1276 use Plack::Builder;
1277 use Plack::App::Catmandu::OAI;
1278  
1279 my $dancer_app = sub {
1280 Dancer->dance(Dancer::Request->new(env => $_[0]));
1281 };
1282  
1283 builder {
1284 enable 'ReverseProxy';
1285 enable '+Dancer::Middleware::Rebase', base => Catmandu->config->{uri_base}, strip => 1;
1286
1287 mount "/oai" => Plack::App::Catmandu::OAI->new(
1288 %{config->{plugins}->{'Catmandu::OAI'}}
1289 )->to_app;
1290  
1291 mount "/" => builder {
1292 # only create session cookies for dancer application
1293 enable "Session";
1294 mount '/' => $dancer_app;
1295 };
1296 };
1297  
1298 =head1 METHODS
1299  
1300 =over 4
1301  
1302 =item to_app
1303  
1304 returns Plack application that can be mounted. Path rebasements are taken into account
1305  
1306 =back
1307  
1308 =head1 AUTHOR
1309  
1310 =over 4
1311
1312 =item Nicolas Franck, C<< <nicolas.franck at ugent.be> >>
1313  
1314 =back
1315
1316 =head1 IMPORTANT
1317  
1318 This module is still a work in progress, and needs further testing before using it in a production system
1319  
1320 =head1 LICENSE AND COPYRIGHT
1321
1322 This program is free software; you can redistribute it and/or modify it
1323 under the terms of either: the GNU General Public License as published
1324 by the Free Software Foundation; or the Artistic License.
1325  
1326 See http://dev.perl.org/licenses/ for more information.
1327  
1328 =cut
↑ Summary