]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Z3950.pm
Sort Z39.50 sources as returned by fetch_service_defs() for a better UX
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Z3950.pm
1 package OpenILS::Application::Search::Z3950;
2 use strict; use warnings;
3 use base qw/OpenILS::Application/;
4
5 use OpenILS::Utils::ZClient;
6 use MARC::Record;
7 use MARC::File::XML;
8 use Unicode::Normalize;
9 use XML::LibXML;
10 use Data::Dumper;
11
12 use OpenILS::Event;
13 use OpenSRF::EX qw(:try);
14 use OpenILS::Utils::ModsParser;
15 use OpenSRF::Utils::SettingsClient;
16 use OpenILS::Application::AppUtils;
17 use OpenSRF::Utils::Logger qw/$logger/;
18 use OpenILS::Utils::CStoreEditor q/:funcs/;
19
20 my $output      = "usmarc"; 
21 my $U = 'OpenILS::Application::AppUtils'; 
22
23 my $sclient;
24 my %services;
25 my $default_service;
26
27
28 __PACKAGE__->register_method(
29         method          => 'do_class_search',
30         api_name                => 'open-ils.search.z3950.search_class',
31         stream          => 1,
32         signature       => q/
33                 Performs a class based Z search.  The classes available
34                 are defined by the 'attr' fields in the config for the
35                 requested service.
36                 @param auth The login session key
37                 @param shash The search hash : { attr : value, attr2: value, ...}
38                 @param service The service to connect to
39                 @param username The username to use when connecting to the service
40                 @param password The password to use when connecting to the service
41         /
42 );
43
44 __PACKAGE__->register_method(
45         method          => 'do_service_search',
46         api_name                => 'open-ils.search.z3950.search_service',
47         signature       => q/
48                 @param auth The login session key
49                 @param query The Z3950 search string to use
50                 @param service The service to connect to
51                 @param username The username to use when connecting to the service
52                 @param password The password to use when connecting to the service
53         /
54 );
55
56
57 __PACKAGE__->register_method(
58         method          => 'do_service_search',
59         api_name                => 'open-ils.search.z3950.search_raw',
60         signature       => q/
61                 @param auth The login session key
62                 @param args An object of search params which must include:
63                         host, port, db and query.  
64                         optional fields include username and password
65         /
66 );
67
68
69 __PACKAGE__->register_method(
70         method  => "query_services",
71         api_name        => "open-ils.search.z3950.retrieve_services",
72         signature       => q/
73                 Returns a list of service names that we have config
74                 data for
75         /
76 );
77
78
79
80 # -------------------------------------------------------------------
81 # What services do we have config info for?
82 # -------------------------------------------------------------------
83 sub query_services {
84         my( $self, $client, $auth ) = @_;
85         my $e = new_editor(authtoken=>$auth);
86         return $e->event unless $e->checkauth;
87         return $e->event unless $e->allowed('REMOTE_Z3950_QUERY');
88
89         return fetch_service_defs();
90 }
91
92 # -------------------------------------------------------------------
93 # What services do we have config info for?
94 # -------------------------------------------------------------------
95 sub fetch_service_defs {
96
97     my $hash = $sclient->config_value('z3950', 'services');
98
99     # overlay config file values with in-db values
100     my $e = new_editor();
101     if($e->can('search_config_z3950_source')) {
102
103         my $sources = $e->search_config_z3950_source(
104             [ { name => { '!=' => undef } },
105               { flesh => 1, flesh_fields => { czs => ['attrs'] } } ]
106         );
107
108         for my $s ( @$sources ) {
109             $$hash{ $s->name } = {
110                 name => $s->name,
111                 label => $s->label,
112                 host => $s->host,
113                 port => $s->port,
114                 db => $s->db,
115                 record_format => $s->record_format,
116                 transmission_format => $s->transmission_format,
117                 auth => $s->auth,
118             };
119
120             for my $a ( @{ $s->attrs } ) {
121                 $$hash{ $a->source }{attrs}{ $a->name } = {
122                     name => $a->name,
123                     label => $a->label,
124                     code => $a->code,
125                     format => $a->format,
126                     source => $a->source,
127                     truncation => $a->truncation,
128                 };
129             }
130         }
131     }
132
133     # Define the set of native catalog services
134     # XXX There are i18n problems here, but let's get the staff client working first
135     # XXX Move into the DB?
136     $hash->{'native-evergreen-catalog'} = {
137         attrs => {
138             title => {code => 'title', label => 'Title'},
139             author => {code => 'author', label => 'Author'},
140             subject => {code => 'subject', label => 'Subject'},
141             keyword => {code => 'keyword', label => 'Keyword'},
142             tcn => {code => 'tcn', label => 'TCN'},
143             isbn => {code => 'isbn', label => 'ISBN'},
144             issn => {code => 'issn', label => 'ISSN'},
145             publisher => {code => 'publisher', label => 'Publisher'},
146             pubdate => {code => 'pubdate', label => 'Pub Date'},
147             item_type => {code => 'item_type', label => 'Item Type'},
148         }
149     };
150
151     %services = %$hash; # cache these internally so we can actually use the db-configured sources
152
153     # Return a sorted list for easier handling in the client
154     my @sorted_sources;
155     foreach my $zsource (sort keys %$hash) {
156         if ($hash->{$zsource}->{name}) {
157             push @sorted_sources, $hash->{$zsource};
158         }
159     }
160     return \@sorted_sources;
161 }
162
163
164
165 # -------------------------------------------------------------------
166 # Load the pre-defined Z server configs
167 # -------------------------------------------------------------------
168 sub child_init {
169     $sclient = OpenSRF::Utils::SettingsClient->new();
170     $default_service = $sclient->config_value("z3950", "default" );
171 }
172
173
174 # -------------------------------------------------------------------
175 # High-level class based search. 
176 # -------------------------------------------------------------------
177 sub do_class_search {
178
179         fetch_service_defs() unless (scalar(keys(%services)));
180
181         my $self                        = shift;
182         my $conn                        = shift;
183         my $auth                        = shift;
184         my $args                        = shift;
185
186         if (!ref($$args{service})) {
187                 $$args{service} = [$$args{service}];
188                 $$args{username} = [$$args{username}];
189                 $$args{password} = [$$args{password}];
190         }
191
192         $$args{async} = 1;
193
194         my @connections;
195         my @results;
196     my @services; 
197         for (my $i = 0; $i < @{$$args{service}}; $i++) {
198                 my %tmp_args = %$args;
199                 $tmp_args{service} = $$args{service}[$i];
200                 $tmp_args{username} = $$args{username}[$i];
201                 $tmp_args{password} = $$args{password}[$i];
202
203                 $logger->debug("z3950: service: $tmp_args{service}, async: $tmp_args{async}");
204
205         if ($tmp_args{service} eq 'native-evergreen-catalog') { 
206             my $method = $self->method_lookup('open-ils.search.biblio.zstyle.staff'); 
207             $conn->respond( 
208                 $self->method_lookup('open-ils.search.biblio.zstyle.staff')->run($auth, \%tmp_args) 
209             ); 
210
211         } else { 
212
213             $tmp_args{query} = compile_query('and', $tmp_args{service}, $tmp_args{search}); 
214     
215             my $res = do_service_search( $self, $conn, $auth, \%tmp_args ); 
216     
217             if ($U->event_code($res)) { 
218                 $conn->respond($res) if $U->event_code($res); 
219
220             } else { 
221                 push @services, $tmp_args{service}; 
222                 push @results, $res->{result}; 
223                 push @connections, $res->{connection}; 
224             } 
225         }
226
227                 $logger->debug("z3950: Result object: $results[$i], Connection object: $connections[$i]");
228         }
229
230         $logger->debug("z3950: Connections created");
231
232     return undef unless (@connections);
233         my @records;
234
235     # local catalog search is not processed with other z39 results;
236     $$args{service} = [grep {$_ ne 'native-evergreen-catalog'} @{$$args{service}}];
237
238     @connections = grep {defined $_} @connections;
239     return undef unless @connections;
240
241         while ((my $index = OpenILS::Utils::ZClient::event( \@connections )) != 0) {
242                 my $ev = $connections[$index - 1]->last_event();
243                 $logger->debug("z3950: Received event $ev");
244                 if ($ev == OpenILS::Utils::ZClient::EVENT_END()) {
245                         my $munged = process_results( $results[$index - 1], $$args{limit}, $$args{offset}, $$args{service}[$index -1] );
246                         $$munged{service} = $$args{service}[$index - 1];
247                         $conn->respond($munged);
248                 }
249         }
250
251         $logger->debug("z3950: Search Complete");
252     return undef;
253 }
254
255
256 # -------------------------------------------------------------------
257 # This handles the host settings, but expects a fully formed z query
258 # -------------------------------------------------------------------
259 sub do_service_search {
260
261         fetch_service_defs() unless (scalar(keys(%services)));
262
263         my $self                        = shift;
264         my $conn                        = shift;
265         my $auth                        = shift;
266         my $args                        = shift;
267         
268         my $info = $services{$$args{service}};
269
270         $$args{host}    = $$info{host};
271         $$args{port}    = $$info{port};
272         $$args{db}              = $$info{db};
273     $logger->debug("z3950: do_search...");
274
275         return do_search( $self, $conn, $auth, $args );
276 }
277
278
279
280 # -------------------------------------------------------------------
281 # This is the low level search method.  All config and query
282 # data must be provided to this method
283 # -------------------------------------------------------------------
284 sub do_search {
285
286         fetch_service_defs() unless (scalar(keys(%services)));
287
288         my $self        = shift;
289         my $conn        = shift;
290         my $auth = shift;
291         my $args = shift;
292
293         my $host                = $$args{host} or return undef;
294         my $port                = $$args{port} or return undef;
295         my $db          = $$args{db}    or return undef;
296         my $query       = $$args{query} or return undef;
297         my $async       = $$args{async} || 0;
298
299         my $limit       = $$args{limit} || 10;
300         my $offset      = $$args{offset} || 0;
301
302         my $username = $$args{username} || "";
303         my $password = $$args{password} || "";
304
305     my $tformat = $services{$args->{service}}->{transmission_format} || $output;
306
307         my $editor = new_editor(authtoken => $auth);
308         return $editor->event unless $editor->checkauth;
309         return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
310
311     $logger->info("z3950: connecting to server $host:$port:$db as $username");
312
313         my $connection = OpenILS::Utils::ZClient->new(
314                 $host, $port,
315                 databaseName                            => $db, 
316                 user                                                    => $username,
317                 password                                                => $password,
318                 async                                                   => $async,
319                 preferredRecordSyntax   => $tformat, 
320         );
321
322         if( ! $connection ) {
323                 $logger->error("z3950: Unable to connect to Z server: ".
324                         "$host:$port:$db:$username:$password");
325                 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
326         }
327
328         my $start = time;
329         my $results;
330         my $err;
331
332         $logger->info("z3950: query => $query");
333
334         try {
335                 $results = $connection->search_pqf( $query );
336         } catch Error with { $err = shift; };
337
338         return OpenILS::Event->new(
339                 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
340
341         return OpenILS::Event->new('Z3950_SEARCH_FAILED', 
342                 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
343
344         $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
345
346         return {result => $results, connection => $connection} if ($async);
347
348         my $munged = process_results($results, $limit, $offset, $$args{service});
349         $munged->{query} = $query;
350
351         return $munged;
352 }
353
354
355 # -------------------------------------------------------------------
356 # Takes a result batch and returns the hitcount and a list of xml
357 # and mvr objects
358 # -------------------------------------------------------------------
359 sub process_results {
360
361         fetch_service_defs() unless (scalar(keys(%services)));
362
363         my $results     = shift;
364         my $limit       = shift || 10;
365         my $offset      = shift || 0;
366     my $service = shift;
367
368     my $rformat = $services{$service}->{record_format};
369     my $tformat = $services{$service}->{transmission_format} || $output;
370
371     $results->option(elementSetName => $rformat);
372     $results->option(preferredRecordSyntax => $tformat);
373     $logger->info("z3950: using record format '$rformat' and transmission format '$tformat'");
374
375         my @records;
376         my $res = {};
377         my $count = $$res{count} = $results->size;
378
379         $logger->info("z3950: search returned $count hits");
380
381         my $tend = $limit + $offset;
382
383         my $end = ($tend <= $count) ? $tend : $count;
384
385         for($offset..$end - 1) {
386
387                 my $err;
388                 my $mods;
389                 my $marc;
390                 my $marcs;
391                 my $marcxml;
392
393                 $logger->info("z3950: fetching record $_");
394
395                 try {
396
397                         my $rec = $results->record($_);
398
399             if ($tformat eq 'usmarc') {
400                         $marc           = MARC::Record->new_from_usmarc($rec->raw());
401             } elsif ($tformat eq 'xml') {
402                         $marc           = MARC::Record->new_from_xml($rec->raw());
403             } else {
404                 die "Unsupported record transmission format $tformat"
405             }
406
407                         $marcs  = $U->entityize($marc->as_xml_record);
408                         $marcs  = $U->strip_ctrl_chars($marcs);
409                         my $doc = XML::LibXML->new->parse_string($marcs);
410                         $marcxml = $U->entityize($doc->documentElement->toString);
411                         $marcxml = $U->strip_ctrl_chars($marcxml);
412         
413                         my $u = OpenILS::Utils::ModsParser->new();
414                         $u->start_mods_batch( $marcxml );
415                         $mods = $u->finish_mods_batch();
416         
417
418                 } catch Error with { $err = shift; };
419
420                 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
421                 $logger->error("z3950: bad XML : $err") if $err;
422
423                 if( $err ) {
424                         warn "\n\n$marcs\n\n";
425                 }
426         }
427         
428         $res->{records} = \@records;
429         return $res;
430 }
431
432
433
434 # -------------------------------------------------------------------
435 # Compiles the class based search query
436 # -------------------------------------------------------------------
437 sub compile_query {
438
439         fetch_service_defs() unless (scalar(keys(%services)));
440
441         my $seperator   = shift;
442         my $service             = shift;
443         my $hash                        = shift;
444
445         my $count = scalar(keys %$hash);
446
447         my $str = "";
448         $str .= "\@$seperator " for (1..$count-1);
449         
450     # -------------------------------------------------------------------
451     # "code" is the bib-1 "use attribute", "format" is the bib-1 
452     # "structure attribute"
453     # -------------------------------------------------------------------
454         for( keys %$hash ) {
455                 next unless ( exists $services{$service}->{attrs}->{$_} );
456                 $str .= '@attr 1=' . $services{$service}->{attrs}->{$_}->{code} . # add the use attribute
457                         ' @attr 4=' . $services{$service}->{attrs}->{$_}->{format}; # add the structure attribute
458                 if (exists $services{$service}->{attrs}->{$_}->{truncation}){
459                         $str .= ' @attr 5=' . $services{$service}->{attrs}->{$_}->{truncation};
460                 }
461                 $str .= " \"" . $$hash{$_} . "\" "; # add the search term
462         }
463         return $str;
464 }
465
466 1;