This commit is contained in:
Jürgen Mummert
2025-12-23 12:12:59 +01:00
parent b98cc225fc
commit dbf58d0163
+76 -78
View File
@@ -2,51 +2,44 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener; namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
#[AsHook('indexPage')]
class IndexPageListener class IndexPageListener
{ {
public function __invoke(string $content, array $pageData, array &$indexData): void public function onIndexPage(string $content, array &$data, array &$set): void
{ {
// Debug nur im CLI (Crawler)
$debug = (PHP_SAPI === 'cli'); $debug = (PHP_SAPI === 'cli');
if ($debug) { if ($debug) {
echo "\n=============================\n"; echo "\n=============================\n";
echo "INDEXPAGE HOOK START\n"; echo "INDEXPAGE HOOK START\n";
echo "URL: " . ($pageData['url'] ?? '[no url]') . "\n"; echo "URL: " . ($set['url'] ?? '[no url]') . "\n";
} }
// 1) Marker finden // --------------------------------------------------
if (!str_contains($content, 'MEILISEARCH_JSON')) { // 1. MEILISEARCH_JSON finden
// --------------------------------------------------
if (
!preg_match(
'#<!--\s*MEILISEARCH_JSON\s*(.*?)\s*-->#s',
$content,
$m
)
) {
if ($debug) { if ($debug) {
echo "❌ MEILISEARCH_JSON marker NOT found\n"; echo "❌ MEILISEARCH_JSON not found\n";
echo "INDEXPAGE HOOK END\n"; echo "INDEXPAGE HOOK END\n";
echo "=============================\n"; echo "=============================\n";
} }
return; return;
} }
// 2) JSON Block extrahieren $json = trim($m[1]);
$json = $this->extractJsonBlock($content); $meta = json_decode($json, true);
if ($json === null) { if (!is_array($meta)) {
if ($debug) {
echo "❌ Could not extract JSON block from MEILISEARCH_JSON\n";
echo "INDEXPAGE HOOK END\n";
echo "=============================\n";
}
return;
}
// 3) JSON dekodieren
$parsed = json_decode($json, true);
if (!is_array($parsed)) {
if ($debug) { if ($debug) {
echo "❌ Invalid JSON in MEILISEARCH_JSON\n"; echo "❌ Invalid JSON in MEILISEARCH_JSON\n";
echo "RAW:\n" . $json . "\n"; echo "RAW JSON:\n$json\n";
echo "JSON ERROR: " . json_last_error_msg() . "\n";
echo "INDEXPAGE HOOK END\n"; echo "INDEXPAGE HOOK END\n";
echo "=============================\n"; echo "=============================\n";
} }
@@ -55,79 +48,84 @@ class IndexPageListener
if ($debug) { if ($debug) {
echo "✅ MEILISEARCH_JSON parsed\n"; echo "✅ MEILISEARCH_JSON parsed\n";
var_dump($parsed); var_dump($meta);
} }
// 4) PRIORITY: event > news > page // --------------------------------------------------
$priority = $parsed['event']['priority'] // 2. PRIORITY (event > news > page)
?? $parsed['news']['priority'] // --------------------------------------------------
?? $parsed['page']['priority']
?? null;
if ($priority !== null && $priority !== '') {
$indexData['priority'] = (int) $priority;
}
// 5) KEYWORDS: event + news + page zusammenführen
$kwParts = [];
foreach (['event', 'news', 'page'] as $scope) { foreach (['event', 'news', 'page'] as $scope) {
if (!empty($parsed[$scope]['keywords'])) { if (!empty($meta[$scope]['priority'])) {
$kwParts[] = (string) $parsed[$scope]['keywords']; $set['priority'] = (int) $meta[$scope]['priority'];
break;
} }
} }
if ($kwParts) { // --------------------------------------------------
$all = preg_split('/\s+/', trim(implode(' ', $kwParts))) ?: []; // 3. KEYWORDS (kombinieren)
$all = array_values(array_unique(array_filter($all))); // --------------------------------------------------
if ($all) { $keywords = [];
$indexData['keywords'] = implode(' ', $all);
foreach (['event', 'news', 'page'] as $scope) {
if (!empty($meta[$scope]['keywords'])) {
$parts = preg_split(
'/\s+/',
trim((string) $meta[$scope]['keywords'])
) ?: [];
$keywords = array_merge($keywords, $parts);
} }
} }
// 6) IMAGEPATH: custom > event > news > page if ($keywords) {
$image = $parsed['custom']['searchimage'] $set['keywords'] = implode(' ', array_unique($keywords));
?? $parsed['event']['searchimage']
?? $parsed['news']['searchimage']
?? $parsed['page']['searchimage']
?? null;
if (!empty($image)) {
$indexData['imagepath'] = trim((string) $image);
} }
// 7) STARTDATE: event.date oder news.date -> timestamp // --------------------------------------------------
$date = $parsed['event']['date'] ?? $parsed['news']['date'] ?? null; // 4. IMAGEPATH
if (!empty($date)) { // Reihenfolge: custom > event > news > page
$ts = strtotime((string) $date); // --------------------------------------------------
foreach (
[
$meta['custom']['searchimage'] ?? null,
$meta['event']['searchimage'] ?? null,
$meta['news']['searchimage'] ?? null,
$meta['page']['searchimage'] ?? null,
] as $img
) {
if ($img) {
$set['imagepath'] = trim((string) $img);
break;
}
}
// --------------------------------------------------
// 5. STARTDATE
// --------------------------------------------------
foreach (['event', 'news'] as $scope) {
if (!empty($meta[$scope]['date'])) {
$ts = strtotime((string) $meta[$scope]['date']);
if ($ts !== false) { if ($ts !== false) {
$indexData['startDate'] = $ts; $set['startDate'] = $ts;
}
break;
} }
} }
// --------------------------------------------------
// DEBUG
// --------------------------------------------------
if ($debug) { if ($debug) {
echo "---- FINAL \$indexData (what should be persisted) ----\n"; echo "---- FINAL \$set ----\n";
var_dump([ var_dump([
'priority' => $indexData['priority'] ?? null, 'priority' => $set['priority'] ?? null,
'keywords' => $indexData['keywords'] ?? null, 'keywords' => $set['keywords'] ?? null,
'imagepath' => $indexData['imagepath'] ?? null, 'imagepath' => $set['imagepath'] ?? null,
'startDate' => $indexData['startDate'] ?? null, 'startDate' => $set['startDate'] ?? null,
]); ]);
echo "INDEXPAGE HOOK END\n"; echo "INDEXPAGE HOOK END\n";
echo "=============================\n"; echo "=============================\n";
} }
} }
private function extractJsonBlock(string $content): ?string
{
// Nimmt alles zwischen:
// <!--
// MEILISEARCH_JSON
// { ... }
// -->
if (!preg_match('/<!--\s*MEILISEARCH_JSON\s*(\{.*?\})\s*-->/s', $content, $m)) {
return null;
}
return trim($m[1]);
}
} }