[Koha-patches] [PATCH] bug 3219: handle variation in Amazon responses

Galen Charlton galen.charlton at liblime.com
Tue May 26 21:20:03 CEST 2009


An Amazon ItemLookup response can sometimes have
more than one Item element, where the ones after
the first appear to be cross references to items
that have the same ISBN.  Changed parsing so that
those responses are no longer treated like pseudohashes.

This fixes the following crash:

[error] detail.pl: Pseudo-hashes are deprecated at /catalogue/detail.pl line
213., referer: /cgi-bin/koha/catalogue/detail.pl

Which may lead to a nasty:
[error] Out of memory!, referer: /cgi-bin/koha/catalogue/detail.pl
---
 C4/External/Amazon.pm |    2 +-
 catalogue/detail.pl   |    8 ++++----
 opac/opac-detail.pl   |    4 ++--
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/C4/External/Amazon.pm b/C4/External/Amazon.pm
index f89200a..47f4b5e 100644
--- a/C4/External/Amazon.pm
+++ b/C4/External/Amazon.pm
@@ -146,7 +146,7 @@ sub get_amazon_details {
     my $xmlsimple = XML::Simple->new();
     my $response = $xmlsimple->XMLin(
         $content,
-        forcearray => [ qw(SimilarProduct EditorialReview Review) ],
+        forcearray => [ qw(SimilarProduct EditorialReview Review Item) ],
     ) unless !$content;
     return $response;
 }
diff --git a/catalogue/detail.pl b/catalogue/detail.pl
index 1f5a291..649c46f 100755
--- a/catalogue/detail.pl
+++ b/catalogue/detail.pl
@@ -239,7 +239,7 @@ if ( C4::Context->preference("AmazonEnabled") == 1 ) {
     if ( $amazon_similars ) {
         my $similar_products_exist;
         my @similar_products;
-        for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
+        for my $similar_product (@{$amazon_details->{Items}->{Item}->[0]->{SimilarProducts}->{SimilarProduct}}) {
             # do we have any of these isbns in our collection?
             my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
             # verify that there is at least one similar item
@@ -252,10 +252,10 @@ if ( C4::Context->preference("AmazonEnabled") == 1 ) {
         $template->param( AMAZON_SIMILAR_PRODUCTS  => \@similar_products      );
     }
     if ( $amazon_reviews ) {
-        my $item = $amazon_details->{Items}->{Item};
+        my $item = $amazon_details->{Items}->{Item}->[0];
         my $editorial_reviews = \@{ $item->{EditorialReviews}->{EditorialReview} };
-        #my $customer_reviews  = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
-        #my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating} || 0;
+        #my $customer_reviews  = \@{$amazon_details->{Items}->{Item}->[0]->{CustomerReviews}->{Review}};
+        #my $average_rating = $amazon_details->{Items}->{Item}->[0]->{CustomerReviews}->{AverageRating} || 0;
         #$template->param( amazon_average_rating    => $average_rating * 20    );
         #$template->param( AMAZON_CUSTOMER_REVIEWS  => $customer_reviews       );
         $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews      );
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index 18b00cb..a29fdb2 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -285,7 +285,7 @@ if ( C4::Context->preference("OPACAmazonEnabled") ) {
     my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour, \@services );
     my $similar_products_exist;
     if ( $amazon_reviews ) {
-        my $item = $amazon_details->{Items}->{Item};
+        my $item = $amazon_details->{Items}->{Item}->[0];
         my $customer_reviews = \@{ $item->{CustomerReviews}->{Review} };
         for my $one_review ( @$customer_reviews ) {
             $one_review->{Date} = format_date($one_review->{Date});
@@ -297,7 +297,7 @@ if ( C4::Context->preference("OPACAmazonEnabled") ) {
         $template->param( AMAZON_EDITORIAL_REVIEWS => $editorial_reviews );
     }
     if ( $amazon_similars ) {
-        my $item = $amazon_details->{Items}->{Item};
+        my $item = $amazon_details->{Items}->{Item}->[0];
         my @similar_products;
         for my $similar_product (@{ $item->{SimilarProducts}->{SimilarProduct} }) {
             # do we have any of these isbns in our collection?
-- 
1.5.6.5




More information about the Koha-patches mailing list