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/;
13 use Time::HiRes qw(time);
14 use OpenSRF::EX qw(:try);
15 use Digest::MD5 qw(md5_hex);
17 sub search_authority {
21 my $session = OpenSRF::AppSession->create("open-ils.storage");
22 return $session->request( 'open-ils.storage.authority.search.marc.atomic' => @_ )->gather(1);
24 __PACKAGE__->register_method(
25 method => "search_authority",
26 api_name => "open-ils.search.authority.fts",
28 note => "Searches authority data for existing controlled terms and crossrefs",
32 sub crossref_authority {
37 my $limit = shift || 10;
39 my $session = OpenSRF::AppSession->create("open-ils.storage");
41 my $freq = $session->request(
42 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit);
43 my $areq = $session->request(
44 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit);
46 my $fr = $freq->gather(1);
47 my $al = $areq->gather(1);
49 return _auth_flatten( $term, $fr, $al, 1 );
62 last unless ($$x[$i]);
63 if ($string =~ /\W$/o) {
64 $string .= ' '.$$x[$i];
66 $string .= ' -- '.$$x[$i];
69 next if (lc($string) eq lc($term));
71 $hash{$string}++ if (lc($$x[0]) eq lc($term));
73 my $from = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
75 # $from = [ @$from[0..4] ] if $limit;
81 last unless ($$x[$i]);
82 if ($string =~ /\W$/o) {
83 $string .= ' '.$$x[$i];
85 $string .= ' -- '.$$x[$i];
88 next if (lc($string) eq lc($term));
90 $hash{$string}++ if (lc($$x[0]) eq lc($term));
92 my $also = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
94 # $also = [ @$also[0..4] ] if $limit;
97 return { from => $from, also => $also };
100 __PACKAGE__->register_method(
101 method => "crossref_authority",
102 api_name => "open-ils.search.authority.crossref",
104 note => "Searches authority data for existing controlled terms and crossrefs",
107 __PACKAGE__->register_method(
108 method => "crossref_authority_batch",
109 api_name => "open-ils.search.authority.crossref.batch",
112 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
114 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
116 Returns an object like so:
119 "term" : { "from" : [ ...], "also" : [...] }
120 "term2" : { "from" : [ ...], "also" : [...] }
125 sub crossref_authority_batch {
126 my( $self, $client, $reqs ) = @_;
130 my $session = OpenSRF::AppSession->create("open-ils.storage");
132 for my $req (@$reqs) {
134 my $class = $req->[0];
135 my $term = $req->[1];
136 next unless $class and $term;
137 warn "Sending authority request for $class : $term\n";
138 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
139 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
141 if( $lastr->[0] ) { #process old data while waiting on new data
142 my $cls = $lastr->[0];
143 my $trm = $lastr->[1];
144 my $fr = $lastr->[2];
145 my $al = $lastr->[3];
146 warn "Flattening $class : $term\n";
147 $response->{$cls} = {} unless exists $response->{$cls};
148 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
151 $lastr->[0] = $class;
153 $lastr->[2] = $freq->gather(1);
154 $lastr->[3] = $areq->gather(1);
157 if( $lastr->[0] ) { #process old data while waiting on new data
158 my $cls = $lastr->[0];
159 my $trm = $lastr->[1];
160 my $fr = $lastr->[2];
161 my $al = $lastr->[3];
162 warn "Flattening $cls : $trm\n";
163 $response->{$cls} = {} unless exists $response->{$cls};
164 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
173 __PACKAGE__->register_method(
174 method => "authority_to_html",
175 api_name => "open-ils.search.authority.to_html" );
177 my $parser = XML::LibXML->new();
178 my $xslt = XML::LibXSLT->new();
182 sub authority_to_html {
183 my( $self, $client, $id ) = @_;
186 my $sclient = OpenSRF::Utils::SettingsClient->new();
187 my $dir = $sclient->config_value( "dirs", "xsl" );
188 my $xsl = $sclient->config_value(
189 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
190 $xsl = $parser->parse_file("$dir/$xsl");
191 $stylesheet = $xslt->parse_stylesheet( $xsl );
194 my $e = new_editor();
195 my $rec = $e->retrieve_authority_record_entry($id) or return $e->event;
196 my $xmldoc = $parser->parse_string($rec->marc);
197 my $html = $stylesheet->transform($xmldoc);
199 return $html->toString();