Bugfix
This commit is contained in:
@@ -6,133 +6,153 @@ class IndexPageListener
|
|||||||
{
|
{
|
||||||
public function onIndexPage(string $content, array &$data, array &$set): void
|
public function onIndexPage(string $content, array &$data, array &$set): void
|
||||||
{
|
{
|
||||||
$isCli = (PHP_SAPI === 'cli');
|
$debug = (PHP_SAPI === 'cli');
|
||||||
|
|
||||||
if ($isCli) {
|
if ($debug) {
|
||||||
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'] ?? $data['url'] ?? '[no url]') . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------
|
// Marker vorhanden?
|
||||||
// 1. MEILISEARCH_JSON Kommentar finden
|
if (!str_contains($content, 'MEILISEARCH_JSON')) {
|
||||||
// --------------------------------------------------
|
if ($debug) {
|
||||||
if (
|
echo "❌ MEILISEARCH_JSON marker NOT found\n";
|
||||||
!preg_match(
|
|
||||||
'#<!--\s*MEILISEARCH_JSON\s*(.*?)\s*-->#s',
|
|
||||||
$content,
|
|
||||||
$m
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
if ($isCli) {
|
|
||||||
echo "❌ MEILISEARCH_JSON not found\n";
|
|
||||||
echo "INDEXPAGE HOOK END\n";
|
echo "INDEXPAGE HOOK END\n";
|
||||||
echo "=============================\n";
|
echo "=============================\n";
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$json = trim($m[1]);
|
// JSON aus Kommentar extrahieren + parsen
|
||||||
$meta = json_decode($json, true);
|
$parsed = $this->extractMeilisearchJson($content);
|
||||||
|
|
||||||
if (!is_array($meta)) {
|
if ($parsed === null) {
|
||||||
if ($isCli) {
|
if ($debug) {
|
||||||
echo "❌ Invalid JSON in MEILISEARCH_JSON\n";
|
echo "❌ Invalid JSON in MEILISEARCH_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";
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($isCli) {
|
if ($debug) {
|
||||||
echo "✅ MEILISEARCH_JSON parsed\n";
|
echo "✅ MEILISEARCH_JSON parsed\n";
|
||||||
var_dump($meta);
|
var_dump($parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------
|
/*
|
||||||
// 2. PRIORITY (event > news > page)
|
* =====================
|
||||||
// --------------------------------------------------
|
* PRIORITY (event > news > page)
|
||||||
foreach (['event', 'news', 'page'] as $scope) {
|
* =====================
|
||||||
if (!empty($meta[$scope]['priority'])) {
|
*/
|
||||||
$data['priority'] = (int) $meta[$scope]['priority'];
|
$priority =
|
||||||
break;
|
$parsed['event']['priority'] ?? null ??
|
||||||
|
$parsed['news']['priority'] ?? null ??
|
||||||
|
$parsed['page']['priority'] ?? null;
|
||||||
|
|
||||||
|
if ($priority !== null && $priority !== '') {
|
||||||
|
$set['priority'] = (int) $priority;
|
||||||
|
$data['priority'] = (int) $priority; // optional
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =====================
|
||||||
|
* KEYWORDS (merge)
|
||||||
|
* =====================
|
||||||
|
*/
|
||||||
|
$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)) ?: [];
|
||||||
// 3. KEYWORDS (zusammenführen)
|
foreach ($parts as $p) {
|
||||||
// --------------------------------------------------
|
$p = trim($p);
|
||||||
$keywords = [];
|
if ($p !== '') {
|
||||||
|
$kw[] = $p;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($keywords) {
|
|
||||||
$data['keywords'] = implode(' ', array_unique($keywords));
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------------------------------
|
|
||||||
// 4. IMAGEPATH
|
|
||||||
// Reihenfolge:
|
|
||||||
// event > news > custom > page
|
|
||||||
// --------------------------------------------------
|
|
||||||
foreach (
|
|
||||||
[
|
|
||||||
$meta['event']['searchimage'] ?? null,
|
|
||||||
$meta['news']['searchimage'] ?? null,
|
|
||||||
$meta['custom']['searchimage'] ?? null,
|
|
||||||
$meta['page']['searchimage'] ?? null,
|
|
||||||
] as $img
|
|
||||||
) {
|
|
||||||
if ($img) {
|
|
||||||
$data['imagepath'] = trim((string) $img);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------------------------------
|
|
||||||
// 5. STARTDATE (event > news)
|
|
||||||
// --------------------------------------------------
|
|
||||||
foreach (['event', 'news'] as $scope) {
|
|
||||||
if (!empty($meta[$scope]['date'])) {
|
|
||||||
$ts = strtotime($meta[$scope]['date']);
|
|
||||||
if ($ts !== false) {
|
|
||||||
$data['startDate'] = $ts;
|
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------
|
if ($kw) {
|
||||||
// 6. DEBUG FINAL
|
$kw = array_values(array_unique($kw));
|
||||||
// --------------------------------------------------
|
$set['keywords'] = implode(' ', $kw);
|
||||||
if ($isCli) {
|
$data['keywords'] = $set['keywords']; // optional
|
||||||
echo "---- FINAL \$data ----\n";
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =====================
|
||||||
|
* IMAGEPATH (event > news > page > custom)
|
||||||
|
* =====================
|
||||||
|
*/
|
||||||
|
$image =
|
||||||
|
$parsed['event']['searchimage'] ?? null ??
|
||||||
|
$parsed['news']['searchimage'] ?? null ??
|
||||||
|
$parsed['page']['searchimage'] ?? null ??
|
||||||
|
$parsed['custom']['searchimage'] ?? null;
|
||||||
|
|
||||||
|
if (is_string($image) && $image !== '') {
|
||||||
|
$set['imagepath'] = trim($image);
|
||||||
|
$data['imagepath'] = $set['imagepath']; // optional
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =====================
|
||||||
|
* STARTDATE (event.date/news.date => timestamp)
|
||||||
|
* =====================
|
||||||
|
*/
|
||||||
|
$date =
|
||||||
|
$parsed['event']['date'] ?? null ??
|
||||||
|
$parsed['news']['date'] ?? null;
|
||||||
|
|
||||||
|
if (is_string($date) && $date !== '') {
|
||||||
|
$ts = strtotime($date);
|
||||||
|
if ($ts !== false) {
|
||||||
|
$set['startDate'] = $ts;
|
||||||
|
$data['startDate'] = $ts; // optional
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($debug) {
|
||||||
|
echo "---- FINAL \$set (what should be persisted) ----\n";
|
||||||
var_dump([
|
var_dump([
|
||||||
'priority' => $data['priority'] ?? null,
|
'priority' => $set['priority'] ?? null,
|
||||||
'keywords' => $data['keywords'] ?? null,
|
'keywords' => $set['keywords'] ?? null,
|
||||||
'imagepath' => $data['imagepath'] ?? null,
|
'imagepath' => $set['imagepath'] ?? null,
|
||||||
'startDate' => $data['startDate'] ?? null,
|
'startDate' => $set['startDate'] ?? null,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
echo "---- RAW \$data ----\n";
|
|
||||||
var_dump($data);
|
|
||||||
|
|
||||||
echo "---- RAW \$set ----\n";
|
|
||||||
var_dump($set);
|
|
||||||
|
|
||||||
echo "INDEXPAGE HOOK END\n";
|
echo "INDEXPAGE HOOK END\n";
|
||||||
echo "=============================\n";
|
echo "=============================\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function extractMeilisearchJson(string $content): ?array
|
||||||
|
{
|
||||||
|
// Erwartetes Format:
|
||||||
|
// <!--
|
||||||
|
// MEILISEARCH_JSON
|
||||||
|
// { ...json... }
|
||||||
|
// -->
|
||||||
|
if (!preg_match('/<!--\s*MEILISEARCH_JSON\s*(\{.*?\})\s*-->/s', $content, $m)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$json = 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