From 5dc4dc099d8799cf98dcda9016f77903d5868eb4 Mon Sep 17 00:00:00 2001 From: Nemo bis Date: Tue, 6 May 2014 18:33:04 +0200 Subject: [PATCH] Save advanced search namespace prefs on Special:Search itself * Checkbox on own row below power search checkboxes per MatmaRex; avoiding a mw-search-ns* id leaves it untouched by All/None JS. * The option searcheverything is removed: a "shortcut" which is no longer necessary now that options can be (un)selected at once with All/None buttons on search page itself. * Require a token for saving: no accidental preferences changes. * Keep the searchoptions/advancedsearchoptions prefs section in case something is using it (no known extension does though); options are converted to "api" type so it's empty and hidden by default. * Add minimal documentation for saveSettings() and friends (@todo since 155ddf6de, 2009!). Bug: 52817 Change-Id: I514cee835988600cc013658049e88a10b670e64a --- RELEASE-NOTES-1.24 | 5 +++ includes/DefaultSettings.php | 8 ----- includes/Preferences.php | 26 +++------------ includes/User.php | 6 +++- includes/search/SearchEngine.php | 14 +-------- includes/specials/SpecialSearch.php | 49 +++++++++++++++++++++++++++++ languages/i18n/en.json | 3 +- languages/i18n/qqq.json | 1 + 8 files changed, 67 insertions(+), 45 deletions(-) diff --git a/RELEASE-NOTES-1.24 b/RELEASE-NOTES-1.24 index 83ced70ef4..b3b34f2985 100644 --- a/RELEASE-NOTES-1.24 +++ b/RELEASE-NOTES-1.24 @@ -14,6 +14,9 @@ production. * Introduced $wgPagePropsHaveSortkey as a backwards-compatibility switch, for using the old schema of the page_props table, in case the respective schema update was not applied. +* $wgSearchEverythingOnlyLoggedIn was removed as the 'searcheverything' + user option was removed. Use $wgNamespacesToBeSearchedDefault instead or + if you used to have $wgDefaultUserOptions['searcheverything'] = 1. === New features in 1.24 === * Added a new hook, "WhatLinksHereProps", to allow extensions to annotate @@ -52,6 +55,8 @@ production. * New methods setVolatile and isVolatile are added to PPFrame, so that extensions such as Cite.php can mark that their output is volatile and shouldn't be cached. +* (bug 52817) Advanced search options are now saved on the search page itself, rather + than in a dedicated pane in the preferences panel. === Bug fixes in 1.24 === * (bug 49116) Footer copyright notice is now always displayed in user language diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 16c0187785..efa6e21286 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -5322,14 +5322,6 @@ $wgNamespacesToBeSearchedDefault = array( NS_MAIN => true, ); -/** - * If set to true the 'searcheverything' preference will be effective only for - * logged-in users. - * Useful for big wikis to maintain different search profiles for anonymous and - * logged-in users. - */ -$wgSearchEverythingOnlyLoggedIn = false; - /** * Disable the internal MySQL-based search, to allow it to be * implemented by an extension instead. diff --git a/includes/Preferences.php b/includes/Preferences.php index 1f1b7dde81..62aac3b177 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -1010,29 +1010,13 @@ class Preferences { * @param array $defaultPreferences */ static function searchPreferences( $user, IContextSource $context, &$defaultPreferences ) { - global $wgContLang; - - $defaultPreferences['searcheverything'] = array( - 'type' => 'toggle', - 'label-message' => 'searcheverything-enable', - 'section' => 'searchoptions/advancedsearchoptions', - ); - - $nsOptions = $wgContLang->getFormattedNamespaces(); - $nsOptions[0] = $context->msg( 'blanknamespace' )->text(); - foreach ( $nsOptions as $ns => $name ) { - if ( $ns < 0 ) { - unset( $nsOptions[$ns] ); + foreach ( MWNamespace::getValidNamespaces() as $n ) { + if ( $n >= 0 ) { + $defaultPreferences[ 'searchNs' . $n ] = array( + 'type' => 'api', + ); } } - - $defaultPreferences['searchnamespaces'] = array( - 'type' => 'multiselect', - 'label-message' => 'defaultns', - 'options' => array_flip( $nsOptions ), - 'section' => 'searchoptions/advancedsearchoptions', - 'prefix' => 'searchNs', - ); } /** diff --git a/includes/User.php b/includes/User.php index 7b35a297c0..7401c4d81d 100644 --- a/includes/User.php +++ b/includes/User.php @@ -2552,6 +2552,8 @@ class User { /** * Set the given option for a user. * + * You need to call saveSettings() to actually write to the database. + * * @param string $oname The option to set * @param mixed $val New value to set */ @@ -4807,7 +4809,9 @@ class User { } /** - * @todo document + * Saves the non-default options for this user, as previously set e.g. via + * setOption(), in the database's "user_properties" (preferences) table. + * Usually used via saveSettings(). */ protected function saveOptions() { $this->loadOptions(); diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php index 3d0655b723..4663ab886c 100644 --- a/includes/search/SearchEngine.php +++ b/includes/search/SearchEngine.php @@ -368,20 +368,8 @@ class SearchEngine { * @return array */ public static function userNamespaces( $user ) { - global $wgSearchEverythingOnlyLoggedIn; - - $searchableNamespaces = SearchEngine::searchableNamespaces(); - - // get search everything preference, that can be set to be read for logged-in users - // it overrides other options - if ( !$wgSearchEverythingOnlyLoggedIn || $user->isLoggedIn() ) { - if ( $user->getOption( 'searcheverything' ) ) { - return array_keys( $searchableNamespaces ); - } - } - $arr = array(); - foreach ( $searchableNamespaces as $ns => $name ) { + foreach ( SearchEngine::searchableNamespaces() as $ns => $name ) { if ( $user->getOption( 'searchNs' . $ns ) ) { $arr[] = $ns; } diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index 33bd87f518..ee7ddfdab8 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -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,39 @@ 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 ) { + if ( $n >= 0 ) { + $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 +974,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 +998,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' ); } diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 85e1bdc43d..87c76032e0 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -901,7 +901,6 @@ "search-interwiki-custom": "", "search-interwiki-more": "(more)", "search-relatedarticle": "Related", - "searcheverything-enable": "Search in all namespaces", "searchrelated": "related", "searchall": "all", "showingresults": "Showing below up to {{PLURAL:$1|1 result|$1 results}} starting with #$2.", @@ -914,6 +913,7 @@ "powersearch-togglelabel": "Check:", "powersearch-toggleall": "All", "powersearch-togglenone": "None", + "powersearch-remember": "Remember selection for future searches", "search-external": "External search", "searchdisabled": "{{SITENAME}} search is disabled.\nYou can search via Google in the meantime.\nNote that their indexes of {{SITENAME}} content may be out of date.", "googlesearch": "
\n\t\n\t\n\t\n\t\n\n\t\n\t\n
\n\t\n\t\n
\n
", @@ -976,7 +976,6 @@ "allowemail": "Enable email from other users", "prefs-searchoptions": "Search", "prefs-namespaces": "Namespaces", - "defaultns": "Otherwise search in these namespaces:", "default": "default", "prefs-files": "Files", "prefs-custom-css": "Custom CSS", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index e8173ea9b6..077f34426c 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -1076,6 +1076,7 @@ "powersearch-togglelabel": "Used in [{{canonicalurl:Special:Search|advanced=1}} Advanced search]. Synonym: \"Select\" as verb.\n{{Identical|Check}}", "powersearch-toggleall": "\"All\" refers to namespaces. It is used in Advanced search: {{canonicalurl:Special:Search|advanced=1}}\n{{Identical|All}}", "powersearch-togglenone": "\"None\" refers to namespaces. It is used in Advanced search: {{canonicalurl:Special:Search|advanced=1}}\n{{Identical|None}}", + "powersearch-remember": "Label for a checkbox to save namespace choice for advanced search to user preferences.", "search-external": "Legend of the fieldset for the input form when the internal search is disabled. Inside the fieldset [[MediaWiki:Searchdisabled]] and [[MediaWiki:Googlesearch]] is shown.", "searchdisabled": "{{doc-singularthey}}\nIn this sentence, \"their indexes\" refers to \"Google's indexes\".\n\nShown on [[Special:Search]] when the internal search is disabled.", "googlesearch": "{{notranslate}}\nShown when [[mw:Manual:$wgDisableTextSearch|$wgDisableTextSearch]] is set to true and no [[mw:Manual:$wgSearchForwardUrl|$wgSearchForwardUrl]] is set.\n\nParameters:\n* $1 - the search term\n* $2 - \"UTF-8\" (hard-coded)\n* $3 - the message {{msg-mw|Searchbutton}}", -- 2.20.1