]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Authority.pm
adding marc retrieving search method
[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
11 use JSON;
12
13 use Time::HiRes qw(time);
14 use OpenSRF::EX qw(:try);
15 use Digest::MD5 qw(md5_hex);
16
17 sub search_authority {
18         my $self = shift;
19         my $client = shift;
20
21         my $session = OpenSRF::AppSession->create("open-ils.storage");
22         return $session->request( 'open-ils.storage.authority.search.marc.atomic' => @_ )->gather(1);
23 }
24 __PACKAGE__->register_method(
25         method          => "search_authority",
26         api_name        => "open-ils.search.authority.fts",
27         argc            => 2, 
28         note            => "Searches authority data for existing controlled terms and crossrefs",
29 );              
30
31
32 sub crossref_authority {
33         my $self = shift;
34         my $client = shift;
35         my $class = shift;
36         my $term = shift;
37         my $limit = shift || 10;
38
39         my $session = OpenSRF::AppSession->create("open-ils.storage");
40
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);
45
46         my $fr = $freq->gather(1);
47         my $al = $areq->gather(1);
48
49         return _auth_flatten( $term, $fr, $al, 1 );
50 }
51
52 sub _auth_flatten {
53         my $term = shift;
54         my $fr = shift;
55         my $al = shift;
56         my $limit = shift;
57
58         my %hash = ();
59         for my $x (@$fr) {
60                 my $string = $$x[0];
61                 for my $i (1..10) {
62                         last unless ($$x[$i]);
63                         if ($string =~ /\W$/o) {
64                                 $string .= ' '.$$x[$i];
65                         } else {
66                                 $string .= ' -- '.$$x[$i];
67                         }
68                 }
69                 next if (lc($string) eq lc($term));
70                 $hash{$string}++;
71                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
72         }
73         my $from = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
74
75 #       $from = [ @$from[0..4] ] if $limit;
76
77         %hash = ();
78         for my $x (@$al) {
79                 my $string = $$x[0];
80                 for my $i (1..10) {
81                         last unless ($$x[$i]);
82                         if ($string =~ /\W$/o) {
83                                 $string .= ' '.$$x[$i];
84                         } else {
85                                 $string .= ' -- '.$$x[$i];
86                         }
87                 }
88                 next if (lc($string) eq lc($term));
89                 $hash{$string}++;
90                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
91         }
92         my $also = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
93
94 #       $also = [ @$also[0..4] ] if $limit;
95
96
97         return { from => $from, also => $also };
98 }
99
100 __PACKAGE__->register_method(
101         method          => "crossref_authority",
102         api_name        => "open-ils.search.authority.crossref",
103         argc            => 2, 
104         note            => "Searches authority data for existing controlled terms and crossrefs",
105 );              
106
107 __PACKAGE__->register_method(
108         method          => "crossref_authority_batch",
109    api_name     => "open-ils.search.authority.crossref.batch",
110    argc         => 1, 
111    note         => <<"  NOTE");
112         Takes an array of class,term pair sub-arrays and performs an authority lookup for each
113
114         PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
115
116         Returns an object like so:
117         {
118                 "classname" : {
119                         "term" : { "from" : [ ...], "also" : [...] }
120                         "term2" : { "from" : [ ...], "also" : [...] }
121                 }
122         }
123         NOTE
124
125 sub crossref_authority_batch {
126         my( $self, $client, $reqs ) = @_;
127
128         my $response = {};
129         my $lastr = [];
130         my $session = OpenSRF::AppSession->create("open-ils.storage");
131
132         for my $req (@$reqs) {
133
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);
140
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 );
149                 }
150
151                 $lastr->[0] = $class;
152                 $lastr->[1] = $term; 
153                 $lastr->[2] = $freq->gather(1);
154                 $lastr->[3] = $areq->gather(1);
155         }
156
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);
165         }
166
167         return $response;
168 }
169
170
171
172
173 __PACKAGE__->register_method(
174         method  => "authority_to_html",
175         api_name        => "open-ils.search.authority.to_html" );
176
177 my $parser              = XML::LibXML->new();
178 my $xslt                        = XML::LibXSLT->new();
179 my $stylesheet;
180
181
182 sub authority_to_html {
183         my( $self, $client, $id ) = @_;
184
185         if( !$stylesheet ) {
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 );
192         }
193
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);
198
199         return $html->toString();
200 }
201
202
203
204
205 1;