]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Authority.pm
change request/gather order as short term solution to the fact that sessions are...
[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::Utils::Fieldmapper;
6 use OpenILS::Application::AppUtils;
7 use XML::LibXML;
8 use XML::LibXSLT;
9 use OpenILS::Utils::Editor q/:funcs/;
10 use OpenSRF::Utils::Logger qw/$logger/;
11
12 use JSON;
13
14 use Time::HiRes qw(time);
15 use OpenSRF::EX qw(:try);
16 use Digest::MD5 qw(md5_hex);
17
18 sub search_authority {
19         my $self = shift;
20         my $client = shift;
21
22         my $session = OpenSRF::AppSession->create("open-ils.storage");
23         return $session->request( 'open-ils.storage.authority.search.marc.atomic' => @_ )->gather(1);
24 }
25 __PACKAGE__->register_method(
26         method          => "search_authority",
27         api_name        => "open-ils.search.authority.fts",
28         argc            => 2, 
29         note            => "Searches authority data for existing controlled terms and crossrefs",
30 );              
31
32
33 sub crossref_authority {
34         my $self = shift;
35         my $client = shift;
36         my $class = shift;
37         my $term = shift;
38         my $limit = shift || 10;
39
40         my $session = OpenSRF::AppSession->create("open-ils.storage");
41
42         $logger->info("authority xref search for $class=$term, limit=$limit");
43
44         my $freq = $session->request(
45                 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit);
46         my $fr = $freq->gather(1);
47
48         my $areq = $session->request(
49                 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit);
50         my $al = $areq->gather(1);
51
52
53         my $data = _auth_flatten( $term, $fr, $al, 1 );
54
55         return $data;
56 }
57
58 sub _auth_flatten {
59         my $term = shift;
60         my $fr = shift;
61         my $al = shift;
62         my $limit = shift;
63
64         my %hash = ();
65         for my $x (@$fr) {
66                 my $string = $$x[0];
67                 for my $i (1..10) {
68                         last unless ($$x[$i]);
69                         if ($string =~ /\W$/o) {
70                                 $string .= ' '.$$x[$i];
71                         } else {
72                                 $string .= ' -- '.$$x[$i];
73                         }
74                 }
75                 next if (lc($string) eq lc($term));
76                 $hash{$string}++;
77                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
78         }
79         my $from = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
80
81 #       $from = [ @$from[0..4] ] if $limit;
82
83         %hash = ();
84         for my $x (@$al) {
85                 my $string = $$x[0];
86                 for my $i (1..10) {
87                         last unless ($$x[$i]);
88                         if ($string =~ /\W$/o) {
89                                 $string .= ' '.$$x[$i];
90                         } else {
91                                 $string .= ' -- '.$$x[$i];
92                         }
93                 }
94                 next if (lc($string) eq lc($term));
95                 $hash{$string}++;
96                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
97         }
98         my $also = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
99
100 #       $also = [ @$also[0..4] ] if $limit;
101
102
103         return { from => $from, also => $also };
104 }
105
106 __PACKAGE__->register_method(
107         method          => "crossref_authority",
108         api_name        => "open-ils.search.authority.crossref",
109         argc            => 2, 
110         note            => "Searches authority data for existing controlled terms and crossrefs",
111 );              
112
113 __PACKAGE__->register_method(
114         method          => "crossref_authority_batch",
115    api_name     => "open-ils.search.authority.crossref.batch",
116    argc         => 1, 
117    note         => <<"  NOTE");
118         Takes an array of class,term pair sub-arrays and performs an authority lookup for each
119
120         PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
121
122         Returns an object like so:
123         {
124                 "classname" : {
125                         "term" : { "from" : [ ...], "also" : [...] }
126                         "term2" : { "from" : [ ...], "also" : [...] }
127                 }
128         }
129         NOTE
130
131 sub crossref_authority_batch {
132         my( $self, $client, $reqs ) = @_;
133
134         my $response = {};
135         my $lastr = [];
136         my $session = OpenSRF::AppSession->create("open-ils.storage");
137
138         for my $req (@$reqs) {
139
140                 my $class = $req->[0];
141                 my $term = $req->[1];
142                 next unless $class and $term;
143                 warn "Sending authority request for $class : $term\n";
144                 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
145                 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
146
147                 if( $lastr->[0] ) { #process old data while waiting on new data
148                         my $cls = $lastr->[0];
149                         my $trm = $lastr->[1];
150                         my $fr  = $lastr->[2];
151                         my $al  = $lastr->[3];
152                         warn "Flattening $class : $term\n";
153                         $response->{$cls} = {} unless exists $response->{$cls};
154                         $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
155                 }
156
157                 $lastr->[0] = $class;
158                 $lastr->[1] = $term; 
159                 $lastr->[2] = $freq->gather(1);
160                 $lastr->[3] = $areq->gather(1);
161         }
162
163         if( $lastr->[0] ) { #process old data while waiting on new data
164                 my $cls = $lastr->[0];
165                 my $trm = $lastr->[1];
166                 my $fr  = $lastr->[2];
167                 my $al  = $lastr->[3];
168                 warn "Flattening $cls : $trm\n";
169                 $response->{$cls} = {} unless exists $response->{$cls};
170                 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
171         }
172
173         return $response;
174 }
175
176
177
178
179 __PACKAGE__->register_method(
180         method  => "authority_to_html",
181         api_name        => "open-ils.search.authority.to_html" );
182
183 my $parser              = XML::LibXML->new();
184 my $xslt                        = XML::LibXSLT->new();
185 my $stylesheet;
186
187
188 sub authority_to_html {
189         my( $self, $client, $id ) = @_;
190
191         if( !$stylesheet ) {
192                 my $sclient = OpenSRF::Utils::SettingsClient->new();
193                 my $dir = $sclient->config_value( "dirs", "xsl" );
194                 my $xsl = $sclient->config_value(
195                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
196                 $xsl = $parser->parse_file("$dir/$xsl");
197                 $stylesheet = $xslt->parse_stylesheet( $xsl );
198         }
199
200         my $e = new_editor();
201         my $rec = $e->retrieve_authority_record_entry($id) or return $e->event;
202         my $xmldoc = $parser->parse_string($rec->marc);
203         my $html = $stylesheet->transform($xmldoc);
204
205         return $html->toString();
206 }
207
208
209
210
211 1;