1 package OpenILS::Application::Search::Z3950;
2 use strict; use warnings;
3 use base qw/OpenILS::Application/;
5 use OpenILS::Utils::ZClient;
8 use Unicode::Normalize;
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/;
20 my $output = "usmarc";
21 my $U = 'OpenILS::Application::AppUtils';
28 __PACKAGE__->register_method(
29 method => 'do_class_search',
30 api_name => 'open-ils.search.z3950.search_class',
33 Performs a class based Z search. The classes available
34 are defined by the 'attr' fields in the config for the
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
44 __PACKAGE__->register_method(
45 method => 'do_service_search',
46 api_name => 'open-ils.search.z3950.search_service',
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
57 __PACKAGE__->register_method(
58 method => 'do_service_search',
59 api_name => 'open-ils.search.z3950.search_raw',
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
69 __PACKAGE__->register_method(
70 method => "query_services",
71 api_name => "open-ils.search.z3950.retrieve_services",
73 Returns a list of service names that we have config
80 # -------------------------------------------------------------------
81 # What services do we have config info for?
82 # -------------------------------------------------------------------
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');
89 if($e->can('search_config_z3950_source')) {
91 my $sources = $e->search_config_z3950_source(
92 [ { name => { '!=' => undef } },
93 { flesh => 1, flesh_fields => { czs => ['attrs'] } }]
97 for my $s ( @$sources ) {
104 record_format => $s->record_format,
105 transmission_format => $s->transmission_format,
109 for my $a ( @{ $s->attrs } ) {
110 $hash{ $a->source }{attrs}{ $a->name } = {
114 format => $a->format,
115 source => $a->source,
116 truncation => $a->truncation,
124 return $sclient->config_value('z3950', 'services');
130 # -------------------------------------------------------------------
131 # Load the pre-defined Z server configs
132 # -------------------------------------------------------------------
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;
141 # -------------------------------------------------------------------
142 # High-level class based search.
143 # -------------------------------------------------------------------
144 sub do_class_search {
151 if (!ref($$args{service})) {
152 $$args{service} = [$$args{service}];
153 $$args{username} = [$$args{username}];
154 $$args{password} = [$$args{password}];
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];
168 $logger->debug("z3950: service: $tmp_args{service}, async: $tmp_args{async}");
170 if ($tmp_args{service} eq 'native-evergreen-catalog') {
171 my $method = $self->method_lookup('open-ils.search.biblio.zstyle');
173 $self->method_lookup('open-ils.search.biblio.zstyle')->run($auth, \%tmp_args)
178 $tmp_args{query} = compile_query('and', $tmp_args{service}, $tmp_args{search});
180 my $res = do_service_search( $self, $conn, $auth, \%tmp_args );
182 if ($U->event_code($res)) {
183 $conn->respond($res) if $U->event_code($res);
186 push @services, $tmp_args{service};
187 push @results, $res->{result};
188 push @connections, $res->{connection};
192 $logger->debug("z3950: Result object: $results[$i], Connection object: $connections[$i]");
195 $logger->debug("z3950: Connections created");
197 return undef unless (@connections);
200 # local catalog search is not processed with other z39 results;
201 $$args{service} = [grep {$_ ne 'native-evergreen-catalog'} @{$$args{service}}];
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);
213 $logger->debug("z3950: Search Complete");
218 # -------------------------------------------------------------------
219 # This handles the host settings, but expects a fully formed z query
220 # -------------------------------------------------------------------
221 sub do_service_search {
228 my $info = $services{$$args{service}};
230 $$args{host} = $$info{host};
231 $$args{port} = $$info{port};
232 $$args{db} = $$info{db};
233 $logger->debug("z3950: do_search...");
235 return do_search( $self, $conn, $auth, $args );
240 # -------------------------------------------------------------------
241 # This is the low level search method. All config and query
242 # data must be provided to this method
243 # -------------------------------------------------------------------
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;
257 my $limit = $$args{limit} || 10;
258 my $offset = $$args{offset} || 0;
260 my $username = $$args{username} || "";
261 my $password = $$args{password} || "";
263 my $tformat = $services{$args->{service}}->{transmission_format} || $output;
265 my $editor = new_editor(authtoken => $auth);
266 return $editor->event unless $editor->checkauth;
267 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
269 $logger->info("z3950: connecting to server $host:$port:$db as $username");
271 my $connection = OpenILS::Utils::ZClient->new(
275 password => $password,
277 preferredRecordSyntax => $tformat,
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;
290 $logger->info("z3950: query => $query");
293 $results = $connection->search_pqf( $query );
294 } catch Error with { $err = shift; };
296 return OpenILS::Event->new(
297 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
299 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
300 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
302 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
304 return {result => $results, connection => $connection} if ($async);
306 my $munged = process_results($results, $limit, $offset, $$args{service});
307 $munged->{query} = $query;
313 # -------------------------------------------------------------------
314 # Takes a result batch and returns the hitcount and a list of xml
316 # -------------------------------------------------------------------
317 sub process_results {
319 my $limit = shift || 10;
320 my $offset = shift || 0;
323 my $rformat = $services{$service}->{record_format};
324 my $tformat = $services{$service}->{transmission_format} || $output;
326 $results->option(elementSetName => $rformat);
327 $results->option(preferredRecordSyntax => $tformat);
328 $logger->info("z3950: using record format '$rformat' and transmission format '$tformat'");
332 my $count = $$res{count} = $results->size;
334 $logger->info("z3950: search returned $count hits");
336 my $tend = $limit + $offset;
338 my $end = ($tend <= $count) ? $tend : $count;
340 for($offset..$end - 1) {
348 $logger->info("z3950: fetching record $_");
352 my $rec = $results->record($_);
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());
359 die "Unsupported record transmission format $tformat"
362 $marcs = entityize($marc->as_xml_record);
363 my $doc = XML::LibXML->new->parse_string($marcs);
364 $marcxml = entityize( $doc->documentElement->toString );
366 my $u = OpenILS::Utils::ModsParser->new();
367 $u->start_mods_batch( $marcxml );
368 $mods = $u->finish_mods_batch();
371 } catch Error with { $err = shift; };
373 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
374 $logger->error("z3950: bad XML : $err") if $err;
377 warn "\n\n$marcs\n\n";
381 $res->{records} = \@records;
387 # -------------------------------------------------------------------
388 # Compiles the class based search query
389 # -------------------------------------------------------------------
392 my $seperator = shift;
396 my $count = scalar(keys %$hash);
399 $str .= "\@$seperator " for (1..$count-1);
401 # -------------------------------------------------------------------
402 # "code" is the bib-1 "use attribute", "format" is the bib-1
403 # "structure attribute"
404 # -------------------------------------------------------------------
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};
412 $str .= " \"" . $$hash{$_} . "\" "; # add the search term
419 # -------------------------------------------------------------------
420 # Handles the unicode
421 # -------------------------------------------------------------------
424 my $form = shift || "";
427 $stuff = NFD($stuff);
429 $stuff = NFC($stuff);
432 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
434 # strip some other unfriendly chars that may leak in
435 $stuff =~ s/([\x{0000}-\x{0008}])//sgoe;