From d9748a022bba92863134677414443a4afbb245be Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 30 Apr 2019 12:30:25 -0300 Subject: [PATCH] Bug 22813: remove repetitive queries inside two nested loops in searchResults This patch moves a query on Koha::Patrons and then the related Koha::Patron::Category that needlessly happens inside two nested loops (all items of all MARC records in the resultset). The Koha::Patron and Koha::Patron::Category are always the same as it is fetched from C4::Context->userenv each time. To test: - Run: $ kshell k$ prove t/db_dependent/Search.t => SUCCESS: Tests pass - Apply this patch - Run: k$ prove t/db_dependent/Search.t => SUCCESS: Tests still pass! - Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Liz Rea Signed-off-by: Martin Renvoize --- C4/Search.pm | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 1a9f51b4e1..b578e2acb6 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -1922,6 +1922,12 @@ sub searchResults { my $lang = $xslfile ? C4::Languages::getlanguage() : undef; my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef; + my $userenv = C4::Context->userenv; + my $patron = ( defined $userenv and $userenv->{number} ) + ? Koha::Patrons->find( $userenv->{number} ) + : undef; + my $patron_category_hide_lost_items = ($patron) ? $patron->category->hidelostitems : 0; + # loop through all of the records we've retrieved for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) { @@ -2112,11 +2118,8 @@ sub searchResults { my $prefix = $item->{$hbranch} . '--' . $item->{location} . $item->{itype} . $item->{itemcallnumber}; # For each grouping of items (onloan, available, unavailable), we build a key to store relevant info about that item - my $userenv = C4::Context->userenv; if ( $item->{onloan} - && $userenv - && $userenv->{number} - && !( Koha::Patrons->find($userenv->{number})->category->hidelostitems && $item->{itemlost} ) ) + and !( $patron_category_hide_lost_items and $item->{itemlost} ) ) { $onloan_count++; my $key = $prefix . $item->{onloan} . $item->{barcode}; -- 2.20.1