1 package OpenILS::Application::Search::Authority;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
5 use OpenILS::Utils::Fieldmapper;
6 use OpenILS::Application::AppUtils;
9 use OpenILS::Utils::Editor q/:funcs/;
10 use OpenSRF::Utils::Logger qw/$logger/;
14 use Time::HiRes qw(time);
15 use OpenSRF::EX qw(:try);
16 use Digest::MD5 qw(md5_hex);
18 sub validate_authority {
22 my $session = OpenSRF::AppSession->create("open-ils.storage");
23 return $session->request( 'open-ils.storage.authority.validate.tag' => @_ )->gather(1);
25 __PACKAGE__->register_method(
26 method => "validate_authority",
27 api_name => "open-ils.search.authority.validate.tag",
29 note => "Validates authority data from existing controlled terms",
32 sub search_authority {
36 my $session = OpenSRF::AppSession->create("open-ils.storage");
37 return $session->request( 'open-ils.storage.authority.search.marc.atomic' => @_ )->gather(1);
39 __PACKAGE__->register_method(
40 method => "search_authority",
41 api_name => "open-ils.search.authority.fts",
43 note => "Searches authority data for existing controlled terms and crossrefs",
47 sub crossref_authority {
52 my $limit = shift || 10;
54 my $session = OpenSRF::AppSession->create("open-ils.storage");
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);
62 my $data = _auth_flatten( $term, $fr, $al, 1 );
77 last unless ($$x[$i]);
78 if ($string =~ /\W$/o) {
79 $string .= ' '.$$x[$i];
81 $string .= ' -- '.$$x[$i];
84 next if (lc($string) eq lc($term));
86 $hash{$string}++ if (lc($$x[0]) eq lc($term));
88 my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
90 # $from = [ @$from[0..4] ] if $limit;
96 last unless ($$x[$i]);
97 if ($string =~ /\W$/o) {
98 $string .= ' '.$$x[$i];
100 $string .= ' -- '.$$x[$i];
103 next if (lc($string) eq lc($term));
105 $hash{$string}++ if (lc($$x[0]) eq lc($term));
107 my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
109 # $also = [ @$also[0..4] ] if $limit;
111 #warn Dumper( { from => $from, also => $also } );
113 return { from => $from, also => $also };
116 __PACKAGE__->register_method(
117 method => "crossref_authority",
118 api_name => "open-ils.search.authority.crossref",
120 note => "Searches authority data for existing controlled terms and crossrefs",
123 __PACKAGE__->register_method(
124 method => "new_crossref_authority_batch",
125 api_name => "open-ils.search.authority.crossref.batch",
128 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
130 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
132 Returns an object like so:
135 "term" : { "from" : [ ...], "also" : [...] }
136 "term2" : { "from" : [ ...], "also" : [...] }
141 sub new_crossref_authority_batch {
142 my( $self, $client, $reqs ) = @_;
146 my $session = OpenSRF::AppSession->create("open-ils.storage");
148 for my $req (@$reqs) {
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);
157 warn "Flattening $class : $term\n";
158 $response->{$class} = {} unless exists $response->{$class};
159 $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
163 #warn Dumper( $response );
167 sub crossref_authority_batch {
168 my( $self, $client, $reqs ) = @_;
172 my $session = OpenSRF::AppSession->create("open-ils.storage");
174 for my $req (@$reqs) {
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);
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 );
193 $lastr->[0] = $class;
195 $lastr->[2] = $freq->gather(1);
196 $lastr->[3] = $areq->gather(1);
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);
215 __PACKAGE__->register_method(
216 method => "authority_to_html",
217 api_name => "open-ils.search.authority.to_html" );
219 my $parser = XML::LibXML->new();
220 my $xslt = XML::LibXSLT->new();
224 sub authority_to_html {
225 my( $self, $client, $id ) = @_;
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 );
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);
241 return $html->toString();