This commit is contained in:
Jürgen Mummert
2025-12-23 11:44:03 +01:00
parent d44b84094d
commit 8a90d2622d
+62 -104
View File
@@ -2,65 +2,54 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener; namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\System;
use Doctrine\DBAL\Connection;
class IndexPageListener class IndexPageListener
{ {
private static bool $shutdownRegistered = false;
/** @var array<string, array{priority?:int, keywords?:string, imagepath?:string, startDate?:int}> */
private static array $queue = [];
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')) {
return;
}
$markers = $this->extractMarkers($content);
if ($markers === []) {
return;
}
// Debug ohne Risiko
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
echo "INDEXPAGE LISTENER ACTIVE: " . ($set['url'] ?? '[no url]') . "\n"; echo "\n=============================\n";
echo "INDEXPAGE HOOK START\n";
echo "URL: " . ($set['url'] ?? '[no url]') . "\n";
} }
// Wir updaten final über checksum (stabil, egal ob URL mit/ohne Domain) // 1. Marker vorhanden?
$checksum = $data['checksum'] ?? null; if (!str_contains($content, 'MEILISEARCH')) {
if (!$checksum) { if (PHP_SAPI === 'cli') {
echo "❌ MEILISEARCH marker NOT found in content\n";
}
return; return;
} }
/* if (PHP_SAPI === 'cli') {
* PRIORITY: event/news > page echo "✅ MEILISEARCH marker found\n";
*/ }
$priority = null;
// 2. Marker extrahieren
$markers = $this->extractMarkers($content);
if (PHP_SAPI === 'cli') {
echo "---- PARSED MARKERS ----\n";
var_dump($markers);
echo "------------------------\n";
}
if ($markers === []) {
if (PHP_SAPI === 'cli') {
echo "❌ Marker array EMPTY after parsing\n";
}
return;
}
// 3. PRIORITY
if (isset($markers['event.priority'])) { if (isset($markers['event.priority'])) {
$priority = (int) $markers['event.priority']; $data['priority'] = (int) $markers['event.priority'];
} elseif (isset($markers['news.priority'])) { } elseif (isset($markers['news.priority'])) {
$priority = (int) $markers['news.priority']; $data['priority'] = (int) $markers['news.priority'];
} elseif (isset($markers['page.priority'])) { } elseif (isset($markers['page.priority'])) {
$priority = (int) $markers['page.priority']; $data['priority'] = (int) $markers['page.priority'];
} }
if ($priority !== null) { // 4. KEYWORDS
$data['priority'] = $priority; // bleibt bei dir schon stehen
$set['priority'] = $priority; // harmless, aber konsistent
}
/*
* KEYWORDS: kombinieren
*/
$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])) {
@@ -71,80 +60,50 @@ class IndexPageListener
} }
} }
$keywords = array_values(array_unique(array_filter($keywords))); if ($keywords) {
$keywordsString = $keywords ? implode(' ', $keywords) : ''; $data['keywords'] = implode(' ', array_unique($keywords));
}
// Wichtig: in $set setzen, weil Contao später oft nochmal keywords finalisiert // 5. IMAGEPATH
$set['keywords'] = $keywordsString; foreach (
['event.searchimage', 'news.searchimage', 'page.searchimage', 'custom.searchimage']
/* as $key
* IMAGEPATH (UUID): event/news > page > custom ) {
*/
$imageUuid = '';
foreach (['event.searchimage', 'news.searchimage', 'page.searchimage', 'custom.searchimage'] as $key) {
if (!empty($markers[$key])) { if (!empty($markers[$key])) {
$imageUuid = trim($markers[$key]); $data['imagepath'] = trim($markers[$key]);
break; break;
} }
} }
// Ebenso: in $set setzen (damit es nicht überschrieben wird) // 6. STARTDATE
$set['imagepath'] = $imageUuid;
/*
* STARTDATE (Timestamp)
*/
$startDate = 0;
foreach (['event.date', 'news.date'] as $key) { foreach (['event.date', 'news.date'] as $key) {
if (!empty($markers[$key])) { if (!empty($markers[$key])) {
$ts = strtotime(trim($markers[$key])); $ts = strtotime($markers[$key]);
if ($ts !== false) { if ($ts !== false) {
$startDate = (int) $ts; $data['startDate'] = $ts;
} }
break; break;
} }
} }
$set['startDate'] = $startDate; // 7. FINAL STATE
if (PHP_SAPI === 'cli') {
echo "---- FINAL \$data ----\n";
var_dump([
'priority' => $data['priority'] ?? null,
'keywords' => $data['keywords'] ?? null,
'imagepath' => $data['imagepath'] ?? null,
'startDate' => $data['startDate'] ?? null,
]);
/* echo "---- RAW \$data ----\n";
* Sicherheitsnetz: am Ende definitiv in tl_search schreiben var_dump($data);
*/
self::$queue[$checksum] = [
'priority' => $priority,
'keywords' => $keywordsString,
'imagepath' => $imageUuid,
'startDate' => $startDate,
];
if (!self::$shutdownRegistered) { echo "---- RAW \$set ----\n";
self::$shutdownRegistered = true; var_dump($set);
register_shutdown_function(function (): void { echo "INDEXPAGE HOOK END\n";
$db = System::getContainer()->get('database_connection'); echo "=============================\n";
foreach (self::$queue as $checksum => $values) {
$params = ['checksum' => $checksum];
$sets = [];
if (array_key_exists('priority', $values) && $values['priority'] !== null) {
$sets[] = 'priority = :priority';
$params['priority'] = $values['priority'];
}
$sets[] = 'keywords = :keywords';
$params['keywords'] = $values['keywords'] ?? '';
$sets[] = 'imagepath = :imagepath';
$params['imagepath'] = $values['imagepath'] ?? '';
$sets[] = 'startDate = :startDate';
$params['startDate'] = $values['startDate'] ?? 0;
$sql = 'UPDATE tl_search SET ' . implode(', ', $sets) . ' WHERE checksum = :checksum';
$db->executeStatement($sql, $params);
}
});
} }
} }
@@ -155,9 +114,8 @@ class IndexPageListener
} }
$markers = []; $markers = [];
foreach (preg_split('/\R/', trim($m[1])) ?: [] as $line) { foreach (preg_split('/\R/', trim($m[1])) as $line) {
$line = trim($line); if (!str_contains($line, '=')) {
if ($line === '' || !str_contains($line, '=')) {
continue; continue;
} }
[$k, $v] = explode('=', $line, 2); [$k, $v] = explode('=', $line, 2);