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 {
23 my $session = OpenSRF::AppSession->create("open-ils.storage");
25 my $freq = $session->request(
26 "open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
27 my $areq = $session->request(
28 "open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
30 my $fr = $freq->gather(1);
31 my $al = $areq->gather(1);
33 return _auth_flatten( $term, $fr, $al, 1 );
46 last unless ($$x[$i]);
47 if ($string =~ /\W$/o) {
48 $string .= ' '.$$x[$i];
50 $string .= ' -- '.$$x[$i];
53 next if (lc($string) eq lc($term));
55 $hash{$string}++ if (lc($$x[0]) eq lc($term));
57 my $from = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
59 # $from = [ @$from[0..4] ] if $limit;
65 last unless ($$x[$i]);
66 if ($string =~ /\W$/o) {
67 $string .= ' '.$$x[$i];
69 $string .= ' -- '.$$x[$i];
72 next if (lc($string) eq lc($term));
74 $hash{$string}++ if (lc($$x[0]) eq lc($term));
76 my $also = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
78 # $also = [ @$also[0..4] ] if $limit;
81 return { from => $from, also => $also };
84 __PACKAGE__->register_method(
85 method => "crossref_authority",
86 api_name => "open-ils.search.authority.crossref",
88 note => "Searches authority data for existing controlled terms and crossrefs",
91 __PACKAGE__->register_method(
92 method => "crossref_authority_batch",
93 api_name => "open-ils.search.authority.crossref.batch",
96 Takes an array of class,term pair sub-arrays and performs an authority lookup for each
98 PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
100 Returns an object like so:
103 "term" : { "from" : [ ...], "also" : [...] }
104 "term2" : { "from" : [ ...], "also" : [...] }
109 sub crossref_authority_batch {
110 my( $self, $client, $reqs ) = @_;
114 my $session = OpenSRF::AppSession->create("open-ils.storage");
116 for my $req (@$reqs) {
118 my $class = $req->[0];
119 my $term = $req->[1];
120 next unless $class and $term;
121 warn "Sending authority request for $class : $term\n";
122 my $freq = $session->request("open-ils.storage.authority.$class.see_from.controlled.atomic",$term, 10);
123 my $areq = $session->request("open-ils.storage.authority.$class.see_also_from.controlled.atomic",$term, 10);
125 if( $lastr->[0] ) { #process old data while waiting on new data
126 my $cls = $lastr->[0];
127 my $trm = $lastr->[1];
128 my $fr = $lastr->[2];
129 my $al = $lastr->[3];
130 warn "Flattening $class : $term\n";
131 $response->{$cls} = {} unless exists $response->{$cls};
132 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1 );
135 $lastr->[0] = $class;
137 $lastr->[2] = $freq->gather(1);
138 $lastr->[3] = $areq->gather(1);
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 $cls : $trm\n";
147 $response->{$cls} = {} unless exists $response->{$cls};
148 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
157 __PACKAGE__->register_method(
158 method => "authority_to_html",
159 api_name => "open-ils.search.authority.to_html" );
161 my $parser = XML::LibXML->new();
162 my $xslt = XML::LibXSLT->new();
166 sub authority_to_html {
167 my( $self, $client, $id ) = @_;
170 my $sclient = OpenSRF::Utils::SettingsClient->new();
171 my $dir = $sclient->config_value( "dirs", "xsl" );
172 my $xsl = $sclient->config_value(
173 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
174 $xsl = $parser->parse_file("$dir/$xsl");
175 $stylesheet = $xslt->parse_stylesheet( $xsl );
178 my $e = new_editor();
179 my $rec = $e->retrieve_authority_record_entry($id) or return $e->event;
180 my $xmldoc = $parser->parse_string($rec->marc);
181 my $html = $stylesheet->transform($xmldoc);
183 return $html->toString();