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