]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Authority.pm
a20dfb166d6f04c6fa2b41fee09d11b7ce3473d6
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Search / Authority.pm
1 package OpenILS::Application::Search::Authority;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
4
5 use OpenILS::Utils::Fieldmapper;
6 use OpenILS::Application::AppUtils;
7 use XML::LibXML;
8 use XML::LibXSLT;
9 use OpenILS::Utils::CStoreEditor q/:funcs/;
10 use OpenSRF::Utils::Logger qw/$logger/;
11
12 use OpenSRF::Utils::JSON;
13
14 use Time::HiRes qw(time);
15 use OpenSRF::EX qw(:try);
16 use Digest::MD5 qw(md5_hex);
17
18 my $cache;
19
20
21 sub validate_authority {
22     my $self = shift;
23     my $client = shift;
24
25     my $session = OpenSRF::AppSession->create("open-ils.storage");
26     return $session->request( 'open-ils.storage.authority.validate.tag' => @_ )->gather(1);
27 }
28 __PACKAGE__->register_method(
29         method      => "validate_authority",
30         api_name    => "open-ils.search.authority.validate.tag",
31         argc        => 4, 
32         note        => "Validates authority data from existing controlled terms",
33 );              
34
35 sub validate_authority_return_records_by_id {
36     my $self = shift;
37     my $client = shift;
38
39     my $session = OpenSRF::AppSession->create("open-ils.storage");
40     return $session->request( 'open-ils.storage.authority.validate.tag.id_list' => @_ )->gather(1);
41 }
42 __PACKAGE__->register_method(
43         method      => "validate_authority_return_records_by_id",
44         api_name    => "open-ils.search.authority.validate.tag.id_list",
45         argc        => 4, 
46         note        => "Validates authority data from existing controlled terms",
47 );              
48
49 sub search_authority {
50     my $self = shift;
51     my $client = shift;
52
53     my $session = OpenSRF::AppSession->create("open-ils.storage");
54     return $session->request( 'open-ils.storage.authority.search.marc.atomic' => @_ )->gather(1);
55 }
56 __PACKAGE__->register_method(
57         method      => "search_authority",
58         api_name    => "open-ils.search.authority.fts",
59         argc        => 2, 
60         note        => "Searches authority data for existing controlled terms and crossrefs",
61 );              
62
63 sub search_authority_by_simple_normalize_heading {
64     my $self = shift;
65     my $client = shift;
66     my $marcxml = shift;
67     my $controlset = shift;
68
69     my $norm_heading_query = {
70         from => [ 'authority.simple_normalize_heading' => $marcxml ]
71     };
72
73     my $e = new_editor();
74     my $norm_heading = $e->json_query($norm_heading_query)->[0]->{'authority.simple_normalize_heading'};
75
76     my $query = {
77         select => { are => ['id'] },
78         from   => 'are',
79         where  => {
80             deleted => 'f',
81             simple_heading => {
82                 'startwith' => $norm_heading
83             },
84             defined($controlset) ? ( control_set => $controlset ) : ()
85         }
86     };
87
88     $client->respond($_->{id}) for @{ $e->json_query( $query ) };
89     $client->respond_complete;
90 }
91 __PACKAGE__->register_method(
92         method      => "search_authority_by_simple_normalize_heading",
93         api_name    => "open-ils.search.authority.simple_heading.from_xml",
94         argc        => 1, 
95         stream      => 1,
96         note        => "Searches authority data by main entry using marcxml, returning 'are' ids; params are marcxml and optional control-set-id",
97 );
98
99 sub search_authority_batch_by_simple_normalize_heading {
100     my $self = shift;
101     my $client = shift;
102     my $search_set = [@_];
103
104     my $m = $self->method_lookup('open-ils.search.authority.simple_heading.from_xml.atomic');
105
106     for my $s ( @$search_set ) {
107         for my $k ( keys %$s ) {
108             $client->respond( { $k => $m->run( $s->{$k}, $k ) } );
109         }
110     }
111
112     $client->respond_complete;
113 }
114 __PACKAGE__->register_method(
115         method      => "search_authority_batch_by_simple_normalize_heading",
116         api_name    => "open-ils.search.authority.simple_heading.from_xml.batch",
117         argc        => 1, 
118         stream      => 1,
119         note        => "Searches authority data by main entry using marcxml, in control-set batches, returning 'are' ids; params are hashes of { control-set-id => marcxml }",
120 );
121
122
123 sub crossref_authority {
124     my $self = shift;
125     my $client = shift;
126     my $class = shift;
127     my $term = shift;
128     my $limit = shift || 10;
129
130     my $session = OpenSRF::AppSession->create("open-ils.storage");
131
132     # Avoid generating spurious errors for more granular indexes, like author|personal
133     $class =~ s/^(.*?)\|.*?$/$1/;
134
135     $logger->info("authority xref search for $class=$term, limit=$limit");
136     my $fr = $session->request(
137         "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit)->gather(1);
138     my $al = $session->request(
139         "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit)->gather(1);
140
141     my $data = _auth_flatten( $term, $fr, $al, 1 );
142
143     return $data;
144 }
145
146 sub _auth_flatten {
147     my $term = shift;
148     my $fr = shift;
149     my $al = shift;
150     my $limit = shift;
151
152     my %hash = ();
153     for my $x (@$fr) {
154         my $string = $$x[0];
155         for my $i (1..10) {
156             last unless ($$x[$i]);
157             if ($string =~ /\W$/o) {
158                 $string .= ' '.$$x[$i];
159             } else {
160                 $string .= ' -- '.$$x[$i];
161             }
162         }
163         next if (lc($string) eq lc($term));
164         $hash{$string}++;
165         $hash{$string}++ if (lc($$x[0]) eq lc($term));
166     }
167     my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
168
169 #   $from = [ @$from[0..4] ] if $limit;
170
171     %hash = ();
172     for my $x (@$al) {
173         my $string = $$x[0];
174         for my $i (1..10) {
175             last unless ($$x[$i]);
176             if ($string =~ /\W$/o) {
177                 $string .= ' '.$$x[$i];
178             } else {
179                 $string .= ' -- '.$$x[$i];
180             }
181         }
182         next if (lc($string) eq lc($term));
183         $hash{$string}++;
184         $hash{$string}++ if (lc($$x[0]) eq lc($term));
185     }
186     my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
187
188 #   $also = [ @$also[0..4] ] if $limit;
189
190     #warn Dumper( { from => $from, also => $also } );
191
192     return { from => $from, also => $also };
193 }
194
195 __PACKAGE__->register_method(
196         method      => "crossref_authority",
197         api_name    => "open-ils.search.authority.crossref",
198         argc        => 2, 
199         note        => "Searches authority data for existing controlled terms and crossrefs",
200 );              
201
202 __PACKAGE__->register_method(
203     #method     => "new_crossref_authority_batch",
204     method      => "crossref_authority_batch2",
205     api_name    => "open-ils.search.authority.crossref.batch",
206     argc        => 1, 
207     note        => <<"    NOTE");
208     Takes an array of class,term pair sub-arrays and performs an authority lookup for each
209
210     PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
211
212     Returns an object like so:
213     {
214         "classname" : {
215             "term" : { "from" : [ ...], "also" : [...] }
216             "term2" : { "from" : [ ...], "also" : [...] }
217         }
218     }
219     NOTE
220
221 sub new_crossref_authority_batch {
222     my( $self, $client, $reqs ) = @_;
223
224     my $response = {};
225     my $lastr = [];
226     my $session = OpenSRF::AppSession->create("open-ils.storage");
227
228     for my $req (@$reqs) {
229
230         my $class = $req->[0];
231         my $term = $req->[1];
232         next unless $class and $term;
233         $logger->info("Sending authority request for $class : $term");
234         my $fr = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10)->gather(1);
235         my $al = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10)->gather(1);
236
237         $response->{$class} = {} unless exists $response->{$class};
238         $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
239
240     }
241
242     #warn Dumper( $response );
243     return $response;
244 }
245
246 sub crossref_authority_batch {
247     my( $self, $client, $reqs ) = @_;
248
249     my $response = {};
250     my $lastr = [];
251     my $session = OpenSRF::AppSession->create("open-ils.storage");
252
253     for my $req (@$reqs) {
254
255         my $class = $req->[0];
256         my $term = $req->[1];
257         next unless $class and $term;
258         $logger->info("Sending authority request for $class : $term");
259         my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
260         my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
261
262         if( $lastr->[0] ) { #process old data while waiting on new data
263             my $cls = $lastr->[0];
264             my $trm = $lastr->[1];
265             my $fr  = $lastr->[2];
266             my $al  = $lastr->[3];
267             $response->{$cls} = {} unless exists $response->{$cls};
268             $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
269         }
270
271         $lastr->[0] = $class;
272         $lastr->[1] = $term; 
273         $lastr->[2] = $freq->gather(1);
274         $lastr->[3] = $areq->gather(1);
275     }
276
277     if( $lastr->[0] ) { #process old data while waiting on new data
278         my $cls = $lastr->[0];
279         my $trm = $lastr->[1];
280         my $fr  = $lastr->[2];
281         my $al  = $lastr->[3];
282         $response->{$cls} = {} unless exists $response->{$cls};
283         $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
284     }
285
286     return $response;
287 }
288
289
290
291
292 sub crossref_authority_batch2 {
293     my( $self, $client, $reqs ) = @_;
294
295     my $response = {};
296     my $lastr = [];
297     my $session = OpenSRF::AppSession->create("open-ils.storage");
298
299     $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
300
301     for my $req (@$reqs) {
302
303         my $class = $req->[0];
304         my $term = $req->[1];
305         next unless $class and $term;
306
307         my $t = $term;
308         $t =~ s/\s//og;
309         my $cdata = $cache->get_cache("oils_authority_${class}_$t");
310
311         if( $cdata ) {
312             $logger->debug("returning authority response from cache..");
313             $response->{$class} = {} unless exists $response->{$class};
314             $response->{$class}->{$term} = $cdata;
315             next;
316         }
317
318         $logger->debug("authority data not found in cache.. fetching from storage");
319
320         $logger->info("Sending authority request for $class : $term");
321         my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
322         my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
323         my $fr = $freq->gather(1);  
324         my $al = $areq->gather(1);
325         $response->{$class} = {} unless exists $response->{$class};
326         my $auth = _auth_flatten( $term, $fr, $al, 1 );
327
328         my $timeout = 7200; #two hours
329         $timeout = 300 if @{$auth->{from}} or @{$auth->{also}}; # 5 minutes
330         $response->{$class}->{$term} = $auth;
331         $logger->debug("adding authority lookup to cache with timeout $timeout");
332         $cache->put_cache("oils_authority_${class}_$t", $auth, $timeout);
333     }
334     return $response;
335 }
336
337
338
339 1;