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 crossref_authority {
22 my $limit = shift || 10;
24 my $session = OpenSRF::AppSession->create("open-ils.storage");
26 my $freq = $session->request(
27 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, $limit);
28 my $areq = $session->request(
29 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, $limit);
31 my $fr = $freq->gather(1);
32 my $al = $areq->gather(1);
34 return _auth_flatten( $term, $fr, $al, 1 );
47 last unless ($$x[$i]);
48 if ($string =~ /\W$/o) {
49 $string .= ' '.$$x[$i];
51 $string .= ' -- '.$$x[$i];
54 next if (lc($string) eq lc($term));
56 $hash{$string}++ if (lc($$x[0]) eq lc($term));
58 my $from = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
60 # $from = [ @$from[0..4] ] if $limit;
66 last unless ($$x[$i]);
67 if ($string =~ /\W$/o) {
68 $string .= ' '.$$x[$i];
70 $string .= ' -- '.$$x[$i];
73 next if (lc($string) eq lc($term));
75 $hash{$string}++ if (lc($$x[0]) eq lc($term));
77 my $also = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
79 # $also = [ @$also[0..4] ] if $limit;
82 return { from => $from, also => $also };
85 __PACKAGE__->register_method(
86 method => "crossref_authority",
87 api_name => "open-ils.search.authority.crossref",
89 note => "Searches authority data for existing controlled terms and crossrefs",
92 __PACKAGE__->register_method(
93 method => "crossref_authority_batch",
94 api_name => "open-ils.search.authority.crossref.batch",
97 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
99 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
101 Returns an object like so:
104 "term" : { "from" : [ ...], "also" : [...] }
105 "term2" : { "from" : [ ...], "also" : [...] }
110 sub crossref_authority_batch {
111 my( $self, $client, $reqs ) = @_;
115 my $session = OpenSRF::AppSession->create("open-ils.storage");
117 for my $req (@$reqs) {
119 my $class = $req->[0];
120 my $term = $req->[1];
121 next unless $class and $term;
122 warn "Sending authority request for $class : $term\n";
123 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
124 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
126 if( $lastr->[0] ) { #process old data while waiting on new data
127 my $cls = $lastr->[0];
128 my $trm = $lastr->[1];
129 my $fr = $lastr->[2];
130 my $al = $lastr->[3];
131 warn "Flattening $class : $term\n";
132 $response->{$cls} = {} unless exists $response->{$cls};
133 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
136 $lastr->[0] = $class;
138 $lastr->[2] = $freq->gather(1);
139 $lastr->[3] = $areq->gather(1);
142 if( $lastr->[0] ) { #process old data while waiting on new data
143 my $cls = $lastr->[0];
144 my $trm = $lastr->[1];
145 my $fr = $lastr->[2];
146 my $al = $lastr->[3];
147 warn "Flattening $cls : $trm\n";
148 $response->{$cls} = {} unless exists $response->{$cls};
149 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
158 __PACKAGE__->register_method(
159 method => "authority_to_html",
160 api_name => "open-ils.search.authority.to_html" );
162 my $parser = XML::LibXML->new();
163 my $xslt = XML::LibXSLT->new();
167 sub authority_to_html {
168 my( $self, $client, $id ) = @_;
171 my $sclient = OpenSRF::Utils::SettingsClient->new();
172 my $dir = $sclient->config_value( "dirs", "xsl" );
173 my $xsl = $sclient->config_value(
174 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
175 $xsl = $parser->parse_file("$dir/$xsl");
176 $stylesheet = $xslt->parse_stylesheet( $xsl );
179 my $e = new_editor();
180 my $rec = $e->retrieve_authority_record_entry($id) or return $e->event;
181 my $xmldoc = $parser->parse_string($rec->marc);
182 my $html = $stylesheet->transform($xmldoc);
184 return $html->toString();