From 445ba883a222938cecad9030687360a14390ba7e Mon Sep 17 00:00:00 2001 From: Sam Smith Date: Wed, 30 Jun 2021 13:51:04 +0100 Subject: [PATCH] search: Disable query highlight for some languages Following on from I551414b1, disable query highlighting for the list of languages provided by @TJones in T281797. The user's interface language can be different from the wiki's content language and so the former is not available at configuration time. Thus, we fetch the user's interface language at request time. Fortunately, @TJones' list of languages is small so there should be little perceivable performance impact from the perpective of the user. Additional changes: - Remove the config.VectorWvuiSearchOptions.value.highlightQuery property from skin.json Bug: T281797 Change-Id: Ib39736a93fa64e82253f88551d125413e672558b --- includes/Hooks.php | 14 ++++++--- includes/Services/LanguageService.php | 45 +++++++++++++++++++++++++++ includes/VectorServices.php | 10 ++++++ skin.json | 3 +- 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 includes/Services/LanguageService.php diff --git a/includes/Hooks.php b/includes/Hooks.php index 332c6dff2..2fe4ca64b 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -53,16 +53,22 @@ class Hooks { } /** - * Passes config variables to skins.vector.search ResourceLoader module. + * Generates config variables for skins.vector.search Resource Loader module (defined in + * skin.json). + * * @param ResourceLoaderContext $context * @param Config $config - * @return array + * @return array */ public static function getVectorWvuiSearchResourceLoaderConfig( ResourceLoaderContext $context, Config $config - ) { - return $config->get( 'VectorWvuiSearchOptions' ); + ): array { + $result = $config->get( 'VectorWvuiSearchOptions' ); + $result['highlightQuery'] = + VectorServices::getLanguageService()->canWordsBeSplitSafely( $context->getLanguage() ); + + return $result; } /** diff --git a/includes/Services/LanguageService.php b/includes/Services/LanguageService.php new file mode 100644 index 000000000..c750977ee --- /dev/null +++ b/includes/Services/LanguageService.php @@ -0,0 +1,45 @@ +splittableLanguages = [ + 'ar', 'ary', 'arz', 'ckb', 'fa', 'glk', 'ks', 'mzn', 'pnb', 'ps', 'sd', 'skr', 'ug', 'ur', + 'as', 'bn', 'bpy', + 'awa', 'bh', 'dty', 'gom', 'hi', 'ks', 'mai', 'mr', 'ne', 'new', 'pi', 'sa', + 'gu', + 'pa', + 'kn', 'tcy', + 'km', + 'ml', + 'or', + 'si', + 'ta', + 'te', + ]; + } + + /** + * Gets whether or not we can split words arbitrarily, for example when highlighting the user's query in the search + * autocomplete widget. + * + * @param string $code + * @return bool + */ + public function canWordsBeSplitSafely( string $code ): bool { + return !in_array( $code, $this->splittableLanguages ); + } +} diff --git a/includes/VectorServices.php b/includes/VectorServices.php index e093d3272..9add2a48e 100644 --- a/includes/VectorServices.php +++ b/includes/VectorServices.php @@ -4,6 +4,7 @@ namespace Vector; use MediaWiki\MediaWikiServices; use Vector\FeatureManagement\FeatureManager; +use Vector\Services\LanguageService; /** * A service locator for services specific to Vector. @@ -24,4 +25,13 @@ final class VectorServices { public static function getFeatureManager(): FeatureManager { return MediaWikiServices::getInstance()->getService( Constants::SERVICE_FEATURE_MANAGER ); } + + /** + * Gets the language service. + * + * @return LanguageService + */ + public static function getLanguageService(): LanguageService { + return new LanguageService(); + } } diff --git a/skin.json b/skin.json index 88e7f6f5c..80ba08d2e 100644 --- a/skin.json +++ b/skin.json @@ -277,8 +277,7 @@ "VectorWvuiSearchOptions": { "value": { "showThumbnail": true, - "showDescription": true, - "highlightQuery": true + "showDescription": true } }, "VectorDefaultSidebarVisibleForAuthorisedUser": {