Net/Z3950/FOLIO.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« DummyRecord.pm ↑ Summary
1 package Net::Z3950::FOLIO;
2  
3 6 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Cpanel::JSON::XS qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Net::Z3950::SimpleServer;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use ZOOM; # For ZOOM::Exception
10 use LWP::UserAgent;
11 use HTTP::Cookies;
12 use MARC::Record;
13 use MARC::File::XML (BinaryEncoding => 'utf8', RecordFormat => 'USMARC');
14 use Data::Dumper; $Data::Dumper::Indent = 1;
15  
16 use Net::Z3950::FOLIO::Session;
17 use Net::Z3950::FOLIO::OPACXMLRecord qw(makeOPACXMLRecord);
18 use Net::Z3950::FOLIO::TransformXML qw(transformXMLRecord);
19 use Net::Z3950::FOLIO::RPN;
20 use Net::Z3950::FOLIO::SurrogateDiagnostic;
21  
22  
23 our $VERSION = 'v4.4.1';
24  
25  
26 sub FORMAT_USMARC { '1.2.840.10003.5.10' }
27 sub FORMAT_XML { '1.2.840.10003.5.109.10' }
28 sub FORMAT_JSON { '1.2.840.10003.5.1000.81.3' }
29 sub ATTRSET_BIB1 { '1.2.840.10003.3.1' }
30  
31  
32 =head1 NAME
33  
34 Net::Z3950::FOLIO - Z39.50 server for FOLIO bibliographic data
35  
36 =head1 SYNOPSIS
37  
38 use Net::Z3950::FOLIO;
39 $service = new Net::Z3950::FOLIO('config');
40 $service->launch_server("someServer", @ARGV);
41  
42 =head1 DESCRIPTION
43  
44 The C<Net::Z3950::FOLIO> module provides all the application logic of
45 a Z39.50 server that allows searching in and retrieval from the
46 inventory module of FOLIO. It is used by the C<z2folio> program, and
47 there is probably no good reason to make any other program to use it.
48  
49 The library has only two public entry points: the C<new()> constructor
50 and the C<launch_server()> method. The synopsis above shows how they
51 are used: a Net::Z3950::FOLIO object is created using C<new()>, then
52 the C<launch_server()> method is invoked on it to start the server.
53 (In fact, this synopsis is essentially the whole of the code of the
54 C<simple2zoom> program. All the work happens inside the library.)
55  
56 =head1 METHODS
57  
58 =head2 new($configBase)
59  
60 $s2z = new Net::Z3950::FOLIO('config');
61  
62 Creates and returns a new Net::Z3950::FOLIO object, configured according to
63 the JSON file C<$configFile.json> specified by the only argument. The format of
64 this file is described in C<Net::Z3950::FOLIO::Config>.
65  
66 =cut
67  
68 sub new {
69 my $class = shift();
70 my($cfgbase) = @_;
71  
72 my $this = bless {
73 cfgbase => $cfgbase || 'config',
74 sessions => {}, # Maps database name to session object
75 }, $class;
76  
77 $this->{server} = Net::Z3950::SimpleServer->new(
78 GHANDLE => $this,
79 INIT => \&_init_handler_wrapper,
80 SEARCH => \&_search_handler_wrapper,
81 FETCH => \&_fetch_handler_wrapper,
82 DELETE => \&_delete_handler_wrapper,
83 SORT => \&_sort_handler_wrapper,
84 );
85  
86 return $this;
87 }
88  
89  
90 sub getSession {
91 my $this = shift();
92 my($name) = @_;
93  
94 if (!$this->{sessions}->{$name}) {
95 my $session = new Net::Z3950::FOLIO::Session($this, $name);
96 $this->{sessions}->{$name} = $session;
97 $session->reloadConfigFile();
98 $session->login($this->{user}, $this->{pass}) if !$session->{cfg}->{nologin};
99 }
100  
101 return $this->{sessions}->{$name};
102 }
103  
104  
105 sub _init_handler_wrapper { _eval_wrapper(\&_init_handler, @_) }
106 sub _search_handler_wrapper { _eval_wrapper(\&_search_handler, @_) }
107 sub _fetch_handler_wrapper { _eval_wrapper(\&_fetch_handler, @_) }
108 sub _delete_handler_wrapper { _eval_wrapper(\&_delete_handler, @_) }
109 sub _sort_handler_wrapper { _eval_wrapper(\&_sort_handler, @_) }
110  
111  
112 sub _eval_wrapper {
113 my $coderef = shift();
114 my $args = shift();
115  
116 eval {
117 &$coderef($args, @_);
118 }; if (ref $@ && $@->isa('ZOOM::Exception')) {
119 if ($@->diagset() eq 'Bib-1') {
120 $args->{ERR_CODE} = $@->code();
121 $args->{ERR_STR} = $@->addinfo();
122 } else {
123 $args->{ERR_CODE} = 100;
124 $args->{ERR_STR} = $@->message() || $@->addinfo();
125 }
126  
127 if ($@->isa('Net::Z3950::FOLIO::SurrogateDiagnostic')) {
128 $args->{SUR_FLAG} = 1;
129 $args->{RECORD} = ""; # To avoid an uninitialized value warning in SimpleServer.pm
130 }
131  
132 } elsif ($@) {
133 # Non-ZOOM exceptions may be generated by the Perl
134 # interpreter, for example if we try to call a method that
135 # does not exist in the relevant class. These should be
136 # considered fatal and not reported to the client.
137 die $@;
138 }
139 }
140  
141  
142 sub _init_handler {
143 my($args) = @_;
144 my $ghandle = $args->{GHANDLE};
145  
146 $args->{IMP_ID} = '81';
147 $args->{IMP_VER} = $Net::Z3950::FOLIO::VERSION;
148 $args->{IMP_NAME} = 'z2folio gateway';
149  
150 # Stash these for subsequent use in getSession when it invokes $session->login()
151 $ghandle->{user} = $args->{USER};
152 $ghandle->{pass} = $args->{PASS};
153  
154 # That's all we can do until we know the database name at search time
155 }
156  
157  
158 sub _search_handler {
159 my($args) = @_;
160 my $ghandle = $args->{GHANDLE};
161  
162 my $bases = $args->{DATABASES};
163 _throw(111, 1) if @$bases != 1; # Too many databases specified
164 my $base = $bases->[0];
165  
166 my $session = $ghandle->getSession($base);
167 $args->{HANDLE} = $session;
168 $session->maybeRefreshToken();
169  
170 if ($args->{CQL}) {
171 $session->{cql} = $args->{CQL};
172 } else {
173 my $type1 = $args->{RPN}->{query};
174 $session->{cql} = $type1->toCQL($session, $args->{RPN}->{attributeSet});
175 warn "search: translated '" . $args->{QUERY} . "' to '" . $session->{cql} . "'\n";
176 }
177  
178 $session->{sortspec} = undef;
179 $args->{HITS} = $session->rerunSearch($args->{SETNAME});
180 }
181  
182  
183 sub _fetch_handler {
184 my($args) = @_;
185 my $session = $args->{HANDLE};
186 _throw(30, $args->{SETNAME}) if !$session;
187  
188 $session->maybeRefreshToken();
189  
190 my $rs = $session->{resultsets}->{$args->{SETNAME}};
191 _throw(30, $args->{SETNAME}) if !$rs; # Result set does not exist
192  
193 my $index1 = $args->{OFFSET};
194 _throw(13, $index1) if $index1 < 1 || $index1 > $rs->totalCount();
195  
196 my $rec = $rs->record($index1-1);
197 if (!defined $rec) {
198 # We need to fetch a chunk of records that contains the
199 # requested one. We'll do this by splitting the whole set into
200 # chunks of the specified size, and fetching the one that
201 # contains the requested record.
202 # XXX Perhaps this should happen inside $rs->record
203 my $index0 = $index1 - 1;
204 my $chunkSize = $session->{cfg}->{chunkSize} || 10;
205 my $chunk = int($index0 / $chunkSize);
206 $session->doSearch($rs, $chunk * $chunkSize, $chunkSize);
207 $rec = $rs->record($index1-1);
208 _throw(1, "missing record") if !defined $rec;
209 }
210  
211 my $comp = lc($args->{COMP} || '');
212 my $format = $args->{REQ_FORM};
213 # warn "REQ_FORM=$format, COMP=$comp\n";
214  
215 my $res;
216 if ($format eq FORMAT_JSON) {
217 $res = $rec->prettyJSON();
218  
219 } elsif ($format eq FORMAT_XML && $comp eq 'raw') {
220 # Mechanical XML translitation of the JSON response
221 $res = $rec->prettyXML();
222  
223 } elsif ($format eq FORMAT_XML && $comp eq 'usmarc') {
224 # MARCXML made from SRS Marc record
225 my $marc = $rec->marcRecord();
226 $res = $marc->as_xml_record();
227 } elsif ($format eq FORMAT_XML && $comp eq 'opac') {
228 # OPAC-format XML
229 $res = makeOPACXMLRecord($rec);
230 } elsif ($format eq FORMAT_XML) {
231 $res = transformXMLRecord($session->{cfg}, $rec, $comp);
232 if (!$res) {
233 my @eslist = qw(raw usmarc opac);
234 my $es = $session->{cfg}->{xmlElementSets};
235 push @eslist, keys(%$es) if $es;
236 _throw(25, 'XML records available in element-sets: ' . join(', ', @eslist));
237 }
238  
239 } elsif ($format eq FORMAT_USMARC && (!$comp || $comp eq 'f' || $comp eq 'b')) {
240 # Static USMARC from SRS
241 my $marc = $rec->marcRecord();
242 $res = $marc->as_usmarc();
243 } elsif ($format eq FORMAT_USMARC) {
244 _throw(25, "USMARC records available in element-sets: f, b");
245  
246 } else {
247 _throw(239, $format); # 239 = Record syntax not supported
248 }
249  
250 $args->{RECORD} = $res;
251 return;
252 }
253  
254  
255 sub _delete_handler {
256 my($args) = @_;
257 my $session = $args->{HANDLE};
258 $session->maybeRefreshToken();
259  
260 my $setname = $args->{SETNAME};
261 if ($session->{resultsets}->{$setname}) {
262 $session->{resultsets}->{$setname} = undef;
263 } else {
264 $args->{STATUS} = 1; # failure-1: Result set did not exist
265 }
266  
267 return;
268 }
269  
270  
271 sub _sort_handler {
272 my($args) = @_;
273 my $session = $args->{HANDLE};
274 $session->maybeRefreshToken();
275  
276 my $setnames = $args->{INPUT};
277 _throw(230, '1') if @$setnames > 1; # Sort: too many input results
278 my $setname = $setnames->[0];
279 my $rs = $session->{resultsets}->{$setname};
280 _throw(30, $args->{SETNAME}) if !$rs; # Result set does not exist
281  
282 my $cqlSort = $session->sortSpecs2CQL($args->{SEQUENCE});
283 _throw(207, Dumper($args->{SEQUENCE})) if !$cqlSort; # Cannot sort according to sequence
284  
285 $session->{sortspec} = $cqlSort;
286 $session->rerunSearch($args->{OUTPUT});
287 }
288  
289  
290 =head2 launch_server($label, @ARGV)
291  
292 $s2z->launch_server("someServer", @ARGV);
293  
294 Launches the Net::Z3950::FOLIO server: this method never returns. The
295 C<$label> string is used in logging, and the C<@ARGV> vector of
296 command-line arguments is interpreted by the YAZ backend server as
297 described at
298 https://software.indexdata.com/yaz/doc/server.invocation.html
299  
300 =cut
301  
302 sub launch_server {
303 my $this = shift();
304 my($label, @argv) = @_;
305  
306 return $this->{server}->launch_server($label, @argv);
307 }
308  
309  
310 sub _throw {
311 my($code, $addinfo, $diagset, $isSurrogate) = @_;
312 $diagset ||= "Bib-1";
313  
314 # HTTP body for errors is sometimes a plain string, sometimes a JSON structure
315 if ($addinfo =~ /^{/) {
316 my $obj = decode_json($addinfo);
317 $addinfo = $obj->{errors} ? $obj->{errors}->[0]->{message} : $obj->{errorMessage};
318 }
319  
320 if ($isSurrogate) {
321 die new Net::Z3950::FOLIO::SurrogateDiagnostic($code, undef, $addinfo, $diagset);
322 } else {
323 die new ZOOM::Exception($code, undef, $addinfo, $diagset);
324 }
325 }
326  
327  
328 =head1 SEE ALSO
329  
330 =over 4
331  
332 =item The C<z2folio> script conveniently launches the server.
333  
334 =item C<Net::Z3950::FOLIO::Config> describes the configuration-file format.
335  
336 =item The C<Net::Z3950::SimpleServer> handles the Z39.50 service.
337  
338 =back
339  
340 =head1 AUTHOR
341  
342 Mike Taylor, E<lt>mike@indexdata.comE<gt>
343  
344 =head1 COPYRIGHT AND LICENSE
345  
346 Copyright (C) 2018 The Open Library Foundation
347  
348 This software is distributed under the terms of the Apache License,
349 Version 2.0. See the file "LICENSE" for more information.
350  
351 =cut
352  
« DummyRecord.pm ↑ Summary