]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Authority.pm
bib and authority marchtml generator now use same code
[Evergreen.git] / Open-ILS / src / perlmods / 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::Editor 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 search_authority {
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.search.marc.atomic' => @_ )->gather(1);
41 }
42 __PACKAGE__->register_method(
43         method          => "search_authority",
44         api_name        => "open-ils.search.authority.fts",
45         argc            => 2, 
46         note            => "Searches authority data for existing controlled terms and crossrefs",
47 );              
48
49
50 sub crossref_authority {
51         my $self = shift;
52         my $client = shift;
53         my $class = shift;
54         my $term = shift;
55         my $limit = shift || 10;
56
57         my $session = OpenSRF::AppSession->create("open-ils.storage");
58
59         $logger->info("authority xref search for $class=$term, limit=$limit");
60         my $fr = $session->request(
61                 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit)->gather(1);
62         my $al = $session->request(
63                 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit)->gather(1);
64
65         my $data = _auth_flatten( $term, $fr, $al, 1 );
66
67         return $data;
68 }
69
70 sub _auth_flatten {
71         my $term = shift;
72         my $fr = shift;
73         my $al = shift;
74         my $limit = shift;
75
76         my %hash = ();
77         for my $x (@$fr) {
78                 my $string = $$x[0];
79                 for my $i (1..10) {
80                         last unless ($$x[$i]);
81                         if ($string =~ /\W$/o) {
82                                 $string .= ' '.$$x[$i];
83                         } else {
84                                 $string .= ' -- '.$$x[$i];
85                         }
86                 }
87                 next if (lc($string) eq lc($term));
88                 $hash{$string}++;
89                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
90         }
91         my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
92
93 #       $from = [ @$from[0..4] ] if $limit;
94
95         %hash = ();
96         for my $x (@$al) {
97                 my $string = $$x[0];
98                 for my $i (1..10) {
99                         last unless ($$x[$i]);
100                         if ($string =~ /\W$/o) {
101                                 $string .= ' '.$$x[$i];
102                         } else {
103                                 $string .= ' -- '.$$x[$i];
104                         }
105                 }
106                 next if (lc($string) eq lc($term));
107                 $hash{$string}++;
108                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
109         }
110         my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
111
112 #       $also = [ @$also[0..4] ] if $limit;
113
114         #warn Dumper( { from => $from, also => $also } );
115
116         return { from => $from, also => $also };
117 }
118
119 __PACKAGE__->register_method(
120         method          => "crossref_authority",
121         api_name        => "open-ils.search.authority.crossref",
122         argc            => 2, 
123         note            => "Searches authority data for existing controlled terms and crossrefs",
124 );              
125
126 __PACKAGE__->register_method(
127         #method         => "new_crossref_authority_batch",
128         method          => "crossref_authority_batch2",
129         api_name        => "open-ils.search.authority.crossref.batch",
130         argc            => 1, 
131         note            => <<"  NOTE");
132         Takes an array of class,term pair sub-arrays and performs an authority lookup for each
133
134         PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
135
136         Returns an object like so:
137         {
138                 "classname" : {
139                         "term" : { "from" : [ ...], "also" : [...] }
140                         "term2" : { "from" : [ ...], "also" : [...] }
141                 }
142         }
143         NOTE
144
145 sub new_crossref_authority_batch {
146         my( $self, $client, $reqs ) = @_;
147
148         my $response = {};
149         my $lastr = [];
150         my $session = OpenSRF::AppSession->create("open-ils.storage");
151
152         for my $req (@$reqs) {
153
154                 my $class = $req->[0];
155                 my $term = $req->[1];
156                 next unless $class and $term;
157                 $logger->info("Sending authority request for $class : $term");
158                 my $fr = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10)->gather(1);
159                 my $al = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10)->gather(1);
160
161                 $response->{$class} = {} unless exists $response->{$class};
162                 $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
163
164         }
165
166         #warn Dumper( $response );
167         return $response;
168 }
169
170 sub crossref_authority_batch {
171         my( $self, $client, $reqs ) = @_;
172
173         my $response = {};
174         my $lastr = [];
175         my $session = OpenSRF::AppSession->create("open-ils.storage");
176
177         for my $req (@$reqs) {
178
179                 my $class = $req->[0];
180                 my $term = $req->[1];
181                 next unless $class and $term;
182                 $logger->info("Sending authority request for $class : $term");
183                 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
184                 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
185
186                 if( $lastr->[0] ) { #process old data while waiting on new data
187                         my $cls = $lastr->[0];
188                         my $trm = $lastr->[1];
189                         my $fr  = $lastr->[2];
190                         my $al  = $lastr->[3];
191                         $response->{$cls} = {} unless exists $response->{$cls};
192                         $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
193                 }
194
195                 $lastr->[0] = $class;
196                 $lastr->[1] = $term; 
197                 $lastr->[2] = $freq->gather(1);
198                 $lastr->[3] = $areq->gather(1);
199         }
200
201         if( $lastr->[0] ) { #process old data while waiting on new data
202                 my $cls = $lastr->[0];
203                 my $trm = $lastr->[1];
204                 my $fr  = $lastr->[2];
205                 my $al  = $lastr->[3];
206                 $response->{$cls} = {} unless exists $response->{$cls};
207                 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
208         }
209
210         return $response;
211 }
212
213
214
215
216 sub crossref_authority_batch2 {
217         my( $self, $client, $reqs ) = @_;
218
219         my $response = {};
220         my $lastr = [];
221         my $session = OpenSRF::AppSession->create("open-ils.storage");
222
223         $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
224
225         for my $req (@$reqs) {
226
227                 my $class = $req->[0];
228                 my $term = $req->[1];
229                 next unless $class and $term;
230
231                 my $t = $term;
232                 $t =~ s/\s//og;
233                 my $cdata = $cache->get_cache("oils_authority_${class}_$t");
234
235                 if( $cdata ) {
236                         $logger->debug("returning authority response from cache..");
237                         $response->{$class} = {} unless exists $response->{$class};
238                         $response->{$class}->{$term} = $cdata;
239                         next;
240                 }
241
242                 $logger->debug("authority data not found in cache.. fetching from storage");
243
244                 $logger->info("Sending authority request for $class : $term");
245                 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
246                 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
247                 my $fr = $freq->gather(1);      
248                 my $al = $areq->gather(1);
249                 $response->{$class} = {} unless exists $response->{$class};
250                 my $auth = _auth_flatten( $term, $fr, $al, 1 );
251
252                 my $timeout = 7200; #two hours
253                 $timeout = 300 if @{$auth->{from}} or @{$auth->{also}}; # 5 minutes
254                 $response->{$class}->{$term} = $auth;
255                 $logger->debug("adding authority lookup to cache with timeout $timeout");
256                 $cache->put_cache("oils_authority_${class}_$t", $auth, $timeout);
257         }
258         return $response;
259 }
260
261
262
263 1;