Skip to content

Commit

Permalink
Merge pull request #12 from visuellverstehen/add-multi-language-support
Browse files Browse the repository at this point in the history
feat: add multi-language support
  • Loading branch information
runepiper authored Mar 18, 2024
2 parents 6a0c622 + bb376f6 commit 0e55997
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
21 changes: 20 additions & 1 deletion Classes/Domain/Model/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Document extends AbstractDomainObject
protected string $content = '';
protected string $type = '';
protected array $_formatted = [];
protected int $languageId = 0;

public function getId(): string
{
Expand Down Expand Up @@ -91,6 +92,16 @@ public function setType(string $type): void
$this->type = strtolower($type);
}

public function getLanguageId(): int
{
return $this->languageId;
}

public function setLanguageId(int $languageId): void
{
$this->languageId = $languageId;
}

public function setFormatted(array $_formatted): void
{
if ($_formatted['content']) {
Expand All @@ -107,6 +118,12 @@ public static function createFromTSFE(TypoScriptFrontendController $tsfe): Docum
preg_match('/<body>(.*?)<\/body>/s', $tsfe->content, $content);
}

// Remove code that shouldn't be indexed
$content = preg_replace('/<select(.*?)<\/select>/s', '', $content);
$content = preg_replace('/<input(.*?)\/>/s', '', $content);
$content = preg_replace('/<label(.*?)\/label>/s', '', $content);
$content = preg_replace('/<svg(.*?)\/svg>/s', '', $content);

// Remove query and fragments from URL
$uri = $tsfe->cObj->getRequest()->getUri();
$url = $uri->getScheme() . '://' . $uri->getAuthority();
Expand All @@ -122,11 +139,12 @@ public static function createFromTSFE(TypoScriptFrontendController $tsfe): Docum
$document = new Document();
$document->setId(md5($url));
$document->setRootPageId($tsfe->getSite()->getRootPageId() ?? 0);
$document->setContent($content[0] ?? '');
$document->setContent(implode(PHP_EOL, $content ?? []));
$document->setType('page');
$document->setTitle($tsfe->page['title'] ?? '');
$document->setUrl($url);
$document->setCrdate($tsfe->page['crdate']);
$document->setLanguageId($tsfe->getLanguage()->getLanguageId() ?? 0);

return $document;
}
Expand All @@ -141,6 +159,7 @@ public function toArray(): array
'url' => $this->url,
'content' => $this->content,
'type' => $this->type,
'languageId' => $this->languageId,
];
}
}
3 changes: 2 additions & 1 deletion Classes/Service/IndexService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct()
try {
if ($this->client->isHealthy()) {
$this->client->index($this->index)->updateSettings([
'filterableAttributes' => ['rootPageId', 'type'],
'filterableAttributes' => ['rootPageId', 'type', 'languageId'],
'sortableAttributes' => ['crdate'],
]);
}
Expand Down Expand Up @@ -106,6 +106,7 @@ public function checkForFiles(TypoScriptFrontendController $tsfe)
$document->setContent($content);
$document->setType('pdf');
$document->setCrdate(filemtime($absolutePath));
$document->setLanguageId(-1);

$this->add($document);
}
Expand Down
1 change: 1 addition & 0 deletions Classes/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function search(string $query, string $sorting = 'crdate_desc', array $ty
// Filter by checking the rootPageId is in the rootline
'filter' => [
'rootPageId = ' . $GLOBALS['TSFE']->getSite()->getRootPageId(),
'languageId IN [-1,' . $GLOBALS['TSFE']->getLanguage()->getLanguageId() . ']',
$typesFilter,
],
]);
Expand Down

0 comments on commit 0e55997

Please sign in to comment.