Salt the "nsToken" used for Special:Search namespace remembering
[lhc/web/wiklou.git] / includes / specials / SpecialSearch.php
index 33bd87f..54e9987 100644 (file)
@@ -207,8 +207,10 @@ 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;
                $term = $search->transformSearchTerm( $term );
 
@@ -251,9 +253,7 @@ class SpecialSearch extends SpecialPage {
                $rewritten = $search->replacePrefixes( $term );
 
                $titleMatches = $search->searchTitle( $rewritten );
-               if ( !( $titleMatches instanceof SearchResultTooMany ) ) {
-                       $textMatches = $search->searchText( $rewritten );
-               }
+               $textMatches = $search->searchText( $rewritten );
 
                $textStatus = null;
                if ( $textMatches instanceof Status ) {
@@ -317,13 +317,6 @@ class SpecialSearch extends SpecialPage {
                        Xml::closeElement( 'div' )
                );
 
-               // Sometimes the search engine knows there are too many hits
-               if ( $titleMatches instanceof SearchResultTooMany ) {
-                       $out->wrapWikiMsg( "==$1==\n", 'toomanymatches' );
-
-                       return;
-               }
-
                $filePrefix = $wgContLang->getFormattedNsText( NS_FILE ) . ':';
                if ( trim( $term ) === '' || $filePrefix === trim( $term ) ) {
                        $out->addHTML( $this->formHeader( $term, 0, 0 ) );
@@ -373,7 +366,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 ) );
@@ -410,7 +403,7 @@ class SpecialSearch extends SpecialPage {
                if ( $num === 0 ) {
                        if ( $textStatus ) {
                                $out->addHTML( '<div class="error">' .
-                                       htmlspecialchars( $textStatus->getWikiText( 'search-error' ) ) . '</div>' );
+                                       $textStatus->getMessage( 'search-error' ) . '</div>' );
                        } else {
                                $out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>",
                                        array( 'search-nonefound', wfEscapeWikiText( $term ) ) );
@@ -522,6 +515,41 @@ class SpecialSearch extends SpecialPage {
                return $opt + $this->extraParams;
        }
 
+       /**
+        * Save namespace preferences when we're supposed to
+        *
+        * @return bool Whether we wrote something
+        */
+       protected function saveNamespaces() {
+               $user = $this->getUser();
+               $request = $this->getRequest();
+
+               if ( $user->isLoggedIn() &&
+                       !is_null( $request->getVal( 'nsRemember' ) ) &&
+                       $user->matchEditToken(
+                               $request->getVal( 'nsToken' ),
+                               'searchnamespace',
+                               $request
+                       )
+               ) {
+                       // Reset namespace preferences: namespaces are not searched
+                       // when they're not mentioned in the URL parameters.
+                       foreach ( MWNamespace::getValidNamespaces() as $n ) {
+                               $user->setOption( 'searchNs' . $n, false );
+                       }
+                       // The request parameters include all the namespaces we just searched.
+                       // Even if they're the same as an existing profile, they're not eaten.
+                       foreach ( $this->namespaces as $n ) {
+                               $user->setOption( 'searchNs' . $n, true );
+                       }
+
+                       $user->saveSettings();
+                       return true;
+               }
+
+               return false;
+       }
+
        /**
         * Show whole set of results
         *
@@ -537,9 +565,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";
 
@@ -940,6 +970,25 @@ class SpecialSearch extends SpecialPage {
                        $hidden .= Html::hidden( $key, $value );
                }
 
+               # Stuff to feed saveNamespaces()
+               $remember = '';
+               $user = $this->getUser();
+               if ( $user->isLoggedIn() ) {
+                       $remember .= Html::hidden(
+                               'nsToken',
+                               $user->getEditToken(
+                                       'searchnamespace',
+                                       $this->getRequest()
+                               )
+                       ) .
+                       Xml::checkLabel(
+                               wfMessage( 'powersearch-remember' )->text(),
+                               'nsRemember',
+                               'mw-search-powersearch-remember',
+                               false
+                       );
+               }
+
                // Return final output
                return Xml::openElement(
                        'fieldset',
@@ -951,6 +1000,8 @@ class SpecialSearch extends SpecialPage {
                        Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
                        implode( Xml::element( 'div', array( 'class' => 'divider' ), '', false ), $showSections ) .
                        $hidden .
+                       Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
+                       $remember .
                        Xml::closeElement( 'fieldset' );
        }