Bugfix
This commit is contained in:
@@ -2,8 +2,18 @@
|
|||||||
|
|
||||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||||
|
|
||||||
|
use Contao\System;
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
|
|
||||||
class IndexPageListener
|
class IndexPageListener
|
||||||
{
|
{
|
||||||
|
private Connection $db;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->db = System::getContainer()->get('database_connection');
|
||||||
|
}
|
||||||
|
|
||||||
public function onIndexPage(string $content, array &$data, array &$set): void
|
public function onIndexPage(string $content, array &$data, array &$set): void
|
||||||
{
|
{
|
||||||
if (!str_contains($content, 'MEILISEARCH')) {
|
if (!str_contains($content, 'MEILISEARCH')) {
|
||||||
@@ -15,56 +25,86 @@ class IndexPageListener
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// URL aus $set (für Crawl vorhanden)
|
||||||
* PRIORITY: event/news > page
|
$url = $set['url'] ?? null;
|
||||||
*/
|
if (!$url) {
|
||||||
if (isset($markers['event.priority'])) {
|
return;
|
||||||
$data['priority'] = (int) $markers['event.priority'];
|
|
||||||
} elseif (isset($markers['news.priority'])) {
|
|
||||||
$data['priority'] = (int) $markers['news.priority'];
|
|
||||||
} elseif (isset($markers['page.priority'])) {
|
|
||||||
$data['priority'] = (int) $markers['page.priority'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// priority: event/news > page
|
||||||
* KEYWORDS: kombinieren
|
$priority = null;
|
||||||
*/
|
if (isset($markers['event.priority'])) {
|
||||||
|
$priority = (int) $markers['event.priority'];
|
||||||
|
} elseif (isset($markers['news.priority'])) {
|
||||||
|
$priority = (int) $markers['news.priority'];
|
||||||
|
} elseif (isset($markers['page.priority'])) {
|
||||||
|
$priority = (int) $markers['page.priority'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// keywords kombiniert
|
||||||
$keywords = [];
|
$keywords = [];
|
||||||
foreach (['event.keywords', 'news.keywords', 'page.keywords'] as $key) {
|
foreach (['event.keywords', 'news.keywords', 'page.keywords'] as $key) {
|
||||||
if (!empty($markers[$key])) {
|
if (!empty($markers[$key])) {
|
||||||
$keywords = array_merge(
|
$keywords = array_merge($keywords, preg_split('/\s+/', trim($markers[$key])) ?: []);
|
||||||
$keywords,
|
|
||||||
preg_split('/\s+/', trim($markers[$key]))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$keywords = array_values(array_unique(array_filter($keywords)));
|
||||||
|
$keywordsString = $keywords ? implode(' ', $keywords) : null;
|
||||||
|
|
||||||
if ($keywords) {
|
// searchimage uuid: event/news > page > custom
|
||||||
$data['keywords'] = implode(' ', array_unique($keywords));
|
$imageUuid = null;
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* SEARCH IMAGE (UUID!)
|
|
||||||
*/
|
|
||||||
foreach (['event.searchimage', 'news.searchimage', 'page.searchimage', 'custom.searchimage'] as $key) {
|
foreach (['event.searchimage', 'news.searchimage', 'page.searchimage', 'custom.searchimage'] as $key) {
|
||||||
if (!empty($markers[$key])) {
|
if (!empty($markers[$key])) {
|
||||||
$data['imagepath'] = trim($markers[$key]);
|
$imageUuid = trim($markers[$key]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// startDate (timestamp)
|
||||||
* START DATE (Timestamp)
|
$startDate = null;
|
||||||
*/
|
|
||||||
foreach (['event.date', 'news.date'] as $key) {
|
foreach (['event.date', 'news.date'] as $key) {
|
||||||
if (!empty($markers[$key])) {
|
if (!empty($markers[$key])) {
|
||||||
$ts = strtotime($markers[$key]);
|
$ts = strtotime(trim($markers[$key]));
|
||||||
if ($ts !== false) {
|
if ($ts !== false) {
|
||||||
$data['startDate'] = $ts;
|
$startDate = (int) $ts;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Nichts zu schreiben? Dann raus.
|
||||||
|
if ($priority === null && $keywordsString === null && $imageUuid === null && $startDate === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ DB-Update (tl_search) anhand der URL
|
||||||
|
$update = [];
|
||||||
|
$params = ['url' => $url];
|
||||||
|
$types = [];
|
||||||
|
|
||||||
|
if ($priority !== null) {
|
||||||
|
$update['priority'] = ':priority';
|
||||||
|
$params['priority'] = $priority;
|
||||||
|
}
|
||||||
|
if ($keywordsString !== null) {
|
||||||
|
$update['keywords'] = ':keywords';
|
||||||
|
$params['keywords'] = $keywordsString;
|
||||||
|
}
|
||||||
|
if ($imageUuid !== null) {
|
||||||
|
$update['imagepath'] = ':imagepath';
|
||||||
|
$params['imagepath'] = $imageUuid;
|
||||||
|
}
|
||||||
|
if ($startDate !== null) {
|
||||||
|
$update['startDate'] = ':startDate';
|
||||||
|
$params['startDate'] = $startDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
$setSql = implode(', ', array_map(fn($col, $ph) => "$col = $ph", array_keys($update), $update));
|
||||||
|
|
||||||
|
$this->db->executeStatement(
|
||||||
|
"UPDATE tl_search SET $setSql WHERE url = :url",
|
||||||
|
$params
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function extractMarkers(string $content): array
|
private function extractMarkers(string $content): array
|
||||||
@@ -74,8 +114,9 @@ class IndexPageListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
$markers = [];
|
$markers = [];
|
||||||
foreach (preg_split('/\R/', trim($m[1])) as $line) {
|
foreach (preg_split('/\R/', trim($m[1])) ?: [] as $line) {
|
||||||
if (!str_contains($line, '=')) {
|
$line = trim($line);
|
||||||
|
if ($line === '' || !str_contains($line, '=')) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
[$k, $v] = explode('=', $line, 2);
|
[$k, $v] = explode('=', $line, 2);
|
||||||
|
|||||||
Reference in New Issue
Block a user