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