Final page of search results sometimes having erroneous "next" link
authorChad Horohoe <chadh@wikimedia.org>
Fri, 30 May 2014 22:28:13 +0000 (15:28 -0700)
committerChad Horohoe <chadh@wikimedia.org>
Fri, 30 May 2014 22:29:46 +0000 (15:29 -0700)
Pre-patch: In the showResults method of the SpecialSearch class, a request is made
through the appropriate SearchEngine object for a single page (according to the
specified page size) of search results. With this approach, it's impossible for the
page formatter to determine whether an additional page is useful when a result set no
smaller than the requested size is returned.

Post-patch: The solution is to request an extra result, which we do not display, but we
do include in our result count. Display of the extra result is avoided by modifying
SpecialSearch->showMatches() to explicitly honor the limit property of its calling
instance. The criterion for not linking to a next page is then updated from
 results < limit.
which enables the next link whenever the current results page is full, to
 results <= limit
which enables the next link only when there will be results displayed on the next page.

Bug: 19938
Change-Id: Ic30586e217fbde4b9d0aeda277a77030c4dce216

includes/specials/SpecialSearch.php

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