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;
70 select => { are => ['id'] },
74 marc => { 'startwith' => {
75 transform => 'authority.simple_normalize_heading',
76 value => [ 'authority.simple_normalize_heading' => $marcxml ]
78 defined($controlset) ? ( control_set => $controlset ) : ()
82 $client->respond($_->{id}) for @{ new_editor()->json_query( $query ) };
83 $client->respond_complete;
85 __PACKAGE__->register_method(
86 method => "search_authority_by_simple_normalize_heading",
87 api_name => "open-ils.search.authority.simple_heading.from_xml",
90 note => "Searches authority data by main entry using marcxml, returning 'are' ids; params are marcxml and optional control-set-id",
93 sub search_authority_batch_by_simple_normalize_heading {
96 my $search_set = [@_];
98 my $m = $self->method_lookup('open-ils.search.authority.simple_heading.from_xml.atomic');
100 for my $s ( @$search_set ) {
101 for my $k ( keys %$s ) {
102 $client->respond( { $k => $m->run( $s->{$k}, $k ) } );
106 $client->respond_complete;
108 __PACKAGE__->register_method(
109 method => "search_authority_batch_by_simple_normalize_heading",
110 api_name => "open-ils.search.authority.simple_heading.from_xml.batch",
113 note => "Searches authority data by main entry using marcxml, in control-set batches, returning 'are' ids; params are hashes of { control-set-id => marcxml }",
117 sub crossref_authority {
122 my $limit = shift || 10;
124 my $session = OpenSRF::AppSession->create("open-ils.storage");
126 # Avoid generating spurious errors for more granular indexes, like author|personal
127 $class =~ s/^(.*?)\|.*?$/$1/;
129 $logger->info("authority xref search for $class=$term, limit=$limit");
130 my $fr = $session->request(
131 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit)->gather(1);
132 my $al = $session->request(
133 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit)->gather(1);
135 my $data = _auth_flatten( $term, $fr, $al, 1 );
150 last unless ($$x[$i]);
151 if ($string =~ /\W$/o) {
152 $string .= ' '.$$x[$i];
154 $string .= ' -- '.$$x[$i];
157 next if (lc($string) eq lc($term));
159 $hash{$string}++ if (lc($$x[0]) eq lc($term));
161 my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
163 # $from = [ @$from[0..4] ] if $limit;
169 last unless ($$x[$i]);
170 if ($string =~ /\W$/o) {
171 $string .= ' '.$$x[$i];
173 $string .= ' -- '.$$x[$i];
176 next if (lc($string) eq lc($term));
178 $hash{$string}++ if (lc($$x[0]) eq lc($term));
180 my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
182 # $also = [ @$also[0..4] ] if $limit;
184 #warn Dumper( { from => $from, also => $also } );
186 return { from => $from, also => $also };
189 __PACKAGE__->register_method(
190 method => "crossref_authority",
191 api_name => "open-ils.search.authority.crossref",
193 note => "Searches authority data for existing controlled terms and crossrefs",
196 __PACKAGE__->register_method(
197 #method => "new_crossref_authority_batch",
198 method => "crossref_authority_batch2",
199 api_name => "open-ils.search.authority.crossref.batch",
202 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
204 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
206 Returns an object like so:
209 "term" : { "from" : [ ...], "also" : [...] }
210 "term2" : { "from" : [ ...], "also" : [...] }
215 sub new_crossref_authority_batch {
216 my( $self, $client, $reqs ) = @_;
220 my $session = OpenSRF::AppSession->create("open-ils.storage");
222 for my $req (@$reqs) {
224 my $class = $req->[0];
225 my $term = $req->[1];
226 next unless $class and $term;
227 $logger->info("Sending authority request for $class : $term");
228 my $fr = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10)->gather(1);
229 my $al = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10)->gather(1);
231 $response->{$class} = {} unless exists $response->{$class};
232 $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
236 #warn Dumper( $response );
240 sub crossref_authority_batch {
241 my( $self, $client, $reqs ) = @_;
245 my $session = OpenSRF::AppSession->create("open-ils.storage");
247 for my $req (@$reqs) {
249 my $class = $req->[0];
250 my $term = $req->[1];
251 next unless $class and $term;
252 $logger->info("Sending authority request for $class : $term");
253 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
254 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
256 if( $lastr->[0] ) { #process old data while waiting on new data
257 my $cls = $lastr->[0];
258 my $trm = $lastr->[1];
259 my $fr = $lastr->[2];
260 my $al = $lastr->[3];
261 $response->{$cls} = {} unless exists $response->{$cls};
262 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
265 $lastr->[0] = $class;
267 $lastr->[2] = $freq->gather(1);
268 $lastr->[3] = $areq->gather(1);
271 if( $lastr->[0] ) { #process old data while waiting on new data
272 my $cls = $lastr->[0];
273 my $trm = $lastr->[1];
274 my $fr = $lastr->[2];
275 my $al = $lastr->[3];
276 $response->{$cls} = {} unless exists $response->{$cls};
277 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
286 sub crossref_authority_batch2 {
287 my( $self, $client, $reqs ) = @_;
291 my $session = OpenSRF::AppSession->create("open-ils.storage");
293 $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
295 for my $req (@$reqs) {
297 my $class = $req->[0];
298 my $term = $req->[1];
299 next unless $class and $term;
303 my $cdata = $cache->get_cache("oils_authority_${class}_$t");
306 $logger->debug("returning authority response from cache..");
307 $response->{$class} = {} unless exists $response->{$class};
308 $response->{$class}->{$term} = $cdata;
312 $logger->debug("authority data not found in cache.. fetching from storage");
314 $logger->info("Sending authority request for $class : $term");
315 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
316 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
317 my $fr = $freq->gather(1);
318 my $al = $areq->gather(1);
319 $response->{$class} = {} unless exists $response->{$class};
320 my $auth = _auth_flatten( $term, $fr, $al, 1 );
322 my $timeout = 7200; #two hours
323 $timeout = 300 if @{$auth->{from}} or @{$auth->{also}}; # 5 minutes
324 $response->{$class}->{$term} = $auth;
325 $logger->debug("adding authority lookup to cache with timeout $timeout");
326 $cache->put_cache("oils_authority_${class}_$t", $auth, $timeout);