Skip to content

Commit

Permalink
Merge branch 'main' into update-for-v12
Browse files Browse the repository at this point in the history
  • Loading branch information
runepiper committed Apr 24, 2024
2 parents 800f626 + 0e55997 commit 9c1b5df
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 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,
];
}
}
5 changes: 3 additions & 2 deletions 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 All @@ -46,7 +46,7 @@ public function add(Document $document)
try {
if ($this->client->isHealthy()) {
$index = $this->client->index($this->index);
$index->addDocuments($document->toArray());
$index->addDocuments($document->toArray(), 'id');
} else {
$this->logger->warning('MeiliSearch is not healthy.');
}
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
8 changes: 1 addition & 7 deletions Classes/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ public function search(string $query, string $sorting = 'crdate_desc', array $ty
return $result;
}

// Do not try to search when caching is disabled
// We might have no index ready and an exception
// would occur
if ($GLOBALS['TSFE']->no_cache === true) {
return $result;
}

[$sortingColumn, $sortingDesc] = explode('_', $sorting);

foreach ($types as $type) {
Expand All @@ -53,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
44 changes: 22 additions & 22 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"name": "visuellverstehen/t3meilisearch",
"description": "Extension to use Meilisearch as search.",
"type": "typo3-cms-extension",
"require": {
"typo3/cms-core": "^10.4 || ^11.5 || ^12.4",
"meilisearch/meilisearch-php": "^0.24",
"spatie/pdf-to-text": "^1.52"
},
"autoload": {
"psr-4": {
"VV\\T3meilisearch\\": "Classes/"
"name": "visuellverstehen/t3meilisearch",
"description": "Extension to use Meilisearch as search.",
"type": "typo3-cms-extension",
"require": {
"typo3/cms-core": "^10.4 || ^11.5 || ^12.4",
"meilisearch/meilisearch-php": "^1.6",
"spatie/pdf-to-text": "^1.52.1"
},
"autoload": {
"psr-4": {
"VV\\T3meilisearch\\": "Classes/"
}
},
"autoload-dev": {
"psr-4": {
"VV\\T3meilisearch\\Tests\\": "Tests/"
}
},
"extra": {
"typo3/cms": {
"extension-key": "t3meilisearch"
}
}
},
"autoload-dev": {
"psr-4": {
"VV\\T3meilisearch\\Tests\\": "Tests/"
}
},
"extra": {
"typo3/cms": {
"extension-key": "t3meilisearch"
}
}
}

0 comments on commit 9c1b5df

Please sign in to comment.