From 9da9e8dbfd1ba823d151b0f3d58033d2010a69ac Mon Sep 17 00:00:00 2001 From: Jon Stovell Date: Thu, 4 Apr 2024 00:25:41 -0600 Subject: [PATCH] Creates admin setting for custom stopwords Signed-off-by: Jon Stovell --- Languages/en_US/Search.php | 3 +++ Sources/Actions/Admin/Search.php | 33 ++++++++++++++++++++++++++------ Sources/Search/SearchApi.php | 4 +--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Languages/en_US/Search.php b/Languages/en_US/Search.php index 48cffd7dc6..fefb7870da 100644 --- a/Languages/en_US/Search.php +++ b/Languages/en_US/Search.php @@ -5,6 +5,9 @@ // Comma-separated list of words that should be ignored when searching in this language. Translators should NOT just translate the words in this list. Instead, replace them with common words that should be ignored when searching in the target languge. $txt['search_stopwords'] = 'a,about,an,are,as,at,be,by,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with'; +$txt['search_stopwords_custom'] = 'Words to ignore when searching'; +$txt['search_stopwords_permanent'] = 'The following words are always ignored:
{list}'; + $txt['set_parameters'] = 'Set Search Parameters'; $txt['choose_board'] = 'Choose boards to search in, or search all'; $txt['all_words'] = 'Match all words'; diff --git a/Sources/Actions/Admin/Search.php b/Sources/Actions/Admin/Search.php index b7e7d574fa..db4d6d9ec6 100644 --- a/Sources/Actions/Admin/Search.php +++ b/Sources/Actions/Admin/Search.php @@ -35,12 +35,6 @@ class Search implements ActionInterface { use BackwardCompatibility; - /***************** - * Class constants - *****************/ - - // code... - /******************* * Public properties *******************/ @@ -118,6 +112,18 @@ public function settings(): void $_POST['search_results_per_page'] = !empty(Config::$modSettings['search_results_per_page']) ? Config::$modSettings['search_results_per_page'] : Config::$modSettings['defaultMaxMessages']; } + if (isset($_POST['search_stopwords_custom'])) { + $_POST['search_stopwords_custom'] = array_diff( + Utils::text2words($_POST['search_stopwords_custom'], PHP_INT_MAX), + Utils::text2words(Lang::$txt['search_stopwords'] ?? '', PHP_INT_MAX), + Utils::text2words(Config::$modSettings['search_stopwords'] ?? '', PHP_INT_MAX), + ); + + sort($_POST['search_stopwords_custom']); + + $_POST['search_stopwords_custom'] = implode(',', array_map([Utils::class, 'htmlspecialchars'], $_POST['search_stopwords_custom'])); + } + ACP::saveDBSettings($config_vars); $_SESSION['adm-save'] = true; @@ -335,6 +341,13 @@ public static function call(): void */ public static function getConfigVars(): array { + $permanent_stopwords = array_unique(array_merge( + Utils::text2words(Lang::$txt['search_stopwords'] ?? '', PHP_INT_MAX), + Utils::text2words(Config::$modSettings['search_stopwords'] ?? '', PHP_INT_MAX), + )); + + sort($permanent_stopwords); + // What are we editing anyway? $config_vars = [ // Permission... @@ -346,8 +359,13 @@ public static function getConfigVars(): array // Some limitations. ['int', 'search_floodcontrol_time', 'subtext' => Lang::$txt['search_floodcontrol_time_desc'], 6, 'postinput' => Lang::$txt['seconds']], + '', + + // Allow the admin to set stopwords. + ['large_text', 'search_stopwords_custom', 'rows' => 8, 'subtext' => '' . Lang::getTxt('search_stopwords_permanent', ['list' => implode(', ', $permanent_stopwords)]) . ''], ]; + // Do any mods want access? IntegrationHook::call('integrate_modify_search_settings', [&$config_vars]); // Perhaps the search method wants to add some settings? @@ -357,6 +375,9 @@ public static function getConfigVars(): array call_user_func_array([$searchAPI, 'searchSettings'], [&$config_vars]); } + // Let the admin set custom stopwords. + Config::$modSettings['search_stopwords_custom'] = implode("\n", explode(',', Config::$modSettings['search_stopwords_custom'] ?? '')); + return $config_vars; } diff --git a/Sources/Search/SearchApi.php b/Sources/Search/SearchApi.php index 52d208f017..acf8747081 100644 --- a/Sources/Search/SearchApi.php +++ b/Sources/Search/SearchApi.php @@ -127,10 +127,8 @@ abstract class SearchApi implements SearchApiInterface * Populated with the contents of: * - Lang::$txt['search_stopwords'] * - Config::$modSettings['search_stopwords'] + * - Config::$modSettings['search_stopwords_custom'] * - All known BBCode tags - * - * @todo Setting to add custom values? - * @todo Maybe only blacklist if they are the only word, or "any" is used? */ public array $blacklisted_words = [];