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::CStoreEditor 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",
63 sub search_authority_by_simple_normalize_heading {
67 my $controlset = shift;
69 my $norm_heading_query = {
70 from => [ 'authority.simple_normalize_heading' => $marcxml ]
74 my $norm_heading = $e->json_query($norm_heading_query)->[0]->{'authority.simple_normalize_heading'};
77 select => { are => ['id'] },
82 'startwith' => $norm_heading
84 defined($controlset) ? ( control_set => $controlset ) : ()
88 $client->respond($_->{id}) for @{ $e->json_query( $query ) };
89 $client->respond_complete;
91 __PACKAGE__->register_method(
92 method => "search_authority_by_simple_normalize_heading",
93 api_name => "open-ils.search.authority.simple_heading.from_xml",
96 note => "Searches authority data by main entry using marcxml, returning 'are' ids; params are marcxml and optional control-set-id",
99 sub search_authority_batch_by_simple_normalize_heading {
102 my $search_set = [@_];
104 my $m = $self->method_lookup('open-ils.search.authority.simple_heading.from_xml.atomic');
106 for my $s ( @$search_set ) {
107 for my $k ( keys %$s ) {
108 $client->respond( { $k => $m->run( $s->{$k}, $k ) } );
112 $client->respond_complete;
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",
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 }",
123 sub crossref_authority {
128 my $limit = shift || 10;
130 my $session = OpenSRF::AppSession->create("open-ils.storage");
132 # Avoid generating spurious errors for more granular indexes, like author|personal
133 $class =~ s/^(.*?)\|.*?$/$1/;
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);
141 my $data = _auth_flatten( $term, $fr, $al, 1 );
156 last unless ($$x[$i]);
157 if ($string =~ /\W$/o) {
158 $string .= ' '.$$x[$i];
160 $string .= ' -- '.$$x[$i];
163 next if (lc($string) eq lc($term));
165 $hash{$string}++ if (lc($$x[0]) eq lc($term));
167 my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
169 # $from = [ @$from[0..4] ] if $limit;
175 last unless ($$x[$i]);
176 if ($string =~ /\W$/o) {
177 $string .= ' '.$$x[$i];
179 $string .= ' -- '.$$x[$i];
182 next if (lc($string) eq lc($term));
184 $hash{$string}++ if (lc($$x[0]) eq lc($term));
186 my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
188 # $also = [ @$also[0..4] ] if $limit;
190 #warn Dumper( { from => $from, also => $also } );
192 return { from => $from, also => $also };
195 __PACKAGE__->register_method(
196 method => "crossref_authority",
197 api_name => "open-ils.search.authority.crossref",
199 note => "Searches authority data for existing controlled terms and crossrefs",
202 __PACKAGE__->register_method(
203 #method => "new_crossref_authority_batch",
204 method => "crossref_authority_batch2",
205 api_name => "open-ils.search.authority.crossref.batch",
208 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
210 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
212 Returns an object like so:
215 "term" : { "from" : [ ...], "also" : [...] }
216 "term2" : { "from" : [ ...], "also" : [...] }
221 sub new_crossref_authority_batch {
222 my( $self, $client, $reqs ) = @_;
226 my $session = OpenSRF::AppSession->create("open-ils.storage");
228 for my $req (@$reqs) {
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);
237 $response->{$class} = {} unless exists $response->{$class};
238 $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
242 #warn Dumper( $response );
246 sub crossref_authority_batch {
247 my( $self, $client, $reqs ) = @_;
251 my $session = OpenSRF::AppSession->create("open-ils.storage");
253 for my $req (@$reqs) {
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);
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 );
271 $lastr->[0] = $class;
273 $lastr->[2] = $freq->gather(1);
274 $lastr->[3] = $areq->gather(1);
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);
292 sub crossref_authority_batch2 {
293 my( $self, $client, $reqs ) = @_;
297 my $session = OpenSRF::AppSession->create("open-ils.storage");
299 $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
301 for my $req (@$reqs) {
303 my $class = $req->[0];
304 my $term = $req->[1];
305 next unless $class and $term;
309 my $cdata = $cache->get_cache("oils_authority_${class}_$t");
312 $logger->debug("returning authority response from cache..");
313 $response->{$class} = {} unless exists $response->{$class};
314 $response->{$class}->{$term} = $cdata;
318 $logger->debug("authority data not found in cache.. fetching from storage");
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 );
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);