Bugfix
This commit is contained in:
@@ -2,19 +2,23 @@
|
|||||||
|
|
||||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||||
|
|
||||||
|
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
|
||||||
|
|
||||||
|
#[AsHook('indexPage')]
|
||||||
class IndexPageListener
|
class IndexPageListener
|
||||||
{
|
{
|
||||||
public function onIndexPage(string $content, array &$data, array &$set): void
|
public function __invoke(string $content, array $pageData, array &$indexData): 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: " . ($set['url'] ?? $data['url'] ?? '[no url]') . "\n";
|
echo "URL: " . ($pageData['url'] ?? '[no url]') . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marker vorhanden?
|
// 1) Marker finden
|
||||||
if (!str_contains($content, 'MEILISEARCH_JSON')) {
|
if (!str_contains($content, 'MEILISEARCH_JSON')) {
|
||||||
if ($debug) {
|
if ($debug) {
|
||||||
echo "❌ MEILISEARCH_JSON marker NOT found\n";
|
echo "❌ MEILISEARCH_JSON marker NOT found\n";
|
||||||
@@ -24,12 +28,25 @@ class IndexPageListener
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON aus Kommentar extrahieren + parsen
|
// 2) JSON Block extrahieren
|
||||||
$parsed = $this->extractMeilisearchJson($content);
|
$json = $this->extractJsonBlock($content);
|
||||||
|
|
||||||
if ($parsed === null) {
|
if ($json === null) {
|
||||||
|
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 "INDEXPAGE HOOK END\n";
|
echo "INDEXPAGE HOOK END\n";
|
||||||
echo "=============================\n";
|
echo "=============================\n";
|
||||||
}
|
}
|
||||||
@@ -41,118 +58,76 @@ class IndexPageListener
|
|||||||
var_dump($parsed);
|
var_dump($parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// 4) PRIORITY: event > news > page
|
||||||
* =====================
|
$priority = $parsed['event']['priority']
|
||||||
* PRIORITY (event > news > page)
|
?? $parsed['news']['priority']
|
||||||
* =====================
|
?? $parsed['page']['priority']
|
||||||
*/
|
?? null;
|
||||||
$priority =
|
|
||||||
$parsed['event']['priority'] ?? null ??
|
|
||||||
$parsed['news']['priority'] ?? null ??
|
|
||||||
$parsed['page']['priority'] ?? null;
|
|
||||||
|
|
||||||
if ($priority !== null && $priority !== '') {
|
if ($priority !== null && $priority !== '') {
|
||||||
$set['priority'] = (int) $priority;
|
$indexData['priority'] = (int) $priority;
|
||||||
$data['priority'] = (int) $priority; // optional
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// 5) KEYWORDS: event + news + page zusammenführen
|
||||||
* =====================
|
$kwParts = [];
|
||||||
* KEYWORDS (merge)
|
foreach (['event', 'news', 'page'] as $scope) {
|
||||||
* =====================
|
if (!empty($parsed[$scope]['keywords'])) {
|
||||||
*/
|
$kwParts[] = (string) $parsed[$scope]['keywords'];
|
||||||
$keywordSources = [
|
|
||||||
$parsed['event']['keywords'] ?? null,
|
|
||||||
$parsed['news']['keywords'] ?? null,
|
|
||||||
$parsed['page']['keywords'] ?? null,
|
|
||||||
];
|
|
||||||
|
|
||||||
$kw = [];
|
|
||||||
foreach ($keywordSources as $s) {
|
|
||||||
if (!is_string($s) || trim($s) === '') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$parts = preg_split('/\s+/', trim($s)) ?: [];
|
|
||||||
foreach ($parts as $p) {
|
|
||||||
$p = trim($p);
|
|
||||||
if ($p !== '') {
|
|
||||||
$kw[] = $p;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($kw) {
|
if ($kwParts) {
|
||||||
$kw = array_values(array_unique($kw));
|
$all = preg_split('/\s+/', trim(implode(' ', $kwParts))) ?: [];
|
||||||
$set['keywords'] = implode(' ', $kw);
|
$all = array_values(array_unique(array_filter($all)));
|
||||||
$data['keywords'] = $set['keywords']; // optional
|
if ($all) {
|
||||||
|
$indexData['keywords'] = implode(' ', $all);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// 6) IMAGEPATH: custom > event > news > page
|
||||||
* =====================
|
$image = $parsed['custom']['searchimage']
|
||||||
* IMAGEPATH (event > news > page > custom)
|
?? $parsed['event']['searchimage']
|
||||||
* =====================
|
?? $parsed['news']['searchimage']
|
||||||
*/
|
?? $parsed['page']['searchimage']
|
||||||
$image =
|
?? null;
|
||||||
$parsed['event']['searchimage'] ?? null ??
|
|
||||||
$parsed['news']['searchimage'] ?? null ??
|
|
||||||
$parsed['page']['searchimage'] ?? null ??
|
|
||||||
$parsed['custom']['searchimage'] ?? null;
|
|
||||||
|
|
||||||
if (is_string($image) && $image !== '') {
|
if (!empty($image)) {
|
||||||
$set['imagepath'] = trim($image);
|
$indexData['imagepath'] = trim((string) $image);
|
||||||
$data['imagepath'] = $set['imagepath']; // optional
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// 7) STARTDATE: event.date oder news.date -> timestamp
|
||||||
* =====================
|
$date = $parsed['event']['date'] ?? $parsed['news']['date'] ?? null;
|
||||||
* STARTDATE (event.date/news.date => timestamp)
|
if (!empty($date)) {
|
||||||
* =====================
|
$ts = strtotime((string) $date);
|
||||||
*/
|
|
||||||
$date =
|
|
||||||
$parsed['event']['date'] ?? null ??
|
|
||||||
$parsed['news']['date'] ?? null;
|
|
||||||
|
|
||||||
if (is_string($date) && $date !== '') {
|
|
||||||
$ts = strtotime($date);
|
|
||||||
if ($ts !== false) {
|
if ($ts !== false) {
|
||||||
$set['startDate'] = $ts;
|
$indexData['startDate'] = $ts;
|
||||||
$data['startDate'] = $ts; // optional
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($debug) {
|
if ($debug) {
|
||||||
echo "---- FINAL \$set (what should be persisted) ----\n";
|
echo "---- FINAL \$indexData (what should be persisted) ----\n";
|
||||||
var_dump([
|
var_dump([
|
||||||
'priority' => $set['priority'] ?? null,
|
'priority' => $indexData['priority'] ?? null,
|
||||||
'keywords' => $set['keywords'] ?? null,
|
'keywords' => $indexData['keywords'] ?? null,
|
||||||
'imagepath' => $set['imagepath'] ?? null,
|
'imagepath' => $indexData['imagepath'] ?? null,
|
||||||
'startDate' => $set['startDate'] ?? null,
|
'startDate' => $indexData['startDate'] ?? null,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
echo "INDEXPAGE HOOK END\n";
|
echo "INDEXPAGE HOOK END\n";
|
||||||
echo "=============================\n";
|
echo "=============================\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function extractMeilisearchJson(string $content): ?array
|
private function extractJsonBlock(string $content): ?string
|
||||||
{
|
{
|
||||||
// Erwartetes Format:
|
// Nimmt alles zwischen:
|
||||||
// <!--
|
// <!--
|
||||||
// MEILISEARCH_JSON
|
// MEILISEARCH_JSON
|
||||||
// { ...json... }
|
// { ... }
|
||||||
// -->
|
// -->
|
||||||
if (!preg_match('/<!--\s*MEILISEARCH_JSON\s*(\{.*?\})\s*-->/s', $content, $m)) {
|
if (!preg_match('/<!--\s*MEILISEARCH_JSON\s*(\{.*?\})\s*-->/s', $content, $m)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$json = trim($m[1]);
|
return trim($m[1]);
|
||||||
|
|
||||||
// BOM / Sonderzeichen am Anfang killen (kommt manchmal beim Copy/Paste vor)
|
|
||||||
$json = preg_replace('/^\xEF\xBB\xBF/', '', $json);
|
|
||||||
|
|
||||||
$data = json_decode($json, true);
|
|
||||||
|
|
||||||
return is_array($data) ? $data : null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user