Skip to content

Commit

Permalink
Set default language for TNTSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Koc committed Dec 29, 2023
1 parent 17b2bab commit 84f4301
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
22 changes: 21 additions & 1 deletion lib/Search/FileSearch/FileSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

Expand All @@ -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;
}
Expand Down
17 changes: 13 additions & 4 deletions lib/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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());
Expand Down Expand Up @@ -138,6 +141,12 @@ 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
Expand Down

0 comments on commit 84f4301

Please sign in to comment.