blib/lib/Dancer/Plugin/Catmandu/SRU.pm

filter: Statementstmt 64.2% 81 / 126 Branchbran 26.9% 7 / 26 Conditioncond 26.4% 9 / 34 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 49.7% 109 / 219 CC 31 SCAR 40.6
CC
31
Cov
52%
__ANON__
85.4
sru_provider
71.7
__ANON__
6.0
CRAP 58.0
↑ Summary
1 package Dancer::Plugin::Catmandu::SRU;
2  
3 =head1 NAME
4  
5 Dancer::Plugin::Catmandu::SRU - SRU server backed by a searchable Catmandu::Store
6  
7 =cut
8  
9 2 our $VERSION = '0.0504';
10  
11 2 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Catmandu;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Catmandu::Fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Catmandu::Exporter::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Catmandu::Util qw(hash_merge);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use SRU::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use SRU::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Dancer qw(:syntax);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶sub sru_provider {
Subroutine sru_provider: called CC=17 SCAR=42.7
22 2 my ($path, %opts) = @_;
23  
24 2 my $setting = hash_merge(plugin_setting, \%opts);
25  
26 2 ▶ my $content_type = $setting->{content_type} // 'text/xml';
Condition50%1/2
$setting->{"content_type"} // "text/xml"
l!l
02
MC/DC0%0/1
$setting->{"content_type"} // "text/xml"
$setting->{"content_type"}
Truth table50%1/2
A
A= $setting->{"content_type"}
Aresult
11
00
27  
28 2 my $default_record_schema = $setting->{default_record_schema};
29  
30 2 my $record_schemas = $setting->{record_schemas};
31  
32 2 my $record_schema_map = {};
33 2 for my $schema (@$record_schemas) {
34 2 $schema = {%$schema};
35 2 my $identifier = $schema->{identifier};
36 2 my $name = $schema->{name};
37 2 ▶ if (my $fix = $schema->{fix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fix = $schema->{"fix"}) 2 0 2
38 2 $schema->{fix} = Catmandu::Fix->new(fixes => $fix);
39 }
40 2 $record_schema_map->{$identifier} = $schema;
41 2 $record_schema_map->{$name} = $schema;
42 }
43  
44 2 my $bag = Catmandu->store($setting->{store})->bag($setting->{bag});
45  
46 2 ▶ my $default_limit = $setting->{limit} // $bag->default_limit;
Condition33%1/3
$setting->{"limit"} // $bag->default_limit
l!l&&r!l&&!r
200
MC/DC0%0/2
$setting->{"limit"} // $bag->default_limit
$setting->{"limit"} $bag->default_limit
Truth table33%1/3
A // B
A= $setting->{"limit"}B= $bag->default_limit
ABresult
1X1
011
000
47 2 ▶ my $maximum_limit = $setting->{maximum_limit} // $bag->maximum_limit;
Condition33%1/3
$setting->{"maximum_limit"} // $bag->maximum_limit
l!l&&r!l&&!r
200
MC/DC0%0/2
$setting->{"maximum_limit"} // $bag->maximum_limit
$setting->{"maximum_limit"} $bag->maximum_limit
Truth table33%1/3
A // B
A= $setting->{"maximum_limit"}B= $bag->maximum_limit
ABresult
1X1
011
000
48  
49 2 ▶ my $template_options = $setting->{template_options} || {};
Condition50%1/2
$setting->{'template_options'} || {}
l!l
02
MC/DC0%0/1
$setting->{'template_options'} || {}
$setting->{'template_options'}
Truth table50%1/2
A
A= $setting->{'template_options'}
Aresult
11
00
50  
51 2 my $database_info = "";
52 2 ▶ if ($setting->{title} || $setting->{description}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setting->{"title"} or $setting->{"description"}) 0 2 2
Condition33%1/3
$setting->{"title"} or $setting->{"description"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$setting->{"title"} or $setting->{"description"}
$setting->{"title"} $setting->{"description"}
Truth table33%1/3
A or B
A= $setting->{"title"}B= $setting->{"description"}
ABresult
1X1
011
000
53 0 $database_info .= qq(<databaseInfo>\n);
54 0 for my $key (qw(title description)) {
55 0 ▶ $database_info .= qq(<$key lang="en" primary="true">$setting->{$key}</$key>\n) if $setting->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $setting->{$key} 0 0 0
56 }
57 0 $database_info .= qq(</databaseInfo>);
58 }
59  
60 2 my $index_info = "";
61 2 ▶ if ($bag->can('cql_mapping') and my $indexes = $bag->cql_mapping->{indexes}) { # TODO all Searchable should have cql_mapping
Branch: 1 branch
BranchTrueFalseTotal
if ($bag->can("cql_mapping") and my $indexes = $bag->cql_mapping->{"indexes"}) 0 2 2
Condition33%1/3
$bag->can("cql_mapping") and my $indexes = $bag->cql_mapping->{"indexes"}
!ll&&!rl&&r
200
MC/DC0%0/2
$bag->can("cql_mapping") and my $indexes = $bag->cql_mapping->{"indexes"}
$bag->can("cql_mapping") my $indexes = $bag->cql_mapping->{"indexes"}
Truth table33%1/3
A and B
A= $bag->can("cql_mapping")B= my $indexes = $bag->cql_mapping->{"indexes"}
ABresult
0X0
100
111
62 0 $index_info .= qq(<indexInfo>\n);
63 0 for my $key (keys %$indexes) {
64 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
65 0 $index_info .= qq(<index><title>$title</title><map><name>$key</name></map></index>\n);
66 }
67 0 $index_info .= qq(</indexInfo>);
68 }
69  
70 2 my $schema_info = qq(<schemaInfo>\n);
71 2 for my $schema (@$record_schemas) {
72 2 ▶ my $title = $schema->{title} || $schema->{name};
Condition33%1/3
$schema->{'title'} || $schema->{'name'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$schema->{'title'} || $schema->{'name'}
$schema->{'title'} $schema->{'name'}
Truth table33%1/3
A || B
A= $schema->{'title'}B= $schema->{'name'}
ABresult
1X1
011
000
73 2 $schema_info .= qq(<schema name="$schema->{name}" identifier="$schema->{identifier}"><title>$title</title></schema>\n);
74 }
75 2 $schema_info .= qq(</schemaInfo>);
76  
77 2 my $config_info = qq(<configInfo>\n);
78 2 $config_info .= qq(<default type="numberOfRecords">$default_limit</default>\n);
79 2 $config_info .= qq(<setting type="maximumRecords">$maximum_limit</setting>\n);
80 2 $config_info .= qq(</configInfo>);
81  
82 9 ▶ get $path => sub {
Subroutine __ANON__: called CC=14 SCAR=44.5
83 9 content_type $content_type;
84  
85 9 my $params = params('query');
86 9 ▶ my $operation = $params->{operation} // 'explain';
Condition100%2/2
$params->{"operation"} // "explain"
l!l
54
MC/DC100%1/1
$params->{"operation"} // "explain"
$params->{"operation"}
Truth table100%2/2
A
A= $params->{"operation"}
Aresult
11
00
87  
88 9 ▶ if ($operation eq 'explain') {
Branch: 2 branches
BranchTrueFalseTotal
if ($operation eq 'explain') { } 7 2 9
elsif ($operation eq 'searchRetrieve') { } 2 0 2
89 7 my $request = SRU::Request::Explain->new(%$params);
90 7 my $response = SRU::Response->newFromRequest($request);
91  
92 7 my $transport = request->scheme;
93 7 my $database = substr request->path, 1;
94 7 my $uri = request->uri_for(request->path_info);
95 7 my $host = $uri->host;
96 7 my $port = $uri->port;
97 7 $response->record(SRU::Response::Record->new(
98 recordSchema => 'http://explain.z3950.org/dtd/2.0/',
99 recordData => <<XML,
100 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
101 <serverInfo protocol="SRU" transport="$transport">
102 <host>$host</host>
103 <port>$port</port>
104 <database>$database</database>
105 </serverInfo>
106 $database_info
107 $index_info
108 $schema_info
109 $config_info
110 </explain>
111 XML
112 ));
113 7 return $response->asXML;
114 }
115 elsif ($operation eq 'searchRetrieve') {
116 2 my $request = SRU::Request::SearchRetrieve->new(%$params);
117 2 my $response = SRU::Response->newFromRequest($request);
118 2 ▶ if (@{$response->diagnostics}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$response->diagnostics;}) 2 0 2
119 2 return $response->asXML;
120 }
121  
122 0 ▶ my $schema = $record_schema_map->{$request->recordSchema || $default_record_schema};
Condition0%0/3
$request->recordSchema || $default_record_schema
l!l&&r!l&&!r
000
MC/DC0%0/2
$request->recordSchema || $default_record_schema
$request->recordSchema $default_record_schema
Truth table0%0/3
A || B
A= $request->recordSchemaB= $default_record_schema
ABresult
1X1
011
000
123 0 ▶ unless ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 0 0 0
124 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(66));
125 0 return $response->asXML;
126 }
127 0 my $identifier = $schema->{identifier};
128 0 my $fix = $schema->{fix};
129 0 my $template = $schema->{template};
130 0 my $layout = $schema->{layout};
131 0 my $cql = $params->{query};
132 0 ▶ if ($setting->{cql_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setting->{"cql_filter"}) 0 0 0
133 # space before the filter is to circumvent a bug in the Solr
134 # 3.6 edismax parser
135 0 $cql = "( $setting->{cql_filter}) and ( $cql)";
136 }
137  
138 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
139 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
140 0 ▶ if ($limit > $maximum_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > $maximum_limit) 0 0 0
141 0 $limit = $maximum_limit;
142 }
143  
144 my $hits = eval {
145 $bag->search(
146 0 ▶ %{ $setting->{default_search_params} || {} },
Condition0%0/2
$setting->{'default_search_params'} || {}
l!l
00
MC/DC0%0/1
$setting->{'default_search_params'} || {}
$setting->{'default_search_params'}
Truth table0%0/2
A
A= $setting->{'default_search_params'}
Aresult
11
00
147 cql_query => $cql,
148 sru_sortkeys => $request->sortKeys,
149 limit => $limit,
150 start => $first - 1,
151 );
152 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $hits = eval { do { $bag->search(%{$setting->{"default_search_params"} or {};}, "cql_query", $cql, "sru_sortkeys", $request->sortKeys, "limit", $limit, "start", $first - 1) } } or do { my $e = $@; if ($e =~ /^cql error/) { $response->addDiagnostic("SRU::Response::Diagnostic"->newFromCode(10)); return $response->asXML; } ; die $e } 0 0 0
153 0 my $e = $@;
154 0 ▶ if ($e =~ /^cql error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /^cql error/) 0 0 0
155 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(10));
156 0 return $response->asXML;
157 }
158 0 die $e;
159 };
160  
161 0 ▶ $hits->each(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
162 0 my $data = $_[0];
163 0 my $metadata = "";
164 0 my $exporter = Catmandu::Exporter::Template->new(
165 %$template_options,
166 template => $template,
167 file => \$metadata
168 );
169 0 ▶ $exporter->add($fix ? $fix->fix($data) : $data);
Branch: 1 branch
BranchTrueFalseTotal
$fix ? : 0 0 0
170 0 $exporter->commit;
171 0 $response->addRecord(SRU::Response::Record->new(
172 recordSchema => $identifier,
173 recordData => $metadata,
174 ));
175 0 });
176 0 $response->numberOfRecords($hits->total);
177 0 return $response->asXML;
178 }
179 else {
180 0 my $request = SRU::Request::Explain->new(%$params);
181 0 my $response = SRU::Response->newFromRequest($request);
182 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(6));
183 0 return $response->asXML;
184 }
185 2 };
186 }
187  
188 2 register sru_provider => \&sru_provider;
189  
190 2 register_plugin;
191  
192 2 1;
193  
194 =head1 SYNOPSIS
195  
196 #!/usr/bin/env perl
197
198 use Dancer;
199 use Catmandu;
200 use Dancer::Plugin::Catmandu::SRU;
201
202 Catmandu->load;
203 Catmandu->config;
204
205 my $options = {};
206  
207 sru_provider '/sru', %$options;
208
209 dance;
210  
211 =head1 DESCRIPTION
212  
213 L<Dancer::Plugin::Catmandu::SRU> is a Dancer plugin to provide SRU services for L<Catmandu::Store>-s that support
214 CQL (such as L<Catmandu::Store::ElasticSearch>). Follow the installation steps below to setup your own SRU server.
215  
216 =head1 REQUIREMENTS
217  
218 In the examples below an ElasticSearch 1.7.2 L<https://www.elastic.co/downloads/past-releases/elasticsearch-1-7-2> server
219 will be used:
220  
221 $ cpanm Dancer Catmandu::SRU Catmandu::Store::ElasticSearch
222  
223 $ wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.zip
224 $ unzip elasticsearch-1.7.2.zip
225 $ cd elasticsearch-1.7.2
226 $ bin/elasticsearch
227  
228 =head1 RECORDS
229  
230 Records stored in the Catmandu::Store can be in any format. Preferably the format should be easy to convert into an
231 XML format. At a minimum each record contains an identifier '_id'. In the examples below we'll configure the SRU
232 to serve Dublin Core records:
233  
234 $ cat sample.yml
235 ---
236 _id: 1
237 creator:
238 - Musterman, Max
239 - Jansen, Jan
240 - Svenson, Sven
241 title:
242 - Test record
243 ...
244  
245 =head1 CATMANDU CONFIGURATION
246  
247 ElasticSearch requires a configuration file to map record fields to CQL terms. Below is a minimal configuration
248 required to query for '_id' and 'title' and 'creator' in the ElasticSearch collection:
249  
250 $ cat catmandu.yml
251 ---
252 store:
253 sru:
254 package: ElasticSearch
255 options:
256 index_name: sru
257 bags:
258 data:
259 cql_mapping:
260 default_index: basic
261 indexes:
262 _id:
263 op:
264 'any': true
265 'all': true
266 '=': true
267 'exact': true
268 field: '_id'
269 creator:
270 op:
271 'any': true
272 'all': true
273 '=': true
274 'exact': true
275 field: 'creator'
276 title:
277 op:
278 'any': true
279 'all': true
280 '=': true
281 'exact': true
282 field: 'title'
283  
284 =head1 IMPORT RECORDS
285  
286 With the Catmandu configuration files in place records can be imported with the L<catmandu> command:
287  
288 # Drop the existing ElasticSearch 'sru' collection
289 $ catmandu drop sru
290  
291 # Import the sample record
292 $ catmandu import YAML to sru < sample.yml
293  
294 # Test if the records are available in the 'sru' collection
295 $ catmandu export sru
296  
297 =head1 DANCER CONFIGURATION
298  
299 The Dancer configuration file 'config.yml' contains basic information for the Catmandu::SRU plugin to work:
300  
301 * store - In which Catmandu::Store are the metadata records stored
302 * bag - In which Catmandu::Bag are the records of this 'store' (use: 'data' as default)
303 * cql_filter - A CQL query to find all records in the database that should be made available to SRU
304 * default_record_schema - The metadataSchema to present records in
305 * limit - The maximum number of records to be returned in each SRU request
306 * maximum_limit - The maximum number of search results to return
307 * record_schemas - An array of all supported record schemas
308 * identifier - The SRU identifier for the schema (see L<http://www.loc.gov/standards/sru/recordSchemas/>)
309 * name - A short descriptive name for the schema
310 * fix - Optionally an array of fixes to apply to the records before they are transformed into XML
311 * template - The path to a Template Toolkit file to transform your records into this format
312 * template_options - An optional hash of configuration options that will be passed to L<Catmandu::Exporter::Template> or L<Template>
313 * content_type - Set a custom content type header, the default is 'text/xml'.
314  
315 Below is a sample minimal configuration for the 'sample.yml' demo above:
316  
317 charset: "UTF-8"
318 plugins:
319 'Catmandu::SRU':
320 store: sru
321 bag: data
322 default_record_schema: dc
323 limit: 200
324 maximum_limit: 500
325 record_schemas:
326 -
327 identifier: "info:srw/schema/1/dc-v1.1"
328 name: dc
329 template: dc.tt
330  
331 =head1 METADATA FORMAT TEMPLATE
332  
333 For each metadata format a Template Toolkit file needs to exist which translate L<Catmandu::Store> records
334 into XML records. The example below contains an example file to transform 'sample.yml' type records into
335 SRU DC:
336  
337 $ cat dc.tt
338 <srw_dc:dc xmlns:srw_dc="info:srw/schema/1/dc-schema"
339 xmlns:dc="http://purl.org/dc/elements/1.1/"
340 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
341 xsi:schemaLocation="info:srw/schema/1/dc-schema http://www.loc.gov/standards/sru/recordSchemas/dc-schema.xsd">
342 [%- FOREACH var IN ['title' 'creator' 'subject' 'description' 'publisher' 'contributor' 'date' 'type' 'format' 'identifier' 'source' 'language' 'relation' 'coverage' 'rights'] %]
343 [%- FOREACH val IN $var %]
344 <dc:[% var %]>[% val | html %]</dc:[% var %]>
345 [%- END %]
346 [%- END %]
347 </srw_dc:dc>
348  
349 =head1 START DANCER
350  
351 If all the required files are available, then a Dancer application can be started. See the 'demo' directory of
352 this distribution for a complete example:
353  
354 $ ls
355 app.pl catmandu.yml config.yml dc.tt
356 $ cat app.pl
357 #!/usr/bin/env perl
358
359 use Dancer;
360 use Catmandu;
361 use Dancer::Plugin::Catmandu::SRU;
362
363 Catmandu->load;
364 Catmandu->config;
365
366 my $options = {};
367  
368 sru_provider '/sru', %$options;
369
370 dance;
371  
372 # Start Dancer
373 $ perl ./app.pl
374
375 # Test queries:
376 $ curl "http://localhost:3000/sru"
377 $ curl "http://localhost:3000/sru?version=1.1&operation=searchRetrieve&query=(_id+%3d+1)"
378 $ catmandu convert SRU --base 'http://localhost:3000/sru' --query '(_id = 1)'
379  
380  
381 =head1 AUTHOR
382  
383 Nicolas Steenlant, C<< <nicolas.steenlant at ugent.be> >>
384  
385 =head1 CONTRIBUTOR
386  
387 Vitali Peil, C<< <vitali.peil at uni-bielefeld.de> >>
388  
389 Patrick Hochstenbach, C<< <patrick.hochstenbach at ugent.be> >>
390  
391 =head1 SEE ALSO
392  
393 L<SRU>, L<Catmandu>, L<Catmandu::Store::ElasticSearch> , L<Catmandu::SRU>
394  
395 =head1 LICENSE
396  
397 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
398  
399 =cut
↑ Summary