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

filter: Statementstmt 63.7% 79 / 124 Branchbran 26.9% 7 / 26 Conditioncond 25.0% 7 / 28 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 50.4% 105 / 208 CC 28 SCAR 39.6
CC
28
Cov
52%
__ANON__
83.4
sru_provider
58.5
__ANON__
2.0
CRAP 52.7
↑ 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.0402';
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=40.7
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=14 SCAR=44.2
80  
81 4 my $params = params('query');
82 4 ▶ my $operation = $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
83  
84 4 ▶ if ($operation eq 'explain') {
Branch: 2 branches
BranchTrueFalseTotal
if ($operation eq 'explain') { } 3 1 4
elsif ($operation eq 'searchRetrieve') { } 1 0 1
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 elsif ($operation eq '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 ▶ unless ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 0 0 0
119 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(66));
120 0 return $response->asXML;
121 }
122 0 my $identifier = $schema->{identifier};
123 0 my $fix = $schema->{fix};
124 0 my $template = $schema->{template};
125 0 my $layout = $schema->{layout};
126 0 my $cql = $params->{query};
127 0 ▶ if ($setting->{cql_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setting->{"cql_filter"}) 0 0 0
128 # space before the filter is to circumvent a bug in the Solr
129 # 3.6 edismax parser
130 0 $cql = "( $setting->{cql_filter}) and ( $cql)";
131 }
132  
133 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
134 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
135 0 ▶ if ($limit > $maximum_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > $maximum_limit) 0 0 0
136 0 $limit = $maximum_limit;
137 }
138  
139 my $hits = eval {
140 $bag->search(
141 0 ▶ %{ $setting->{default_search_params} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$setting->{"default_search_params"} or {} 0 0 0
142 cql_query => $cql,
143 sru_sortkeys => $request->sortKeys,
144 limit => $limit,
145 start => $first - 1,
146 );
147 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
148 0 my $e = $@;
149 0 ▶ if ($e =~ /^cql error/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /^cql error/) 0 0 0
150 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(10));
151 0 return $response->asXML;
152 }
153 0 die $e;
154 };
155  
156 $hits->each(sub {
157 0 ▶ my $data = $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 my $metadata = "";
159 0 my $exporter = Catmandu::Exporter::Template->new(
160 template => $template,
161 file => \$metadata,
162 fix => $fix,
163 );
164 0 $exporter->add($data);
165 0 $exporter->commit;
166 0 $response->addRecord(SRU::Response::Record->new(
167 recordSchema => $identifier,
168 recordData => $metadata,
169 ));
170 0 });
171 0 $response->numberOfRecords($hits->total);
172 0 return $response->asXML;
173 }
174 else {
175 0 my $request = SRU::Request::Explain->new(%$params);
176 0 my $response = SRU::Response->newFromRequest($request);
177 0 $response->addDiagnostic(SRU::Response::Diagnostic->newFromCode(6));
178 0 return $response->asXML;
179 }
180 1 };
181 }
182  
183 2 register sru_provider => \&sru_provider;
184  
185 2 register_plugin;
186  
187 2 1;
188  
189 =head1 SYNOPSIS
190  
191 use Dancer;
192 use Dancer::Plugin::Catmandu::SRU;
193  
194 sru_provider '/sru';
195  
196 =head1 CONFIGURATION
197  
198 plugins:
199 'Catmandu::SRU':
200 store: search
201 bag: publicationItem
202 cql_filter: 'submissionstatus exact public'
203 default_record_schema: mods
204 limit: 200
205 maximum_limit: 500
206 record_schemas:
207 -
208 identifier: "info:srw/schema/1/mods-v3.3"
209 name: mods
210 fix:
211 - publication_to_mods()
212 template: views/mods.tt
213  
214 =head1 AUTHOR
215  
216 Nicolas Steenlant, C<< <nicolas.steenlant at ugent.be> >>
217  
218 =head1 CONTRIBUTOR
219  
220 Vitali Peil, C<< <vitali.peil at uni-bielefeld.de> >>
221  
222 =head1 SEE ALSO
223  
224 L<SRU>, L<Catmandu>, L<Catmandu::Store>, L<Dancer::Plugin::Catmandu::OAI>
225  
226 =head1 LICENSE
227  
228 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
229  
230 =cut
↑ Summary Build »