Skip to content

Commit

Permalink
Creates admin setting for custom stopwords
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Stovell <[email protected]>
  • Loading branch information
Sesquipedalian committed Apr 7, 2024
1 parent 15a2ecf commit 9da9e8d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Languages/en_US/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:<br>{list}';

$txt['set_parameters'] = 'Set Search Parameters';
$txt['choose_board'] = 'Choose boards to search in, or search all';
$txt['all_words'] = 'Match all words';
Expand Down
33 changes: 27 additions & 6 deletions Sources/Actions/Admin/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ class Search implements ActionInterface
{
use BackwardCompatibility;

/*****************
* Class constants
*****************/

// code...

/*******************
* Public properties
*******************/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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...
Expand All @@ -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' => '<span class="infobox block">' . Lang::getTxt('search_stopwords_permanent', ['list' => implode(', ', $permanent_stopwords)]) . '</span>'],
];

// Do any mods want access?
IntegrationHook::call('integrate_modify_search_settings', [&$config_vars]);

// Perhaps the search method wants to add some settings?
Expand All @@ -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;
}

Expand Down
4 changes: 1 addition & 3 deletions Sources/Search/SearchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down

0 comments on commit 9da9e8d

Please sign in to comment.