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';
30 __PACKAGE__->register_method(
31 method => 'apply_credentials',
32 api_name => 'open-ils.search.z3950.apply_credentials',
34 desc => "Apply credentials for a Z39.50 server",
36 {desc => 'Authtoken', type => 'string'},
37 {desc => 'Z39.50 Source (server) name', type => 'string'},
38 {desc => 'Context org unit', type => 'number'},
39 {desc => 'Username', type => 'string'},
40 {desc => 'Password', type => 'string'}
43 desc => 'Event; SUCCESS on success, other event type on error'
48 sub apply_credentials {
49 my ($self, $client, $auth, $source, $ctx_ou, $username, $password) = @_;
51 my $e = new_editor(authtoken => $auth, xact => 1);
53 return $e->die_event unless
55 $e->allowed('ADMIN_Z3950_SOURCE', $ctx_ou);
57 $e->json_query({from => [
58 'config.z3950_source_credentials_apply',
59 $source, $ctx_ou, $username, $password
60 ]}) or return $e->die_event;
64 return OpenILS::Event->new('SUCCESS');
69 __PACKAGE__->register_method(
70 method => 'do_class_search',
71 api_name => 'open-ils.search.z3950.search_class',
74 Performs a class based Z search. The classes available
75 are defined by the 'attr' fields in the config for the
77 @param auth The login session key
78 @param shash The search hash : { attr : value, attr2: value, ...}
79 @param service The service to connect to
80 @param username The username to use when connecting to the service
81 @param password The password to use when connecting to the service
85 __PACKAGE__->register_method(
86 method => 'do_service_search',
87 api_name => 'open-ils.search.z3950.search_service',
89 @param auth The login session key
90 @param query The Z3950 search string to use
91 @param service The service to connect to
92 @param username The username to use when connecting to the service
93 @param password The password to use when connecting to the service
98 __PACKAGE__->register_method(
99 method => 'do_service_search',
100 api_name => 'open-ils.search.z3950.search_raw',
102 @param auth The login session key
103 @param args An object of search params which must include:
104 host, port, db and query.
105 optional fields include username and password
110 __PACKAGE__->register_method(
111 method => "query_services",
112 api_name => "open-ils.search.z3950.retrieve_services",
114 @param auth The login session key
115 Returns a list of service names that we have config
122 # -------------------------------------------------------------------
123 # What services do we have config info for?
124 # -------------------------------------------------------------------
126 my( $self, $client, $auth ) = @_;
127 my $e = new_editor(authtoken=>$auth);
128 return $e->event unless $e->checkauth;
129 return $e->event unless $e->allowed('REMOTE_Z3950_QUERY');
131 return fetch_service_defs($e);
134 # -------------------------------------------------------------------
135 # What services do we have config info for?
136 # -------------------------------------------------------------------
137 sub fetch_service_defs {
139 my $editor_with_authtoken = shift;
141 my $hash = $sclient->config_value('z3950', 'services');
143 # overlay config file values with in-db values
144 my $e = $editor_with_authtoken || new_editor();
145 if($e->can('search_config_z3950_source')) {
147 my $sources = $e->search_config_z3950_source(
148 [ { name => { '!=' => undef } },
149 { flesh => 1, flesh_fields => { czs => ['attrs'] } } ]
152 for my $s ( @$sources ) {
153 $$hash{ $s->name } = {
159 record_format => $s->record_format,
160 transmission_format => $s->transmission_format,
162 use_perm => ($s->use_perm) ?
163 $e->retrieve_permission_perm_list($s->use_perm)->code : ''
166 for my $a ( @{ $s->attrs } ) {
167 $$hash{ $a->source }{attrs}{ $a->name } = {
171 format => $a->format,
172 source => $a->source,
173 truncation => $a->truncation,
179 # Define the set of native catalog services
180 # XXX There are i18n problems here, but let's get the staff client working first
181 # XXX Move into the DB?
182 $hash->{'native-evergreen-catalog'} = {
184 title => {code => 'title', label => 'Title'},
185 author => {code => 'author', label => 'Author'},
186 subject => {code => 'subject', label => 'Subject'},
187 keyword => {code => 'keyword', label => 'Keyword'},
188 tcn => {code => 'tcn', label => 'TCN'},
189 isbn => {code => 'isbn', label => 'ISBN'},
190 issn => {code => 'issn', label => 'ISSN'},
191 publisher => {code => 'publisher', label => 'Publisher'},
192 pubdate => {code => 'pubdate', label => 'Pub Date'},
193 item_type => {code => 'item_type', label => 'Item Type'},
197 # then filter out any services which the requestor lacks the perm for
198 if ($editor_with_authtoken) {
199 foreach my $s (keys %{ $hash }) {
200 if ($$hash{$s}{use_perm}) {
203 $e->requestor->ws_ou,
212 %services = %$hash; # cache these internally so we can actually use the db-configured sources
218 # -------------------------------------------------------------------
219 # Load the pre-defined Z server configs
220 # -------------------------------------------------------------------
222 $sclient = OpenSRF::Utils::SettingsClient->new();
223 $default_service = $sclient->config_value("z3950", "default" );
227 # -------------------------------------------------------------------
228 # High-level class based search.
229 # -------------------------------------------------------------------
230 sub do_class_search {
232 fetch_service_defs() unless (scalar(keys(%services)));
239 if (!ref($$args{service})) {
240 $$args{service} = [$$args{service}];
241 $$args{username} = [$$args{username}];
242 $$args{password} = [$$args{password}];
250 for (my $i = 0; $i < @{$$args{service}}; $i++) {
251 my %tmp_args = %$args;
252 $tmp_args{service} = $$args{service}[$i];
253 $tmp_args{username} = $$args{username}[$i];
254 $tmp_args{password} = $$args{password}[$i];
256 $logger->debug("z3950: service: $tmp_args{service}, async: $tmp_args{async}");
258 if ($tmp_args{service} eq 'native-evergreen-catalog') {
259 my $method = $self->method_lookup('open-ils.search.biblio.zstyle.staff');
261 $self->method_lookup('open-ils.search.biblio.zstyle.staff')->run($auth, \%tmp_args)
266 $tmp_args{query} = compile_query('and', $tmp_args{service}, $tmp_args{search});
268 my $res = do_service_search( $self, $conn, $auth, \%tmp_args );
270 if ($U->event_code($res)) {
271 $conn->respond($res) if $U->event_code($res);
274 push @services, $tmp_args{service};
275 push @results, $res->{result};
276 push @connections, $res->{connection};
280 $logger->debug("z3950: Result object: $results[$i], Connection object: $connections[$i]");
283 $logger->debug("z3950: Connections created");
285 return undef unless (@connections);
288 # local catalog search is not processed with other z39 results;
289 $$args{service} = [grep {$_ ne 'native-evergreen-catalog'} @{$$args{service}}];
291 @connections = grep {defined $_} @connections;
292 return undef unless @connections;
294 while ((my $index = OpenILS::Utils::ZClient::event( \@connections )) != 0) {
295 my $ev = $connections[$index - 1]->last_event();
296 $logger->debug("z3950: Received event $ev");
297 if ($ev == OpenILS::Utils::ZClient::EVENT_END()) {
298 my $munged = process_results( $results[$index - 1], $$args{limit}, $$args{offset}, $$args{service}[$index -1] );
299 $$munged{service} = $$args{service}[$index - 1];
300 $conn->respond($munged);
304 $logger->debug("z3950: Search Complete");
309 # -------------------------------------------------------------------
310 # This handles the host settings, but expects a fully formed z query
311 # -------------------------------------------------------------------
312 sub do_service_search {
314 fetch_service_defs() unless (scalar(keys(%services)));
321 my $info = $services{$$args{service}};
323 $$args{host} = $$info{host};
324 $$args{port} = $$info{port};
325 $$args{db} = $$info{db};
326 $logger->debug("z3950: do_search...");
328 return do_search( $self, $conn, $auth, $args );
333 # -------------------------------------------------------------------
334 # This is the low level search method. All config and query
335 # data must be provided to this method
336 # -------------------------------------------------------------------
339 fetch_service_defs() unless (scalar(keys(%services)));
346 my $host = $$args{host} or return undef;
347 my $port = $$args{port} or return undef;
348 my $db = $$args{db} or return undef;
349 my $query = $$args{query} or return undef;
350 my $async = $$args{async} || 0;
352 my $limit = $$args{limit} || 10;
353 my $offset = $$args{offset} || 0;
355 my $editor = new_editor(authtoken => $auth);
356 return $editor->event unless
357 $editor->checkauth and
358 $editor->allowed('REMOTE_Z3950_QUERY', $editor->requestor->ws_ou);
360 my $creds = $editor->json_query({from => [
361 'config.z3950_source_credentials_lookup',
362 $$args{service}, $editor->requestor->ws_ou
365 # use the caller-provided username/password if offered.
366 # otherwise, use the stored credentials.
367 my $username = $$args{username} || $creds->{username} || "";
368 my $password = $$args{password} || $creds->{password} || "";
370 my $tformat = $services{$args->{service}}->{transmission_format} || $output;
372 $logger->info("z3950: connecting to server $host:$port:$db as $username");
374 my $connection = OpenILS::Utils::ZClient->new(
378 password => $password,
380 preferredRecordSyntax => $tformat,
383 if( ! $connection ) {
384 $logger->error("z3950: Unable to connect to Z server: ".
385 "$host:$port:$db:$username:$password");
386 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
393 $logger->info("z3950: query => $query");
396 $results = $connection->search_pqf( $query );
397 } catch Error with { $err = shift; };
399 return OpenILS::Event->new(
400 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
402 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
403 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
405 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
407 return {result => $results, connection => $connection} if ($async);
409 my $munged = process_results($results, $limit, $offset, $$args{service});
410 $munged->{query} = $query;
416 # -------------------------------------------------------------------
417 # Takes a result batch and returns the hitcount and a list of xml
419 # -------------------------------------------------------------------
420 sub process_results {
422 fetch_service_defs() unless (scalar(keys(%services)));
425 my $limit = shift || 10;
426 my $offset = shift || 0;
429 my $rformat = $services{$service}->{record_format};
430 my $tformat = $services{$service}->{transmission_format} || $output;
432 $results->option(elementSetName => $rformat);
433 $results->option(preferredRecordSyntax => $tformat);
434 $logger->info("z3950: using record format '$rformat' and transmission format '$tformat'");
438 my $count = $$res{count} = $results->size;
440 $logger->info("z3950: search returned $count hits");
442 my $tend = $limit + $offset;
444 my $end = ($tend <= $count) ? $tend : $count;
446 for($offset..$end - 1) {
454 $logger->info("z3950: fetching record $_");
458 my $rec = $results->record($_);
460 if ($tformat eq 'usmarc') {
461 $marc = MARC::Record->new_from_usmarc($rec->raw());
462 } elsif ($tformat eq 'xml') {
463 $marc = MARC::Record->new_from_xml($rec->raw());
465 die "Unsupported record transmission format $tformat"
468 $marcs = $U->entityize($marc->as_xml_record);
469 $marcs = $U->strip_ctrl_chars($marcs);
470 my $doc = XML::LibXML->new->parse_string($marcs);
471 $marcxml = $U->entityize($doc->documentElement->toString);
472 $marcxml = $U->strip_ctrl_chars($marcxml);
474 my $u = OpenILS::Utils::ModsParser->new();
475 $u->start_mods_batch( $marcxml );
476 $mods = $u->finish_mods_batch();
479 } catch Error with { $err = shift; };
481 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
482 $logger->error("z3950: bad XML : $err") if $err;
485 warn "\n\n$marcs\n\n";
489 $res->{records} = \@records;
495 # -------------------------------------------------------------------
496 # Compiles the class based search query
497 # -------------------------------------------------------------------
500 fetch_service_defs() unless (scalar(keys(%services)));
502 my $separator = shift;
506 my $count = scalar(keys %$hash);
509 $str .= "\@$separator " for (1..$count-1);
511 # -------------------------------------------------------------------
512 # "code" is the bib-1 "use attribute", "format" is the bib-1
513 # "structure attribute"
514 # -------------------------------------------------------------------
516 next unless ( exists $services{$service}->{attrs}->{$_} );
517 $str .= '@attr 1=' . $services{$service}->{attrs}->{$_}->{code} . # add the use attribute
518 ' @attr 4=' . $services{$service}->{attrs}->{$_}->{format}; # add the structure attribute
519 if (exists $services{$service}->{attrs}->{$_}->{truncation}
520 && $services{$service}->{attrs}->{$_}->{truncation} >= 0) {
521 $str .= ' @attr 5=' . $services{$service}->{attrs}->{$_}->{truncation};
523 $str .= " \"" . $$hash{$_} . "\" "; # add the search term