SpecialSearch: Fix pagination
authorChad Horohoe <chadh@wikimedia.org>
Tue, 1 Jul 2014 19:28:53 +0000 (12:28 -0700)
committerChad Horohoe <chadh@wikimedia.org>
Tue, 1 Jul 2014 19:38:22 +0000 (12:38 -0700)
This basically reverts f4357b6 but fixes it while we're here. Requesting
one extra result causes extra load and weird queries against the search
backend...all to be thrown away. Plus it's showing "Results 1-21 of N"
when we're only showing 20 which is a regression.

All search backends implement getTotalHits() so use that count instead
to figure out if we're on the last page.

Change-Id: Ib38b1e124bea7133f48209ef8af1b1e26b4dba75

includes/specials/SpecialSearch.php

index 13591aa..f119288 100644 (file)
@@ -207,8 +207,7 @@ class SpecialSearch extends SpecialPage {
 
                $profile = new ProfileSection( __METHOD__ );
                $search = $this->getSearchEngine();
-               // Request an extra result to determine whether a "next page" link is useful
-               $search->setLimitOffset( $this->limit + 1, $this->offset );
+               $search->setLimitOffset( $this->limit, $this->offset );
                $search->setNamespaces( $this->namespaces );
                $this->saveNamespaces();
                $search->prefix = $this->mPrefix;
@@ -352,7 +351,7 @@ class SpecialSearch extends SpecialPage {
                                        $this->offset,
                                        $this->limit,
                                        $this->powerSearchOptions() + array( 'search' => $term ),
-                                       max( $titleMatchesNum, $textMatchesNum ) <= $this->limit
+                                       $this->limit + $this->offset >= $totalRes
                                );
                        }
                        wfRunHooks( 'SpecialSearchResults', array( $term, &$titleMatches, &$textMatches ) );
@@ -551,11 +550,9 @@ class SpecialSearch extends SpecialPage {
 
                $out = "<ul class='mw-search-results'>\n";
                $result = $matches->next();
-               $count = 0;
-               while ( $result && $count < $this->limit ) {
+               while ( $result ) {
                        $out .= $this->showHit( $result, $terms );
                        $result = $matches->next();
-                       $count++;
                }
                $out .= "</ul>\n";