]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Z3950.pm
MORE errors caught by strict
[working/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/OpenSRF::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         signature       => q/
31                 Performs a class based Z search.  The classes available
32                 are defined by the 'attr' fields in the config for the
33                 requested service.
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
39         /
40 );
41
42 __PACKAGE__->register_method(
43         method          => 'do_service_search',
44         api_name                => 'open-ils.search.z3950.search_service',
45         signature       => q/
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
51         /
52 );
53
54
55 __PACKAGE__->register_method(
56         method          => 'do_service_search',
57         api_name                => 'open-ils.search.z3950.search_raw',
58         signature       => q/
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
63         /
64 );
65
66
67 __PACKAGE__->register_method(
68         method  => "query_services",
69         api_name        => "open-ils.search.z3950.retrieve_services",
70         signature       => q/
71                 Returns a list of service names that we have config
72                 data for
73         /
74 );
75
76
77
78 # -------------------------------------------------------------------
79 # What services do we have config info for?
80 # -------------------------------------------------------------------
81 sub query_services {
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');
87 }
88
89
90
91 # -------------------------------------------------------------------
92 # Load the pre-defined Z server configs
93 # -------------------------------------------------------------------
94 sub initialize {
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;
99 }
100
101
102 # -------------------------------------------------------------------
103 # High-level class based search. 
104 # -------------------------------------------------------------------
105 sub do_class_search {
106
107         my $self                        = shift;
108         my $conn                        = shift;
109         my $auth                        = shift;
110         my $args                        = shift;
111
112         if (ref($$args{service}) =~ /ARRAY/o) {
113                 $$args{service} = [$$args{service}];
114                 $$args{username} = [$$args{username}];
115                 $$args{password} = [$$args{password}];
116         }
117
118         $$args{async} = 1;
119
120         $$args{query} = 
121                 compile_query('and', $$args{service}, $$args{search});
122
123         my @results;
124         for (my $i = 0; $i < @{$$args{service}}; $i++) {
125                 my %tmp_args = %$args;
126                 $tmp_args{service} = $$args{service}[$i];
127                 $tmp_args{username} = $$args{username}[$i];
128                 $tmp_args{password} = $$args{password}[$i];
129                 $results[$i] = $self->do_service_search( $conn, $auth, \%tmp_args );
130         }
131
132         my @records;
133         while ((my $index = OpenILS::Utils::ZClient::event( \@results )) != 0) {
134                 my $ev = $results[$index - 1]->last_event();
135                 if ($ev == OpenILS::Utils::ZClient::Event::END) {
136                         my $munged = process_results( $results[$index - 1], ($$args{limit} || 10), ($$args{offset} || 0) );
137                         $$munged{service} = $$args{service}[$index];
138                         $conn->respond($munged);
139                 }
140         }
141 }
142
143
144 # -------------------------------------------------------------------
145 # This handles the host settings, but expects a fully formed z query
146 # -------------------------------------------------------------------
147 sub do_service_search {
148
149         my $self                        = shift;
150         my $conn                        = shift;
151         my $auth                        = shift;
152         my $args                        = shift;
153         
154         my $info = $services{$$args{service}};
155
156         $$args{host}    = $$info{host},
157         $$args{port}    = $$info{port},
158         $$args{db}              = $$info{db},
159
160         return $self->do_search( $conn, $auth, $args );
161 }
162
163
164
165 # -------------------------------------------------------------------
166 # This is the low level search method.  All config and query
167 # data must be provided to this method
168 # -------------------------------------------------------------------
169 sub do_search {
170
171         my $self        = shift;
172         my $conn        = shift;
173         my $auth = shift;
174         my $args = shift;
175
176         my $host                = $$args{host} or return undef;
177         my $port                = $$args{port} or return undef;
178         my $db          = $$args{db}    or return undef;
179         my $query       = $$args{query} or return undef;
180         my $async       = $$args{async} || 0;
181
182         my $limit       = $$args{limit} || 10;
183         my $offset      = $$args{offset} || 0;
184
185         my $username = $$args{username} || "";
186         my $password = $$args{password} || "";
187
188         my $editor = new_editor(authtoken => $auth);
189         return $editor->event unless $editor->checkauth;
190         return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
191
192         my $connection = OpenILS::Utils::ZClient->new(
193                 $host, $port,
194                 databaseName                            => $db, 
195                 user                                                    => $username,
196                 async                                                   => $async,
197                 password                                                => $password,
198                 preferredRecordSyntax   => $output, 
199         );
200
201         if( ! $connection ) {
202                 $logger->error("z3950: Unable to connect to Z server: ".
203                         "$host:$port:$db:$username:$password");
204                 return OpenILS::Event->new('Z3950_LOGIN_FAILED') unless $connection;
205         }
206
207         my $start = time;
208         my $results;
209         my $err;
210
211         $logger->info("z3950: query => $query");
212
213         try {
214                 $results = $connection->search_pqf( $query );
215         } catch Error with { $err = shift; };
216
217         return OpenILS::Event->new(
218                 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
219
220         return OpenILS::Event->new('Z3950_SEARCH_FAILED', 
221                 debug => $connection->errcode." => ".$connection->errmsg." : query = $query") unless $results;
222
223         $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
224
225         return $results if ($async);
226
227         my $munged = process_results($results, $limit, $offset);
228         $munged->{query} = $query;
229
230         return $munged;
231 }
232
233
234 # -------------------------------------------------------------------
235 # Takes a result batch and returns the hitcount and a list of xml
236 # and mvr objects
237 # -------------------------------------------------------------------
238 sub process_results {
239         my $results     = shift;
240         my $limit       = shift;
241         my $offset      = shift;
242
243         $results->option(elementSetName => "FI"); # full records with no holdings
244
245         my @records;
246         my $res = {};
247         my $count = $$res{count} = $results->size;
248
249         $logger->info("z3950: search returned $count hits");
250
251         my $tend = $limit + $offset;
252
253         my $end = ($tend <= $count) ? $tend : $count;
254
255         for($offset..$end - 1) {
256
257                 my $err;
258                 my $mods;
259                 my $marc;
260                 my $marcs;
261                 my $marcxml;
262
263                 $logger->info("z3950: fetching record $_");
264
265                 try {
266
267                         my $rec = $results->record($_);
268                         $marc           = MARC::Record->new_from_usmarc($rec->raw());
269                         $marcs  = entityize($marc->as_xml_record);
270                         my $doc = XML::LibXML->new->parse_string($marcs);
271                         $marcxml = entityize( $doc->documentElement->toString );
272         
273                         my $u = OpenILS::Utils::ModsParser->new();
274                         $u->start_mods_batch( $marcxml );
275                         $mods = $u->finish_mods_batch();
276         
277
278                 } catch Error with { $err = shift; };
279
280                 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
281                 $logger->error("z3950: bad XML : $err") if $err;
282
283                 if( $err ) {
284                         warn "\n\n$marcs\n\n";
285                 }
286         }
287         
288         $res->{records} = \@records;
289         return $res;
290 }
291
292
293
294 # -------------------------------------------------------------------
295 # Compiles the class based search query
296 # -------------------------------------------------------------------
297 sub compile_query {
298
299         my $seperator   = shift;
300         my $service             = shift;
301         my $hash                        = shift;
302
303         my $count = scalar(keys %$hash);
304
305         my $str = "";
306         $str .= "\@$seperator " for (1..$count-1);
307         
308     # -------------------------------------------------------------------
309     # "code" is the bib-1 "use attribute", "format" is the bib-1 
310     # "structure attribute"
311     # -------------------------------------------------------------------
312         for( keys %$hash ) {
313 #               $str .= '@attr ' .
314 #                       $services{$service}->{attrs}->{$_}->{format} . '=' .
315 #                       $services{$service}->{attrs}->{$_}->{code} . " \"" . $$hash{$_} . "\" ";                
316         $str .= 
317             '@attr 1=' . $services{$service}->{attrs}->{$_}->{code} . # add the use attribute
318             ' @attr 4=' . $services{$service}->{attrs}->{$_}->{format} . # add the structure attribute
319             " \"" . $$hash{$_} . "\" "; # add the search term
320         }
321         return $str;
322 }
323
324
325
326 # -------------------------------------------------------------------
327 # Handles the unicode
328 # -------------------------------------------------------------------
329 sub entityize {
330         my $stuff = shift;
331         my $form = shift || "";
332         
333         if ($form eq 'D') {
334                 $stuff = NFD($stuff);
335         } else {
336                 $stuff = NFC($stuff);
337         }
338         
339         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
340
341         # strip some other unfriendly chars that may leak in
342    $stuff =~ s/([\x{0000}-\x{0008}])//sgoe; 
343
344         return $stuff;
345 }
346
347
348 1;