This commit is contained in:
Jürgen Mummert
2025-12-23 11:58:09 +01:00
parent c90d8ed859
commit 64ebc8e4a5
+61 -78
View File
@@ -2,30 +2,24 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener; namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\Database;
class IndexPageListener class IndexPageListener
{ {
public function onIndexPage(array &$data, array $set, array $page): void public function onIndexPage(string $content, array &$data, array &$set): 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']}\n"; echo "URL: " . ($set['url'] ?? '[no url]') . "\n";
} }
// -------------------------------------------------- // --------------------------------------------------
// 1. MEILISEARCH_JSON aus HTML extrahieren // 1. JSON-Marker finden
// -------------------------------------------------- // --------------------------------------------------
if ( if (
!isset($set['content']) || !preg_match('#MEILISEARCH_JSON\s*(\{.*?\})#s', $content, $m)
!preg_match('#MEILISEARCH_JSON\s*(\{.*?\})#s', $set['content'], $m)
) { ) {
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
echo " Kein MEILISEARCH_JSON gefunden\n"; echo "❌ MEILISEARCH_JSON not found\n";
echo "INDEXPAGE HOOK END\n"; echo "INDEXPAGE HOOK END\n";
echo "=============================\n"; echo "=============================\n";
} }
@@ -36,7 +30,7 @@ class IndexPageListener
if (!is_array($meta)) { if (!is_array($meta)) {
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
echo "MEILISEARCH_JSON ist kein valides JSON\n"; echo "Invalid JSON in MEILISEARCH_JSON\n";
echo "INDEXPAGE HOOK END\n"; echo "INDEXPAGE HOOK END\n";
echo "=============================\n"; echo "=============================\n";
} }
@@ -44,91 +38,80 @@ class IndexPageListener
} }
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
echo "✅ MEILISEARCH_JSON gefunden\n"; echo "✅ MEILISEARCH_JSON parsed\n";
echo "---- RAW JSON ----\n";
var_dump($meta); var_dump($meta);
echo "------------------\n";
} }
// -------------------------------------------------- // --------------------------------------------------
// 2. Sauberes Mapping (klar definierte Priorität) // 2. PRIORITY (klar definierte Reihenfolge)
// -------------------------------------------------- // --------------------------------------------------
$priority = if (isset($meta['event']['priority'])) {
$meta['event']['priority'] $data['priority'] = (int) $meta['event']['priority'];
?? $meta['news']['priority'] } elseif (isset($meta['news']['priority'])) {
?? $meta['page']['priority'] $data['priority'] = (int) $meta['news']['priority'];
?? null; } elseif (isset($meta['page']['priority'])) {
$data['priority'] = (int) $meta['page']['priority'];
$keywords =
$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); // 3. KEYWORDS
// --------------------------------------------------
$keywords = [];
foreach (['event', 'news', 'page'] as $scope) {
if (!empty($meta[$scope]['keywords'])) {
$keywords = array_merge(
$keywords,
preg_split('/\s+/', trim($meta[$scope]['keywords'])) ?: []
);
}
} }
if ($imagepath !== null) { if ($keywords) {
$update['imagepath'] = (string) $imagepath; $data['keywords'] = implode(' ', array_unique($keywords));
} }
if ($startDate !== null) { // --------------------------------------------------
// ISO-Datum → UNIX-Timestamp // 4. IMAGEPATH (custom > event > news > page)
$ts = strtotime($startDate); // --------------------------------------------------
foreach (
[
$meta['custom']['searchimage'] ?? null,
$meta['event']['searchimage'] ?? null,
$meta['news']['searchimage'] ?? null,
$meta['page']['searchimage'] ?? null,
] as $uuid
) {
if ($uuid) {
$data['imagepath'] = (string) $uuid;
break;
}
}
// --------------------------------------------------
// 5. STARTDATE
// --------------------------------------------------
foreach (['event', 'news'] as $scope) {
if (!empty($meta[$scope]['date'])) {
$ts = strtotime($meta[$scope]['date']);
if ($ts !== false) { if ($ts !== false) {
$update['startDate'] = $ts; $data['startDate'] = $ts;
} }
break;
} }
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 // DEBUG
// -------------------------------------------------- // --------------------------------------------------
Database::getInstance()
->prepare(
'UPDATE tl_search %s WHERE url=?'
)
->set($update)
->execute($set['url']);
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
echo "✅ tl_search aktualisiert\n"; echo "---- FINAL \$data ----\n";
var_dump([
'priority' => $data['priority'] ?? null,
'keywords' => $data['keywords'] ?? null,
'imagepath' => $data['imagepath'] ?? null,
'startDate' => $data['startDate'] ?? null,
]);
echo "INDEXPAGE HOOK END\n"; echo "INDEXPAGE HOOK END\n";
echo "=============================\n"; echo "=============================\n";
} }