From 7aac1571b59cecb607c654e7a595785a876b1a12 Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Thu, 22 Jun 2017 07:49:42 +0000 Subject: [PATCH] Bug 18618: Add a pagination for suggestions *** Suggestions are now sent from Mana as follows - Koha requests the first 20 suggestions - It processes them and estimates how much it will need to get all suggestions - It requests Mana the number of suggestions needed - It repeats the 3 first steps until (1) Mana doesn't have suggestions anymore (2) It got 10 suggestions to display *** Koha interface is a bit more verbose while searching (just a bit). --- misc/cronjobs/mana_send_pairs.pl | 20 +++-- opac/svc/mana/getSuggestion | 139 +++++++++++++++++++------------ 2 files changed, 98 insertions(+), 61 deletions(-) diff --git a/misc/cronjobs/mana_send_pairs.pl b/misc/cronjobs/mana_send_pairs.pl index 50ea86b7a8..e307ed0ff8 100644 --- a/misc/cronjobs/mana_send_pairs.pl +++ b/misc/cronjobs/mana_send_pairs.pl @@ -1,3 +1,4 @@ +#/!usr/bin/perl use Modern::Perl; use C4::Context; @@ -15,17 +16,19 @@ my $man = 0; my $verbose = 0; my $date=DateTime->now()->strftime("%F"); -print $date; - +my $securitytoken; +my $MANA_IP; GetOptions( 'help|?' => \$help, 'man' => \$man, 'v' => \$verbose, - 'date|d:s' => \$date + 'date|d:s' => \$date, + 'security_token|s:s' => \$securitytoken, + 'mana_ip|i:s' => \$MANA_IP ); - +my $securitytoken = C4::Context->preference('ManaToken'); $date.="%"; my $dbh = C4::Context->dbh; my $query = q{ @@ -68,6 +71,8 @@ $sth->execute; my $row; my @reading_pairs; my $cter = 0; + +#for each reading pair, processes the id pairs while ( $row = $sth->fetchrow_hashref ){ $row->{isbn1} = NormalizeISBN({ isbn => $row->{isbn1}, format => 'ISBN-13', strip_hyphens => 1 }); $row->{isbn2} = NormalizeISBN({ isbn => $row->{isbn2}, format => 'ISBN-13', strip_hyphens => 1 }); @@ -80,17 +85,15 @@ $row->{isbn2} = NormalizeISBN({ isbn => $row->{isbn2}, format => 'ISBN-13', stri $currentpair->{ idtype2 } = $key2; $currentpair->{ documentid2 } = $row->{ $key2."2" }; push @reading_pairs, $currentpair; - } } } } -my $securitytoken = C4::Context->preference('ManaToken'); -my $MANA_IP; my $content; + +#informations for mana $MANA_IP=C4::Context->config("mana_config"); -print $MANA_IP; $content->{ securitytoken } = $securitytoken; $content->{ reading_pairs } = \@reading_pairs; @@ -100,6 +103,7 @@ my $request = HTTP::Request->new( POST => $url ); my $json = to_json( $content, { utf8 => 1 } ); #print $json; +#send to mana $request->content($json); my $result = Koha::SharedContent::manaRequest( $request ); print Data::Dumper::Dumper( $result ); diff --git a/opac/svc/mana/getSuggestion b/opac/svc/mana/getSuggestion index 3760451e94..682b867f3b 100755 --- a/opac/svc/mana/getSuggestion +++ b/opac/svc/mana/getSuggestion @@ -42,7 +42,19 @@ use constant DURATION_BEFORE_REFRESH => DateTime::Duration->new( months => 3 ); use C4::Output qw( output_with_http_headers ); my $input = new CGI; binmode STDOUT, ":encoding(UTF-8)"; + + + + my $biblionumber = $input->param( "biblionumber" ); + +#case we come with the script import_suggestions.pl +my $temp_biblionumber; +eval { $temp_biblionumber = from_json( $input->param( "POSTDATA" ) )->{biblionumber}; }; +if ( $temp_biblionumber ){ + $biblionumber = $temp_biblionumber; +} + my $biblioitem = Koha::Biblioitems->find( $biblionumber ); my $local_suggestions; @@ -65,10 +77,12 @@ if ( $local_suggestions and DateTime::Duration->compare( ($now - $timestamp), DU push @biblios, Koha::Biblios->find( $local_suggestions->{ $key } ); } } + else{ # get all informations to ask mana my $idtype; my $documentid; + my $notnormalizeddocumentid; if ( $biblioitem->isbn ){ $idtype= "isbn"; $documentid= NormalizeISBN({ isbn => $biblioitem->isbn, format => 'ISBN-13', strip_hyphens => 1 }); @@ -84,80 +98,101 @@ else{ else{ die "error: no propper identifier"; } + $notnormalizeddocumentid = $documentid; + my $offset = 1; + my $length = 10; + my $mananotover; + + do{ + #request mana + my $mana_ip = C4::Context->config('mana_config'); + my $url = "$mana_ip/getsuggestion/$notnormalizeddocumentid/$idtype?offset=$offset&length=$length"; + my $request = HTTP::Request->new( GET => $url ); + $request->content_type('aplication/json'); + my $response = Koha::SharedContent::manaRequest( $request ); + + #error handling + my $resources = $response->{data}; + unless ( $resources ){ + my $msg; + $msg = $response->{msg}; + if ( $msg ){ + die $msg; + } + else{ + die "Unknown error"; + } + } - #request mana - my $mana_ip = C4::Context->config('mana_config'); - my $url = "$mana_ip/getsuggestion/$documentid/$idtype"; - my $request = HTTP::Request->new( GET => $url ); - $request->content_type('aplication/json'); - my $response = Koha::SharedContent::manaRequest( $request ); - - #error handling - my $resources = $response->{data}; - unless ( $resources ){ - my $msg; - $msg = $response->{msg}; - if ( $msg ){ - die $msg; + if ( scalar @{ $resources } < $length ){ + $mananotover = 0; } else{ - die "Unknown error"; + $mananotover = 1; } - } - #create the list of owned suggested resources - my $ownedbiblioitem; - my $ownedbiblio; - foreach my $resource ( @{ $resources } ){ - #  isbn processsing - if ( $resource->{idtype} eq "isbn" ){ - $documentid= NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 }); - $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); - #if we don't have such a biblioitem, we try to format else the isbn - unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ - $documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 }); - $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); - } + #create the list of owned suggested resources + my $ownedbiblioitem; + foreach my $resource ( @{ $resources } ){ + #  isbn processsing + if ( $resource->{idtype} eq "isbn" ){ + $documentid= NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; - unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ - $documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 }); - $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); - } + #if we don't have such a biblioitem, we try to format else the isbn + unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ + $documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; + } - unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ - $documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 }); - $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); - } + unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ + $documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; + } + unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ + $documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; + } + + } + #others ids do not need any processing + else{ + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $resource->{documentid} }); + } + if (scalar @{ $ownedbiblioitem->unblessed() } ){ + my $ownedbiblio = Koha::Biblios->find( @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} ); + push @biblios, $ownedbiblio; + } } - #others ids do not need any processing - else{ - $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); - } - if (scalar @{ $ownedbiblioitem->unblessed() } ){ - $ownedbiblio = Koha::Biblios->find( @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} ); - push @biblios, $ownedbiblio; - } - } + #the number of requested resource is inversely proportionnal to the found_items/returned_items ratio, +1 is here just to avoid 0 + my $newlength = int( ( 10*( $length + $offset ) - $length ) / (scalar @biblios + 1 )); + if ( $newlength > 500 ){ + $newlength = 500; + } + $offset += $length; + $length = $newlength; + + + } while( (scalar @biblios < 10 ) and $mananotover ); #preparing new suggestion my $newSuggestion; my $cter = scalar @biblios; $cter = 10 unless ($cter < 10); $newSuggestion->{ biblionumber } = $biblionumber; - if ( scalar @biblios < 11 ){ - while ( $cter > 1 ){ - $cter--; - $newSuggestion->{ "biblionumber".$cter }=$biblios[ $cter ]->biblionumber; + while ( $cter > 0 ){ + if ( $biblios[ $cter-1 ] and $biblios[ $cter-1 ]->biblionumber){ + $newSuggestion->{ "biblionumber".$cter }=$biblios[ $cter-1 ]->biblionumber; } + $cter--; } if ( $local_suggestions ){ Koha::Reading_suggestions->find( $biblionumber )->delete(); } Koha::Reading_suggestion->new( $newSuggestion )->store; - } my ( $template, $loggedinuser, $cookie ) = get_template_and_user( @@ -170,9 +205,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( } ); - $template->param( JacketImage => 1); $template->param( suggestions => \@biblios ); - output_with_http_headers $input, $cookie, $template->output, 'json'; -- 2.17.1