]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Z3950.pm
expose the new in-db z39.50 config
[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     if($e->can('search_config_z3950_source')) {
90
91         my $sources = $e->search_config_z3950_source( 
92             [ { name => { '!=' => undef } }, 
93             { flesh => 1, flesh_fields => { czs => ['attrs'] } }]  
94         ); 
95
96         my %hash = (); 
97         for my $s ( @$sources ) { 
98             $hash{ $s->name } = { 
99                 name => $s->name, 
100                 label => $s->label, 
101                 host => $s->host, 
102                 port => $s->port, 
103                 db => $s->db, 
104                 record_format => $s->record_format, 
105                 transmission_format => $s->transmission_format, 
106                 auth => $s->auth, 
107             }; 
108
109             for my $a ( @{ $s->attrs } ) { 
110                 $hash{ $a->source }{attrs}{ $a->name } = { 
111                     name => $a->name, 
112                     label => $a->label, 
113                     code => $a->code, 
114                     format => $a->format, 
115                     source => $a->source, 
116                     truncation => $a->truncation, 
117                 }; 
118             } 
119         } 
120
121         return \%hash; 
122
123     } else {
124         return $sclient->config_value('z3950', 'services');
125     }
126 }
127
128
129
130 # -------------------------------------------------------------------
131 # Load the pre-defined Z server configs
132 # -------------------------------------------------------------------
133 sub initialize {
134         $sclient = OpenSRF::Utils::SettingsClient->new();
135         $default_service = $sclient->config_value("z3950", "default" );
136         my $servs = $sclient->config_value("z3950", "services" );
137         $services{$_} = $$servs{$_} for keys %$servs;
138 }
139
140
141 # -------------------------------------------------------------------
142 # High-level class based search. 
143 # -------------------------------------------------------------------
144 sub do_class_search {
145
146         my $self                        = shift;
147         my $conn                        = shift;
148         my $auth                        = shift;
149         my $args                        = shift;
150
151         if (!ref($$args{service})) {
152                 $$args{service} = [$$args{service}];
153                 $$args{username} = [$$args{username}];
154                 $$args{password} = [$$args{password}];
155         }
156
157         $$args{async} = 1;
158
159         my @connections;
160         my @results;
161     my @services; 
162         for (my $i = 0; $i < @{$$args{service}}; $i++) {
163                 my %tmp_args = %$args;
164                 $tmp_args{service} = $$args{service}[$i];
165                 $tmp_args{username} = $$args{username}[$i];
166                 $tmp_args{password} = $$args{password}[$i];
167
168                 $logger->debug("z3950: service: $tmp_args{service}, async: $tmp_args{async}");
169
170         if ($tmp_args{service} eq 'native-evergreen-catalog') { 
171             my $method = $self->method_lookup('open-ils.search.biblio.zstyle'); 
172             $conn->respond( 
173                 $self->method_lookup('open-ils.search.biblio.zstyle')->run($auth, \%tmp_args) 
174             ); 
175
176         } else { 
177
178             $tmp_args{query} = compile_query('and', $tmp_args{service}, $tmp_args{search}); 
179     
180             my $res = do_service_search( $self, $conn, $auth, \%tmp_args ); 
181     
182             if ($U->event_code($res)) { 
183                 $conn->respond($res) if $U->event_code($res); 
184
185             } else { 
186                 push @services, $tmp_args{service}; 
187                 push @results, $res->{result}; 
188                 push @connections, $res->{connection}; 
189             } 
190         }
191
192                 $logger->debug("z3950: Result object: $results[$i], Connection object: $connections[$i]");
193         }
194
195         $logger->debug("z3950: Connections created");
196
197     return undef unless (@connections);
198         my @records;
199
200     # local catalog search is not processed with other z39 results;
201     $$args{service} = [grep {$_ ne 'native-evergreen-catalog'} @{$$args{service}}];
202
203         while ((my $index = OpenILS::Utils::ZClient::event( \@connections )) != 0) {
204                 my $ev = $connections[$index - 1]->last_event();
205                 $logger->debug("z3950: Received event $ev");
206                 if ($ev == OpenILS::Utils::ZClient::EVENT_END()) {
207                         my $munged = process_results( $results[$index - 1], $$args{limit}, $$args{offset}, $$args{service}[$index -1] );
208                         $$munged{service} = $$args{service}[$index - 1];
209                         $conn->respond($munged);
210                 }
211         }
212
213         $logger->debug("z3950: Search Complete");
214     return undef;
215 }
216
217
218 # -------------------------------------------------------------------
219 # This handles the host settings, but expects a fully formed z query
220 # -------------------------------------------------------------------
221 sub do_service_search {
222
223         my $self                        = shift;
224         my $conn                        = shift;
225         my $auth                        = shift;
226         my $args                        = shift;
227         
228         my $info = $services{$$args{service}};
229
230         $$args{host}    = $$info{host};
231         $$args{port}    = $$info{port};
232         $$args{db}              = $$info{db};
233     $logger->debug("z3950: do_search...");
234
235         return do_search( $self, $conn, $auth, $args );
236 }
237
238
239
240 # -------------------------------------------------------------------
241 # This is the low level search method.  All config and query
242 # data must be provided to this method
243 # -------------------------------------------------------------------
244 sub do_search {
245
246         my $self        = shift;
247         my $conn        = shift;
248         my $auth = shift;
249         my $args = shift;
250
251         my $host                = $$args{host} or return undef;
252         my $port                = $$args{port} or return undef;
253         my $db          = $$args{db}    or return undef;
254         my $query       = $$args{query} or return undef;
255         my $async       = $$args{async} || 0;
256
257         my $limit       = $$args{limit} || 10;
258         my $offset      = $$args{offset} || 0;
259
260         my $username = $$args{username} || "";
261         my $password = $$args{password} || "";
262
263     my $tformat = $services{$args->{service}}->{transmission_format} || $output;
264
265         my $editor = new_editor(authtoken => $auth);
266         return $editor->event unless $editor->checkauth;
267         return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
268
269     $logger->info("z3950: connecting to server $host:$port:$db as $username");
270
271         my $connection = OpenILS::Utils::ZClient->new(
272                 $host, $port,
273                 databaseName                            => $db, 
274                 user                                                    => $username,
275                 password                                                => $password,
276                 async                                                   => $async,
277                 preferredRecordSyntax   => $tformat, 
278         );
279
280         if( ! $connection ) {
281                 $logger->error("z3950: Unable to connect to Z server: ".
282                         "$host:$port:$db:$username:$password");
283                 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
284         }
285
286         my $start = time;
287         my $results;
288         my $err;
289
290         $logger->info("z3950: query => $query");
291
292         try {
293                 $results = $connection->search_pqf( $query );
294         } catch Error with { $err = shift; };
295
296         return OpenILS::Event->new(
297                 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
298
299         return OpenILS::Event->new('Z3950_SEARCH_FAILED', 
300                 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
301
302         $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
303
304         return {result => $results, connection => $connection} if ($async);
305
306         my $munged = process_results($results, $limit, $offset, $$args{service});
307         $munged->{query} = $query;
308
309         return $munged;
310 }
311
312
313 # -------------------------------------------------------------------
314 # Takes a result batch and returns the hitcount and a list of xml
315 # and mvr objects
316 # -------------------------------------------------------------------
317 sub process_results {
318         my $results     = shift;
319         my $limit       = shift || 10;
320         my $offset      = shift || 0;
321     my $service = shift;
322
323     my $rformat = $services{$service}->{record_format};
324     my $tformat = $services{$service}->{transmission_format} || $output;
325
326     $results->option(elementSetName => $rformat);
327     $results->option(preferredRecordSyntax => $tformat);
328     $logger->info("z3950: using record format '$rformat' and transmission format '$tformat'");
329
330         my @records;
331         my $res = {};
332         my $count = $$res{count} = $results->size;
333
334         $logger->info("z3950: search returned $count hits");
335
336         my $tend = $limit + $offset;
337
338         my $end = ($tend <= $count) ? $tend : $count;
339
340         for($offset..$end - 1) {
341
342                 my $err;
343                 my $mods;
344                 my $marc;
345                 my $marcs;
346                 my $marcxml;
347
348                 $logger->info("z3950: fetching record $_");
349
350                 try {
351
352                         my $rec = $results->record($_);
353
354             if ($tformat eq 'usmarc') {
355                         $marc           = MARC::Record->new_from_usmarc($rec->raw());
356             } elsif ($tformat eq 'xml') {
357                         $marc           = MARC::Record->new_from_xml($rec->raw());
358             } else {
359                 die "Unsupported record transmission format $tformat"
360             }
361
362                         $marcs  = entityize($marc->as_xml_record);
363                         my $doc = XML::LibXML->new->parse_string($marcs);
364                         $marcxml = entityize( $doc->documentElement->toString );
365         
366                         my $u = OpenILS::Utils::ModsParser->new();
367                         $u->start_mods_batch( $marcxml );
368                         $mods = $u->finish_mods_batch();
369         
370
371                 } catch Error with { $err = shift; };
372
373                 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
374                 $logger->error("z3950: bad XML : $err") if $err;
375
376                 if( $err ) {
377                         warn "\n\n$marcs\n\n";
378                 }
379         }
380         
381         $res->{records} = \@records;
382         return $res;
383 }
384
385
386
387 # -------------------------------------------------------------------
388 # Compiles the class based search query
389 # -------------------------------------------------------------------
390 sub compile_query {
391
392         my $seperator   = shift;
393         my $service             = shift;
394         my $hash                        = shift;
395
396         my $count = scalar(keys %$hash);
397
398         my $str = "";
399         $str .= "\@$seperator " for (1..$count-1);
400         
401     # -------------------------------------------------------------------
402     # "code" is the bib-1 "use attribute", "format" is the bib-1 
403     # "structure attribute"
404     # -------------------------------------------------------------------
405         for( keys %$hash ) {
406                 next unless ( exists $services{$service}->{attrs}->{$_} );
407                 $str .= '@attr 1=' . $services{$service}->{attrs}->{$_}->{code} . # add the use attribute
408                         ' @attr 4=' . $services{$service}->{attrs}->{$_}->{format}; # add the structure attribute
409                 if (exists $services{$service}->{attrs}->{$_}->{truncation}){
410                         $str .= ' @attr 5=' . $services{$service}->{attrs}->{$_}->{truncation};
411                 }
412                 $str .= " \"" . $$hash{$_} . "\" "; # add the search term
413         }
414         return $str;
415 }
416
417
418
419 # -------------------------------------------------------------------
420 # Handles the unicode
421 # -------------------------------------------------------------------
422 sub entityize {
423         my $stuff = shift;
424         my $form = shift || "";
425         
426         if ($form eq 'D') {
427                 $stuff = NFD($stuff);
428         } else {
429                 $stuff = NFC($stuff);
430         }
431         
432         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
433
434         # strip some other unfriendly chars that may leak in
435    $stuff =~ s/([\x{0000}-\x{0008}])//sgoe; 
436
437         return $stuff;
438 }
439
440
441 1;