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