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