Remove redundant filter for negative namespace ids in search preferences
[lhc/web/wiklou.git] / includes / specials / SpecialSearch.php
index 2713a5f..ea0a29a 100644 (file)
@@ -30,7 +30,7 @@
 class SpecialSearch extends SpecialPage {
        /**
         * Current search profile. Search profile is just a name that identifies
-        * the active search tab on the search page (content, help, discussions...)
+        * the active search tab on the search page (content, discussions...)
         * For users tt replaces the set of enabled namespaces from the query
         * string when applicable. Extensions can add new profiles with hooks
         * with custom search options just for that profile.
@@ -209,6 +209,7 @@ class SpecialSearch extends SpecialPage {
                $search = $this->getSearchEngine();
                $search->setLimitOffset( $this->limit, $this->offset );
                $search->setNamespaces( $this->namespaces );
+               $this->saveNamespaces();
                $search->prefix = $this->mPrefix;
                $term = $search->transformSearchTerm( $term );
 
@@ -522,6 +523,37 @@ 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' ) )
+               ) {
+                       // 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
         *
@@ -940,6 +972,19 @@ 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() ) .
+                       Xml::checkLabel(
+                               wfMessage( 'powersearch-remember' )->text(),
+                               'nsRemember',
+                               'mw-search-powersearch-remember',
+                               false
+                       );
+               }
+
                // Return final output
                return Xml::openElement(
                        'fieldset',
@@ -951,6 +996,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' );
        }
 
@@ -975,14 +1022,6 @@ class SpecialSearch extends SpecialPage {
                                'tooltip' => 'searchprofile-images-tooltip',
                                'namespaces' => array( NS_FILE ),
                        ),
-                       'help' => array(
-                               'message' => 'searchprofile-project',
-                               'tooltip' => 'searchprofile-project-tooltip',
-                               'namespaces' => SearchEngine::helpNamespaces(),
-                               'namespace-messages' => SearchEngine::namespacesAsText(
-                                       SearchEngine::helpNamespaces()
-                               ),
-                       ),
                        'all' => array(
                                'message' => 'searchprofile-everything',
                                'tooltip' => 'searchprofile-everything-tooltip',