This commit is contained in:
Jürgen Mummert
2025-12-23 11:30:38 +01:00
parent 1c433f3737
commit c72f5faf32
+53 -47
View File
@@ -7,6 +7,11 @@ use Doctrine\DBAL\Connection;
class IndexPageListener class IndexPageListener
{ {
private static bool $shutdownRegistered = false;
/** @var array<string, array{keywords?:string, imagepath?:string, startDate?:int}> */
private static array $queue = [];
private Connection $db; private Connection $db;
public function __construct() public function __construct()
@@ -14,34 +19,37 @@ class IndexPageListener
$this->db = System::getContainer()->get('database_connection'); $this->db = System::getContainer()->get('database_connection');
} }
public function onIndexPage(string $content, array &$data, array &$set): void public function onIndexPage(string $content, array &$pageData, array &$indexData): void
{ {
if (!str_contains($content, 'MEILISEARCH')) { if (!str_contains($content, 'MEILISEARCH')) {
return; return;
} }
// Debug (ohne Crash)
if (PHP_SAPI === 'cli') {
echo "INDEXPAGE LISTENER ACTIVE: " . ($indexData['url'] ?? '[no url]') . "\n";
}
$markers = $this->extractMarkers($content); $markers = $this->extractMarkers($content);
if ($markers === []) { if ($markers === []) {
return; return;
} }
// URL aus $set (für Crawl vorhanden) // priority klappt bei dir schon -> lassen wir direkt im indexData
$url = $set['url'] ?? null; if (isset($markers['event.priority'])) {
$indexData['priority'] = (int) $markers['event.priority'];
} elseif (isset($markers['news.priority'])) {
$indexData['priority'] = (int) $markers['news.priority'];
} elseif (isset($markers['page.priority'])) {
$indexData['priority'] = (int) $markers['page.priority'];
}
$url = $indexData['url'] ?? null;
if (!$url) { if (!$url) {
return; return;
} }
// priority: event/news > page // keywords kombinieren
$priority = null;
if (isset($markers['event.priority'])) {
$priority = (int) $markers['event.priority'];
} elseif (isset($markers['news.priority'])) {
$priority = (int) $markers['news.priority'];
} elseif (isset($markers['page.priority'])) {
$priority = (int) $markers['page.priority'];
}
// keywords kombiniert
$keywords = []; $keywords = [];
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])) {
@@ -49,10 +57,10 @@ class IndexPageListener
} }
} }
$keywords = array_values(array_unique(array_filter($keywords))); $keywords = array_values(array_unique(array_filter($keywords)));
$keywordsString = $keywords ? implode(' ', $keywords) : null; $keywordsString = $keywords ? implode(' ', $keywords) : '';
// searchimage uuid: event/news > page > custom // searchimage uuid: event/news > page > custom
$imageUuid = null; $imageUuid = '';
foreach (['event.searchimage', 'news.searchimage', 'page.searchimage', 'custom.searchimage'] as $key) { foreach (['event.searchimage', 'news.searchimage', 'page.searchimage', 'custom.searchimage'] as $key) {
if (!empty($markers[$key])) { if (!empty($markers[$key])) {
$imageUuid = trim($markers[$key]); $imageUuid = trim($markers[$key]);
@@ -60,8 +68,8 @@ class IndexPageListener
} }
} }
// startDate (timestamp) // startDate
$startDate = null; $startDate = 0;
foreach (['event.date', 'news.date'] as $key) { foreach (['event.date', 'news.date'] as $key) {
if (!empty($markers[$key])) { if (!empty($markers[$key])) {
$ts = strtotime(trim($markers[$key])); $ts = strtotime(trim($markers[$key]));
@@ -72,40 +80,38 @@ class IndexPageListener
} }
} }
// Nichts zu schreiben? Dann raus. // In Queue legen (Core überschreibt später, wir schreiben am Ende final in tl_search)
if ($priority === null && $keywordsString === null && $imageUuid === null && $startDate === null) { self::$queue[$url] = [
return; 'keywords' => $keywordsString,
} 'imagepath' => $imageUuid,
'startDate' => $startDate,
];
// ✅ DB-Update (tl_search) anhand der URL // Shutdown einmalig registrieren
$update = []; if (!self::$shutdownRegistered) {
$params = ['url' => $url]; self::$shutdownRegistered = true;
$types = [];
if ($priority !== null) { register_shutdown_function(function (): void {
$update['priority'] = ':priority'; $db = System::getContainer()->get('database_connection');
$params['priority'] = $priority;
}
if ($keywordsString !== null) {
$update['keywords'] = ':keywords';
$params['keywords'] = $keywordsString;
}
if ($imageUuid !== null) {
$update['imagepath'] = ':imagepath';
$params['imagepath'] = $imageUuid;
}
if ($startDate !== null) {
$update['startDate'] = ':startDate';
$params['startDate'] = $startDate;
}
$setSql = implode(', ', array_map(fn($col, $ph) => "$col = $ph", array_keys($update), $update)); foreach (self::$queue as $url => $values) {
$db->executeStatement(
$this->db->executeStatement( 'UPDATE tl_search
"UPDATE tl_search SET $setSql WHERE url = :url", SET keywords = :keywords,
$params imagepath = :imagepath,
startDate = :startDate
WHERE url = :url',
[
'keywords' => $values['keywords'] ?? '',
'imagepath' => $values['imagepath'] ?? '',
'startDate' => $values['startDate'] ?? 0,
'url' => $url,
]
); );
} }
});
}
}
private function extractMarkers(string $content): array private function extractMarkers(string $content): array
{ {