1 package OpenILS::Application::Search::Z3950;
2 use strict; use warnings;
3 use base qw/OpenSRF::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::Editor q/:funcs/;
20 my $output = "USMARC";
27 __PACKAGE__->register_method(
28 method => 'do_class_search',
29 api_name => 'open-ils.search.z3950.search_class',
31 Performs a class based Z search. The classes available
32 are defined by the 'attr' fields in the config for the
34 @param auth The login session key
35 @param shash The search hash : { attr : value, attr2: value, ...}
36 @param service The service to connect to
37 @param username The username to use when connecting to the service
38 @param password The password to use when connecting to the service
42 __PACKAGE__->register_method(
43 method => 'do_service_search',
44 api_name => 'open-ils.search.z3950.search_service',
46 @param auth The login session key
47 @param query The Z3950 search string to use
48 @param service The service to connect to
49 @param username The username to use when connecting to the service
50 @param password The password to use when connecting to the service
55 __PACKAGE__->register_method(
56 method => 'do_service_search',
57 api_name => 'open-ils.search.z3950.search_raw',
59 @param auth The login session key
60 @param args An object of search params which must include:
61 host, port, db and query.
62 optional fields include username and password
67 __PACKAGE__->register_method(
68 method => "query_services",
69 api_name => "open-ils.search.z3950.retrieve_services",
71 Returns a list of service names that we have config
78 # -------------------------------------------------------------------
79 # What services do we have config info for?
80 # -------------------------------------------------------------------
82 my( $self, $client, $auth ) = @_;
83 my $e = new_editor(authtoken=>$auth);
84 return $e->event unless $e->checkauth;
85 return $e->event unless $e->allowed('REMOTE_Z3950_QUERY');
86 return $sclient->config_value('z3950', 'services');
91 # -------------------------------------------------------------------
92 # Load the pre-defined Z server configs
93 # -------------------------------------------------------------------
95 $sclient = OpenSRF::Utils::SettingsClient->new();
96 $default_service = $sclient->config_value("z3950", "default" );
97 my $servs = $sclient->config_value("z3950", "services" );
98 $services{$_} = $$servs{$_} for keys %$servs;
102 # -------------------------------------------------------------------
103 # High-level class based search.
104 # -------------------------------------------------------------------
105 sub do_class_search {
112 if (!ref($$args{service})) {
113 $$args{service} = [$$args{service}];
114 $$args{username} = [$$args{username}];
115 $$args{password} = [$$args{password}];
121 compile_query('and', $$args{service}, $$args{search});
124 for (my $i = 0; $i < @{$$args{service}}; $i++) {
125 my %tmp_args = %$args;
126 $tmp_args{service} = $$args{service}[$i];
127 $tmp_args{username} = $$args{username}[$i];
128 $tmp_args{password} = $$args{password}[$i];
129 $results[$i] = $self->do_service_search( $conn, $auth, \%tmp_args );
133 while ((my $index = OpenILS::Utils::ZClient::event( \@results )) != 0) {
134 my $ev = $results[$index - 1]->last_event();
135 if ($ev == OpenILS::Utils::ZClient::Event::END()) {
136 my $munged = process_results( $results[$index - 1], ($$args{limit} || 10), ($$args{offset} || 0) );
137 $$munged{service} = $$args{service}[$index];
138 $conn->respond($munged);
144 # -------------------------------------------------------------------
145 # This handles the host settings, but expects a fully formed z query
146 # -------------------------------------------------------------------
147 sub do_service_search {
154 my $info = $services{$$args{service}};
156 $$args{host} = $$info{host},
157 $$args{port} = $$info{port},
158 $$args{db} = $$info{db},
160 return $self->do_search( $conn, $auth, $args );
165 # -------------------------------------------------------------------
166 # This is the low level search method. All config and query
167 # data must be provided to this method
168 # -------------------------------------------------------------------
176 my $host = $$args{host} or return undef;
177 my $port = $$args{port} or return undef;
178 my $db = $$args{db} or return undef;
179 my $query = $$args{query} or return undef;
180 my $async = $$args{async} || 0;
182 my $limit = $$args{limit} || 10;
183 my $offset = $$args{offset} || 0;
185 my $username = $$args{username} || "";
186 my $password = $$args{password} || "";
188 my $editor = new_editor(authtoken => $auth);
189 return $editor->event unless $editor->checkauth;
190 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
192 my $connection = OpenILS::Utils::ZClient->new(
197 password => $password,
198 preferredRecordSyntax => $output,
201 if( ! $connection ) {
202 $logger->error("z3950: Unable to connect to Z server: ".
203 "$host:$port:$db:$username:$password");
204 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
211 $logger->info("z3950: query => $query");
214 $results = $connection->search_pqf( $query );
215 } catch Error with { $err = shift; };
217 return OpenILS::Event->new(
218 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
220 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
221 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
223 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
225 return $results if ($async);
227 my $munged = process_results($results, $limit, $offset);
228 $munged->{query} = $query;
234 # -------------------------------------------------------------------
235 # Takes a result batch and returns the hitcount and a list of xml
237 # -------------------------------------------------------------------
238 sub process_results {
243 $results->option(elementSetName => "FI"); # full records with no holdings
247 my $count = $$res{count} = $results->size;
249 $logger->info("z3950: search returned $count hits");
251 my $tend = $limit + $offset;
253 my $end = ($tend <= $count) ? $tend : $count;
255 for($offset..$end - 1) {
263 $logger->info("z3950: fetching record $_");
267 my $rec = $results->record($_);
268 $marc = MARC::Record->new_from_usmarc($rec->raw());
269 $marcs = entityize($marc->as_xml_record);
270 my $doc = XML::LibXML->new->parse_string($marcs);
271 $marcxml = entityize( $doc->documentElement->toString );
273 my $u = OpenILS::Utils::ModsParser->new();
274 $u->start_mods_batch( $marcxml );
275 $mods = $u->finish_mods_batch();
278 } catch Error with { $err = shift; };
280 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
281 $logger->error("z3950: bad XML : $err") if $err;
284 warn "\n\n$marcs\n\n";
288 $res->{records} = \@records;
294 # -------------------------------------------------------------------
295 # Compiles the class based search query
296 # -------------------------------------------------------------------
299 my $seperator = shift;
303 my $count = scalar(keys %$hash);
306 $str .= "\@$seperator " for (1..$count-1);
308 # -------------------------------------------------------------------
309 # "code" is the bib-1 "use attribute", "format" is the bib-1
310 # "structure attribute"
311 # -------------------------------------------------------------------
314 # $services{$service}->{attrs}->{$_}->{format} . '=' .
315 # $services{$service}->{attrs}->{$_}->{code} . " \"" . $$hash{$_} . "\" ";
317 '@attr 1=' . $services{$service}->{attrs}->{$_}->{code} . # add the use attribute
318 ' @attr 4=' . $services{$service}->{attrs}->{$_}->{format} . # add the structure attribute
319 " \"" . $$hash{$_} . "\" "; # add the search term
326 # -------------------------------------------------------------------
327 # Handles the unicode
328 # -------------------------------------------------------------------
331 my $form = shift || "";
334 $stuff = NFD($stuff);
336 $stuff = NFC($stuff);
339 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
341 # strip some other unfriendly chars that may leak in
342 $stuff =~ s/([\x{0000}-\x{0008}])//sgoe;