From d1c1150e31b68c532a9146d5d595d5e53c950c2e Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Tue, 26 May 2009 14:20:03 -0500 Subject: [PATCH] bug 3219: handle variation in Amazon responses 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 Signed-off-by: Galen Charlton --- C4/External/Amazon.pm | 2 +- catalogue/detail.pl | 49 +++++++++++++++++++++++++++++++------------ opac/opac-detail.pl | 37 ++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 27 deletions(-) diff --git a/C4/External/Amazon.pm b/C4/External/Amazon.pm index 35f9f1b68d..41076cf3a5 100644 --- a/C4/External/Amazon.pm +++ b/C4/External/Amazon.pm @@ -136,7 +136,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 bf608cb685..a8a0cfa3d4 100755 --- a/catalogue/detail.pl +++ b/catalogue/detail.pl @@ -208,20 +208,43 @@ if (C4::Context->preference("FRBRizeEditions")==1) { }; if ($@) { warn "XISBN Failed $@"; } } -if ( C4::Context->preference("AmazonContent") == 1 ) { - my $similar_products_exist; - my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour ); - my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}}; - my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}}; - my @similar_products; - for my $similar_product (@{$amazon_details->{Items}->{Item}->{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 - if (scalar(@$similar_biblionumbers)){ - $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]); - push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN} }; +if ( C4::Context->preference("AmazonEnabled") == 1 ) { + $template->param( AmazonTld => get_amazon_tld() ); + my $amazon_reviews = C4::Context->preference("AmazonReviews"); + my $amazon_similars = C4::Context->preference("AmazonSimilarItems"); + my @services; + if ( $amazon_reviews ) { + $template->param( AmazonReviews => 1 ); + push( @services, 'EditorialReview' ); + } + if ( $amazon_similars ) { + $template->param( AmazonSimilarItems => 1 ); + push( @services, 'Similarities' ); + } + my $amazon_details = &get_amazon_details( $isbn, $record, $marcflavour, \@services ); + if ( $amazon_similars ) { + my $similar_products_exist; + my @similar_products; + 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 + if (scalar(@$similar_biblionumbers)){ + $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]); + push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN} }; + } } + $template->param( AmazonSimilarItems => $similar_products_exist ); + $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products ); + } + if ( $amazon_reviews ) { + my $item = $amazon_details->{Items}->{Item}->[0]; + my $editorial_reviews = \@{ $item->{EditorialReviews}->{EditorialReview} }; + #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 ); } my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}}; my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating} || 0; diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index e697d3b1b3..dd2360d052 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -277,20 +277,31 @@ if ( C4::Context->preference("OPACAmazonEnabled") ) { } if ( C4::Context->preference("OPACAmazonEnabled") && C4::Context->preference("OPACAmazonSimilarItems") ) { my $similar_products_exist; - my $amazon_details = &get_amazon_details( $xisbn, $record, $marcflavour ); - my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}}; - my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}}; - for my $one_review (@$customer_reviews) { - $one_review->{Date} = format_date($one_review->{Date}); + my $amazon_reviews = C4::Context->preference("AmazonReviews"); + my $amazon_similars = C4::Context->preference("AmazonSimilarItems"); + if ( $amazon_reviews ) { + 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}); + } + my $editorial_reviews = \@{ $item->{EditorialReviews}->{EditorialReview} }; + my $average_rating = $item->{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 ); } - my @similar_products; - for my $similar_product (@{$amazon_details->{Items}->{Item}->{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 - if (scalar(@$similar_biblionumbers)){ - $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]); - push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN} }; + if ( $amazon_similars ) { + 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? + my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN}); + # verify that there is at least one similar item + if (scalar(@$similar_biblionumbers)){ + $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]); + push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN} }; + } } } my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}}; -- 2.39.5