2 package OpenILS::Application::Search::Z3950;
3 use strict; use warnings;
4 use base qw/OpenSRF::Application/;
9 use Unicode::Normalize;
14 use OpenSRF::EX qw(:try);
15 use OpenILS::Utils::ModsParser;
16 use OpenSRF::Utils::SettingsClient;
17 use OpenILS::Application::AppUtils;
18 use OpenSRF::Utils::Logger qw/$logger/;
19 use OpenILS::Utils::Editor q/:funcs/;
21 my $output = "USMARC";
28 __PACKAGE__->register_method(
29 method => 'do_class_search',
30 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 {
114 compile_query('and', $$args{service}, $$args{search});
116 return $self->do_service_search( $conn, $auth, $args );
120 # -------------------------------------------------------------------
121 # This handles the host settings, but expects a fully formed z query
122 # -------------------------------------------------------------------
123 sub do_service_search {
130 my $info = $services{$$args{service}};
132 $$args{host} = $$info{host},
133 $$args{port} = $$info{port},
134 $$args{db} = $$info{db},
136 return $self->do_search( $conn, $auth, $args );
141 # -------------------------------------------------------------------
142 # This is the low level search method. All config and query
143 # data must be provided to this method
144 # -------------------------------------------------------------------
152 my $host = $$args{host} or return undef;
153 my $port = $$args{port} or return undef;
154 my $db = $$args{db} or return undef;
155 my $query = $$args{query} or return undef;
157 my $limit = $$args{limit} || 10;
158 my $offset = $$args{offset} || 0;
160 my $username = $$args{username} || "";
161 my $password = $$args{password} || "";
163 my $editor = new_editor(authtoken => $auth);
164 return $editor->event unless $editor->checkauth;
165 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
167 my $connection = new Net::Z3950::Connection(
171 password => $password,
172 preferredRecordSyntax => $output,
175 if( ! $connection ) {
176 $logger->error("z3950: Unable to connect to Z server: ".
177 "$host:$port:$db:$username:$password");
178 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
185 $logger->info("z3950: query => $query");
188 $results = $connection->search( $query );
189 } catch Error with { $err = shift; };
191 return OpenILS::Event->new(
192 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
194 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
195 debug => $connection->errcode.":".$connection->errmsg) unless $results;
197 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
199 my $munged = process_results($results, $limit, $offset);
200 $munged->{query} = $query;
206 # -------------------------------------------------------------------
207 # Takes a result batch and returns the hitcount and a list of xml
209 # -------------------------------------------------------------------
210 sub process_results {
215 $results->option(elementSetName => "FI"); # full records with no holdings
219 my $count = $$res{count} = $results->size;
221 $logger->info("z3950: search returned $count hits");
223 my $tend = $limit + $offset;
224 $offset++; # records start at 1
226 my $end = ($tend <= $count) ? $tend : $count;
234 $logger->info("z3950: fetching record $_");
238 my $rec = $results->record($_);
239 my $marc = MARC::Record->new_from_usmarc($rec->rawdata());
240 my $doc = XML::LibXML->new->parse_string($marc->as_xml_record);
241 $marcxml = entityize( $doc->documentElement->toString );
243 my $u = OpenILS::Utils::ModsParser->new();
244 $u->start_mods_batch( $marcxml );
245 $mods = $u->finish_mods_batch();
248 } catch Error with { $err = shift; };
250 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
251 $logger->error("z3950: bad XML : $err") if $err;
254 $res->{records} = \@records;
260 # -------------------------------------------------------------------
261 # Compiles the class based search query
262 # -------------------------------------------------------------------
265 my $seperator = shift;
269 my $count = scalar(keys %$hash);
272 $str .= "\@$seperator " for (1..$count-1);
276 $services{$service}->{attrs}->{$_}->{format} . '=' .
277 $services{$service}->{attrs}->{$_}->{code} . " \"" . $$hash{$_} . "\" ";
284 # -------------------------------------------------------------------
285 # Handles the unicode
286 # -------------------------------------------------------------------
289 my $form = shift || "";
292 $stuff = NFD($stuff);
294 $stuff = NFC($stuff);
297 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;