Bug 22813: remove repetitive queries inside two nested loops in searchResults
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 30 Apr 2019 15:30:25 +0000 (12:30 -0300)
committerLucas Gass <lucas@bywatersolutions.com>
Sun, 26 May 2019 12:22:47 +0000 (12:22 +0000)
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>
(cherry picked from commit 10efe4e3f239c47b847c53f06f82245e07d54759)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
(cherry picked from commit 738aafb92dcfb73f51f9302fa9506040a8cc2930)

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>

C4/Search.pm

index dc5935d..211e1fe 100644 (file)
@@ -1909,6 +1909,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++ ) {
 
@@ -2095,11 +2101,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};