1 package OpenILS::Application::Search::Z3950;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
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 {
113 compile_query('and', $$args{service}, $$args{search});
115 return $self->do_service_search( $conn, $auth, $args );
119 # -------------------------------------------------------------------
120 # This handles the host settings, but expects a fully formed z query
121 # -------------------------------------------------------------------
122 sub do_service_search {
129 my $info = $services{$$args{service}};
131 $$args{host} = $$info{host},
132 $$args{port} = $$info{port},
133 $$args{db} = $$info{db},
135 return $self->do_search( $conn, $auth, $args );
140 # -------------------------------------------------------------------
141 # This is the low level search method. All config and query
142 # data must be provided to this method
143 # -------------------------------------------------------------------
151 my $host = $$args{host} or return undef;
152 my $port = $$args{port} or return undef;
153 my $db = $$args{db} or return undef;
154 my $query = $$args{query} or return undef;
156 my $limit = $$args{limit} || 10;
157 my $offset = $$args{offset} || 0;
159 my $username = $$args{username} || "";
160 my $password = $$args{password} || "";
162 my $editor = new_editor(authtoken => $auth);
163 return $editor->event unless $editor->checkauth;
164 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
166 my $connection = new Net::Z3950::Connection(
170 password => $password,
171 preferredRecordSyntax => $output,
174 if( ! $connection ) {
175 $logger->error("z3950: Unable to connect to Z server: ".
176 "$host:$port:$db:$username:$password");
177 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
184 $logger->info("z3950: query => $query");
187 $results = $connection->search( $query );
188 } catch Error with { $err = shift; };
190 return OpenILS::Event->new(
191 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
193 return OpenILS::Event->new('Z3950_SEARCH_FAILED',
194 debug => $connection->errcode.":".$connection->errmsg) unless $results;
196 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
198 my $munged = process_results($results, $limit, $offset);
199 $munged->{query} = $query;
205 # -------------------------------------------------------------------
206 # Takes a result batch and returns the hitcount and a list of xml
208 # -------------------------------------------------------------------
209 sub process_results {
214 $results->option(elementSetName => "FI"); # full records with no holdings
218 my $count = $$res{count} = $results->size;
220 $logger->info("z3950: search returned $count hits");
222 my $tend = $limit + $offset;
223 $offset++; # records start at 1
225 my $end = ($tend <= $count) ? $tend : $count;
235 $logger->info("z3950: fetching record $_");
239 my $rec = $results->record($_);
240 $marc = MARC::Record->new_from_usmarc($rec->rawdata());
241 $marcs = entityize($marc->as_xml_record);
242 my $doc = XML::LibXML->new->parse_string($marcs);
243 $marcxml = entityize( $doc->documentElement->toString );
245 my $u = OpenILS::Utils::ModsParser->new();
246 $u->start_mods_batch( $marcxml );
247 $mods = $u->finish_mods_batch();
250 } catch Error with { $err = shift; };
252 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
253 $logger->error("z3950: bad XML : $err") if $err;
256 warn "\n\n$marcs\n\n";
260 $res->{records} = \@records;
266 # -------------------------------------------------------------------
267 # Compiles the class based search query
268 # -------------------------------------------------------------------
271 my $seperator = shift;
275 my $count = scalar(keys %$hash);
278 $str .= "\@$seperator " for (1..$count-1);
282 $services{$service}->{attrs}->{$_}->{format} . '=' .
283 $services{$service}->{attrs}->{$_}->{code} . " \"" . $$hash{$_} . "\" ";
290 # -------------------------------------------------------------------
291 # Handles the unicode
292 # -------------------------------------------------------------------
295 my $form = shift || "";
298 $stuff = NFD($stuff);
300 $stuff = NFC($stuff);
303 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
305 # strip some other unfriendly chars that may leak in
306 $stuff =~ s/([\x{0000}-\x{0008}])//sgoe;