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}, $$args{service}[$index -1] );
149 $$munged{service} = $$args{service}[$index - 1];
150 $conn->respond($munged);
154 $logger->debug("z3950: Search Complete");
159 # -------------------------------------------------------------------
160 # This handles the host settings, but expects a fully formed z query
161 # -------------------------------------------------------------------
162 sub do_service_search {
169 my $info = $services{$$args{service}};
171 $$args{host} = $$info{host};
172 $$args{port} = $$info{port};
173 $$args{db} = $$info{db};
175 return do_search( $self, $conn, $auth, $args );
180 # -------------------------------------------------------------------
181 # This is the low level search method. All config and query
182 # data must be provided to this method
183 # -------------------------------------------------------------------
191 my $host = $$args{host} or return undef;
192 my $port = $$args{port} or return undef;
193 my $db = $$args{db} or return undef;
194 my $query = $$args{query} or return undef;
195 my $async = $$args{async} || 0;
197 my $limit = $$args{limit} || 10;
198 my $offset = $$args{offset} || 0;
200 my $username = $$args{username} || "";
201 my $password = $$args{password} || "";
203 my $editor = new_editor(authtoken => $auth);
204 return $editor->event unless $editor->checkauth;
205 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
207 my $connection = OpenILS::Utils::ZClient->new(
211 password => $password,
213 preferredRecordSyntax => $output,
216 if( ! $connection ) {
217 $logger->error("z3950: Unable to connect to Z server: ".
218 "$host:$port:$db:$username:$password");
219 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
226 $logger->info("z3950: query => $query");
229 $results = $connection->search_pqf( $query );
230 } catch Error with { $err = shift; };
232 return OpenILS::Event->new(
233 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
235 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
236 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
238 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
240 return {result => $results, connection => $connection} if ($async);
242 my $munged = process_results($results, $limit, $offset, $$args{service});
243 $munged->{query} = $query;
249 # -------------------------------------------------------------------
250 # Takes a result batch and returns the hitcount and a list of xml
252 # -------------------------------------------------------------------
253 sub process_results {
255 my $limit = shift || 10;
256 my $offset = shift || 0;
259 my $rformat = $services{$service}->{record_format} || 'FI';
260 $results->option(elementSetName => $rformat);
261 $logger->info("z3950: using record format '$rformat'");
265 my $count = $$res{count} = $results->size;
267 $logger->info("z3950: search returned $count hits");
269 my $tend = $limit + $offset;
271 my $end = ($tend <= $count) ? $tend : $count;
273 for($offset..$end - 1) {
281 $logger->info("z3950: fetching record $_");
285 my $rec = $results->record($_);
286 $marc = MARC::Record->new_from_usmarc($rec->raw());
287 $marcs = entityize($marc->as_xml_record);
288 my $doc = XML::LibXML->new->parse_string($marcs);
289 $marcxml = entityize( $doc->documentElement->toString );
291 my $u = OpenILS::Utils::ModsParser->new();
292 $u->start_mods_batch( $marcxml );
293 $mods = $u->finish_mods_batch();
296 } catch Error with { $err = shift; };
298 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
299 $logger->error("z3950: bad XML : $err") if $err;
302 warn "\n\n$marcs\n\n";
306 $res->{records} = \@records;
312 # -------------------------------------------------------------------
313 # Compiles the class based search query
314 # -------------------------------------------------------------------
317 my $seperator = shift;
321 my $count = scalar(keys %$hash);
324 $str .= "\@$seperator " for (1..$count-1);
326 # -------------------------------------------------------------------
327 # "code" is the bib-1 "use attribute", "format" is the bib-1
328 # "structure attribute"
329 # -------------------------------------------------------------------
331 next unless ( exists $services{$service}->{attrs}->{$_} );
332 $str .= '@attr 1=' . $services{$service}->{attrs}->{$_}->{code} . # add the use attribute
333 ' @attr 4=' . $services{$service}->{attrs}->{$_}->{format}; # add the structure attribute
334 if (exists $services{$service}->{attrs}->{$_}->{truncation}){
335 $str .= ' @attr 5=' . $services{$service}->{attrs}->{$_}->{truncation};
337 $str .= " \"" . $$hash{$_} . "\" "; # add the search term
344 # -------------------------------------------------------------------
345 # Handles the unicode
346 # -------------------------------------------------------------------
349 my $form = shift || "";
352 $stuff = NFD($stuff);
354 $stuff = NFC($stuff);
357 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
359 # strip some other unfriendly chars that may leak in
360 $stuff =~ s/([\x{0000}-\x{0008}])//sgoe;