Fixing isbn regex to not match unless isbn is valid
[koha.git] / C4 / XISBN.pm
1 package C4::XISBN;
2 # Copyright (C) 2007 LibLime
3 # Joshua Ferraro <jmf@liblime.com>
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use XML::Simple;
21 #use LWP::Simple;
22 use C4::Biblio;
23 use C4::Items;
24
25 use LWP::UserAgent;
26 use HTTP::Request::Common;
27
28 use strict;
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30
31 BEGIN {
32         require Exporter;
33         $VERSION = 3.01;
34         @ISA = qw(Exporter);
35         @EXPORT_OK = qw(
36                 &get_xisbns
37                 &get_biblio_from_xisbn
38         &get_biblionumber_from_isbn
39         );
40 }
41
42 sub get_biblionumber_from_isbn {
43     my $isbn = shift;
44      if ($isbn =~ /(\d{9,}[X]*)/) {
45         $isbn = $1.'%';
46     }
47     my @biblionumbers;
48     my $dbh=C4::Context->dbh;
49     my $query = "SELECT biblionumber FROM biblioitems WHERE isbn LIKE ? LIMIT 10";
50     my $sth = $dbh->prepare($query);
51     $sth->execute($isbn);
52     while ( my $biblionumber = $sth->fetchrow_hashref() ) {
53         push (@biblionumbers, $biblionumber);
54     }
55     return \@biblionumbers;
56 }
57 =head1 NAME
58
59 C4::XISBN - Functions for retrieving XISBN content in Koha
60
61 =head1 FUNCTIONS
62
63 This module provides facilities for retrieving ThingISBN and XISBN content in Koha
64
65 =cut
66
67 sub get_biblio_from_xisbn {
68     my $xisbn = shift;
69     $xisbn .='%' if ($xisbn =~ /(\d{9,}[X]*)/);
70     my $dbh = C4::Context->dbh;
71     my $query = "SELECT biblionumber FROM biblioitems WHERE isbn LIKE ?";
72     my $sth = $dbh->prepare($query);
73     $sth->execute($xisbn);
74     my $xbib_data =  $sth->fetchrow_hashref();
75     my $xbiblio;
76     if ($xbib_data->{biblionumber}) {
77         $xbiblio = GetBiblioData($xbib_data->{biblionumber});
78         $xbiblio->{isbn} =~ /(\d{9,}[X]*)/;
79         $xbiblio->{amazonisbn} = $1;
80         $xbiblio->{items} = GetItemsByBiblioitemnumber($xbib_data->{biblionumber});
81     }
82     return ($xbiblio);
83 }
84
85 =head1 get_xisbns($isbn);
86
87 =head2 $isbn is an ISBN string
88
89 =cut
90
91 sub get_xisbns {
92     my ( $isbn ) = @_;
93     my ($response,$thing_response,$xisbn_response,$gapines_response);
94     $isbn =~ /(\d{9,}[X]*)/;
95     $isbn = $1;
96     # THINGISBN
97     if ( C4::Context->preference('ThingISBN') ) {
98         my $url = "http://www.librarything.com/api/thingISBN/".$isbn;
99         $thing_response = _get_url($url,'thingisbn');
100     }
101
102     # XISBN
103     if ( C4::Context->preference('XISBN') ) {
104         my $affiliate_id=C4::Context->preference('OCLCAffiliateID');
105         my $limit = C4::Context->preference('XISBNDailyLimit') || 499;
106         my $reached_limit = _service_throttle('xisbn',$limit);
107         my $url = "http://xisbn.worldcat.org/webservices/xid/isbn/".$isbn."?method=getEditions&format=xml&fl=form,year,lang,ed";
108         $url.="&ai=".$affiliate_id if $affiliate_id;
109         unless ($reached_limit) {
110             $xisbn_response = _get_url($url,'xisbn');
111         }
112     }
113
114     # PINES ISBN (Experimental)
115     #if ( C4::Context->preference('PINESISBN') ) {
116     #    my $url = "http://www.librarything.com/api/thingISBN/".$isbn;
117     #    $gapines_response = _get_url($url,'thingisbn');
118     #}
119     $response->{isbn} = [ @{ $xisbn_response->{isbn} or [] }, @{ $thing_response->{isbn} or [] }, @{ $gapines_response->{isbn} or [] } ];
120     my @xisbns;
121     my $unique_xisbns; # a hashref
122
123     # loop through each ISBN and scope to the local collection
124     for my $response_data( @{ $response->{ isbn } } ) {
125         next if $response_data->{'content'} eq $isbn;
126         next if $isbn eq $response_data;
127         next if $unique_xisbns->{ $response_data->{content} };
128         $unique_xisbns->{ $response_data->{content} }++;
129         my $xbiblio= get_biblio_from_xisbn($response_data->{content});
130         push @xisbns, $xbiblio if $xbiblio; #response_data->{xbiblio}; #->{biblionumber}; # if $xbiblionumber;
131     }
132     return \@xisbns;
133 }
134
135 sub _get_url {
136     my ($url,$service_type) = @_;
137     my $ua = LWP::UserAgent->new(
138         timeout => 2
139         );
140
141     my $response = $ua->get($url);
142     if ($response->is_success) {
143         warn "WARNING could not retrieve $service_type $url" unless $response;
144         if ($response) {
145             my $xmlsimple = XML::Simple->new();
146             my $content = $xmlsimple->XMLin(
147             $response->content,
148             ForceArray => [ qw(isbn) ],
149             ForceContent => 1,
150             );
151             return $content;
152         }
153     } else {
154         warn "WARNING: URL Request Failed " . $response->status_line . "\n";
155     }
156
157 }
158
159
160 # Throttle services to the specified amount
161 sub _service_throttle {
162     my ($service_type,$daily_limit) = @_;
163     my $dbh = C4::Context->dbh;
164     my $sth = $dbh->prepare("SELECT service_count FROM services_throttle WHERE service_type=?");
165     $sth->execute($service_type);
166     my $count = 1;
167
168     while (my $counter = $sth->fetchrow_hashref()) {
169         $count = $counter->{service_count} if $counter->{service_count};
170     }
171
172     # we're over the limit
173     return 1 if $count >= $daily_limit;
174
175     # not over the limit
176     $count++;
177     $sth = $dbh->do("UPDATE services_throttle SET service_count=$count WHERE service_type='xisbn'");
178     return undef;
179 }
180
181 1;
182 __END__
183
184 =head1 NOTES
185
186 =head1 AUTHOR
187
188 Joshua Ferraro <jmf@liblime.com>
189
190 =cut
191