jenkins-bot has submitted this change and it was merged.

Change subject: Allow users to customize search suggestion profile
......................................................................


Allow users to customize search suggestion profile

This patch adds a new section "completion" in the Search preference
tab.

Related core change: I471cd090730d2a25cb70d622ec3bebbe9583118c

Bug: T132477
Bug: T123015
Change-Id: Icd577c8ebc6e162befe30bde4fe276e633d2e434
---
M CirrusSearch.php
M i18n/en.json
M i18n/qqq.json
M includes/CirrusSearch.php
M includes/Hooks.php
M profiles/SuggestProfiles.php
M tests/browser/features/prefix_search_api.feature
7 files changed, 94 insertions(+), 34 deletions(-)

Approvals:
  Cindy-the-browser-test-bot: Looks good to me, but someone else must approve
  EBernhardson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/CirrusSearch.php b/CirrusSearch.php
index 0029b87..bdcfb9f 100644
--- a/CirrusSearch.php
+++ b/CirrusSearch.php
@@ -1052,6 +1052,8 @@
 $wgHooks[ 'GetBetaFeaturePreferences' ][] = 
'CirrusSearch\Hooks::getBetaFeaturePreferences';
 $wgHooks[ 'APIAfterExecute' ][] = 'CirrusSearch\Hooks::onAPIAfterExecute';
 $wgHooks[ 'SpecialSearchResults' ][] = 
