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 return fetch_service_defs();
92 # -------------------------------------------------------------------
93 # What services do we have config info for?
94 # -------------------------------------------------------------------
95 sub fetch_service_defs {
97 my $hash = $sclient->config_value('z3950', 'services');
99 # overlay config file values with in-db values
100 my $e = new_editor();
101 if($e->can('search_config_z3950_source')) {
103 my $sources = $e->search_config_z3950_source(
104 [ { name => { '!=' => undef } },
105 { flesh => 1, flesh_fields => { czs => ['attrs'] } } ]
108 for my $s ( @$sources ) {
109 $$hash{ $s->name } = {
115 record_format => $s->record_format,
116 transmission_format => $s->transmission_format,
120 for my $a ( @{ $s->attrs } ) {
121 $$hash{ $a->source }{attrs}{ $a->name } = {
125 format => $a->format,
126 source => $a->source,
127 truncation => $a->truncation,
133 # Define the set of native catalog services
134 # XXX There are i18n problems here, but let's get the staff client working first
135 # XXX Move into the DB?
136 $hash->{'native-evergreen-catalog'} = {
138 title => {code => 'title', label => 'Title'},
139 author => {code => 'author', label => 'Author'},
140 subject => {code => 'subject', label => 'Subject'},
141 keyword => {code => 'keyword', label => 'Keyword'},
142 tcn => {code => 'tcn', label => 'TCN'},
143 isbn => {code => 'isbn', label => 'ISBN'},
144 issn => {code => 'issn', label => 'ISSN'},
145 publisher => {code => 'publisher', label => 'Publisher'},
146 pubdate => {code => 'pubdate', label => 'Pub Date'},
147 item_type => {code => 'item_type', label => 'Item Type'},
151 %services = %$hash; # cache these internally so we can actually use the db-configured sources
153 # Return a sorted list for easier handling in the client
155 foreach my $zsource (sort keys %$hash) {
156 if ($hash->{$zsource}->{name}) {
157 push @sorted_sources, $hash->{$zsource};
160 return \@sorted_sources;
165 # -------------------------------------------------------------------
166 # Load the pre-defined Z server configs
167 # -------------------------------------------------------------------
169 $sclient = OpenSRF::Utils::SettingsClient->new();
170 $default_service = $sclient->config_value("z3950", "default" );
174 # -------------------------------------------------------------------
175 # High-level class based search.
176 # -------------------------------------------------------------------
177 sub do_class_search {
179 fetch_service_defs() unless (scalar(keys(%services)));
186 if (!ref($$args{service})) {
187 $$args{service} = [$$args{service}];
188 $$args{username} = [$$args{username}];
189 $$args{password} = [$$args{password}];
197 for (my $i = 0; $i < @{$$args{service}}; $i++) {
198 my %tmp_args = %$args;
199 $tmp_args{service} = $$args{service}[$i];
200 $tmp_args{username} = $$args{username}[$i];
201 $tmp_args{password} = $$args{password}[$i];
203 $logger->debug("z3950: service: $tmp_args{service}, async: $tmp_args{async}");
205 if ($tmp_args{service} eq 'native-evergreen-catalog') {
206 my $method = $self->method_lookup('open-ils.search.biblio.zstyle.staff');
208 $self->method_lookup('open-ils.search.biblio.zstyle.staff')->run($auth, \%tmp_args)
213 $tmp_args{query} = compile_query('and', $tmp_args{service}, $tmp_args{search});
215 my $res = do_service_search( $self, $conn, $auth, \%tmp_args );
217 if ($U->event_code($res)) {
218 $conn->respond($res) if $U->event_code($res);
221 push @services, $tmp_args{service};
222 push @results, $res->{result};
223 push @connections, $res->{connection};
227 $logger->debug("z3950: Result object: $results[$i], Connection object: $connections[$i]");
230 $logger->debug("z3950: Connections created");
232 return undef unless (@connections);
235 # local catalog search is not processed with other z39 results;
236 $$args{service} = [grep {$_ ne 'native-evergreen-catalog'} @{$$args{service}}];
238 @connections = grep {defined $_} @connections;
239 return undef unless @connections;
241 while ((my $index = OpenILS::Utils::ZClient::event( \@connections )) != 0) {
242 my $ev = $connections[$index - 1]->last_event();
243 $logger->debug("z3950: Received event $ev");
244 if ($ev == OpenILS::Utils::ZClient::EVENT_END()) {
245 my $munged = process_results( $results[$index - 1], $$args{limit}, $$args{offset}, $$args{service}[$index -1] );
246 $$munged{service} = $$args{service}[$index - 1];
247 $conn->respond($munged);
251 $logger->debug("z3950: Search Complete");
256 # -------------------------------------------------------------------
257 # This handles the host settings, but expects a fully formed z query
258 # -------------------------------------------------------------------
259 sub do_service_search {
261 fetch_service_defs() unless (scalar(keys(%services)));
268 my $info = $services{$$args{service}};
270 $$args{host} = $$info{host};
271 $$args{port} = $$info{port};
272 $$args{db} = $$info{db};
273 $logger->debug("z3950: do_search...");
275 return do_search( $self, $conn, $auth, $args );
280 # -------------------------------------------------------------------
281 # This is the low level search method. All config and query
282 # data must be provided to this method
283 # -------------------------------------------------------------------
286 fetch_service_defs() unless (scalar(keys(%services)));
293 my $host = $$args{host} or return undef;
294 my $port = $$args{port} or return undef;
295 my $db = $$args{db} or return undef;
296 my $query = $$args{query} or return undef;
297 my $async = $$args{async} || 0;
299 my $limit = $$args{limit} || 10;
300 my $offset = $$args{offset} || 0;
302 my $username = $$args{username} || "";
303 my $password = $$args{password} || "";
305 my $tformat = $services{$args->{service}}->{transmission_format} || $output;
307 my $editor = new_editor(authtoken => $auth);
308 return $editor->event unless $editor->checkauth;
309 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
311 $logger->info("z3950: connecting to server $host:$port:$db as $username");
313 my $connection = OpenILS::Utils::ZClient->new(
317 password => $password,
319 preferredRecordSyntax => $tformat,
322 if( ! $connection ) {
323 $logger->error("z3950: Unable to connect to Z server: ".
324 "$host:$port:$db:$username:$password");
325 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
332 $logger->info("z3950: query => $query");
335 $results = $connection->search_pqf( $query );
336 } catch Error with { $err = shift; };
338 return OpenILS::Event->new(
339 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
341 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
342 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
344 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
346 return {result => $results, connection => $connection} if ($async);
348 my $munged = process_results($results, $limit, $offset, $$args{service});
349 $munged->{query} = $query;
355 # -------------------------------------------------------------------
356 # Takes a result batch and returns the hitcount and a list of xml
358 # -------------------------------------------------------------------
359 sub process_results {
361 fetch_service_defs() unless (scalar(keys(%services)));
364 my $limit = shift || 10;
365 my $offset = shift || 0;
368 my $rformat = $services{$service}->{record_format};
369 my $tformat = $services{$service}->{transmission_format} || $output;
371 $results->option(elementSetName => $rformat);
372 $results->option(preferredRecordSyntax => $tformat);
373 $logger->info("z3950: using record format '$rformat' and transmission format '$tformat'");
377 my $count = $$res{count} = $results->size;
379 $logger->info("z3950: search returned $count hits");
381 my $tend = $limit + $offset;
383 my $end = ($tend <= $count) ? $tend : $count;
385 for($offset..$end - 1) {
393 $logger->info("z3950: fetching record $_");
397 my $rec = $results->record($_);
399 if ($tformat eq 'usmarc') {
400 $marc = MARC::Record->new_from_usmarc($rec->raw());
401 } elsif ($tformat eq 'xml') {
402 $marc = MARC::Record->new_from_xml($rec->raw());
404 die "Unsupported record transmission format $tformat"
407 $marcs = $U->entityize($marc->as_xml_record);
408 $marcs = $U->strip_ctrl_chars($marcs);
409 my $doc = XML::LibXML->new->parse_string($marcs);
410 $marcxml = $U->entityize($doc->documentElement->toString);
411 $marcxml = $U->strip_ctrl_chars($marcxml);
413 my $u = OpenILS::Utils::ModsParser->new();
414 $u->start_mods_batch( $marcxml );
415 $mods = $u->finish_mods_batch();
418 } catch Error with { $err = shift; };
420 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
421 $logger->error("z3950: bad XML : $err") if $err;
424 warn "\n\n$marcs\n\n";
428 $res->{records} = \@records;
434 # -------------------------------------------------------------------
435 # Compiles the class based search query
436 # -------------------------------------------------------------------
439 fetch_service_defs() unless (scalar(keys(%services)));
441 my $seperator = shift;
445 my $count = scalar(keys %$hash);
448 $str .= "\@$seperator " for (1..$count-1);
450 # -------------------------------------------------------------------
451 # "code" is the bib-1 "use attribute", "format" is the bib-1
452 # "structure attribute"
453 # -------------------------------------------------------------------
455 next unless ( exists $services{$service}->{attrs}->{$_} );
456 $str .= '@attr 1=' . $services{$service}->{attrs}->{$_}->{code} . # add the use attribute
457 ' @attr 4=' . $services{$service}->{attrs}->{$_}->{format}; # add the structure attribute
458 if (exists $services{$service}->{attrs}->{$_}->{truncation}){
459 $str .= ' @attr 5=' . $services{$service}->{attrs}->{$_}->{truncation};
461 $str .= " \"" . $$hash{$_} . "\" "; # add the search term