]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Z3950.pm
added search formats to the z search, added lccn, item_type
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Z3950.pm
1 #!/usr/bin/perl
2 package OpenILS::Application::Search::Z3950;
3 use strict; use warnings;
4 use base qw/OpenSRF::Application/;
5
6 use Net::Z3950;
7 use MARC::Record;
8 use MARC::File::XML;
9 use Unicode::Normalize;
10 use XML::LibXML;
11 use Data::Dumper;
12
13 use OpenILS::Event;
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/;
20
21 my $output      = "USMARC"; 
22
23 my $sclient;
24 my %services;
25 my $default_service;
26
27
28 __PACKAGE__->register_method(
29         method          => 'do_class_search',
30         api_name                => 'open-ils.search.z3950.search_class',
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         $$args{query} = 
114                 compile_query('and', $$args{service}, $$args{search});
115
116         return $self->do_service_search( $conn, $auth, $args );
117 }
118
119
120 # -------------------------------------------------------------------
121 # This handles the host settings, but expects a fully formed z query
122 # -------------------------------------------------------------------
123 sub do_service_search {
124
125         my $self                        = shift;
126         my $conn                        = shift;
127         my $auth                        = shift;
128         my $args                        = shift;
129         
130         my $info = $services{$$args{service}};
131
132         $$args{host}    = $$info{host},
133         $$args{port}    = $$info{port},
134         $$args{db}              = $$info{db},
135
136         return $self->do_search( $conn, $auth, $args );
137 }
138
139
140
141 # -------------------------------------------------------------------
142 # This is the low level search method.  All config and query
143 # data must be provided to this method
144 # -------------------------------------------------------------------
145 sub do_search {
146
147         my $self        = shift;
148         my $conn        = shift;
149         my $auth = shift;
150         my $args = shift;
151
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;
156
157         my $limit       = $$args{limit} || 10;
158         my $offset      = $$args{offset} || 0;
159
160         my $username = $$args{username} || "";
161         my $password = $$args{password} || "";
162
163         my $editor = new_editor(authtoken => $auth);
164         return $editor->event unless $editor->checkauth;
165         return $editor->event unless $editor->allowed('REMOTE_Z3950_QUERY');
166
167         my $connection = new Net::Z3950::Connection(
168                 $host, $port,
169                 databaseName                            => $db, 
170                 user                                                    => $username,
171                 password                                                => $password,
172                 preferredRecordSyntax   => $output, 
173         );
174
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;
179         }
180
181         my $start = time;
182         my $results;
183         my $err;
184
185         $logger->info("z3950: query => $query");
186
187         try {
188                 $results = $connection->search( $query );
189         } catch Error with { $err = shift; };
190
191         return OpenILS::Event->new(
192                 'Z3950_BAD_QUERY', payload => $query, debug => "$err") if $err;
193
194         return OpenILS::Event->new('Z3950_SEARCH_FAILED', 
195                 debug => $connection->errcode.":".$connection->errmsg) unless $results;
196
197         $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
198
199         my $munged = process_results($results, $limit, $offset);
200         $munged->{query} = $query;
201
202         return $munged;
203 }
204
205
206 # -------------------------------------------------------------------
207 # Takes a result batch and returns the hitcount and a list of xml
208 # and mvr objects
209 # -------------------------------------------------------------------
210 sub process_results {
211         my $results     = shift;
212         my $limit       = shift;
213         my $offset      = shift;
214
215         $results->option(elementSetName => "FI"); # full records with no holdings
216
217         my @records;
218         my $res = {};
219         my $count = $$res{count} = $results->size;
220
221         $logger->info("z3950: search returned $count hits");
222
223         my $tend = $limit + $offset;
224         $offset++; # records start at 1
225
226         my $end = ($tend <= $count) ? $tend : $count;
227
228         for($offset..$end) {
229
230                 my $err;
231                 my $mods;
232                 my $marcxml;
233
234                 $logger->info("z3950: fetching record $_");
235
236                 try {
237
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 );
242         
243                         my $u = OpenILS::Utils::ModsParser->new();
244                         $u->start_mods_batch( $marcxml );
245                         $mods = $u->finish_mods_batch();
246         
247
248                 } catch Error with { $err = shift; };
249
250                 push @records, { 'mvr' => $mods, 'marcxml' => $marcxml } unless $err;
251                 $logger->error("z3950: bad XML : $err") if $err;
252         }
253         
254         $res->{records} = \@records;
255         return $res;
256 }
257
258
259
260 # -------------------------------------------------------------------
261 # Compiles the class based search query
262 # -------------------------------------------------------------------
263 sub compile_query {
264
265         my $seperator   = shift;
266         my $service             = shift;
267         my $hash                        = shift;
268
269         my $count = scalar(keys %$hash);
270
271         my $str = "";
272         $str .= "\@$seperator " for (1..$count-1);
273         
274         for( keys %$hash ) {
275                 $str .= '@attr ' .
276                         $services{$service}->{attrs}->{$_}->{format} . '=' .
277                         $services{$service}->{attrs}->{$_}->{code} . " \"" . $$hash{$_} . "\" ";                
278         }
279         return $str;
280 }
281
282
283
284 # -------------------------------------------------------------------
285 # Handles the unicode
286 # -------------------------------------------------------------------
287 sub entityize {
288         my $stuff = shift;
289         my $form = shift || "";
290         
291         if ($form eq 'D') {
292                 $stuff = NFD($stuff);
293         } else {
294                 $stuff = NFC($stuff);
295         }
296         
297         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
298         return $stuff;
299 }
300
301
302 1;