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'] },
75 'startwith' => [ 'authority.simple_normalize_heading' => $marcxml ]
77 defined($controlset) ? ( control_set => $controlset ) : ()
81 $client->respond($_->{id}) for @{ new_editor()->json_query( $query ) };
82 $client->respond_complete;
84 __PACKAGE__->register_method(
85 method => "search_authority_by_simple_normalize_heading",
86 api_name => "open-ils.search.authority.simple_heading.from_xml",
89 note => "Searches authority data by main entry using marcxml, returning 'are' ids; params are marcxml and optional control-set-id",
92 sub search_authority_batch_by_simple_normalize_heading {
95 my $search_set = [@_];
97 my $m = $self->method_lookup('open-ils.search.authority.simple_heading.from_xml.atomic');
99 for my $s ( @$search_set ) {
100 for my $k ( keys %$s ) {
101 $client->respond( { $k => $m->run( $s->{$k}, $k ) } );
105 $client->respond_complete;
107 __PACKAGE__->register_method(
108 method => "search_authority_batch_by_simple_normalize_heading",
109 api_name => "open-ils.search.authority.simple_heading.from_xml.batch",
112 note => "Searches authority data by main entry using marcxml, in control-set batches, returning 'are' ids; params are hashes of { control-set-id => marcxml }",
116 sub crossref_authority {
121 my $limit = shift || 10;
123 my $session = OpenSRF::AppSession->create("open-ils.storage");
125 # Avoid generating spurious errors for more granular indexes, like author|personal
126 $class =~ s/^(.*?)\|.*?$/$1/;
128 $logger->info("authority xref search for $class=$term, limit=$limit");
129 my $fr = $session->request(
130 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit)->gather(1);
131 my $al = $session->request(
132 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit)->gather(1);
134 my $data = _auth_flatten( $term, $fr, $al, 1 );
149 last unless ($$x[$i]);
150 if ($string =~ /\W$/o) {
151 $string .= ' '.$$x[$i];
153 $string .= ' -- '.$$x[$i];
156 next if (lc($string) eq lc($term));
158 $hash{$string}++ if (lc($$x[0]) eq lc($term));
160 my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
162 # $from = [ @$from[0..4] ] if $limit;
168 last unless ($$x[$i]);
169 if ($string =~ /\W$/o) {
170 $string .= ' '.$$x[$i];
172 $string .= ' -- '.$$x[$i];
175 next if (lc($string) eq lc($term));
177 $hash{$string}++ if (lc($$x[0]) eq lc($term));
179 my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
181 # $also = [ @$also[0..4] ] if $limit;
183 #warn Dumper( { from => $from, also => $also } );
185 return { from => $from, also => $also };
188 __PACKAGE__->register_method(
189 method => "crossref_authority",
190 api_name => "open-ils.search.authority.crossref",
192 note => "Searches authority data for existing controlled terms and crossrefs",
195 __PACKAGE__->register_method(
196 #method => "new_crossref_authority_batch",
197 method => "crossref_authority_batch2",
198 api_name => "open-ils.search.authority.crossref.batch",
201 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
203 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
205 Returns an object like so:
208 "term" : { "from" : [ ...], "also" : [...] }
209 "term2" : { "from" : [ ...], "also" : [...] }
214 sub new_crossref_authority_batch {
215 my( $self, $client, $reqs ) = @_;
219 my $session = OpenSRF::AppSession->create("open-ils.storage");
221 for my $req (@$reqs) {
223 my $class = $req->[0];
224 my $term = $req->[1];
225 next unless $class and $term;
226 $logger->info("Sending authority request for $class : $term");
227 my $fr = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10)->gather(1);
228 my $al = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10)->gather(1);
230 $response->{$class} = {} unless exists $response->{$class};
231 $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
235 #warn Dumper( $response );
239 sub crossref_authority_batch {
240 my( $self, $client, $reqs ) = @_;
244 my $session = OpenSRF::AppSession->create("open-ils.storage");
246 for my $req (@$reqs) {
248 my $class = $req->[0];
249 my $term = $req->[1];
250 next unless $class and $term;
251 $logger->info("Sending authority request for $class : $term");
252 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
253 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
255 if( $lastr->[0] ) { #process old data while waiting on new data
256 my $cls = $lastr->[0];
257 my $trm = $lastr->[1];
258 my $fr = $lastr->[2];
259 my $al = $lastr->[3];
260 $response->{$cls} = {} unless exists $response->{$cls};
261 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
264 $lastr->[0] = $class;
266 $lastr->[2] = $freq->gather(1);
267 $lastr->[3] = $areq->gather(1);
270 if( $lastr->[0] ) { #process old data while waiting on new data
271 my $cls = $lastr->[0];
272 my $trm = $lastr->[1];
273 my $fr = $lastr->[2];
274 my $al = $lastr->[3];
275 $response->{$cls} = {} unless exists $response->{$cls};
276 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
285 sub crossref_authority_batch2 {
286 my( $self, $client, $reqs ) = @_;
290 my $session = OpenSRF::AppSession->create("open-ils.storage");
292 $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
294 for my $req (@$reqs) {
296 my $class = $req->[0];
297 my $term = $req->[1];
298 next unless $class and $term;
302 my $cdata = $cache->get_cache("oils_authority_${class}_$t");
305 $logger->debug("returning authority response from cache..");
306 $response->{$class} = {} unless exists $response->{$class};
307 $response->{$class}->{$term} = $cdata;
311 $logger->debug("authority data not found in cache.. fetching from storage");
313 $logger->info("Sending authority request for $class : $term");
314 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
315 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
316 my $fr = $freq->gather(1);
317 my $al = $areq->gather(1);
318 $response->{$class} = {} unless exists $response->{$class};
319 my $auth = _auth_flatten( $term, $fr, $al, 1 );
321 my $timeout = 7200; #two hours
322 $timeout = 300 if @{$auth->{from}} or @{$auth->{also}}; # 5 minutes
323 $response->{$class}->{$term} = $auth;
324 $logger->debug("adding authority lookup to cache with timeout $timeout");
325 $cache->put_cache("oils_authority_${class}_$t", $auth, $timeout);