]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Authority.pm
7a7135ae4a4bdf2bd50a49b319c97620b0275f27
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Authority.pm
1 package OpenILS::Application::Search::Authority;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
5 use OpenILS::Utils::Fieldmapper;
6 use OpenILS::Application::AppUtils;
7 use XML::LibXML;
8 use XML::LibXSLT;
9 use OpenILS::Utils::Editor q/:funcs/;
10 use OpenSRF::Utils::Logger qw/$logger/;
11
12 use JSON;
13
14 use Time::HiRes qw(time);
15 use OpenSRF::EX qw(:try);
16 use Digest::MD5 qw(md5_hex);
17
18 sub search_authority {
19         my $self = shift;
20         my $client = shift;
21
22         my $session = OpenSRF::AppSession->create("open-ils.storage");
23         return $session->request( 'open-ils.storage.authority.search.marc.atomic' => @_ )->gather(1);
24 }
25 __PACKAGE__->register_method(
26         method          => "search_authority",
27         api_name        => "open-ils.search.authority.fts",
28         argc            => 2, 
29         note            => "Searches authority data for existing controlled terms and crossrefs",
30 );              
31
32
33 sub crossref_authority {
34         my $self = shift;
35         my $client = shift;
36         my $class = shift;
37         my $term = shift;
38         my $limit = shift || 10;
39
40         my $session = OpenSRF::AppSession->create("open-ils.storage");
41
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);
47
48         my $data = _auth_flatten( $term, $fr, $al, 1 );
49
50         return $data;
51 }
52
53 sub _auth_flatten {
54         my $term = shift;
55         my $fr = shift;
56         my $al = shift;
57         my $limit = shift;
58
59         my %hash = ();
60         for my $x (@$fr) {
61                 my $string = $$x[0];
62                 for my $i (1..10) {
63                         last unless ($$x[$i]);
64                         if ($string =~ /\W$/o) {
65                                 $string .= ' '.$$x[$i];
66                         } else {
67                                 $string .= ' -- '.$$x[$i];
68                         }
69                 }
70                 next if (lc($string) eq lc($term));
71                 $hash{$string}++;
72                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
73         }
74         my $from = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
75
76 #       $from = [ @$from[0..4] ] if $limit;
77
78         %hash = ();
79         for my $x (@$al) {
80                 my $string = $$x[0];
81                 for my $i (1..10) {
82                         last unless ($$x[$i]);
83                         if ($string =~ /\W$/o) {
84                                 $string .= ' '.$$x[$i];
85                         } else {
86                                 $string .= ' -- '.$$x[$i];
87                         }
88                 }
89                 next if (lc($string) eq lc($term));
90                 $hash{$string}++;
91                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
92         }
93         my $also = [keys %hash]; #[ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
94
95 #       $also = [ @$also[0..4] ] if $limit;
96
97         #warn Dumper( { from => $from, also => $also } );
98
99         return { from => $from, also => $also };
100 }
101
102 __PACKAGE__->register_method(
103         method          => "crossref_authority",
104         api_name        => "open-ils.search.authority.crossref",
105         argc            => 2, 
106         note            => "Searches authority data for existing controlled terms and crossrefs",
107 );              
108
109 __PACKAGE__->register_method(
110         method          => "new_crossref_authority_batch",
111         api_name        => "open-ils.search.authority.crossref.batch",
112         argc            => 1, 
113         note            => <<"  NOTE");
114         Takes an array of class,term pair sub-arrays and performs an authority lookup for each
115
116         PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
117
118         Returns an object like so:
119         {
120                 "classname" : {
121                         "term" : { "from" : [ ...], "also" : [...] }
122                         "term2" : { "from" : [ ...], "also" : [...] }
123                 }
124         }
125         NOTE
126
127 sub new_crossref_authority_batch {
128         my( $self, $client, $reqs ) = @_;
129
130         my $response = {};
131         my $lastr = [];
132         my $session = OpenSRF::AppSession->create("open-ils.storage");
133
134         for my $req (@$reqs) {
135
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);
142
143                 warn "Flattening $class : $term\n";
144                 $response->{$class} = {} unless exists $response->{$class};
145                 $response->{$class}->{$term} = _auth_flatten( $term, $fr, $al, 1 );
146
147         }
148
149         #warn Dumper( $response );
150         return $response;
151 }
152
153 sub crossref_authority_batch {
154         my( $self, $client, $reqs ) = @_;
155
156         my $response = {};
157         my $lastr = [];
158         my $session = OpenSRF::AppSession->create("open-ils.storage");
159
160         for my $req (@$reqs) {
161
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);
168
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 );
177                 }
178
179                 $lastr->[0] = $class;
180                 $lastr->[1] = $term; 
181                 $lastr->[2] = $freq->gather(1);
182                 $lastr->[3] = $areq->gather(1);
183         }
184
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);
193         }
194
195         return $response;
196 }
197
198
199
200
201 __PACKAGE__->register_method(
202         method  => "authority_to_html",
203         api_name        => "open-ils.search.authority.to_html" );
204
205 my $parser              = XML::LibXML->new();
206 my $xslt                        = XML::LibXSLT->new();
207 my $stylesheet;
208
209
210 sub authority_to_html {
211         my( $self, $client, $id ) = @_;
212
213         if( !$stylesheet ) {
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 );
220         }
221
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);
226
227         return $html->toString();
228 }
229
230
231
232
233 1;