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