1 package OpenILS::Application::Search::Z3950;
2 use strict; use warnings;
3 use base qw/OpenILS::Application/;
5 use OpenILS::Utils::ZClient;
7 use MARC::File::XML (BinaryEncoding => 'UTF-8');
9 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 MARC::Charset->assume_unicode(1);
21 MARC::Charset->ignore_errors(1);
23 my $output = "usmarc";
24 my $U = 'OpenILS::Application::AppUtils';
31 __PACKAGE__->register_method(
32 method => 'do_class_search',
33 api_name => 'open-ils.search.z3950.search_class',
36 Performs a class based Z search. The classes available
37 are defined by the 'attr' fields in the config for the
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
47 __PACKAGE__->register_method(
48 method => 'do_service_search',
49 api_name => 'open-ils.search.z3950.search_service',
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
60 __PACKAGE__->register_method(
61 method => 'do_service_search',
62 api_name => 'open-ils.search.z3950.search_raw',
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
72 __PACKAGE__->register_method(
73 method => "query_services",
74 api_name => "open-ils.search.z3950.retrieve_services",
76 @param auth The login session key
77 Returns a list of service names that we have config
84 # -------------------------------------------------------------------
85 # What services do we have config info for?
86 # -------------------------------------------------------------------
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');
93 return fetch_service_defs($e);
96 # -------------------------------------------------------------------
97 # What services do we have config info for?
98 # -------------------------------------------------------------------
99 sub fetch_service_defs {
101 my $editor_with_authtoken = shift;
103 my $hash = $sclient->config_value('z3950', 'services');
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')) {
109 my $sources = $e->search_config_z3950_source(
110 [ { name => { '!=' => undef } },
111 { flesh => 1, flesh_fields => { czs => ['attrs'] } } ]
114 for my $s ( @$sources ) {
115 $$hash{ $s->name } = {
121 record_format => $s->record_format,
122 transmission_format => $s->transmission_format,
124 use_perm => ($s->use_perm) ?
125 $e->retrieve_permission_perm_list($s->use_perm)->code : ''
128 for my $a ( @{ $s->attrs } ) {
129 $$hash{ $a->source }{attrs}{ $a->name } = {
133 format => $a->format,
134 source => $a->source,
135 truncation => $a->truncation,
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'} = {
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'},
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}) {
165 $e->requestor->ws_ou,
174 %services = %$hash; # cache these internally so we can actually use the db-configured sources
180 # -------------------------------------------------------------------
181 # Load the pre-defined Z server configs
182 # -------------------------------------------------------------------
184 $sclient = OpenSRF::Utils::SettingsClient->new();
185 $default_service = $sclient->config_value("z3950", "default" );
189 # -------------------------------------------------------------------
190 # High-level class based search.
191 # -------------------------------------------------------------------
192 sub do_class_search {
194 fetch_service_defs() unless (scalar(keys(%services)));
201 if (!ref($$args{service})) {
202 $$args{service} = [$$args{service}];
203 $$args{username} = [$$args{username}];
204 $$args{password} = [$$args{password}];
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];
218 $logger->debug("z3950: service: $tmp_args{service}, async: $tmp_args{async}");
220 if ($tmp_args{service} eq 'native-evergreen-catalog') {
221 my $method = $self->method_lookup('open-ils.search.biblio.zstyle.staff');
223 $self->method_lookup('open-ils.search.biblio.zstyle.staff')->run($auth, \%tmp_args)
228 $tmp_args{query} = compile_query('and', $tmp_args{service}, $tmp_args{search});
230 my $res = do_service_search( $self, $conn, $auth, \%tmp_args );
232 if ($U->event_code($res)) {
233 $conn->respond($res) if $U->event_code($res);
236 push @services, $tmp_args{service};
237 push @results, $res->{result};
238 push @connections, $res->{connection};
242 $logger->debug("z3950: Result object: $results[$i], Connection object: $connections[$i]");
245 $logger->debug("z3950: Connections created");
247 return undef unless (@connections);
250 # local catalog search is not processed with other z39 results;
251 $$args{service} = [grep {$_ ne 'native-evergreen-catalog'} @{$$args{service}}];
253 @connections = grep {defined $_} @connections;
254 return undef unless @connections;
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);
266 $logger->debug("z3950: Search Complete");
271 # -------------------------------------------------------------------
272 # This handles the host settings, but expects a fully formed z query
273 # -------------------------------------------------------------------
274 sub do_service_search {
276 fetch_service_defs() unless (scalar(keys(%services)));
283 my $info = $services{$$args{service}};
285 $$args{host} = $$info{host};
286 $$args{port} = $$info{port};
287 $$args{db} = $$info{db};
288 $logger->debug("z3950: do_search...");
290 return do_search( $self, $conn, $auth, $args );
295 # -------------------------------------------------------------------
296 # This is the low level search method. All config and query
297 # data must be provided to this method
298 # -------------------------------------------------------------------
301 fetch_service_defs() unless (scalar(keys(%services)));
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;
314 my $limit = $$args{limit} || 10;
315 my $offset = $$args{offset} || 0;
317 my $username = $$args{username} || "";
318 my $password = $$args{password} || "";
320 my $tformat = $services{$args->{service}}->{transmission_format} || $output;
322 my $editor = new_editor(authtoken => $auth);
323 return $editor->event unless $editor->checkauth;
324 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
326 $logger->info("z3950: connecting to server $host:$port:$db as $username");
328 my $connection = OpenILS::Utils::ZClient->new(
332 password => $password,
334 preferredRecordSyntax => $tformat,
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;
347 $logger->info("z3950: query => $query");
350 $results = $connection->search_pqf( $query );
351 } catch Error with { $err = shift; };
353 return OpenILS::Event->new(
354 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
356 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
357 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
359 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
361 return {result => $results, connection => $connection} if ($async);
363 my $munged = process_results($results, $limit, $offset, $$args{service});
364 $munged->{query} = $query;
370 # -------------------------------------------------------------------
371 # Takes a result batch and returns the hitcount and a list of xml
373 # -------------------------------------------------------------------
374 sub process_results {
376 fetch_service_defs() unless (scalar(keys(%services)));
379 my $limit = shift || 10;
380 my $offset = shift || 0;
383 my $rformat = $services{$service}->{record_format};
384 my $tformat = $services{$service}->{transmission_format} || $output;
386 $results->option(elementSetName => $rformat);
387 $results->option(preferredRecordSyntax => $tformat);
388 $logger->info("z3950: using record format '$rformat' and transmission format '$tformat'");
392 my $count = $$res{count} = $results->size;
394 $logger->info("z3950: search returned $count hits");
396 my $tend = $limit + $offset;
398 my $end = ($tend <= $count) ? $tend : $count;
400 for($offset..$end - 1) {
408 $logger->info("z3950: fetching record $_");
412 my $rec = $results->record($_);
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());
419 die "Unsupported record transmission format $tformat"
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);
428 my $u = OpenILS::Utils::ModsParser->new();
429 $u->start_mods_batch( $marcxml );
430 $mods = $u->finish_mods_batch();
433 } catch Error with { $err = shift; };
435 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
436 $logger->error("z3950: bad XML : $err") if $err;
439 warn "\n\n$marcs\n\n";
443 $res->{records} = \@records;
449 # -------------------------------------------------------------------
450 # Compiles the class based search query
451 # -------------------------------------------------------------------
454 fetch_service_defs() unless (scalar(keys(%services)));
456 my $separator = shift;
460 my $count = scalar(keys %$hash);
463 $str .= "\@$separator " for (1..$count-1);
465 # -------------------------------------------------------------------
466 # "code" is the bib-1 "use attribute", "format" is the bib-1
467 # "structure attribute"
468 # -------------------------------------------------------------------
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};
477 $str .= " \"" . $$hash{$_} . "\" "; # add the search term