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::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',
32 Performs a class based Z search. The classes available
33 are defined by the 'attr' fields in the config for the
35 @param auth The login session key
36 @param shash The search hash : { attr : value, attr2: value, ...}
37 @param service The service to connect to
38 @param username The username to use when connecting to the service
39 @param password The password to use when connecting to the service
43 __PACKAGE__->register_method(
44 method => 'do_service_search',
45 api_name => 'open-ils.search.z3950.search_service',
47 @param auth The login session key
48 @param query The Z3950 search string to use
49 @param service The service to connect to
50 @param username The username to use when connecting to the service
51 @param password The password to use when connecting to the service
56 __PACKAGE__->register_method(
57 method => 'do_service_search',
58 api_name => 'open-ils.search.z3950.search_raw',
60 @param auth The login session key
61 @param args An object of search params which must include:
62 host, port, db and query.
63 optional fields include username and password
68 __PACKAGE__->register_method(
69 method => "query_services",
70 api_name => "open-ils.search.z3950.retrieve_services",
72 Returns a list of service names that we have config
79 # -------------------------------------------------------------------
80 # What services do we have config info for?
81 # -------------------------------------------------------------------
83 my( $self, $client, $auth ) = @_;
84 my $e = new_editor(authtoken=>$auth);
85 return $e->event unless $e->checkauth;
86 return $e->event unless $e->allowed('REMOTE_Z3950_QUERY');
87 return $sclient->config_value('z3950', 'services');
92 # -------------------------------------------------------------------
93 # Load the pre-defined Z server configs
94 # -------------------------------------------------------------------
96 $sclient = OpenSRF::Utils::SettingsClient->new();
97 $default_service = $sclient->config_value("z3950", "default" );
98 my $servs = $sclient->config_value("z3950", "services" );
99 $services{$_} = $$servs{$_} for keys %$servs;
103 # -------------------------------------------------------------------
104 # High-level class based search.
105 # -------------------------------------------------------------------
106 sub do_class_search {
113 if (!ref($$args{service})) {
114 $$args{service} = [$$args{service}];
115 $$args{username} = [$$args{username}];
116 $$args{password} = [$$args{password}];
123 for (my $i = 0; $i < @{$$args{service}}; $i++) {
124 my %tmp_args = %$args;
125 $tmp_args{service} = $$args{service}[$i];
126 $tmp_args{username} = $$args{username}[$i];
127 $tmp_args{password} = $$args{password}[$i];
129 $logger->debug("z3950: service: $tmp_args{service}, async: $tmp_args{async}");
131 $tmp_args{query} = compile_query('and', $tmp_args{service}, $tmp_args{search});
133 my $res = do_service_search( $self, $conn, $auth, \%tmp_args );
135 push @results, $res->{result};
136 push @connections, $res->{connection};
138 $logger->debug("z3950: Result object: $results[$i], Connection object: $connections[$i]");
141 $logger->debug("z3950: Connections created");
144 while ((my $index = OpenILS::Utils::ZClient::event( \@connections )) != 0) {
145 my $ev = $connections[$index - 1]->last_event();
146 $logger->debug("z3950: Received event $ev");
147 if ($ev == OpenILS::Utils::ZClient::EVENT_END()) {
148 my $munged = process_results( $results[$index - 1], $$args{limit}, $$args{offset} );
149 $$munged{service} = $$args{service}[$index - 1];
150 $conn->respond($munged);
154 $logger->debug("z3950: Search Complete");
158 # -------------------------------------------------------------------
159 # This handles the host settings, but expects a fully formed z query
160 # -------------------------------------------------------------------
161 sub do_service_search {
168 my $info = $services{$$args{service}};
170 $$args{host} = $$info{host},
171 $$args{port} = $$info{port},
172 $$args{db} = $$info{db},
174 return do_search( $self, $conn, $auth, $args );
179 # -------------------------------------------------------------------
180 # This is the low level search method. All config and query
181 # data must be provided to this method
182 # -------------------------------------------------------------------
190 my $host = $$args{host} or return undef;
191 my $port = $$args{port} or return undef;
192 my $db = $$args{db} or return undef;
193 my $query = $$args{query} or return undef;
194 my $async = $$args{async} || 0;
196 my $limit = $$args{limit} || 10;
197 my $offset = $$args{offset} || 0;
199 my $username = $$args{username} || "";
200 my $password = $$args{password} || "";
202 my $editor = new_editor(authtoken => $auth);
203 return $editor->event unless $editor->checkauth;
204 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
206 my $connection = OpenILS::Utils::ZClient->new(
210 password => $password,
212 preferredRecordSyntax => $output,
215 if( ! $connection ) {
216 $logger->error("z3950: Unable to connect to Z server: ".
217 "$host:$port:$db:$username:$password");
218 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
225 $logger->info("z3950: query => $query");
228 $results = $connection->search_pqf( $query );
229 } catch Error with { $err = shift; };
231 return OpenILS::Event->new(
232 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
234 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
235 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
237 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
239 return {result => $results, connection => $connection} if ($async);
241 my $munged = process_results($results, $limit, $offset);
242 $munged->{query} = $query;
248 # -------------------------------------------------------------------
249 # Takes a result batch and returns the hitcount and a list of xml
251 # -------------------------------------------------------------------
252 sub process_results {
254 my $limit = shift || 10;
255 my $offset = shift || 0;
257 $results->option(elementSetName => "FI"); # full records with no holdings
261 my $count = $$res{count} = $results->size;
263 $logger->info("z3950: search returned $count hits");
265 my $tend = $limit + $offset;
267 my $end = ($tend <= $count) ? $tend : $count;
269 for($offset..$end - 1) {
277 $logger->info("z3950: fetching record $_");
281 my $rec = $results->record($_);
282 $marc = MARC::Record->new_from_usmarc($rec->raw());
283 $marcs = entityize($marc->as_xml_record);
284 my $doc = XML::LibXML->new->parse_string($marcs);
285 $marcxml = entityize( $doc->documentElement->toString );
287 my $u = OpenILS::Utils::ModsParser->new();
288 $u->start_mods_batch( $marcxml );
289 $mods = $u->finish_mods_batch();
292 } catch Error with { $err = shift; };
294 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
295 $logger->error("z3950: bad XML : $err") if $err;
298 warn "\n\n$marcs\n\n";
302 $res->{records} = \@records;
308 # -------------------------------------------------------------------
309 # Compiles the class based search query
310 # -------------------------------------------------------------------
313 my $seperator = shift;
317 my $count = scalar(keys %$hash);
320 $str .= "\@$seperator " for (1..$count-1);
322 # -------------------------------------------------------------------
323 # "code" is the bib-1 "use attribute", "format" is the bib-1
324 # "structure attribute"
325 # -------------------------------------------------------------------
327 next unless ( exists $services{$service}->{attrs}->{$_} );
328 $str .= '@attr 1=' . $services{$service}->{attrs}->{$_}->{code} . # add the use attribute
329 ' @attr 4=' . $services{$service}->{attrs}->{$_}->{format} . # add the structure attribute
330 " \"" . $$hash{$_} . "\" "; # add the search term
337 # -------------------------------------------------------------------
338 # Handles the unicode
339 # -------------------------------------------------------------------
342 my $form = shift || "";
345 $stuff = NFD($stuff);
347 $stuff = NFC($stuff);
350 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
352 # strip some other unfriendly chars that may leak in
353 $stuff =~ s/([\x{0000}-\x{0008}])//sgoe;