1 package OpenILS::Application::Search::Authority;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
5 use OpenILS::Utils::Fieldmapper;
6 use OpenILS::Application::AppUtils;
9 use OpenILS::Utils::Editor q/:funcs/;
10 use OpenSRF::Utils::Logger qw/$logger/;
12 use OpenSRF::Utils::JSON;
14 use Time::HiRes qw(time);
15 use OpenSRF::EX qw(:try);
16 use Digest::MD5 qw(md5_hex);
21 sub validate_authority {
25 my $session = OpenSRF::AppSession->create("open-ils.storage");
26 return $session->request( 'open-ils.storage.authority.validate.tag' => @_ )->gather(1);
28 __PACKAGE__->register_method(
29 method => "validate_authority",
30 api_name => "open-ils.search.authority.validate.tag",
32 note => "Validates authority data from existing controlled terms",
35 sub validate_authority_return_records_by_id {
39 my $session = OpenSRF::AppSession->create("open-ils.storage");
40 return $session->request( 'open-ils.storage.authority.validate.tag.id_list' => @_ )->gather(1);
42 __PACKAGE__->register_method(
43 method => "validate_authority_return_records_by_id",
44 api_name => "open-ils.search.authority.validate.tag.id_list",
46 note => "Validates authority data from existing controlled terms",
49 sub search_authority {
53 my $session = OpenSRF::AppSession->create("open-ils.storage");
54 return $session->request( 'open-ils.storage.authority.search.marc.atomic' => @_ )->gather(1);
56 __PACKAGE__->register_method(
57 method => "search_authority",
58 api_name => "open-ils.search.authority.fts",
60 note => "Searches authority data for existing controlled terms and crossrefs",
64 sub crossref_authority {
69 my $limit = shift || 10;
71 my $session = OpenSRF::AppSession->create("open-ils.storage");
73 # Avoid generating spurious errors for more granular indexes, like author|personal
74 $class =~ s/^(.*?)\|.*?$/$1/;
76 $logger->info("authority xref search for $class=$term, limit=$limit");
77 my $fr = $session->request(
78 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit)->gather(1);
79 my $al = $session->request(
80 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit)->gather(1);
82 my $data = _auth_flatten( $term, $fr, $al, 1 );
97 last unless ($$x[$i]);
98 if ($string =~ /\W$/o) {
99 $string .= ' '.$$x[$i];
101 $string .= ' -- '.$$x[$i];
104 next if (lc($string) eq lc($term));
106 $hash{$string}++ if (lc($$x[0]) eq lc($term));
108 my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
110 # $from = [ @$from[0..4] ] if $limit;
116 last unless ($$x[$i]);
117 if ($string =~ /\W$/o) {
118 $string .= ' '.$$x[$i];
120 $string .= ' -- '.$$x[$i];
123 next if (lc($string) eq lc($term));
125 $hash{$string}++ if (lc($$x[0]) eq lc($term));
127 my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
129 # $also = [ @$also[0..4] ] if $limit;
131 #warn Dumper( { from => $from, also => $also } );
133 return { from => $from, also => $also };
136 __PACKAGE__->register_method(
137 method => "crossref_authority",
138 api_name => "open-ils.search.authority.crossref",
140 note => "Searches authority data for existing controlled terms and crossrefs",
143 __PACKAGE__->register_method(
144 #method => "new_crossref_authority_batch",
145 method => "crossref_authority_batch2",
146 api_name => "open-ils.search.authority.crossref.batch",
149 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
151 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
153 Returns an object like so:
156 "term" : { "from" : [ ...], "also" : [...] }
157 "term2" : { "from" : [ ...], "also" : [...] }
162 sub new_crossref_authority_batch {
163 my( $self, $client, $reqs ) = @_;
167 my $session = OpenSRF::AppSession->create("open-ils.storage");
169 for my $req (@$reqs) {
171 my $class = $req->[0];
172 my $term = $req->[1];
173 next unless $class and $term;
174 $logger->info("Sending authority request for $class : $term");
175 my $fr = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10)->gather(1);
176 my $al = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10)->gather(1);
178 $response->{$class} = {} unless exists $response->{$class};
179 $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
183 #warn Dumper( $response );
187 sub crossref_authority_batch {
188 my( $self, $client, $reqs ) = @_;
192 my $session = OpenSRF::AppSession->create("open-ils.storage");
194 for my $req (@$reqs) {
196 my $class = $req->[0];
197 my $term = $req->[1];
198 next unless $class and $term;
199 $logger->info("Sending authority request for $class : $term");
200 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
201 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
203 if( $lastr->[0] ) { #process old data while waiting on new data
204 my $cls = $lastr->[0];
205 my $trm = $lastr->[1];
206 my $fr = $lastr->[2];
207 my $al = $lastr->[3];
208 $response->{$cls} = {} unless exists $response->{$cls};
209 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
212 $lastr->[0] = $class;
214 $lastr->[2] = $freq->gather(1);
215 $lastr->[3] = $areq->gather(1);
218 if( $lastr->[0] ) { #process old data while waiting on new data
219 my $cls = $lastr->[0];
220 my $trm = $lastr->[1];
221 my $fr = $lastr->[2];
222 my $al = $lastr->[3];
223 $response->{$cls} = {} unless exists $response->{$cls};
224 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
233 sub crossref_authority_batch2 {
234 my( $self, $client, $reqs ) = @_;
238 my $session = OpenSRF::AppSession->create("open-ils.storage");
240 $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
242 for my $req (@$reqs) {
244 my $class = $req->[0];
245 my $term = $req->[1];
246 next unless $class and $term;
250 my $cdata = $cache->get_cache("oils_authority_${class}_$t");
253 $logger->debug("returning authority response from cache..");
254 $response->{$class} = {} unless exists $response->{$class};
255 $response->{$class}->{$term} = $cdata;
259 $logger->debug("authority data not found in cache.. fetching from storage");
261 $logger->info("Sending authority request for $class : $term");
262 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
263 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
264 my $fr = $freq->gather(1);
265 my $al = $areq->gather(1);
266 $response->{$class} = {} unless exists $response->{$class};
267 my $auth = _auth_flatten( $term, $fr, $al, 1 );
269 my $timeout = 7200; #two hours
270 $timeout = 300 if @{$auth->{from}} or @{$auth->{also}}; # 5 minutes
271 $response->{$class}->{$term} = $auth;
272 $logger->debug("adding authority lookup to cache with timeout $timeout");
273 $cache->put_cache("oils_authority_${class}_$t", $auth, $timeout);