]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/AddedContent/Syndetic.pm
Patch from Dan Scott to move JSON to OpenSRF::Utils::JSON:
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / AddedContent / Syndetic.pm
1 package OpenILS::WWW::AddedContent::Syndetic;
2 use strict; use warnings;
3 use LWP::UserAgent;
4 use OpenSRF::Utils::Logger qw/$logger/;
5 use OpenSRF::Utils::SettingsParser;
6 use OpenSRF::Utils::JSON;
7 use OpenSRF::EX qw/:try/;
8 use OpenILS::WWW::AddedContent;
9
10
11
12 sub new {
13         my( $class, $args ) = @_;
14         $class = ref $class || $class;
15         return bless($args, $class);
16 }
17
18 sub base_url {
19         my $self = shift;
20         return $self->{base_url};
21 }
22
23 sub userid {
24         my $self = shift;
25         return $self->{userid};
26 }
27
28
29 # --------------------------------------------------------------------------
30
31 sub toc_html {
32         my( $self, $key ) = @_;
33         return $self->send_html(
34                 $self->fetch_content('toc.html', $key));
35 }
36
37 sub toc_xml {
38         my( $self, $key ) = @_;
39         return $self->send_xml(
40                 $self->fetch_content('toc.xml', $key));
41 }
42
43 sub toc_json {
44         my( $self, $key ) = @_;
45         return $self->send_json(
46                 $self->fetch_content('toc.xml', $key));
47 }
48
49 # --------------------------------------------------------------------------
50
51 sub anotes_html {
52         my( $self, $key ) = @_;
53         return $self->send_html(
54                 $self->fetch_content('anotes.html', $key));
55 }
56
57 sub anotes_xml {
58         my( $self, $key ) = @_;
59         return $self->send_xml(
60                 $self->fetch_content('anotes.xml', $key));
61 }
62
63 sub anotes_json {
64         my( $self, $key ) = @_;
65         return $self->send_json(
66                 $self->fetch_content('anotes.xml', $key));
67 }
68
69
70 # --------------------------------------------------------------------------
71
72 sub excerpt_html {
73         my( $self, $key ) = @_;
74         return $self->send_html(
75                 $self->fetch_content('dbchapter.html', $key));
76 }
77
78 sub excerpt_xml {
79         my( $self, $key ) = @_;
80         return $self->send_xml(
81                 $self->fetch_content('dbchapter.xml', $key));
82 }
83
84 sub excerpt_json {
85         my( $self, $key ) = @_;
86         return $self->send_json(
87                 $self->fetch_content('dbchapter.xml', $key));
88 }
89
90 # --------------------------------------------------------------------------
91
92 sub reviews_html {
93         my( $self, $key ) = @_;
94
95         my %reviews;
96
97         $reviews{ljreview} = $self->fetch_content('ljreview.html', $key);
98         $reviews{pwreview} = $self->fetch_content('pwreview.html', $key);
99         $reviews{slreview} = $self->fetch_content('slreview.html', $key);
100         $reviews{chreview} = $self->fetch_content('chreview.html', $key);
101         $reviews{blreview} = $self->fetch_content('blreview.html', $key);
102         $reviews{hbreview} = $self->fetch_content('hbreview.html', $key);
103         $reviews{kirkreview} = $self->fetch_content('kirkreview.html', $key);
104
105         for(keys %reviews) {
106                 if( ! $self->data_exists($reviews{$_}) ) {
107                         delete $reviews{$_};
108                         next;
109                 }
110                 $reviews{$_} =~ s/<!.*?>//og; # Strip any doctype declarations
111         }
112
113         return 0 if scalar(keys %reviews) == 0;
114         
115         #my $html = "<div>";
116         my $html;
117         $html .= $reviews{$_} for keys %reviews;
118         #$html .= "</div>";
119
120         return $self->send_html($html);
121 }
122
123 # we have to aggregate the reviews
124 sub reviews_xml {
125         my( $self, $key ) = @_;
126         my %reviews;
127
128         $reviews{ljreview} = $self->fetch_content('ljreview.xml', $key);
129         $reviews{pwreview} = $self->fetch_content('pwreview.xml', $key);
130         $reviews{slreview} = $self->fetch_content('slreview.xml', $key);
131         $reviews{chreview} = $self->fetch_content('chreview.xml', $key);
132         $reviews{blreview} = $self->fetch_content('blreview.xml', $key);
133         $reviews{hbreview} = $self->fetch_content('hbreview.xml', $key);
134         $reviews{kirkreview} = $self->fetch_content('kirkreview.xml', $key);
135
136         for(keys %reviews) {
137                 if( ! $self->data_exists($reviews{$_}) ) {
138                         delete $reviews{$_};
139                         next;
140                 }
141                 # Strip the xml and doctype declarations
142                 $reviews{$_} =~ s/<\?xml.*?>//og;
143                 $reviews{$_} =~ s/<!.*?>//og;
144         }
145
146         return 0 if scalar(keys %reviews) == 0;
147         
148         my $xml = "<reviews>";
149         $xml .= $reviews{$_} for keys %reviews;
150         $xml .= "</reviews>";
151
152         return $self->send_xml($xml);
153 }
154
155
156 sub reviews_json {
157         my( $self, $key ) = @_;
158         return $self->send_json(
159                 $self->fetch_content('dbchapter.xml', $key));
160 }
161
162 # --------------------------------------------------------------------------
163
164
165 sub data_exists {
166         my( $self, $data ) = @_;
167         return 0 if $data =~ m/<title>error<\/title>/iog;
168         return 1;
169 }
170
171
172 sub send_json {
173         my( $self, $xml ) = @_;
174         return 0 unless $self->data_exists($xml);
175         my $doc;
176
177         try {
178                 $doc = XML::LibXML->new->parse_string($xml);
179         } catch Error with {
180                 my $err = shift;
181                 $logger->error("added content XML parser error: $err\n\n$xml");
182                 $doc = undef;
183         };
184
185         return 0 unless $doc;
186         my $perl = OpenSRF::Utils::SettingsParser::XML2perl($doc->documentElement);
187         my $json = OpenSRF::Utils::JSON->perl2JSON($perl);
188         print "Content-type: text/plain\n\n";
189         print $json;
190         return 1;
191 }
192
193 sub send_xml {
194         my( $self, $xml ) = @_;
195         return 0 unless $self->data_exists($xml);
196         print "Content-Type: application/xml\n\n";
197         print $xml;
198         return 1;
199 }
200
201 sub send_html {
202         my( $self, $content ) = @_;
203         return 0 unless $self->data_exists($content);
204
205         # Hide anything that might contain a link since it will be broken
206         my $HTML = <<"  HTML";
207                 <div>
208                         <style type='text/css'>
209                                 div.ac input, div.ac a[href],div.ac img, div.ac button { display: none; visibility: hidden }
210                         </style>
211                         <div class='ac'>
212                                 $content
213                         </div>
214                 </div>
215         HTML
216
217         print "Content-type: text/html\n\n";
218         print $HTML;
219
220         return 1;
221 }
222
223 sub fetch_content {
224         my( $self, $page, $key ) = @_;
225         my $uname = $self->userid;
226         my $url = $self->base_url . "?isbn=$key/$page&client=$uname&type=rw12";
227     return OpenILS::WWW::AddedContent->get_url($url);
228 }
229
230
231
232 1;