This commit is contained in:
Jürgen Mummert
2025-12-23 11:09:17 +01:00
parent 1fd612e530
commit faaa6fdcac
+26 -22
View File
@@ -6,23 +6,31 @@ class IndexPageListener
{ {
public function onIndexPage(string $content, array &$data, array &$set): void public function onIndexPage(string $content, array &$data, array &$set): void
{ {
// Schneller Exit, wenn kein Marker existiert
if (!str_contains($content, 'MEILISEARCH')) { if (!str_contains($content, 'MEILISEARCH')) {
return; return;
} }
$markers = $this->extractMarkers($content); $markers = $this->extractMarkers($content);
if ($markers === []) {
return;
}
/* /*
* ===================== * =====================
* PRIORITY * PRIORITY
* event/news > page
* ===================== * =====================
*/ */
if (isset($markers['event.priority'])) { if (isset($markers['event.priority'])) {
$data['priority'] = (int) $markers['event.priority']; $data['priority'] = (int) $markers['event.priority'];
$set['priority'] = true;
} elseif (isset($markers['news.priority'])) { } elseif (isset($markers['news.priority'])) {
$data['priority'] = (int) $markers['news.priority']; $data['priority'] = (int) $markers['news.priority'];
$set['priority'] = true;
} elseif (isset($markers['page.priority'])) { } elseif (isset($markers['page.priority'])) {
$data['priority'] = (int) $markers['page.priority']; $data['priority'] = (int) $markers['page.priority'];
$set['priority'] = true;
} }
/* /*
@@ -34,31 +42,27 @@ class IndexPageListener
foreach (['event.keywords', 'news.keywords', 'page.keywords'] as $key) { foreach (['event.keywords', 'news.keywords', 'page.keywords'] as $key) {
if (!empty($markers[$key])) { if (!empty($markers[$key])) {
$parts = preg_split('/\s+/', trim($markers[$key])); $parts = preg_split('/\s+/', trim($markers[$key])) ?: [];
$keywords = array_merge($keywords, $parts); $keywords = array_merge($keywords, $parts);
} }
} }
if ($keywords !== []) { if ($keywords !== []) {
$keywords = array_unique($keywords); $keywords = array_values(array_unique(array_filter($keywords)));
$data['keywords'] = implode(' ', $keywords); $data['keywords'] = implode(' ', $keywords);
$set['keywords'] = true;
} }
/* /*
* ===================== * =====================
* SEARCH IMAGE (UUID) * SEARCH IMAGE (UUID)
* event/news > page > custom
* ===================== * =====================
*/ */
foreach ( foreach (['event.searchimage', 'news.searchimage', 'page.searchimage', 'custom.searchimage'] as $key) {
[
'event.searchimage',
'news.searchimage',
'page.searchimage',
'custom.searchimage',
] as $key
) {
if (!empty($markers[$key])) { if (!empty($markers[$key])) {
$data['imagepath'] = $markers[$key]; $data['imagepath'] = trim($markers[$key]); // vorerst nur UUID
$set['imagepath'] = true;
break; break;
} }
} }
@@ -70,27 +74,27 @@ class IndexPageListener
*/ */
foreach (['event.date', 'news.date'] as $key) { foreach (['event.date', 'news.date'] as $key) {
if (!empty($markers[$key])) { if (!empty($markers[$key])) {
$timestamp = strtotime($markers[$key]); $ts = strtotime(trim($markers[$key]));
if ($timestamp !== false) { if ($ts !== false) {
$data['startdate'] = $timestamp; $data['startDate'] = $ts;
$set['startDate'] = true;
} }
break; break;
} }
} }
} }
/**
* Extrahiert MEILISEARCH Marker aus HTML-Kommentar
*/
private function extractMarkers(string $content): array private function extractMarkers(string $content): array
{ {
if (!preg_match('/<!--\s*MEILISEARCH(.*?)-->/s', $content, $match)) { // Kommentarblock isolieren
if (!preg_match('/<!--\s*MEILISEARCH(.*?)-->/s', $content, $m)) {
return []; return [];
} }
$lines = preg_split('/\R/', trim($match[1])); $block = trim($m[1]);
$markers = []; $lines = preg_split('/\R/', $block) ?: [];
$markers = [];
foreach ($lines as $line) { foreach ($lines as $line) {
$line = trim($line); $line = trim($line);
@@ -98,8 +102,8 @@ class IndexPageListener
continue; continue;
} }
[$key, $value] = explode('=', $line, 2); [$k, $v] = explode('=', $line, 2);
$markers[trim($key)] = trim($value); $markers[trim($k)] = trim($v);
} }
return $markers; return $markers;