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 search_authority {
22 my $session = OpenSRF::AppSession->create("open-ils.storage");
23 return $session->request( 'open-ils.storage.authority.search.marc.atomic' => @_ )->gather(1);
25 __PACKAGE__->register_method(
26 method => "search_authority",
27 api_name => "open-ils.search.authority.fts",
29 note => "Searches authority data for existing controlled terms and crossrefs",
33 sub crossref_authority {
38 my $limit = shift || 10;
40 my $session = OpenSRF::AppSession->create("open-ils.storage");
42 $logger->info("authority xref search for $class=$term, limit=$limit");
43 my $fr = $session->request(
44 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit)->gather(1);
45 my $al = $session->request(
46 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit)->gather(1);
48 my $data = _auth_flatten( $term, $fr, $al, 1 );
63 last unless ($$x[$i]);
64 if ($string =~ /\W$/o) {
65 $string .= ' '.$$x[$i];
67 $string .= ' -- '.$$x[$i];
70 next if (lc($string) eq lc($term));
72 $hash{$string}++ if (lc($$x[0]) eq lc($term));
74 my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
76 # $from = [ @$from[0..4] ] if $limit;
82 last unless ($$x[$i]);
83 if ($string =~ /\W$/o) {
84 $string .= ' '.$$x[$i];
86 $string .= ' -- '.$$x[$i];
89 next if (lc($string) eq lc($term));
91 $hash{$string}++ if (lc($$x[0]) eq lc($term));
93 my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
95 # $also = [ @$also[0..4] ] if $limit;
97 #warn Dumper( { from => $from, also => $also } );
99 return { from => $from, also => $also };
102 __PACKAGE__->register_method(
103 method => "crossref_authority",
104 api_name => "open-ils.search.authority.crossref",
106 note => "Searches authority data for existing controlled terms and crossrefs",
109 __PACKAGE__->register_method(
110 method => "new_crossref_authority_batch",
111 api_name => "open-ils.search.authority.crossref.batch",
114 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
116 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
118 Returns an object like so:
121 "term" : { "from" : [ ...], "also" : [...] }
122 "term2" : { "from" : [ ...], "also" : [...] }
127 sub new_crossref_authority_batch {
128 my( $self, $client, $reqs ) = @_;
132 my $session = OpenSRF::AppSession->create("open-ils.storage");
134 for my $req (@$reqs) {
136 my $class = $req->[0];
137 my $term = $req->[1];
138 next unless $class and $term;
139 warn "Sending authority request for $class : $term\n";
140 my $fr = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10)->gather(1);
141 my $al = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10)->gather(1);
143 warn "Flattening $class : $term\n";
144 $response->{$class} = {} unless exists $response->{$class};
145 $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
149 #warn Dumper( $response );
153 sub crossref_authority_batch {
154 my( $self, $client, $reqs ) = @_;
158 my $session = OpenSRF::AppSession->create("open-ils.storage");
160 for my $req (@$reqs) {
162 my $class = $req->[0];
163 my $term = $req->[1];
164 next unless $class and $term;
165 warn "Sending authority request for $class : $term\n";
166 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
167 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
169 if( $lastr->[0] ) { #process old data while waiting on new data
170 my $cls = $lastr->[0];
171 my $trm = $lastr->[1];
172 my $fr = $lastr->[2];
173 my $al = $lastr->[3];
174 warn "Flattening $class : $term\n";
175 $response->{$cls} = {} unless exists $response->{$cls};
176 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
179 $lastr->[0] = $class;
181 $lastr->[2] = $freq->gather(1);
182 $lastr->[3] = $areq->gather(1);
185 if( $lastr->[0] ) { #process old data while waiting on new data
186 my $cls = $lastr->[0];
187 my $trm = $lastr->[1];
188 my $fr = $lastr->[2];
189 my $al = $lastr->[3];
190 warn "Flattening $cls : $trm\n";
191 $response->{$cls} = {} unless exists $response->{$cls};
192 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
201 __PACKAGE__->register_method(
202 method => "authority_to_html",
203 api_name => "open-ils.search.authority.to_html" );
205 my $parser = XML::LibXML->new();
206 my $xslt = XML::LibXSLT->new();
210 sub authority_to_html {
211 my( $self, $client, $id ) = @_;
214 my $sclient = OpenSRF::Utils::SettingsClient->new();
215 my $dir = $sclient->config_value( "dirs", "xsl" );
216 my $xsl = $sclient->config_value(
217 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
218 $xsl = $parser->parse_file("$dir/$xsl");
219 $stylesheet = $xslt->parse_stylesheet( $xsl );
222 my $e = new_editor();
223 my $rec = $e->retrieve_authority_record_entry($id) or return $e->event;
224 my $xmldoc = $parser->parse_string($rec->marc);
225 my $html = $stylesheet->transform($xmldoc);
227 return $html->toString();