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

filter: Statementstmt 66.6% 80 / 120 Branchbran 25.0% 4 / 16 Conditioncond 25.0% 7 / 28 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 53.0% 103 / 194 CC 23 SCAR 34.8
CC
23
Cov
55%
sru_provider
52.2
__ANON__
34.1
__ANON__
2.0
CRAP 32.4
↑ Summary Build »
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.03';
10  
11 2 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Dancer qw(:syntax);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Catmandu;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Catmandu::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Catmandu::Fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Catmandu::Exporter::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use SRU::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use SRU::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 sub sru_provider {
22 1 ▶ my ($path) = @_;
Subroutine sru_provider: called CC=15 SCAR=39.6
23  
24 1 my $setting = plugin_setting;
25  
26 1 my $default_record_schema = $setting->{default_record_schema};
27  
28 1 my $record_schemas = $setting->{record_schemas};
29  
30 1 my $record_schema_map = {};
31 1 for my $schema (@$record_schemas) {
32 1 $schema = {%$schema};
33 1 my $identifier = $schema->{identifier};
34 1 my $name = $schema->{name};
35 1 ▶ if (my $fix = $schema->{fix}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fix = $schema->{"fix"}) 1 0 1
36 1 $schema->{fix} = Catmandu::Fix->new(fixes => $fix);
37 }
38 1 $record_schema_map->{$identifier} = $schema;
39 1 $record_schema_map->{$name} = $schema;
40 }
41  
42 1 my $bag = Catmandu->store($setting->{store})->bag($setting->{bag});
43  
44 1 ▶ my $default_limit = $setting->{limit} // $bag->default_limit;
Condition33%1/3
$setting->{"limit"} // $bag->default_limit
l!l&&r!l&&!r
100
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
45 1 ▶ my $maximum_limit = $setting->{maximum_limit} // $bag->maximum_limit;
Condition33%1/3
$setting->{"maximum_limit"} // $bag->maximum_limit
l!l&&r!l&&!r
100
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
46  
47 1 my $database_info = "";
48 1 ▶ if ($setting->{title} || $setting->{description}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setting->{"title"} or $setting->{"description"}) 0 1 1
Condition33%1/3
$setting->{"title"} or $setting->{"description"}
l!l&&r!l&&!r
001
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
49 0 $database_info .= qq(<databaseInfo>\n);
50 0 for my $key (qw(title description)) {
51 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
52 }
53 0 $database_info .= qq(</databaseInfo>);
54 }
55  
56 1 my $index_info = "";
57 1 ▶ 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 1 1
Condition33%1/3
$bag->can("cql_mapping") and my $indexes = $bag->cql_mapping->{"indexes"}
!ll&&!rl&&r
100
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
58 0 $index_info .= qq(<indexInfo>\n);
59 0 for my $key (keys %$indexes) {
60 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
61 0 $index_info .= qq(<index><title>$title</title><map><name>$key</name></map></index>\n);
62 }
63 0 $index_info .= qq(</indexInfo>);
64 }
65  
66 1 my $schema_info = qq(<schemaInfo>\n);
67 1 for my $schema (@$record_schemas) {
68 1 ▶ my $title = $schema->{title} || $schema->{name};
Condition33%1/3
$schema->{'title'} || $schema->{'name'}
l!l&&r!l&&!r
010
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
69 1 $schema_info .= qq(<schema name="$schema->{name}" identifier="$schema->{identifier}"><title>$title</title></schema>\n);
70 }
71 1 $schema_info .= qq(</schemaInfo>);
72  
73 1 my $config_info = qq(<configInfo>\n);
74 1 $config_info .= qq(<default type="numberOfRecords">$default_limit</default>\n);
75 1 $config_info .= qq(<setting type="maximumRecords">$maximum_limit</setting>\n);
76 1 $config_info .= qq(</configInfo>);
77  
78 get $path => sub {
79 4 ▶ content_type 'xml';
Subroutine __ANON__: called CC=9 SCAR=35.3
80  
81 4 my $params = params('query');
82  
83 4 ▶ given ($params->{operation} // 'explain') {
Condition100%2/2
$params->{"operation"} // "explain"
l!l
22
MC/DC100%1/1
$params->{"operation"} // "explain"
$params->{"operation"}
Truth table100%2/2
A
A= $params->{"operation"}
Aresult
11
00
84 4 when ('explain') {
85 3 my $request = SRU::Request::Explain->new(%$params);
86 3 my $response = SRU::Response->newFromRequest($request);
87  
88 3 my $transport = request->scheme;
89 3 my $database = substr request->path, 1;
90 3 my $host = request->host; $host =~ s/:.+//;
91 3 my $port = request->port;
92 3 $response->record(SRU::Response::Record->new(
93 recordSchema => 'http://explain.z3950.org/dtd/2.1/',
94 recordData => <<XML,
95 <explain xmlns="http://explain.z3950.org/dtd/2.1/">
96 <serverInfo protocol="SRU" method="GET" transport="$transport">
97 <host>$host</host>
98 <port>$port</port>
99 <database>$database</database>
100 </serverInfo>
101 $database_info
102 $index_info
103 $schema_info
104 $config_info
105 </explain>
106 XML
107 ));
108 3 return $response->asXML;
109 }
110 1 when ('searchRetrieve') {
111 1 my $request = SRU::Request::SearchRetrieve->new(%$params);
112 1 my $response = SRU::Response->newFromRequest($request);
113 1 ▶ if (@{$response->diagnostics}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$response->diagnostics;}) 1 0 1
114 1 return $response->asXML;
115 }
116  
117 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
118 0 my $identifier = $schema->{identifier};
119 0 my $fix = $schema->{fix};
120 0 my $template = $schema->{template};
121 0 my $layout = $schema->{layout};
122 0 my $cql = $params->{query};
123 0 ▶ if ($setting->{cql_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setting->{"cql_filter"}) 0 0 0
124 0 $cql = "($setting->{cql_filter}) and ($cql)";
125 }
126  
127 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
128 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
129 my $hits = eval {
130 0 $bag->search(
131 cql_query => $cql,
132 sru_sortkeys => $request->sortKeys,
133 limit => $limit,
134 start => $first - 1,
135 );
136 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $hits = eval { do { $bag->search("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
137 0 my $e = $@;
138 0 ▶ if ($e =~ /^cql error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /^cql error/) 0 0 0
139 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(10));
140 0 return $response->asXML;
141 }
142 0 die $e;
143 };
144  
145 $hits->each(sub {
146 0 ▶ my $data = $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 my $metadata = "";
148 0 my $exporter = Catmandu::Exporter::Template->new(
149 template => $template,
150 file => \$metadata,
151 fix => $fix,
152 );
153 0 $exporter->add($data);
154 0 $exporter->commit;
155 0 $response->addRecord(SRU::Response::Record->new(
156 recordSchema => $identifier,
157 recordData => $metadata,
158 ));
159 0 });
160 0 $response->numberOfRecords($hits->total);
161 0 return $response->asXML;
162 }
163 0 default {
164 0 my $request = SRU::Request::Explain->new(%$params);
165 0 my $response = SRU::Response->newFromRequest($request);
166 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(6));
167 0 return $response->asXML;
168 }
169 }
170 1 };
171 }
172  
173 2 register sru_provider => \&sru_provider;
174  
175 2 register_plugin;
176  
177 2 1;
178  
179 =head1 SYNOPSIS
180  
181 use Dancer;
182 use Dancer::Plugin::Catmandu::SRU;
183  
184 sru_provider '/sru';
185  
186 =head1 CONFIGURATION
187  
188 plugins:
189 'Catmandu::SRU':
190 store: search
191 bag: publicationItem
192 cql_filter: 'submissionstatus exact public'
193 default_record_schema: mods
194 limit: 200
195 maximum_limit: 500
196 record_schemas:
197 -
198 identifier: "info:srw/schema/1/mods-v3.3"
199 name: mods
200 fix:
201 - publication_to_mods()
202 template: views/mods.tt
203  
204 =head1 AUTHOR
205  
206 Nicolas Steenlant, C<< <nicolas.steenlant at ugent.be> >>
207  
208 =head1 CONTRIBUTOR
209  
210 Vitali Peil, C<< <vitali.peil at uni-bielefeld.de> >>
211  
212 =head1 SEE ALSO
213  
214 L<SRU>, L<Catmandu>, L<Catmandu::Store>, L<Dancer::Plugin::Catmandu::OAI>
215  
216 =head1 LICENSE
217  
218 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
219  
220 =cut
↑ Summary Build »