From 738aafb92dcfb73f51f9302fa9506040a8cc2930 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 Signed-off-by: Nick Clemens (cherry picked from commit 10efe4e3f239c47b847c53f06f82245e07d54759) 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 ef81e19e7c..0ea3b64fc8 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -1916,6 +1916,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++ ) { @@ -2106,11 +2112,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.39.5