This commit is contained in:
Jürgen Mummert
2025-12-23 11:52:30 +01:00
parent 8a90d2622d
commit ca6878c50a
@@ -16,11 +16,11 @@ class MeilisearchPageMarkerListener
return $buffer; return $buffer;
} }
$lines = ['MEILISEARCH']; $data = [];
/* /*
* ===================== * =====================
* PAGE (Basis) * PAGE
* ===================== * =====================
*/ */
$pageImageUuid = null; $pageImageUuid = null;
@@ -28,12 +28,14 @@ class MeilisearchPageMarkerListener
if (isset($GLOBALS['objPage']) && $GLOBALS['objPage'] instanceof PageModel) { if (isset($GLOBALS['objPage']) && $GLOBALS['objPage'] instanceof PageModel) {
$page = $GLOBALS['objPage']; $page = $GLOBALS['objPage'];
$data['page'] = [];
if (!empty($page->priority)) { if (!empty($page->priority)) {
$lines[] = 'page.priority=' . (int) $page->priority; $data['page']['priority'] = (int) $page->priority;
} }
if (!empty($page->keywords)) { if (!empty($page->keywords)) {
$lines[] = 'page.keywords=' . trim((string) $page->keywords); $data['page']['keywords'] = trim((string) $page->keywords);
} }
if (!empty($page->searchimage)) { if (!empty($page->searchimage)) {
@@ -43,7 +45,7 @@ class MeilisearchPageMarkerListener
/* /*
* ===================== * =====================
* SCHEMA.ORG BLÖCKE * SCHEMA.ORG JSON-LD
* ===================== * =====================
*/ */
preg_match_all( preg_match_all(
@@ -55,102 +57,92 @@ class MeilisearchPageMarkerListener
foreach ($jsonBlocks[1] as $json) { foreach ($jsonBlocks[1] as $json) {
/* /*
* =====================
* EVENT * EVENT
* =====================
*/ */
if (preg_match('#"@type"\s*:\s*"Event"#', $json)) { if (preg_match('#"@type"\s*:\s*"Event"#', $json)) {
$data['event'] ??= [];
if (preg_match('#\\\/schema\\\/events\\\/(\d+)#', $json, $m)) { if (preg_match('#\\\/schema\\\/events\\\/(\d+)#', $json, $m)) {
$event = CalendarEventsModel::findByPk((int) $m[1]); $event = CalendarEventsModel::findByPk((int) $m[1]);
if ($event !== null) { if ($event !== null) {
if (!empty($event->priority)) { if (!empty($event->priority)) {
$lines[] = 'event.priority=' . (int) $event->priority; $data['event']['priority'] = (int) $event->priority;
} }
if (!empty($event->keywords)) { if (!empty($event->keywords)) {
$lines[] = 'event.keywords=' . trim((string) $event->keywords); $data['event']['keywords'] = trim((string) $event->keywords);
} }
if ($event->addImage && !empty($event->singleSRC)) { if ($event->addImage && !empty($event->singleSRC)) {
$lines[] = 'event.searchimage=' . StringUtil::binToUuid($event->singleSRC); $data['event']['searchimage'] = StringUtil::binToUuid($event->singleSRC);
} }
} }
} }
if (preg_match('#"startDate"\s*:\s*"([^"]+)"#', $json, $dm)) { if (preg_match('#"startDate"\s*:\s*"([^"]+)"#', $json, $dm)) {
$lines[] = 'event.date=' . $dm[1]; $data['event']['date'] = $dm[1];
} }
} }
/* /*
* =====================
* NEWS * NEWS
* =====================
*/ */
if (preg_match('#"@type"\s*:\s*"NewsArticle"#', $json)) { if (preg_match('#"@type"\s*:\s*"NewsArticle"#', $json)) {
$data['news'] ??= [];
if (preg_match('#\\\/schema\\\/news\\\/(\d+)#', $json, $m)) { if (preg_match('#\\\/schema\\\/news\\\/(\d+)#', $json, $m)) {
$news = NewsModel::findByPk((int) $m[1]); $news = NewsModel::findByPk((int) $m[1]);
if ($news !== null) { if ($news !== null) {
if (!empty($news->priority)) { if (!empty($news->priority)) {
$lines[] = 'news.priority=' . (int) $news->priority; $data['news']['priority'] = (int) $news->priority;
} }
if (!empty($news->keywords)) { if (!empty($news->keywords)) {
$lines[] = 'news.keywords=' . trim((string) $news->keywords); $data['news']['keywords'] = trim((string) $news->keywords);
} }
if ($news->addImage && !empty($news->singleSRC)) { if ($news->addImage && !empty($news->singleSRC)) {
$lines[] = 'news.searchimage=' . StringUtil::binToUuid($news->singleSRC); $data['news']['searchimage'] = StringUtil::binToUuid($news->singleSRC);
} }
} }
} }
if (preg_match('#"datePublished"\s*:\s*"([^"]+)"#', $json, $dm)) { if (preg_match('#"datePublished"\s*:\s*"([^"]+)"#', $json, $dm)) {
$lines[] = 'news.date=' . $dm[1]; $data['news']['date'] = $dm[1];
} }
} }
} }
/* /*
* ===================== * CUSTOM SEARCHIMAGE (Markup)
* CUSTOM SEARCHIMAGE (UUID direkt aus Markup)
* =====================
*/ */
if ( if (
preg_match( preg_match('#data-searchimage-uuid="([a-f0-9\-]{36})"#i', $buffer, $m)
'#data-searchimage-uuid="([a-f0-9\-]{36})"#i',
$buffer,
$m
)
) { ) {
$lines[] = 'custom.searchimage=' . $m[1]; $data['custom']['searchimage'] = $m[1];
} }
/* /*
* =====================
* PAGE IMAGE FALLBACK * PAGE IMAGE FALLBACK
* =====================
*/ */
if ($pageImageUuid) { if ($pageImageUuid) {
$lines[] = 'page.searchimage=' . $pageImageUuid; $data['page']['searchimage'] = $pageImageUuid;
} else { } else {
$fallback = Config::get('meilisearch_fallback_image'); $fallback = Config::get('meilisearch_fallback_image');
if ($fallback) { if ($fallback) {
$lines[] = 'page.searchimage=' . StringUtil::binToUuid($fallback); $data['page']['searchimage'] = StringUtil::binToUuid($fallback);
} }
} }
if (count($lines) === 1) { if ($data === []) {
return $buffer; return $buffer;
} }
$marker = $marker =
"\n<!--\n" . "\n<!--\nMEILISEARCH_JSON\n" .
implode("\n", array_unique($lines)) . json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) .
"\n-->\n"; "\n-->\n";
return str_contains($buffer, '</body>') return str_contains($buffer, '</body>')