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

filter: Statementstmt 36.3% 60 / 165 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 55.5% 15 / 27 Podpod 33.3% 1 / 3 totaltotal 29.9% 76 / 254 CC 26 SCAR 45.4
CC
26
Cov
28%
__ANON__
210.0
to_app
156.0
__ANON__
6.0
CRAP 93.4
↑ Summary
1 package Plack::App::Catmandu::SRU;
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;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Catmandu::Fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Catmandu::Exporter::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use SRU::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use SRU::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Types::Standard qw(Str ArrayRef HashRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Types::Common::String qw(NonEmptyStr);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Types::Common::Numeric qw(PositiveInt);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use feature qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶no warnings qw(experimental::signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 has store_name => (
22 is => 'ro',
23 isa => Str,
24 init_arg => 'store',
25 );
26  
27 1 has bag_name => (
28 is => 'ro',
29 isa => Str,
30 init_arg => 'bag',
31 );
32  
33 has content_type => (
34 is => 'ro',
35 isa => Str,
36 0 ▶ default => sub { 'text/xml'; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 1 );
38  
39 1 has cql_filter => (
40 is => 'ro',
41 isa => NonEmptyStr,
42 );
43  
44 1 has default_record_schema => (
45 is => 'ro',
46 isa => NonEmptyStr,
47 required => 1,
48 );
49  
50 has record_schemas => (
51 is => 'ro',
52 isa => ArrayRef[HashRef],
53 0 ▶ default => sub { []; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 1 );
55  
56 1 has title => (
57 is => 'ro',
58 isa => NonEmptyStr,
59 );
60  
61 1 has description => (
62 is => 'ro',
63 isa => NonEmptyStr,
64 );
65  
66 has template_options => (
67 is => 'ro',
68 isa => HashRef,
69 0 ▶ default => sub { +{}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 1 );
71  
72 has default_search_params => (
73 is => 'ro',
74 isa => HashRef,
75 0 ▶ default => sub { {}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 1 );
77  
78 1 has limit => (
79 is => 'lazy',
80 isa => PositiveInt,
81 );
82  
83 1 has maximum_limit => (
84 is => 'lazy',
85 isa => PositiveInt,
86 );
87  
88 1 has bag => (
89 is => 'lazy',
90 isa => ConsumerOf['Catmandu::CQLSearchable'],
91 init_arg => undef,
92 );
93  
94 0 ▶sub _build_bag ($self) {
Subroutine _build_bag: not called CC=1 SCAR=6.9
95 0 Catmandu->store($self->store_name)->bag($self->bag_name);
96 }
97  
98 0 ▶sub _build_limit ($self) {
Subroutine _build_limit: not called CC=1 SCAR=6.9
99 0 $self->bag->default_limit;
100 }
101  
102 0 ▶sub _build_maximum_limit ($self) {
Subroutine _build_maximum_limit: not called CC=1 SCAR=6.9
103 0 $self->bag->maximum_limit;
104 }
105  
106 0 ▶sub to_app ($self) {
Subroutine to_app: not called CC=12 SCAR=50.5
Pod documented
107 0 my $default_limit = $self->limit;
108 0 my $maximum_limit = $self->maximum_limit;
109 0 my $template_options= $self->template_options;
110 0 my $bag = $self->bag;
111  
112 0 my $record_schema_map = {};
113 0 for my $schema (@{$self->record_schemas}) {
114 0 $schema = {%$schema};
115 0 my $identifier = $schema->{identifier};
116 0 my $name = $schema->{name};
117 0 ▶ if (my $fix = $schema->{fix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fix = $schema->{"fix"}) 0 0 0
118 0 $schema->{fix} = Catmandu::Fix->new(fixes => $fix);
119 }
120 0 $record_schema_map->{$identifier} = $schema;
121 0 $record_schema_map->{$name} = $schema;
122 }
123  
124 0 my $database_info = "";
125 0 ▶ if ($self->title || $self->description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->title or $self->description) 0 0 0
Condition0%0/3
$self->title or $self->description
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->title or $self->description
$self->title $self->description
Truth table0%0/3
A or B
A= $self->titleB= $self->description
ABresult
1X1
011
000
126 0 $database_info .= qq(<databaseInfo>\n);
127 0 for my $key (qw(title description)) {
128 0 ▶ $database_info .= qq(<$key lang="en" primary="true">).$self->$key.qq(</$key>\n) if $self->$key;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$key 0 0 0
129 }
130 0 $database_info .= qq(</databaseInfo>);
131 }
132  
133 0 my $index_info = "";
134 0 ▶ if (my $indexes = $bag->cql_mapping->{indexes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $indexes = $bag->cql_mapping->{"indexes"}) 0 0 0
135 0 $index_info .= qq(<indexInfo>\n);
136 0 for my $key (keys %$indexes) {
137 0 ▶ my $title = $indexes->{$key}{title} || $key;
Condition0%0/3
$indexes->{$key}{'title'} || $key
l!l&&r!l&&!r
000
MC/DC0%0/2
$indexes->{$key}{'title'} || $key
$indexes->{$key}{'title'} $key
Truth table0%0/3
A || B
A= $indexes->{$key}{'title'}B= $key
ABresult
1X1
011
000
138 0 $index_info .= qq(<index><title>$title</title><map><name>$key</name></map></index>\n);
139 }
140 0 $index_info .= qq(</indexInfo>);
141 }
142  
143 0 my $schema_info = qq(<schemaInfo>\n);
144 0 for my $schema (@{$self->record_schemas}) {
145 0 ▶ my $title = $schema->{title} || $schema->{name};
Condition0%0/3
$schema->{'title'} || $schema->{'name'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$schema->{'title'} || $schema->{'name'}
$schema->{'title'} $schema->{'name'}
Truth table0%0/3
A || B
A= $schema->{'title'}B= $schema->{'name'}
ABresult
1X1
011
000
146 0 $schema_info .= qq(<schema name="$schema->{name}" identifier="$schema->{identifier}"><title>$title</title></schema>\n);
147 }
148 0 $schema_info .= qq(</schemaInfo>);
149  
150 0 my $config_info = qq(<configInfo>\n);
151 0 $config_info .= qq(<default type="numberOfRecords">$default_limit</default>\n);
152 0 $config_info .= qq(<setting type="maximumRecords">$maximum_limit</setting>\n);
153 0 $config_info .= qq(</configInfo>);
154  
155 0 ▶ sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
156 0 my $env = $_[0];
157  
158 0 my $req = Plack::Request->new($env);
159  
160 0 ▶ return not_found() if $req->method() ne "GET";
Branch: 1 branch
BranchTrueFalseTotal
if $req->method ne "GET" 0 0 0
161  
162 0 my $params = $req->query_parameters();
163 0 ▶ my $operation = $params->get('operation') // 'explain';
Condition0%0/2
$params->get("operation") // "explain"
l!l
00
MC/DC0%0/1
$params->get("operation") // "explain"
$params->get("operation")
Truth table0%0/2
A
A= $params->get("operation")
Aresult
11
00
164  
165 0 ▶ if ($operation eq 'explain') {
Branch: 2 branches
BranchTrueFalseTotal
if ($operation eq 'explain') { } 0 0 0
elsif ($operation eq 'searchRetrieve') { } 0 0 0
166 0 my $request = SRU::Request::Explain->new($params->flatten);
167 0 my $response = SRU::Response->newFromRequest($request);
168 0 my $transport = $req->scheme;
169 0 my $uri = URI->new($req->base().$req->request_uri);
170 0 my $host = $uri->host;
171 0 my $port = $uri->port;
172 0 my $database = (split(/\//o, $uri->path))[-1];
173 0 $response->record(SRU::Response::Record->new(
174 recordSchema => 'http://explain.z3950.org/dtd/2.0/',
175 recordData => <<XML,
176 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
177 <serverInfo protocol="SRU" transport="$transport">
178 <host>$host</host>
179 <port>$port</port>
180 <database>$database</database>
181 </serverInfo>
182 $database_info
183 $index_info
184 $schema_info
185 $config_info
186 </explain>
187 XML
188 ));
189 0 return $self->render_sru_response($response);
190 }
191 elsif ($operation eq 'searchRetrieve') {
192 0 my $request = SRU::Request::SearchRetrieve->new($params->flatten);
193 0 my $response = SRU::Response->newFromRequest($request);
194 0 ▶ if (@{$response->diagnostics}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$response->diagnostics;}) 0 0 0
195 0 return $self->render_sru_response($response);
196 }
197  
198 0 ▶ my $schema = $record_schema_map->{$request->recordSchema || $self->default_record_schema};
Condition0%0/3
$request->recordSchema || $self->default_record_schema
l!l&&r!l&&!r
000
MC/DC0%0/2
$request->recordSchema || $self->default_record_schema
$request->recordSchema $self->default_record_schema
Truth table0%0/3
A || B
A= $request->recordSchemaB= $self->default_record_schema
ABresult
1X1
011
000
199 0 ▶ unless ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 0 0 0
200 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(66));
201 0 return $self->render_sru_response($response);
202 }
203 0 my $identifier = $schema->{identifier};
204 0 my $fix = $schema->{fix};
205 0 my $template = $schema->{template};
206 0 my $layout = $schema->{layout};
207 0 my $cql = $params->get('query');
208 0 ▶ if ($self->cql_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cql_filter) 0 0 0
209 # space before the filter is to circumvent a bug in the Solr
210 # 3.6 edismax parser
211 0 $cql = "( ".$self->cql_filter.") and ( $cql)";
212 }
213  
214 0 ▶ my $first = $request->startRecord // 1;
Condition0%0/2
$request->startRecord // 1
l!l
00
MC/DC0%0/1
$request->startRecord // 1
$request->startRecord
Truth table0%0/2
A
A= $request->startRecord
Aresult
11
00
215 0 ▶ my $limit = $request->maximumRecords // $default_limit;
Condition0%0/3
$request->maximumRecords // $default_limit
l!l&&r!l&&!r
000
MC/DC0%0/2
$request->maximumRecords // $default_limit
$request->maximumRecords $default_limit
Truth table0%0/3
A // B
A= $request->maximumRecordsB= $default_limit
ABresult
1X1
011
000
216 0 ▶ if ($limit > $maximum_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > $maximum_limit) 0 0 0
217 0 $limit = $maximum_limit;
218 }
219  
220 my $hits = eval {
221 $bag->search(
222 0 %{$self->default_search_params},
223 cql_query => $cql,
224 sru_sortkeys => $request->sortKeys,
225 limit => $limit,
226 start => $first - 1,
227 );
228 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $hits = eval { do { $bag->search(%{$self->default_search_params;}, "cql_query", $cql, "sru_sortkeys", $request->sortKeys, "limit", $limit, "start", $first - 1) } } or do { my $e = $@; if (index($e, "cql error") == 0) { $response->addDiagnostic("SRU::Response::Diagnostic"->newFromCode(10)); return $self->render_sru_response($response); } ; "Catmandu::Error"->throw($e) } 0 0 0
229 0 my $e = $@;
230 0 ▶ if (index($e, 'cql error') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($e, "cql error") == 0) 0 0 0
231 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(10));
232 0 return $self->render_sru_response($response);
233 }
234 0 Catmandu::Error->throw($e);
235 };
236  
237 0 ▶ $hits->each(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
238 0 my $data = $_[0];
239 0 my $metadata = "";
240 0 my $exporter = Catmandu::Exporter::Template->new(
241 %$template_options,
242 template => $template,
243 file => \$metadata
244 );
245 0 ▶ $exporter->add($fix ? $fix->fix($data) : $data);
Branch: 1 branch
BranchTrueFalseTotal
$fix ? : 0 0 0
246 0 $exporter->commit;
247 0 $response->addRecord(SRU::Response::Record->new(
248 recordSchema => $identifier,
249 recordData => $metadata,
250 ));
251 0 });
252 0 $response->numberOfRecords($hits->total);
253 0 return $self->render_sru_response($response);
254 }
255 else {
256 0 my $request = SRU::Request::Explain->new($params->flatten);
257 0 my $response = SRU::Response->newFromRequest($request);
258 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(6));
259 0 return $self->render_sru_response($response);
260 }
261 0 };
262 }
263  
264 0 ▶sub render_sru_response ($self, $response) {
Subroutine render_sru_response: not called CC=1 SCAR=6.9
Pod undocumented
265 0 my $body = $response->asXML;
266 0 utf8::encode($body);
267 0 [200, ['Content-Type' => $self->content_type], [$body]];
268 }
269  
270 0 ▶sub not_found ($self) {
Subroutine not_found: not called CC=1 SCAR=6.9
Pod undocumented
271 0 [404, ['Content-Type' => 'text/plain'], ['not found']];
272 }
273  
274 1 1;
275  
276 =head1 NAME
277  
278 Plack::App::Catmandu::SRU - drop in replacement for Dancer::Plugin::Catmandu::SRU
279  
280 =head1 SYNOPSIS
281  
282 use Plack::Builder;
283 Plack::App::Catmandu::SRU;
284  
285 builder {
286 enable 'ReverseProxy';
287 enable '+Dancer::Middleware::Rebase', base => Catmandu->config->{uri_base}, strip => 1;
288 mount "/sru" => Plack::App::Catmandu::SRU->new(
289 store => 'search',
290 bag => 'publication',
291 cql_filter => 'type = dataset',
292 limit => 100,
293 maximum_limit => 500,
294 record_schemas => [
295 {
296 identifier => "info:srw/schema/1/mods-v3.6",
297 title => "MODS",
298 name => "mods_36",
299 template => "views/export/mods_36.tt",
300 fix => 'fixes/pub.fix'
301 },
302 ],
303 )->to_app;
304 };
305  
306 =head1 CONSTRUCTOR ARGUMENTS
307  
308 =over
309  
310 =item store
311  
312 Name of Catmandu store in your catmandu store configuration
313  
314 Default: C<default>
315  
316 =item bag
317  
318 Name of Catmandu bag in your catmandu store configuration
319  
320 Default: C<data>
321  
322 This must be a bag that implements L<Catmandu::CQLSearchable>, and that configures a C<cql_mapping>
323  
324 =item cql_filter
325  
326 A CQL query to find all records in the database that should be made available to SRU
327  
328 =item default_record_schema
329  
330 default metadata schema all records are shown in, when SRU parameter C<recordSchema> is not gi en . Should be one listed in C<record_schemas>
331  
332 =item limit
333  
334 The default number of records to be returned in each SRU request, when SRU parameter C<maximumRecords> is not given during a searchRetrieve request.
335  
336 When not provided in the constructor, it is derived from the default limit of your catmandu bag (see L<Catmandu::Searchable#default_limit>)
337  
338 =item maximum_limit
339  
340 The maximum value allowed for request parameter C<maximumRecords>.
341  
342 When not provided in the constructor, it is derived from the maximum limit of your catmandu bag (see L<Catmandu::Searchable#maximum_limit>)
343  
344 =item record_schemas
345  
346 An array of all supported record schemas. Each item in the array is an object with attributes:
347  
348 * identifier - The SRU identifier for the schema (see L<http://www.loc.gov/standards/sru/recordSchemas/>)
349  
350 * name - A short descriptive name for the schema
351  
352 * fix - Optionally an array of fixes to apply to the records before they are transformed into XML
353  
354 * template - The path to a Template Toolkit file to transform your records into this format
355  
356 =item template_options
357  
358 An optional hash of configuration options that will be passed to L<Catmandu::Exporter::Template> or L<Template>
359  
360 =item content_type
361  
362 Set a custom content type header, the default is C<text/xml>.
363  
364 =item title
365  
366 Title shown in databaseInfo
367  
368 =item description
369  
370 Description shown in databaseInfo
371  
372 =item default_search_params
373  
374 Extra search parameters added during search in your catmandu bag:
375  
376 $bag->search(
377 %{$self->default_search_params},
378 cql_query => $cql,
379 sru_sortkeys => $request->sortKeys,
380 limit => $limit,
381 start => $first - 1,
382 );
383  
384 Must be a hash reference
385  
386 Note that search parameter C<cql_query>, C<sru_sortkeys>, C<limit> and C<start> are overwritten
387  
388 =back
389  
390 As this is meant as a drop in replacement for L<Dancer::Plugin::Catmandu::SRU> all arguments should be the same.
391  
392 So all arguments can be taken from your previous dancer plugin configuration, if necessary:
393  
394 use Dancer;
395 use Catmandu;
396 use Plack::Builder;
397 use Plack::App::Catmandu::SRU;
398  
399 my $dancer_app = sub {
400 Dancer->dance(Dancer::Request->new(env => $_[0]));
401 };
402  
403 builder {
404 enable 'ReverseProxy';
405 enable '+Dancer::Middleware::Rebase', base => Catmandu->config->{uri_base}, strip => 1;
406
407 mount "/sru" => Plack::App::Catmandu::SRU->new(
408 %{config->{plugins}->{'Catmandu::SRU'}}
409 )->to_app;
410  
411 mount "/" => builder {
412 # only create session cookies for dancer application
413 enable "Session";
414 mount '/' => $dancer_app;
415 };
416 };
417  
418 =head1 METHODS
419  
420 =over 4
421  
422 =item to_app
423  
424 returns Plack application that can be mounted. Path rebasements are taken into account
425  
426 =back
427  
428 =head1 AUTHOR
429  
430 =over 4
431
432 =item Nicolas Franck, C<< <nicolas.franck at ugent.be> >>
433  
434 =back
435
436 =head1 IMPORTANT
437  
438 This module is still a work in progress, and needs further testing before using it in a production system
439  
440 =head1 LICENSE AND COPYRIGHT
441
442 This program is free software; you can redistribute it and/or modify it
443 under the terms of either: the GNU General Public License as published
444 by the Free Software Foundation; or the Artistic License.
445  
446 See http://dev.perl.org/licenses/ for more information.
447  
448 =cut
↑ Summary