Bug 12858 [QA Followup] - Don't call webservice if we have no data
[koha.git] / C4 / External / Syndetics.pm
blob50f1e649208dd7d4f1aa82ca3a00026c18e9a21e
1 package C4::External::Syndetics;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use XML::Simple;
21 use XML::LibXML;
22 use LWP::Simple;
23 use LWP::UserAgent;
24 use HTTP::Request::Common;
26 use strict;
27 use warnings;
29 use vars qw($VERSION @ISA @EXPORT);
31 BEGIN {
32 require Exporter;
33 $VERSION = 3.07.00.049;
34 @ISA = qw(Exporter);
35 @EXPORT = qw(
36 &get_syndetics_index
37 &get_syndetics_summary
38 &get_syndetics_toc
39 &get_syndetics_editions
40 &get_syndetics_excerpt
41 &get_syndetics_reviews
42 &get_syndetics_anotes
46 # package-level variable
47 my $parser = XML::LibXML->new();
49 =head1 NAME
51 C4::External::Syndetics - Functions for retrieving Syndetics content in Koha
53 =head1 FUNCTIONS
55 This module provides facilities for retrieving Syndetics.com content in Koha
57 =head2 get_syndetics_summary
59 my $syndetics_summary= &get_syndetics_summary( $isbn );
61 Get Summary data from Syndetics
63 =cut
65 sub get_syndetics_index {
66 my ( $isbn, $upc, $oclc ) = @_;
68 return unless ( $isbn || $upc || $oclc );
70 my $response = _fetch_syndetics_content('INDEX.XML', $isbn, $upc, $oclc);
71 unless ($response->content_type =~ /xml/) {
72 return;
75 my $content = $response->content;
76 my $xmlsimple = XML::Simple->new();
77 $response = $xmlsimple->XMLin(
78 $content,
79 ) unless !$content;
81 my $syndetics_elements;
82 for my $available_type ('SUMMARY','TOC','FICTION','AWARDS1','SERIES1','SPSUMMARY','SPREVIEW', 'AVPROFILE', 'AVSUMMARY','DBCHAPTER','LJREVIEW','PWREVIEW','SLJREVIEW','CHREVIEW','BLREVIEW','HBREVIEW','KIREVIEW','CRITICASREVIEW','ANOTES') {
83 if (exists $response->{$available_type} && $response->{$available_type} =~ /$available_type/) {
84 $syndetics_elements->{$available_type} = $available_type;
88 return $syndetics_elements if $syndetics_elements;
91 sub get_syndetics_summary {
92 my ( $isbn, $upc, $oclc, $syndetics_elements ) = @_;
94 my $summary_type = exists($syndetics_elements->{'AVSUMMARY'}) ? 'AVSUMMARY.XML' : 'SUMMARY.XML';
95 my $response = _fetch_syndetics_content($summary_type, $isbn, $upc, $oclc);
96 unless ($response->content_type =~ /xml/) {
97 return;
100 my $content = $response->content;
102 my $summary;
103 eval {
104 my $doc = $parser->parse_string($content);
105 $summary = $doc->findvalue('//Fld520');
107 if ($@) {
108 warn "Error parsing Syndetics $summary_type";
110 return $summary if $summary;
113 sub get_syndetics_toc {
114 my ( $isbn,$upc,$oclc ) = @_;
116 my $response = _fetch_syndetics_content('TOC.XML', $isbn, $upc, $oclc);
117 unless ($response->content_type =~ /xml/) {
118 return;
121 my $content = $response->content;
122 my $xmlsimple = XML::Simple->new();
123 $response = $xmlsimple->XMLin(
124 $content,
125 forcearray => [ qw(Fld970) ],
126 ) unless !$content;
127 # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
128 my $toc;
129 $toc = \@{$response->{VarFlds}->{VarDFlds}->{SSIFlds}->{Fld970}} if $response;
130 return $toc if $toc;
133 sub get_syndetics_excerpt {
134 my ( $isbn,$upc,$oclc ) = @_;
136 my $response = _fetch_syndetics_content('DBCHAPTER.XML', $isbn, $upc, $oclc);
137 unless ($response->content_type =~ /xml/) {
138 return;
141 my $content = $response->content;
142 my $xmlsimple = XML::Simple->new();
143 $response = $xmlsimple->XMLin(
144 $content,
145 forcearray => [ qw(Fld520) ],
146 ) unless !$content;
147 # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
148 my $excerpt;
149 $excerpt = \@{$response->{VarFlds}->{VarDFlds}->{Notes}->{Fld520}} if $response;
150 return XMLout($excerpt, NoEscape => 1) if $excerpt;
153 sub get_syndetics_reviews {
154 my ( $isbn,$upc,$oclc,$syndetics_elements ) = @_;
156 my @reviews;
157 my $review_sources = [
158 {title => 'Library Journal Review', file => 'LJREVIEW.XML', element => 'LJREVIEW'},
159 {title => 'Publishers Weekly Review', file => 'PWREVIEW.XML', element => 'PWREVIEW'},
160 {title => 'School Library Journal Review', file => 'SLJREVIEW.XML', element => 'SLJREVIEW'},
161 {title => 'CHOICE Review', file => 'CHREVIEW.XML', element => 'CHREVIEW'},
162 {title => 'Booklist Review', file => 'BLREVIEW.XML', element => 'BLREVIEW'},
163 {title => 'Horn Book Review', file => 'HBREVIEW.XML', element => 'HBREVIEW'},
164 {title => 'Kirkus Book Review', file => 'KIREVIEW.XML', element => 'KIREVIEW'},
165 {title => 'Criticas Review', file => 'CRITICASREVIEW.XML', element => 'CRITICASREVIEW'},
166 {title => 'Spanish Review', file => 'SPREVIEW.XML', element => 'SPREVIEW'},
169 for my $source (@$review_sources) {
170 if ($syndetics_elements->{$source->{element}} and $source->{element} =~ $syndetics_elements->{$source->{element}}) {
172 } else {
173 #warn "Skipping $source->{element} doesn't match $syndetics_elements->{$source->{element}} \n";
174 next;
176 my $response = _fetch_syndetics_content($source->{file}, $isbn, $upc, $oclc);
177 unless ($response->content_type =~ /xml/) {
178 next;
181 my $content = $response->content;
183 eval {
184 my $doc = $parser->parse_string($content);
186 # note that using findvalue strips any HTML elements embedded
187 # in that review. That helps us handle slight differences
188 # in the output provided by Syndetics 'old' and 'new' versions
189 # of their service and cleans any questionable HTML that
190 # may be present in the reviews, but does mean that any
191 # <B> and <I> tags used to format the review are also gone.
192 my $result = $doc->findvalue('//Fld520');
193 push @reviews, {title => $source->{title}, reviews => [ { content => $result } ]} if $result;
195 if ($@) {
196 warn "Error parsing Syndetics $source->{title} review";
199 return \@reviews;
202 sub get_syndetics_editions {
203 my ( $isbn,$upc,$oclc ) = @_;
205 my $response = _fetch_syndetics_content('FICTION.XML', $isbn, $upc, $oclc);
206 unless ($response->content_type =~ /xml/) {
207 return;
210 my $content = $response->content;
212 my $xmlsimple = XML::Simple->new();
213 $response = $xmlsimple->XMLin(
214 $content,
215 forcearray => [ qw(Fld020) ],
216 ) unless !$content;
217 # manipulate response USMARC VarFlds VarDFlds Notes Fld520 a
218 my $similar_items;
219 $similar_items = \@{$response->{VarFlds}->{VarDFlds}->{NumbCode}->{Fld020}} if $response;
220 return $similar_items if $similar_items;
223 sub get_syndetics_anotes {
224 my ( $isbn,$upc,$oclc) = @_;
226 my $response = _fetch_syndetics_content('ANOTES.XML', $isbn, $upc, $oclc);
227 unless ($response->content_type =~ /xml/) {
228 return;
231 my $content = $response->content;
233 my $xmlsimple = XML::Simple->new();
234 $response = $xmlsimple->XMLin(
235 $content,
236 forcearray => [ qw(Fld980) ],
237 ForceContent => 1,
238 ) unless !$content;
239 my @anotes;
240 for my $fld980 (@{$response->{VarFlds}->{VarDFlds}->{SSIFlds}->{Fld980}}) {
241 # this is absurd, but sometimes this data serializes differently
242 if(ref($fld980->{a}->{content}) eq 'ARRAY') {
243 for my $content (@{$fld980->{a}->{content}}) {
244 push @anotes, {content => $content};
248 else {
249 push @anotes, {content => $fld980->{a}->{content}};
252 return \@anotes;
255 sub _fetch_syndetics_content {
256 my ( $element, $isbn, $upc, $oclc ) = @_;
258 $isbn = '' unless defined $isbn;
259 $upc = '' unless defined $upc;
260 $oclc = '' unless defined $oclc;
262 my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
264 my $url = "http://www.syndetics.com/index.aspx?isbn=$isbn/$element&client=$syndetics_client_code&type=xw10&upc=$upc&oclc=$oclc";
265 my $ua = LWP::UserAgent->new;
266 $ua->timeout(10);
267 $ua->env_proxy;
268 my $response = $ua->get($url);
270 warn "could not retrieve $url" unless $response->content;
271 return $response;
275 __END__
277 =head1 NOTES
279 =cut
281 =head1 AUTHOR
283 Joshua Ferraro <jmf@liblime.com>
285 =cut