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 $tformat = $services{$args->{service}}->{transmission_format} || $output;
205 my $editor = new_editor(authtoken => $auth);
206 return $editor->event unless $editor->checkauth;
207 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
209 my $connection = OpenILS::Utils::ZClient->new(
213 password => $password,
215 preferredRecordSyntax => $tformat,
218 if( ! $connection ) {
219 $logger->error("z3950: Unable to connect to Z server: ".
220 "$host:$port:$db:$username:$password");
221 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
228 $logger->info("z3950: query => $query");
231 $results = $connection->search_pqf( $query );
232 } catch Error with { $err = shift; };
234 return OpenILS::Event->new(
235 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
237 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
238 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
240 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
242 return {result => $results, connection => $connection} if ($async);
244 my $munged = process_results($results, $limit, $offset, $$args{service});
245 $munged->{query} = $query;
251 # -------------------------------------------------------------------
252 # Takes a result batch and returns the hitcount and a list of xml
254 # -------------------------------------------------------------------
255 sub process_results {
257 my $limit = shift || 10;
258 my $offset = shift || 0;
261 my $tformat = $services{$service}->{transmission_format} || $output;
262 my $rformat = $services{$service}->{record_format} || 'FI';
263 $results->option(elementSetName => $rformat);
264 $logger->info("z3950: using record format '$rformat'");
268 my $count = $$res{count} = $results->size;
270 $logger->info("z3950: search returned $count hits");
272 my $tend = $limit + $offset;
274 my $end = ($tend <= $count) ? $tend : $count;
276 for($offset..$end - 1) {
284 $logger->info("z3950: fetching record $_");
288 my $rec = $results->record($_);
290 if ($tformat eq 'usmarc') {
291 $marc = MARC::Record->new_from_usmarc($rec->raw());
292 } elsif ($tformat eq 'xml') {
293 $marc = MARC::Record->new_from_xml($rec->raw());
295 die "Unsupported record transmission format $tformat"
298 $marcs = entityize($marc->as_xml_record);
299 my $doc = XML::LibXML->new->parse_string($marcs);
300 $marcxml = entityize( $doc->documentElement->toString );
302 my $u = OpenILS::Utils::ModsParser->new();
303 $u->start_mods_batch( $marcxml );
304 $mods = $u->finish_mods_batch();
307 } catch Error with { $err = shift; };
309 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
310 $logger->error("z3950: bad XML : $err") if $err;
313 warn "\n\n$marcs\n\n";
317 $res->{records} = \@records;
323 # -------------------------------------------------------------------
324 # Compiles the class based search query
325 # -------------------------------------------------------------------
328 my $seperator = shift;
332 my $count = scalar(keys %$hash);
335 $str .= "\@$seperator " for (1..$count-1);
337 # -------------------------------------------------------------------
338 # "code" is the bib-1 "use attribute", "format" is the bib-1
339 # "structure attribute"
340 # -------------------------------------------------------------------
342 next unless ( exists $services{$service}->{attrs}->{$_} );
343 $str .= '@attr 1=' . $services{$service}->{attrs}->{$_}->{code} . # add the use attribute
344 ' @attr 4=' . $services{$service}->{attrs}->{$_}->{format}; # add the structure attribute
345 if (exists $services{$service}->{attrs}->{$_}->{truncation}){
346 $str .= ' @attr 5=' . $services{$service}->{attrs}->{$_}->{truncation};
348 $str .= " \"" . $$hash{$_} . "\" "; # add the search term
355 # -------------------------------------------------------------------
356 # Handles the unicode
357 # -------------------------------------------------------------------
360 my $form = shift || "";
363 $stuff = NFD($stuff);
365 $stuff = NFC($stuff);
368 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
370 # strip some other unfriendly chars that may leak in
371 $stuff =~ s/([\x{0000}-\x{0008}])//sgoe;