Bug 13642 - Remove MARC::Crosswalk::DublinCore from Koha
[koha.git] / C4 / External / Syndetics.pm
1 package C4::External::Syndetics;
2 # Copyright (C) 2006 LibLime
3 # <jmf at liblime dot com>
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use XML::Simple;
21 use XML::LibXML;
22 use LWP::Simple;
23 use LWP::UserAgent;
24 use HTTP::Request::Common;
25
26 use strict;
27 use warnings;
28
29 use vars qw($VERSION @ISA @EXPORT);
30
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
43     );
44 }
45
46 # package-level variable
47 my $parser = XML::LibXML->new();
48
49 =head1 NAME
50
51 C4::External::Syndetics - Functions for retrieving Syndetics content in Koha
52
53 =head1 FUNCTIONS
54
55 This module provides facilities for retrieving Syndetics.com content in Koha
56
57 =head2 get_syndetics_summary
58
59   my $syndetics_summary= &get_syndetics_summary( $isbn );
60
61 Get Summary data from Syndetics
62
63 =cut
64
65 sub get_syndetics_index {
66     my ( $isbn, $upc, $oclc ) = @_;
67
68     return unless ( $isbn || $upc || $oclc );
69
70     my $response = _fetch_syndetics_content('INDEX.XML', $isbn, $upc, $oclc);
71     unless ($response->content_type =~ /xml/) {
72         return;
73     }
74
75     my $content = $response->content;
76     my $xmlsimple = XML::Simple->new();
77     $response = $xmlsimple->XMLin(
78         $content,
79     ) unless !$content;
80
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;
85         }
86     }
87
88     return $syndetics_elements if $syndetics_elements;
89 }
90
91 sub get_syndetics_summary {
92     my ( $isbn, $upc, $oclc, $syndetics_elements ) = @_;
93
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;
98     }  
99
100     my $content = $response->content;
101
102     my $summary;
103     eval { 
104         my $doc = $parser->parse_string($content);
105         $summary = $doc->findvalue('//Fld520');
106     };
107     if ($@) {
108         warn "Error parsing Syndetics $summary_type";
109     }
110     return $summary if $summary;
111 }
112
113 sub get_syndetics_toc {
114     my ( $isbn,$upc,$oclc ) = @_;
115
116     my $response = _fetch_syndetics_content('TOC.XML', $isbn, $upc, $oclc);
117     unless ($response->content_type =~ /xml/) {
118         return;
119     }  
120
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;
131 }
132
133 sub get_syndetics_excerpt {
134     my ( $isbn,$upc,$oclc ) = @_;
135
136     my $response = _fetch_syndetics_content('DBCHAPTER.XML', $isbn, $upc, $oclc);
137     unless ($response->content_type =~ /xml/) {
138         return;
139     }  
140         
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;
151 }
152
153 sub get_syndetics_reviews {
154     my ( $isbn,$upc,$oclc,$syndetics_elements ) = @_;
155
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'},
167     ];
168
169     for my $source (@$review_sources) {
170         if ($syndetics_elements->{$source->{element}} and $source->{element} =~ $syndetics_elements->{$source->{element}}) {
171
172         } else {
173             #warn "Skipping $source->{element} doesn't match $syndetics_elements->{$source->{element}} \n";
174             next;
175         }
176         my $response = _fetch_syndetics_content($source->{file}, $isbn, $upc, $oclc);
177         unless ($response->content_type =~ /xml/) {
178             next;
179         }
180
181         my $content = $response->content;
182        
183         eval { 
184             my $doc = $parser->parse_string($content);
185
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;
194         };
195         if ($@) {
196             warn "Error parsing Syndetics $source->{title} review";
197         }
198     }
199     return \@reviews;
200 }
201
202 sub get_syndetics_editions {
203     my ( $isbn,$upc,$oclc ) = @_;
204
205     my $response = _fetch_syndetics_content('FICTION.XML', $isbn, $upc, $oclc);
206     unless ($response->content_type =~ /xml/) {
207         return;
208     }  
209
210     my $content = $response->content;
211
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;
221 }
222
223 sub get_syndetics_anotes {
224     my ( $isbn,$upc,$oclc) = @_;
225
226     my $response = _fetch_syndetics_content('ANOTES.XML', $isbn, $upc, $oclc);
227     unless ($response->content_type =~ /xml/) {
228         return;
229     }
230
231     my $content = $response->content;
232
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};
245                 
246             }
247         }
248         else {
249             push @anotes, {content => $fld980->{a}->{content}};
250         }
251     }
252     return \@anotes;
253 }
254
255 sub _fetch_syndetics_content {
256     my ( $element, $isbn, $upc, $oclc ) = @_;
257
258     $isbn = '' unless defined $isbn;
259     $upc  = '' unless defined $upc;
260     $oclc = '' unless defined $oclc;
261
262     my $syndetics_client_code = C4::Context->preference('SyndeticsClientCode');
263
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);
269
270     warn "could not retrieve $url" unless $response->content;
271     return $response;
272
273 }
274 1;
275 __END__
276
277 =head1 NOTES
278
279 =cut
280
281 =head1 AUTHOR
282
283 Joshua Ferraro <jmf@liblime.com>
284
285 =cut