'CirrusSearch\Hooks::onSpecialSearchResults';
+$wgHooks[ 'GetPreferences' ][] = 'CirrusSearch\Hooks::onGetPreferences';
+$wgHooks[ 'UserGetDefaultOptions' ][] = 
'CirrusSearch\Hooks::onUserGetDefaultOptions';
 
 /**
  * i18n
diff --git a/i18n/en.json b/i18n/en.json
index 327b96f..2f16c21 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -31,8 +31,12 @@
        "cirrussearch-completion-profile-strict": "Strict profile with few 
punctuation characters removed but diacritics and stress marks are kept.",
        "cirrussearch-completion-profile-classic": "Classic prefix, few 
punctuation characters and some diacritics removed.",
        "cirrussearch-completion-profile-normal": "Few punctuation characters, 
some diacritics and stopwords removed.",
+       "cirrussearch-completion-profile-normal-subphrases": "Few punctuation 
characters, some diacritics and stopwords removed. It will match also 
subphrases (can be subphrases or subpages depending on internal wiki 
configuration).",
        "cirrussearch-completion-profile-fuzzy": "Similar to normal with typo 
correction (two typos supported).",
+       "cirrussearch-completion-profile-fuzzy-subphrases": "Similar to normal 
with typo correction (two typos supported). It will match also subphrases (can 
be subphrases or subpages depending on internal wiki configuration).",
        "cirrussearch-qi-profile-classic": "Ranking based on the number of 
incoming links, some templates, article language and recency 
(templates/language/recency may not be activated on this wiki).",
        "cirrussearch-qi-profile-classic-noboostlinks": "Ranking based on some 
templates, article language and recency when activated on this wiki.",
-       "cirrussearch-qi-profile-empty": "Ranking based solely on query 
dependent features (for debug only)."
+       "cirrussearch-qi-profile-empty": "Ranking based solely on query 
dependent features (for debug only).",
+       "cirrussearch-pref-completion-profile-help": "Set the behavior for 
autocomplete (search-as-you-type) suggestions.",
+       "prefs-completion": "Search completion"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index a3bd485..6c4dd35 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -39,8 +39,12 @@
        "cirrussearch-completion-profile-strict": "Strict profile for 
completion (search as you type).",
        "cirrussearch-completion-profile-classic": "Classic profile for 
completion (search as you type).",
        "cirrussearch-completion-profile-normal": "Normal profile for 
completion (search as you type).",
+       "cirrussearch-completion-profile-normal-subphrases": "Normal profile 
with subphrases for completion (search as you type).",
        "cirrussearch-completion-profile-fuzzy": "Fuzzy profile for completion 
(search as you type).",
+       "cirrussearch-completion-profile-fuzzy-subphrases": "Fuzzy profile with 
subphrases for completion (search as you type).",
        "cirrussearch-qi-profile-classic": "Classic query independent search 
profile (affects fulltext search ranking algorithm).",
        "cirrussearch-qi-profile-classic-noboostlinks": "Classic query 
independent search profile without boost links (affects fulltext search ranking 
algorithm).",
-       "cirrussearch-qi-profile-empty": "Empty query independent search 
profile (affects fulltext search ranking algorithm)."
+       "cirrussearch-qi-profile-empty": "Empty query independent search 
profile (affects fulltext search ranking algorithm).",
+       "cirrussearch-pref-completion-profile-help": "Help message for 
completion profile settings in the user preferences.",
+       "prefs-completion": "Section name for completion settings in the user 
preferences."
 }
diff --git a/includes/CirrusSearch.php b/includes/CirrusSearch.php
index 676609b..b9c892f 100644
--- a/includes/CirrusSearch.php
+++ b/includes/CirrusSearch.php
@@ -605,7 +605,6 @@
         * @return SearchSuggestionSet
         */
        protected function completionSearchBackend( $search ) {
-
                if ( in_array( NS_SPECIAL, $this->namespaces ) ) {
                        // delegate special search to parent
                        return parent::completionSearchBackend( $search );
@@ -704,44 +703,40 @@
        }
 
        /**
-        * {@inheritDoc}
+        * @param string $profileType
+        * @param User|null $user
+        * @return array|null
         */
-       public function getProfiles( $profileType ) {
+       public function getProfiles( $profileType, User $user = null ) {
                switch( $profileType ) {
                case SearchEngine::COMPLETION_PROFILE_TYPE:
                        if ( $this->config->get( 
'CirrusSearchUseCompletionSuggester' ) == 'no' ) {
                                // No profile selection if completion suggester 
is disabled.
                                return [];
                        }
-                       $profiles = [];
-                       $allowedFields = [ 'suggest' => true, 'suggest-stop' => 
true ];
-                       // Check that we can use the subphrases FST
-                       if ( $this->config->getElement( 
'CirrusSearchCompletionSuggesterSubphrases', 'use' ) ) {
-                               $allowedFields['suggest-subphrases'] = true;
+
+                       $userDefault = $this->config->get( 
'CirrusSearchCompletionSettings' );
+                       $allowedProfiles = 
$this->getAllowedCompletionProfiles();
+                       // Only check user options if the user is logged to 
avoid loading
+                       // default user options.
+                       if ( $user !== null && $user->getId() !== 0 &&
+                               $user->getOption( 
'cirrussearch-pref-completion-profile' ) !== null &&
+                               array_key_exists(
+                                       $user->getOption( 
'cirrussearch-pref-completion-profile' ),
+                                       $allowedProfiles )
+                       ) {
+
+                               $userDefault = $user->getOption( 
'cirrussearch-pref-completion-profile' );
                        }
-                       foreach( $this->config->get( 
'CirrusSearchCompletionProfiles' ) as $name => $settings ) {
-                               $allowed = true;
-                               foreach( $settings as $value ) {
-                                       if ( !array_key_exists( 
$value['field'], $allowedFields ) ) {
-                                               $allowed = false;
-                                               break;
-                                       }
-                               }
-                               if ( !$allowed ) {
-                                       continue;
-                               }
+
+                       $profiles = [];
+                       foreach( array_keys( $allowedProfiles ) as $name ) {
                                $profiles[] = [
                                        'name' => $name,
                                        'desc-message' => 
'cirrussearch-completion-profile-' . $name,
-                                       'default' => $this->config->get( 
'CirrusSearchCompletionSettings' ) == $name,
+                                       'default' => $userDefault === $name,
                                ];
                        }
-                       // Add fallback to prefixsearch
-                       $profiles[] = [
-                               'name' => 
self::COMPLETION_PREFIX_FALLBACK_PROFILE,
-                               'desc-message' => 
'cirrussearch-completion-profile-' . self::COMPLETION_PREFIX_FALLBACK_PROFILE,
-                               'default' => false,
-                       ];
                        return $profiles;
                case SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE:
                        $profiles = [];
@@ -773,6 +768,35 @@
        }
 
        /**
+        * Return the list of completion profiles allowed
+        * @return array[] list of profiles indexed by profile name
+        */
+       private function getAllowedCompletionProfiles() {
+               $profiles = [];
+               $allowedFields = [ 'suggest' => true, 'suggest-stop' => true ];
+               // Check that we can use the subphrases FST
+               if ( $this->config->getElement( 
'CirrusSearchCompletionSuggesterSubphrases', 'use' ) ) {
+                       $allowedFields['suggest-subphrases'] = true;
+               }
+               foreach( $this->config->get( 'CirrusSearchCompletionProfiles' ) 
as $name => $settings ) {
+                       $allowed = true;
+                       foreach( $settings as $value ) {
+                               if ( !array_key_exists( $value['field'], 
$allowedFields ) ) {
+                                       $allowed = false;
+                                       break;
+                               }
+                       }
+                       if ( !$allowed ) {
+                               continue;
+                       }
+                       $profiles[$name] = $settings;
+               }
+               // Add fallback to prefixsearch
+               $profiles[self::COMPLETION_PREFIX_FALLBACK_PROFILE] = [];
+               return $profiles;
+       }
+
+       /**
         * Create a search field definition
         * @param string $name
         * @param int    $type
diff --git a/includes/Hooks.php b/includes/Hooks.php
index a36ddb5..69bc527 100644
--- a/includes/Hooks.php
+++ b/includes/Hooks.php
@@ -806,4 +806,30 @@
                        $textMatches
                ] );
        }
+
+       public static function onGetPreferences( $user, &$preferences ) {
+               $search = new CirrusSearch();
+               $profiles = $search->getProfiles( 
\SearchEngine::COMPLETION_PROFILE_TYPE, $user );
+               if ( !empty( $profiles ) ) {
+                       $options = [];
+                       foreach( $profiles as $prof ) {
+                               $options[wfMessage( $prof['desc-message'] 
)->text()] = $prof['name'];
+                       }
+                       $preferences['cirrussearch-pref-completion-profile'] = 
array(
+                               'type' => 'radio',
+                               'section' => 'searchoptions/completion',
+                               'options' => $options,
+                               'help-message' => 
'cirrussearch-pref-completion-profile-help',
+                       );
+               }
+               return true;
+       }
+
+       public static function onUserGetDefaultOptions( &$defaultOptions ) {
+               $config = MediaWikiServices::getInstance()
+                               ->getConfigFactory()
+                               ->makeConfig( 'CirrusSearch' );
+               $defaultOptions['cirrussearch-pref-completion-profile'] = 
$config->get( 'CirrusSearchCompletionSettings' );
+               return true;
+       }
 }
diff --git a/profiles/SuggestProfiles.php b/profiles/SuggestProfiles.php
index 239baf7..fb88f29 100644
--- a/profiles/SuggestProfiles.php
+++ b/profiles/SuggestProfiles.php
@@ -54,7 +54,7 @@
                ],
        ],
        // Normal with subphrases
-       'normal_subphrases' => [
+       'normal-subphrases' => [
                'plain-normal' => [
                        'field' => 'suggest',
                        'min_query_len' => 0,
@@ -149,7 +149,7 @@
                        ]
                ]
        ],
-       'fuzzy_subphrases' => [
+       'fuzzy-subphrases' => [
                // Defines the list of suggest queries to run in the same 
request.
                // key is the name of the suggestion request
                'plain' => [
diff --git a/tests/browser/features/prefix_search_api.feature 
b/tests/browser/features/prefix_search_api.feature
index 50ec32d..b238670 100644
--- a/tests/browser/features/prefix_search_api.feature
+++ b/tests/browser/features/prefix_search_api.feature
@@ -144,10 +144,10 @@
     | endym     | classic           | the API should produce list of length 0  
            |
     | endym     | normal            | the API should produce list of length 0  
            |
     | endym     | fuzzy             | the API should produce list of length 0  
            |
-    | endym     | normal_subphrases | Hyperion Cantos/Endymion is the first 
api suggestion |
-    | endym     | fuzzy_subphrases  | Hyperion Cantos/Endymion is the first 
api suggestion |
-    | endimion  | normal_subphrases | the API should produce list of length 0  
            |
-    | endimion  | fuzzy_subphrases  | Hyperion Cantos/Endymion is the first 
api suggestion |
+    | endym     | normal-subphrases | Hyperion Cantos/Endymion is the first 
api suggestion |
+    | endym     | fuzzy-subphrases  | Hyperion Cantos/Endymion is the first 
api suggestion |
+    | endimion  | normal-subphrases | the API should produce list of length 0  
            |
+    | endimion  | fuzzy-subphrases  | Hyperion Cantos/Endymion is the first 
api suggestion |
   # Just take too long to run on a regular basis
   # @redirect @huge
   # Scenario: Prefix search on pages with tons of redirects is reasonably fast

-- 
To view, visit https://gerrit.wikimedia.org/r/281468
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icd577c8ebc6e162befe30bde4fe276e633d2e434
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: DCausse <dcau...@wikimedia.org>
Gerrit-Reviewer: Cindy-the-browser-test-bot <bernhardsone...@gmail.com>
Gerrit-Reviewer: DCausse <dcau...@wikimedia.org>
Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org>
Gerrit-Reviewer: Gehel <gleder...@wikimedia.org>
Gerrit-Reviewer: Manybubbles <never...@wikimedia.org>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to