]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Authority.pm
added HTMLizer
[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 crossref_authority {
18         my $self = shift;
19         my $client = shift;
20         my $class = shift;
21         my $term = shift;
22
23         my $session = OpenSRF::AppSession->create("open-ils.storage");
24
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);
29
30         my $fr = $freq->gather(1);
31         my $al = $areq->gather(1);
32
33         return _auth_flatten( $term, $fr, $al, 1 );
34 }
35
36 sub _auth_flatten {
37         my $term = shift;
38         my $fr = shift;
39         my $al = shift;
40         my $limit = shift;
41
42         my %hash = ();
43         for my $x (@$fr) {
44                 my $string = $$x[0];
45                 for my $i (1..10) {
46                         last unless ($$x[$i]);
47                         if ($string =~ /\W$/o) {
48                                 $string .= ' '.$$x[$i];
49                         } else {
50                                 $string .= ' -- '.$$x[$i];
51                         }
52                 }
53                 next if (lc($string) eq lc($term));
54                 $hash{$string}++;
55                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
56         }
57         my $from = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
58
59 #       $from = [ @$from[0..4] ] if $limit;
60
61         %hash = ();
62         for my $x (@$al) {
63                 my $string = $$x[0];
64                 for my $i (1..10) {
65                         last unless ($$x[$i]);
66                         if ($string =~ /\W$/o) {
67                                 $string .= ' '.$$x[$i];
68                         } else {
69                                 $string .= ' -- '.$$x[$i];
70                         }
71                 }
72                 next if (lc($string) eq lc($term));
73                 $hash{$string}++;
74                 $hash{$string}++ if (lc($$x[0]) eq lc($term));
75         }
76         my $also = [ sort { $hash{$b} <=> $hash{$a} || $a cmp $b } keys %hash ];
77
78 #       $also = [ @$also[0..4] ] if $limit;
79
80
81         return { from => $from, also => $also };
82 }
83
84 __PACKAGE__->register_method(
85         method          => "crossref_authority",
86         api_name        => "open-ils.search.authority.crossref",
87         argc            => 2, 
88         note            => "Searches authority data for existing controlled terms and crossrefs",
89 );              
90
91 __PACKAGE__->register_method(
92         method          => "crossref_authority_batch",
93    api_name     => "open-ils.search.authority.crossref.batch",
94    argc         => 1, 
95    note         => <<"  NOTE");
96         Takes an array of class,term pair sub-arrays and performs an authority lookup for each
97
98         PARAMS( [ ["subject", "earth"], ["author","shakespeare"] ] );
99
100         Returns an object like so:
101         {
102                 "classname" : {
103                         "term" : { "from" : [ ...], "also" : [...] }
104                         "term2" : { "from" : [ ...], "also" : [...] }
105                 }
106         }
107         NOTE
108
109 sub crossref_authority_batch {
110         my( $self, $client, $reqs ) = @_;
111
112         my $response = {};
113         my $lastr = [];
114         my $session = OpenSRF::AppSession->create("open-ils.storage");
115
116         for my $req (@$reqs) {
117
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);
124
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 );
133                 }
134
135                 $lastr->[0] = $class;
136                 $lastr->[1] = $term; 
137                 $lastr->[2] = $freq->gather(1);
138                 $lastr->[3] = $areq->gather(1);
139         }
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 $cls : $trm\n";
147                 $response->{$cls} = {} unless exists $response->{$cls};
148                 $response->{$cls}->{$trm} = _auth_flatten( $trm, $fr, $al, 1);
149         }
150
151         return $response;
152 }
153
154
155
156
157 __PACKAGE__->register_method(
158         method  => "authority_to_html",
159         api_name        => "open-ils.search.authority.to_html" );
160
161 my $parser              = XML::LibXML->new();
162 my $xslt                        = XML::LibXSLT->new();
163 my $stylesheet;
164
165
166 sub authority_to_html {
167         my( $self, $client, $id ) = @_;
168
169         if( !$stylesheet ) {
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 );
176         }
177
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);
182
183         return  $html->toString();
184 }
185
186
187
188
189 1;