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 <tomascohen@theke.io>

Signed-off-by: Liz Rea <wizzyrea@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
This commit is contained in:
Tomás Cohen Arazi 2019-04-30 12:30:25 -03:00 committed by Nick Clemens
parent c1513a10c3
commit 10efe4e3f2

View file

@ -1922,6 +1922,12 @@ sub searchResults {
my $lang = $xslfile ? C4::Languages::getlanguage() : undef; my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : 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 # loop through all of the records we've retrieved
for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) { for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) {
@ -2112,11 +2118,8 @@ sub searchResults {
my $prefix = $item->{$hbranch} . '--' . $item->{location} . $item->{itype} . $item->{itemcallnumber}; 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 # 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} if ( $item->{onloan}
&& $userenv and !( $patron_category_hide_lost_items and $item->{itemlost} ) )
&& $userenv->{number}
&& !( Koha::Patrons->find($userenv->{number})->category->hidelostitems && $item->{itemlost} ) )
{ {
$onloan_count++; $onloan_count++;
my $key = $prefix . $item->{onloan} . $item->{barcode}; my $key = $prefix . $item->{onloan} . $item->{barcode};