This commit is contained in:
Jürgen Mummert
2025-12-23 11:55:02 +01:00
parent ca6878c50a
commit c90d8ed859
+108 -99
View File
@@ -2,126 +2,135 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener; namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\Database;
class IndexPageListener class IndexPageListener
{ {
public function onIndexPage(string $content, array &$data, array &$set): void public function onIndexPage(array &$data, array $set, array $page): void
{ {
// --------------------------------------------------
// DEBUG START
// --------------------------------------------------
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
echo "\n=============================\n"; echo "\n=============================\n";
echo "INDEXPAGE HOOK START\n"; echo "INDEXPAGE HOOK START\n";
echo "URL: " . ($set['url'] ?? '[no url]') . "\n"; echo "URL: {$set['url']}\n";
} }
// 1. Marker vorhanden? // --------------------------------------------------
if (!str_contains($content, 'MEILISEARCH')) { // 1. MEILISEARCH_JSON aus HTML extrahieren
if (PHP_SAPI === 'cli') { // --------------------------------------------------
echo "❌ MEILISEARCH marker NOT found in content\n"; if (
} !isset($set['content']) ||
return; !preg_match('#MEILISEARCH_JSON\s*(\{.*?\})#s', $set['content'], $m)
}
if (PHP_SAPI === 'cli') {
echo "✅ MEILISEARCH marker found\n";
}
// 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'])) {
$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'];
}
// 4. KEYWORDS
$keywords = [];
foreach (['event.keywords', 'news.keywords', 'page.keywords'] as $key) {
if (!empty($markers[$key])) {
$keywords = array_merge(
$keywords,
preg_split('/\s+/', trim($markers[$key])) ?: []
);
}
}
if ($keywords) {
$data['keywords'] = implode(' ', array_unique($keywords));
}
// 5. IMAGEPATH
foreach (
['event.searchimage', 'news.searchimage', 'page.searchimage', 'custom.searchimage']
as $key
) { ) {
if (!empty($markers[$key])) { if (PHP_SAPI === 'cli') {
$data['imagepath'] = trim($markers[$key]); echo "❌ Kein MEILISEARCH_JSON gefunden\n";
break; echo "INDEXPAGE HOOK END\n";
echo "=============================\n";
} }
return;
} }
// 6. STARTDATE $meta = json_decode($m[1], true);
foreach (['event.date', 'news.date'] as $key) {
if (!empty($markers[$key])) { if (!is_array($meta)) {
$ts = strtotime($markers[$key]); if (PHP_SAPI === 'cli') {
if ($ts !== false) { echo "❌ MEILISEARCH_JSON ist kein valides JSON\n";
$data['startDate'] = $ts; echo "INDEXPAGE HOOK END\n";
} echo "=============================\n";
break;
} }
return;
} }
// 7. FINAL STATE
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
echo "---- FINAL \$data ----\n"; echo "✅ MEILISEARCH_JSON gefunden\n";
var_dump([ echo "---- RAW JSON ----\n";
'priority' => $data['priority'] ?? null, var_dump($meta);
'keywords' => $data['keywords'] ?? null, echo "------------------\n";
'imagepath' => $data['imagepath'] ?? null, }
'startDate' => $data['startDate'] ?? null,
]);
echo "---- RAW \$data ----\n"; // --------------------------------------------------
var_dump($data); // 2. Sauberes Mapping (klar definierte Priorität)
// --------------------------------------------------
$priority =
$meta['event']['priority']
?? $meta['news']['priority']
?? $meta['page']['priority']
?? null;
echo "---- RAW \$set ----\n"; $keywords =
var_dump($set); $meta['event']['keywords']
?? $meta['news']['keywords']
?? $meta['page']['keywords']
?? null;
$imagepath =
$meta['custom']['searchimage']
?? $meta['event']['searchimage']
?? $meta['news']['searchimage']
?? $meta['page']['searchimage']
?? null;
$startDate =
$meta['event']['date']
?? $meta['news']['date']
?? null;
// --------------------------------------------------
// 3. Daten vorbereiten
// --------------------------------------------------
$update = [];
if ($priority !== null) {
$update['priority'] = (int) $priority;
}
if ($keywords !== null) {
$update['keywords'] = trim((string) $keywords);
}
if ($imagepath !== null) {
$update['imagepath'] = (string) $imagepath;
}
if ($startDate !== null) {
// ISO-Datum → UNIX-Timestamp
$ts = strtotime($startDate);
if ($ts !== false) {
$update['startDate'] = $ts;
}
}
if (PHP_SAPI === 'cli') {
echo "---- FINAL UPDATE ----\n";
var_dump($update);
echo "----------------------\n";
}
if (!$update) {
if (PHP_SAPI === 'cli') {
echo "️ Keine Felder zu aktualisieren\n";
echo "INDEXPAGE HOOK END\n";
echo "=============================\n";
}
return;
}
// --------------------------------------------------
// 4. tl_search aktualisieren
// --------------------------------------------------
Database::getInstance()
->prepare(
'UPDATE tl_search %s WHERE url=?'
)
->set($update)
->execute($set['url']);
if (PHP_SAPI === 'cli') {
echo "✅ tl_search aktualisiert\n";
echo "INDEXPAGE HOOK END\n"; echo "INDEXPAGE HOOK END\n";
echo "=============================\n"; echo "=============================\n";
} }
} }
private function extractMarkers(string $content): array
{
if (!preg_match('/<!--\s*MEILISEARCH(.*?)-->/s', $content, $m)) {
return [];
}
$markers = [];
foreach (preg_split('/\R/', trim($m[1])) as $line) {
if (!str_contains($line, '=')) {
continue;
}
[$k, $v] = explode('=', $line, 2);
$markers[trim($k)] = trim($v);
}
return $markers;
}
} }