]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Authority.pm
limiting authority searches
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Authority.pm
1 package OpenILS::Application::Search::Authority;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
5 use OpenILS::EX;
6
7 use OpenILS::Utils::Fieldmapper;
8 use OpenILS::Application::AppUtils;
9
10 use JSON;
11
12 use Time::HiRes qw(time);
13 use OpenSRF::EX qw(:try);
14 use Digest::MD5 qw(md5_hex);
15
16 sub crossref_authority {
17         my $self = shift;
18         my $client = shift;
19         my $class = shift;
20         my $term = shift;
21
22         my $session = OpenSRF::AppSession->create("open-ils.storage");
23         $session->connect;
24
25         my $freq = $session->request(
26                 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
27         my $areq = $session->request(
28                 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
29
30         my $fr = $freq->gather(1);
31         my $al = $areq->gather(1);
32         $session->disconnect;
33
34         return _auth_flatten( $term, $fr, $al, 1 );
35 }
36
37 sub _auth_flatten {
38         my $term = shift;
39         my $fr = shift;
40         my $al = shift;
41         my $limit = shift;
42
43         my %hash = ();
44         for my $x (@$fr) {
45                 my $string = $$x[0];
46                 for my $i (1..10) {
47                         last unless ($$x[$i]);
48                         if ($string =~ /\W$/o) {
49                                 $string .= ' '.$$x[$i];
50                         } else {
51                                 $string .= ' -- '.$$x[$i];
52                         }
53                 }
54                 next if (lc($string) eq lc($term));
55                 $hash{$string}++;
56                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
57         }
58         my $from = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
59
60 #       $from = [ @$from[0..4] ] if $limit;
61
62         %hash = ();
63         for my $x (@$al) {
64                 my $string = $$x[0];
65                 for my $i (1..10) {
66                         last unless ($$x[$i]);
67                         if ($string =~ /\W$/o) {
68                                 $string .= ' '.$$x[$i];
69                         } else {
70                                 $string .= ' -- '.$$x[$i];
71                         }
72                 }
73                 next if (lc($string) eq lc($term));
74                 $hash{$string}++;
75                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
76         }
77         my $also = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
78
79 #       $also = [ @$also[0..4] ] if $limit;
80
81
82         return { from => $from, also => $also };
83 }
84
85 __PACKAGE__->register_method(
86         method          => "crossref_authority",
87         api_name        => "open-ils.search.authority.crossref",
88         argc            => 2, 
89         note            => "Searches authority data for existing controlled terms and crossrefs",
90 );              
91
92 __PACKAGE__->register_method(
93         method          => "crossref_authority_batch",
94    api_name     => "open-ils.search.authority.crossref.batch",
95    argc         => 1, 
96    note         => <<"  NOTE");
97         Takes an array of class,term pair sub-arrays and performs an authority lookup for each
98
99         PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
100
101         Returns an object like so:
102         {
103                 "classname" : {
104                         "term" : { "from" : [ ...], "also" : [...] }
105                         "term2" : { "from" : [ ...], "also" : [...] }
106                 }
107         }
108         NOTE
109
110 sub crossref_authority_batch {
111         my( $self, $client, $reqs ) = @_;
112
113         my $response = {};
114         my $lastr = [];
115         my $session = OpenSRF::AppSession->create("open-ils.storage");
116
117         for my $req (@$reqs) {
118
119                 my $class = $req->[0];
120                 my $term = $req->[1];
121                 next unless $class and $term;
122                 warn "Sending authority request for $class : $term\n";
123                 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
124                 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
125
126                 if( $lastr->[0] ) { #process old data while waiting on new data
127                         my $cls = $lastr->[0];
128                         my $trm = $lastr->[1];
129                         my $fr  = $lastr->[2];
130                         my $al  = $lastr->[3];
131                         warn "Flattening $class : $term\n";
132                         $response->{$cls} = {} unless exists $response->{$cls};
133                         $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
134                 }
135
136                 $lastr->[0] = $class;
137                 $lastr->[1] = $term; 
138                 $lastr->[2] = $freq->gather(1);
139                 $lastr->[3] = $areq->gather(1);
140         }
141
142         if( $lastr->[0] ) { #process old data while waiting on new data
143                 my $cls = $lastr->[0];
144                 my $trm = $lastr->[1];
145                 my $fr  = $lastr->[2];
146                 my $al  = $lastr->[3];
147                 warn "Flattening $cls : $trm\n";
148                 $response->{$cls} = {} unless exists $response->{$cls};
149                 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
150         }
151
152         return $response;
153 }
154
155
156 1;