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 my $services = $sclient->config_value('z3950', 'services');
88 $services = { $services } unless ref($services);
89 return [ keys %$services ];
94 # -------------------------------------------------------------------
95 # Load the pre-defined Z server configs
96 # -------------------------------------------------------------------
98 $sclient = OpenSRF::Utils::SettingsClient->new();
99 $default_service = $sclient->config_value("z3950", "default" );
100 my $servs = $sclient->config_value("z3950", "services" );
101 $services{$_} = $$servs{$_} for keys %$servs;
105 # -------------------------------------------------------------------
106 # High-level class based search.
107 # -------------------------------------------------------------------
108 sub do_class_search {
116 compile_query('and', $$args{service}, $$args{search});
118 return $self->do_service_search( $conn, $auth, $args );
122 # -------------------------------------------------------------------
123 # This handles the host settings, but expects a fully formed z query
124 # -------------------------------------------------------------------
125 sub do_service_search {
132 my $info = $services{$$args{service}};
134 $$args{host} = $$info{host},
135 $$args{port} = $$info{port},
136 $$args{db} = $$info{db},
138 return $self->do_search( $conn, $auth, $args );
143 # -------------------------------------------------------------------
144 # This is the low level search method. All config and query
145 # data must be provided to this method
146 # -------------------------------------------------------------------
154 my $host = $$args{host} or return undef;
155 my $port = $$args{port} or return undef;
156 my $db = $$args{db} or return undef;
157 my $query = $$args{query} or return undef;
159 my $limit = $$args{limit} || 10;
160 my $offset = $$args{offset} || 0;
162 my $username = $$args{username} || "";
163 my $password = $$args{password} || "";
165 my $editor = new_editor(authtoken => $auth);
166 return $editor->event unless $editor->checkauth;
167 return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
169 my $connection = new Net::Z3950::Connection(
173 password => $password,
174 preferredRecordSyntax => $output,
177 if( ! $connection ) {
178 $logger->error("z3950: Unable to connect to Z server: ".
179 "$host:$port:$db:$username:$password");
180 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
184 my $results = $connection->search( $query );
185 $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
187 return OpenILS::Event->new('Z3950_SEARCH_FAILED') unless $results;
189 return process_results($results, $limit, $offset);
193 # -------------------------------------------------------------------
194 # Takes a result batch and returns the hitcount and a list of xml
196 # -------------------------------------------------------------------
197 sub process_results {
202 $results->option(elementSetName => "FI"); # full records with no holdings
206 my $count = $$res{count} = $results->size;
208 $logger->info("z3950: search returned $count hits");
210 my $tend = $limit + $offset;
211 $offset++; # records start at 1
213 my $end = ($tend <= $count) ? $tend : $count;
221 $logger->info("z3950: fetching record $_");
225 my $rec = $results->record($_);
226 my $marc = MARC::Record->new_from_usmarc($rec->rawdata());
227 my $doc = XML::LibXML->new->parse_string($marc->as_xml_record);
228 $marcxml = entityize( $doc->documentElement->toString );
230 my $u = OpenILS::Utils::ModsParser->new();
231 $u->start_mods_batch( $marcxml );
232 $mods = $u->finish_mods_batch();
235 } catch Error with { $err = shift; };
237 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
238 $logger->error("z3950: bad XML : $err") if $err;
241 $res->{records} = \@records;
247 # -------------------------------------------------------------------
248 # Compiles the class based search query
249 # -------------------------------------------------------------------
252 my $seperator = shift;
256 my $count = scalar(keys %$hash);
259 $str .= "\@$seperator " for (1..$count-1);
262 $str .= '@attr 1=' . $services{$service}->{attrs}->{$_} . " \"" . $$hash{$_} . "\" ";
269 # -------------------------------------------------------------------
270 # Handles the unicode
271 # -------------------------------------------------------------------
274 my $form = shift || "";
277 $stuff = NFD($stuff);
279 $stuff = NFC($stuff);
282 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;