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 search_authority {
39 my $session = OpenSRF::AppSession->create("open-ils.storage");
40 return $session->request( 'open-ils.storage.authority.search.marc.atomic' => @_ )->gather(1);
42 __PACKAGE__->register_method(
43 method => "search_authority",
44 api_name => "open-ils.search.authority.fts",
46 note => "Searches authority data for existing controlled terms and crossrefs",
50 sub crossref_authority {
55 my $limit = shift || 10;
57 my $session = OpenSRF::AppSession->create("open-ils.storage");
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);
65 my $data = _auth_flatten( $term, $fr, $al, 1 );
80 last unless ($$x[$i]);
81 if ($string =~ /\W$/o) {
82 $string .= ' '.$$x[$i];
84 $string .= ' -- '.$$x[$i];
87 next if (lc($string) eq lc($term));
89 $hash{$string}++ if (lc($$x[0]) eq lc($term));
91 my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
93 # $from = [ @$from[0..4] ] if $limit;
99 last unless ($$x[$i]);
100 if ($string =~ /\W$/o) {
101 $string .= ' '.$$x[$i];
103 $string .= ' -- '.$$x[$i];
106 next if (lc($string) eq lc($term));
108 $hash{$string}++ if (lc($$x[0]) eq lc($term));
110 my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
112 # $also = [ @$also[0..4] ] if $limit;
114 #warn Dumper( { from => $from, also => $also } );
116 return { from => $from, also => $also };
119 __PACKAGE__->register_method(
120 method => "crossref_authority",
121 api_name => "open-ils.search.authority.crossref",
123 note => "Searches authority data for existing controlled terms and crossrefs",
126 __PACKAGE__->register_method(
127 #method => "new_crossref_authority_batch",
128 method => "crossref_authority_batch2",
129 api_name => "open-ils.search.authority.crossref.batch",
132 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
134 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
136 Returns an object like so:
139 "term" : { "from" : [ ...], "also" : [...] }
140 "term2" : { "from" : [ ...], "also" : [...] }
145 sub new_crossref_authority_batch {
146 my( $self, $client, $reqs ) = @_;
150 my $session = OpenSRF::AppSession->create("open-ils.storage");
152 for my $req (@$reqs) {
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);
161 $response->{$class} = {} unless exists $response->{$class};
162 $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
166 #warn Dumper( $response );
170 sub crossref_authority_batch {
171 my( $self, $client, $reqs ) = @_;
175 my $session = OpenSRF::AppSession->create("open-ils.storage");
177 for my $req (@$reqs) {
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);
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 );
195 $lastr->[0] = $class;
197 $lastr->[2] = $freq->gather(1);
198 $lastr->[3] = $areq->gather(1);
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);
216 sub crossref_authority_batch2 {
217 my( $self, $client, $reqs ) = @_;
221 my $session = OpenSRF::AppSession->create("open-ils.storage");
223 $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
225 for my $req (@$reqs) {
227 my $class = $req->[0];
228 my $term = $req->[1];
229 next unless $class and $term;
233 my $cdata = $cache->get_cache("oils_authority_${class}_$t");
236 $logger->debug("returning authority response from cache..");
237 $response->{$class} = {} unless exists $response->{$class};
238 $response->{$class}->{$term} = $cdata;
242 $logger->debug("authority data not found in cache.. fetching from storage");
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 );
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);