From caa58e5937735b8d5b9d468ca678f3cd9c30d7f3 Mon Sep 17 00:00:00 2001 From: Konstantin Myakshin Date: Tue, 5 Dec 2023 02:30:16 +0200 Subject: [PATCH] Set default language for TNTSearch Signed-off-by: Konstantin Myakshin --- lib/Search/FileSearch/FileSearcher.php | 22 +++++++++++++++++++++- lib/Service/SearchService.php | 16 ++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/Search/FileSearch/FileSearcher.php b/lib/Search/FileSearch/FileSearcher.php index 0226de06f2..469fd1af59 100644 --- a/lib/Search/FileSearch/FileSearcher.php +++ b/lib/Search/FileSearch/FileSearcher.php @@ -21,13 +21,29 @@ class FileSearcher extends TNTSearch { 'storage' => '' ]; + public const SUPPORTED_LANGUAGES = [ + 'ar' => 'Arabic', + 'cr' => 'Croatian', + 'fr' => 'French', + 'de' => 'German', + 'en' => 'Porter', + 'it' => 'Italian', + 'lv' => 'Latvian', + 'pl' => 'Polish', + 'pt' => 'Portuguese', + 'ru' => 'Russian', + 'uk' => 'Ukrainian', + ]; + protected FileIndexer $indexer; + private string $language; - public function __construct() { + public function __construct(string $language = 'no') { parent::__construct(); $this->loadConfig(); $this->asYouType(true); $this->fuzziness(true); + $this->language = $language; } /** @@ -83,6 +99,8 @@ public function selectIndex($indexName): FileIndexer { $this->index->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->indexer->setIndex($this->index); + $this->indexer->setLanguage($this->language); + return $this->indexer; } @@ -93,6 +111,8 @@ public function selectIndex($indexName): FileIndexer { */ public function createIndex($indexName = '', $disableOutput = false): FileIndexer { $this->indexer->createIndex($indexName); + $this->indexer->setLanguage($this->language); + $this->index = $this->indexer->getIndex(); return $this->indexer; } diff --git a/lib/Service/SearchService.php b/lib/Service/SearchService.php index 7119bfab81..54c14b628b 100644 --- a/lib/Service/SearchService.php +++ b/lib/Service/SearchService.php @@ -12,6 +12,7 @@ use OCP\Files\Folder; use OCP\Files\GenericFileException; use OCP\Files\InvalidPathException; +use OCP\IConfig; use OCP\ITempManager; use OCP\Lock\LockedException; use PDO; @@ -23,15 +24,18 @@ class SearchService { private CollectiveFolderManager $collectiveFolderManager; private ITempManager $tempManager; private LoggerInterface $logger; + private IConfig $config; public function __construct( CollectiveFolderManager $collectiveFolderManager, ITempManager $tempManager, - LoggerInterface $logger + LoggerInterface $logger, + IConfig $config ) { $this->collectiveFolderManager = $collectiveFolderManager; $this->tempManager = $tempManager; $this->logger = $logger; + $this->config = $config; } /** @@ -48,8 +52,7 @@ public function indexCollective(Collective $collective): void { throw new FileSearchException('Collectives search service could not find folder for collective.', 0, $e); } - $searcher = new FileSearcher(); - $indexer = $searcher->createIndex($indexPath); + $indexer = $this->createFileSearcher()->createIndex($indexPath); $indexer->runOnDirectory($collectiveFolder); $this->saveIndex($collective, $indexPath); @@ -69,7 +72,7 @@ public function searchCollective(Collective $collective, string $term, int $maxR return []; } - $searcher = new FileSearcher(); + $searcher = $this->createFileSearcher(); $file = $this->getIndexForCollective($collective); if ($file === null) { $this->logger->warning('Collectives search failed to find search index for collective with ID ' . $collective->getId()); @@ -138,6 +141,11 @@ private function getOrCreateIndexForCollective(Collective $collective): ?File { return $file instanceof File ? $file : null; } + private function createFileSearcher(): FileSearcher { + $defaultLanguage = $this->config->getSystemValue('default_language', 'en'); + return new FileSearcher(FileSearcher::SUPPORTED_LANGUAGES[$defaultLanguage] ?? 'no'); + } + /** * @return Folder * @throws FileSearchException