[Rt-commit] rt branch 5.0/combine-search-and-count created. rt-5.0.3-84-gc95cac51e3

BPS Git Server git at git.bestpractical.com
Fri Aug 26 20:27:20 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/combine-search-and-count has been created
        at  c95cac51e31a0e8945e24c44e87c43f2c4b8e42f (commit)

- Log -----------------------------------------------------------------
commit c95cac51e31a0e8945e24c44e87c43f2c4b8e42f
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Aug 27 01:12:45 2022 +0800

    Combine search and count for search result pages if possible
    
    This commit also fixes the handling of $OrderBy on search result page to
    exclude empty values, to make combined SQL work: search result page
    calls CountAll only, of which SQL doesn't include the "ORDER BY ..."
    part, the issue wouldn't be exposed without combined SQL.
    
    To make sure the combined search returns the demanded resultset, we need
    to call CountAll after all setups like OrderByCols, RowsPerPage, etc.

diff --git a/share/html/Elements/CollectionList b/share/html/Elements/CollectionList
index 46c8fcccc7..7d99531dc7 100644
--- a/share/html/Elements/CollectionList
+++ b/share/html/Elements/CollectionList
@@ -66,19 +66,6 @@ elsif (!$Collection && $Class eq 'RT::Assets') {
 # dashboard mail can be suppressed if there are no results
 $$HasResults = 0 if $HasResults && !defined($$HasResults);
 
-$TotalFound = $Collection->CountAll() unless defined $TotalFound;
-return '' if !$TotalFound && !$ShowEmpty;
-
-if ( $Rows ) {
-    if ( $TotalFound <= $Rows ) {
-        $Page = 1;
-    }
-    else {
-        my $MaxPage = int( $TotalFound / $Rows ) + ( $TotalFound % $Rows ? 1 : 0 );
-        $Page = $MaxPage if $Page > $MaxPage;
-    }
-}
-
 # XXX: ->{'order_by'} is hacky, but there is no way to check if
 # collection is ordered or not
 if ( @OrderBy && ($AllowSorting || $PreferOrderBy || !$Collection->{'order_by'}) ) {
@@ -97,6 +84,24 @@ $Collection->RowsPerPage( $Rows ) if $Rows;
 $Page = 1 unless $Page && $Page > 0;    # workaround problems with $Page = '' or undef
 $Collection->GotoPage( $Page - 1 ); # SB uses page 0 as the first page
 
+$Collection->CombineSearchAndCount(1) if $Collection->can('CombineSearchAndCount');
+$TotalFound = $Collection->CountAll() unless defined $TotalFound;
+return '' if !$TotalFound && !$ShowEmpty;
+
+# Adjust page after CountAll, otherwise $TotalFound could be not defined.
+# On the other hand, CountAll needs page info if it's a combined search,
+# thus we have to set page first and then adjust it here.
+if ( $Rows ) {
+    if ( $TotalFound <= $Rows ) {
+        $Page = 1;
+    }
+    else {
+        my $MaxPage = int( $TotalFound / $Rows ) + ( $TotalFound % $Rows ? 1 : 0 );
+        $Page = $MaxPage if $Page > $MaxPage;
+    }
+}
+$Collection->GotoPage( $Page - 1 );
+
 # DisplayFormat lets us use a "temporary" format for display, while 
 # still using our original format for next/prev page links.
 # bulk update uses this feature to add checkboxes
diff --git a/share/html/Search/Results.html b/share/html/Search/Results.html
index e0de84d99f..762bbe49f9 100644
--- a/share/html/Search/Results.html
+++ b/share/html/Search/Results.html
@@ -62,6 +62,7 @@
 
 <& /Elements/CollectionList, 
     Query => $Query,
+    Collection => $session{$session_name},
     TotalFound => $count,
     AllowSorting => 1,
     OrderBy => $OrderBy,
@@ -189,13 +190,14 @@ if ($OrderBy =~ /\|/) {
     my @OrderBy = split /\|/,$OrderBy;
     my @Order = split /\|/,$Order;
     $session{$session_name}->OrderByCols(
-        map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } } ( 0
-        .. $#OrderBy ) );; 
+        map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } }
+        grep { $OrderBy[$_] } ( 0 .. $#OrderBy ) );
 } else {
     $session{$session_name}->OrderBy(FIELD => $OrderBy, ORDER => $Order); 
 }
 $session{$session_name}->RowsPerPage( $Rows ) if $Rows;
 $session{$session_name}->GotoPage( $Page - 1 );
+$session{$session_name}->CombineSearchAndCount(1) if $session{$session_name}->can('CombineSearchAndCount');
 
 $session{$hash_name} = {
     Format      => $Format,

-----------------------------------------------------------------------


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list