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");
44 my $freq = $session->request(
45 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit);
46 my $fr = $freq->gather(1);
48 my $areq = $session->request(
49 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit);
50 my $al = $areq->gather(1);
53 my $data = _auth_flatten( $term, $fr, $al, 1 );
68 last unless ($$x[$i]);
69 if ($string =~ /\W$/o) {
70 $string .= ' '.$$x[$i];
72 $string .= ' -- '.$$x[$i];
75 next if (lc($string) eq lc($term));
77 $hash{$string}++ if (lc($$x[0]) eq lc($term));
79 my $from = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
81 # $from = [ @$from[0..4] ] if $limit;
87 last unless ($$x[$i]);
88 if ($string =~ /\W$/o) {
89 $string .= ' '.$$x[$i];
91 $string .= ' -- '.$$x[$i];
94 next if (lc($string) eq lc($term));
96 $hash{$string}++ if (lc($$x[0]) eq lc($term));
98 my $also = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
100 # $also = [ @$also[0..4] ] if $limit;
103 return { from => $from, also => $also };
106 __PACKAGE__->register_method(
107 method => "crossref_authority",
108 api_name => "open-ils.search.authority.crossref",
110 note => "Searches authority data for existing controlled terms and crossrefs",
113 __PACKAGE__->register_method(
114 method => "crossref_authority_batch",
115 api_name => "open-ils.search.authority.crossref.batch",
118 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
120 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
122 Returns an object like so:
125 "term" : { "from" : [ ...], "also" : [...] }
126 "term2" : { "from" : [ ...], "also" : [...] }
131 sub crossref_authority_batch {
132 my( $self, $client, $reqs ) = @_;
136 my $session = OpenSRF::AppSession->create("open-ils.storage");
138 for my $req (@$reqs) {
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);
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 );
157 $lastr->[0] = $class;
159 $lastr->[2] = $freq->gather(1);
160 $lastr->[3] = $areq->gather(1);
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);
179 __PACKAGE__->register_method(
180 method => "authority_to_html",
181 api_name => "open-ils.search.authority.to_html" );
183 my $parser = XML::LibXML->new();
184 my $xslt = XML::LibXSLT->new();
188 sub authority_to_html {
189 my( $self, $client, $id ) = @_;
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 );
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);
205 return $html->toString();