]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Z3950.pm
add support for xml-based z servers (such as xml-backed zebra without yaz-proxy)
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Z3950.pm
1 package OpenILS::Application::Search::Z3950;
2 use strict; use warnings;
3 use base qw/OpenILS::Application/;
4
5 use OpenILS::Utils::ZClient;
6 use MARC::Record;
7 use MARC::File::XML;
8 use Unicode::Normalize;
9 use XML::LibXML;
10 use Data::Dumper;
11
12 use OpenILS::Event;
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/;
19
20 my $output      = "USMARC"; 
21
22 my $sclient;
23 my %services;
24 my $default_service;
25
26
27 __PACKAGE__->register_method(
28         method          => 'do_class_search',
29         api_name                => 'open-ils.search.z3950.search_class',
30         stream          => 1,
31         signature       => q/
32                 Performs a class based Z search.  The classes available
33                 are defined by the 'attr' fields in the config for the
34                 requested service.
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
40         /
41 );
42
43 __PACKAGE__->register_method(
44         method          => 'do_service_search',
45         api_name                => 'open-ils.search.z3950.search_service',
46         signature       => q/
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
52         /
53 );
54
55
56 __PACKAGE__->register_method(
57         method          => 'do_service_search',
58         api_name                => 'open-ils.search.z3950.search_raw',
59         signature       => q/
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
64         /
65 );
66
67
68 __PACKAGE__->register_method(
69         method  => "query_services",
70         api_name        => "open-ils.search.z3950.retrieve_services",
71         signature       => q/
72                 Returns a list of service names that we have config
73                 data for
74         /
75 );
76
77
78
79 # -------------------------------------------------------------------
80 # What services do we have config info for?
81 # -------------------------------------------------------------------
82 sub query_services {
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');
88 }
89
90
91
92 # -------------------------------------------------------------------
93 # Load the pre-defined Z server configs
94 # -------------------------------------------------------------------
95 sub initialize {
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;
100 }
101
102
103 # -------------------------------------------------------------------
104 # High-level class based search. 
105 # -------------------------------------------------------------------
106 sub do_class_search {
107
108         my $self                        = shift;
109         my $conn                        = shift;
110         my $auth                        = shift;
111         my $args                        = shift;
112
113         if (!ref($$args{service})) {
114                 $$args{service} = [$$args{service}];
115                 $$args{username} = [$$args{username}];
116                 $$args{password} = [$$args{password}];
117         }
118
119         $$args{async} = 1;
120
121         my @connections;
122         my @results;
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];
128
129                 $logger->debug("z3950: service: $tmp_args{service}, async: $tmp_args{async}");
130
131                 $tmp_args{query} = compile_query('and', $tmp_args{service}, $tmp_args{search});
132
133                 my $res = do_service_search( $self, $conn, $auth, \%tmp_args );
134
135                 push @results, $res->{result};
136                 push @connections, $res->{connection};
137
138                 $logger->debug("z3950: Result object: $results[$i], Connection object: $connections[$i]");
139         }
140
141         $logger->debug("z3950: Connections created");
142
143         my @records;
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);
151                 }
152         }
153
154         $logger->debug("z3950: Search Complete");
155     return undef;
156 }
157
158
159 # -------------------------------------------------------------------
160 # This handles the host settings, but expects a fully formed z query
161 # -------------------------------------------------------------------
162 sub do_service_search {
163
164         my $self                        = shift;
165         my $conn                        = shift;
166         my $auth                        = shift;
167         my $args                        = shift;
168         
169         my $info = $services{$$args{service}};
170
171         $$args{host}    = $$info{host};
172         $$args{port}    = $$info{port};
173         $$args{db}              = $$info{db};
174
175         return do_search( $self, $conn, $auth, $args );
176 }
177
178
179
180 # -------------------------------------------------------------------
181 # This is the low level search method.  All config and query
182 # data must be provided to this method
183 # -------------------------------------------------------------------
184 sub do_search {
185
186         my $self        = shift;
187         my $conn        = shift;
188         my $auth = shift;
189         my $args = shift;
190
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;
196
197         my $limit       = $$args{limit} || 10;
198         my $offset      = $$args{offset} || 0;
199
200         my $username = $$args{username} || "";
201         my $password = $$args{password} || "";
202
203     my $tformat = $services{$service}->{transmission_format} || $output;
204
205         my $editor = new_editor(authtoken => $auth);
206         return $editor->event unless $editor->checkauth;
207         return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
208
209         my $connection = OpenILS::Utils::ZClient->new(
210                 $host, $port,
211                 databaseName                            => $db, 
212                 user                                                    => $username,
213                 password                                                => $password,
214                 async                                                   => $async,
215                 preferredRecordSyntax   => $tformat, 
216         );
217
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;
222         }
223
224         my $start = time;
225         my $results;
226         my $err;
227
228         $logger->info("z3950: query => $query");
229
230         try {
231                 $results = $connection->search_pqf( $query );
232         } catch Error with { $err = shift; };
233
234         return OpenILS::Event->new(
235                 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
236
237         return OpenILS::Event->new('Z3950_SEARCH_FAILED', 
238                 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
239
240         $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
241
242         return {result => $results, connection => $connection} if ($async);
243
244         my $munged = process_results($results, $limit, $offset, $$args{service});
245         $munged->{query} = $query;
246
247         return $munged;
248 }
249
250
251 # -------------------------------------------------------------------
252 # Takes a result batch and returns the hitcount and a list of xml
253 # and mvr objects
254 # -------------------------------------------------------------------
255 sub process_results {
256         my $results     = shift;
257         my $limit       = shift || 10;
258         my $offset      = shift || 0;
259     my $service = shift;
260
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'");
265
266         my @records;
267         my $res = {};
268         my $count = $$res{count} = $results->size;
269
270         $logger->info("z3950: search returned $count hits");
271
272         my $tend = $limit + $offset;
273
274         my $end = ($tend <= $count) ? $tend : $count;
275
276         for($offset..$end - 1) {
277
278                 my $err;
279                 my $mods;
280                 my $marc;
281                 my $marcs;
282                 my $marcxml;
283
284                 $logger->info("z3950: fetching record $_");
285
286                 try {
287
288                         my $rec = $results->record($_);
289
290             if ($tformat eq 'usmarc') {
291                         $marc           = MARC::Record->new_from_usmarc($rec->raw());
292             } else if ($tformat eq 'xml') {
293                         $marc           = MARC::Record->new_from_xml($rec->raw());
294             } else {
295                 die "Unsupported record transmission format $tformat"
296             }
297
298                         $marcs  = entityize($marc->as_xml_record);
299                         my $doc = XML::LibXML->new->parse_string($marcs);
300                         $marcxml = entityize( $doc->documentElement->toString );
301         
302                         my $u = OpenILS::Utils::ModsParser->new();
303                         $u->start_mods_batch( $marcxml );
304                         $mods = $u->finish_mods_batch();
305         
306
307                 } catch Error with { $err = shift; };
308
309                 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
310                 $logger->error("z3950: bad XML : $err") if $err;
311
312                 if( $err ) {
313                         warn "\n\n$marcs\n\n";
314                 }
315         }
316         
317         $res->{records} = \@records;
318         return $res;
319 }
320
321
322
323 # -------------------------------------------------------------------
324 # Compiles the class based search query
325 # -------------------------------------------------------------------
326 sub compile_query {
327
328         my $seperator   = shift;
329         my $service             = shift;
330         my $hash                        = shift;
331
332         my $count = scalar(keys %$hash);
333
334         my $str = "";
335         $str .= "\@$seperator " for (1..$count-1);
336         
337     # -------------------------------------------------------------------
338     # "code" is the bib-1 "use attribute", "format" is the bib-1 
339     # "structure attribute"
340     # -------------------------------------------------------------------
341         for( keys %$hash ) {
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};
347                 }
348                 $str .= " \"" . $$hash{$_} . "\" "; # add the search term
349         }
350         return $str;
351 }
352
353
354
355 # -------------------------------------------------------------------
356 # Handles the unicode
357 # -------------------------------------------------------------------
358 sub entityize {
359         my $stuff = shift;
360         my $form = shift || "";
361         
362         if ($form eq 'D') {
363                 $stuff = NFD($stuff);
364         } else {
365                 $stuff = NFC($stuff);
366         }
367         
368         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
369
370         # strip some other unfriendly chars that may leak in
371    $stuff =~ s/([\x{0000}-\x{0008}])//sgoe; 
372
373         return $stuff;
374 }
375
376
377 1;