This commit is contained in:
Jürgen Mummert
2025-12-30 20:33:41 +01:00
parent 987441700f
commit 2a2c68cfb1
+103 -12
View File
@@ -13,17 +13,37 @@ class IndexPageListener
private readonly OfficeIndexService $officeIndexService,
) {}
private function debug(string $message, array $context = []): void
{
// Debug bewusst immer aktiv (bis du es wieder entfernst)
// Kontext kurz halten, damit Logs nicht explodieren
$ctx = $context ? ' | ' . json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : '';
error_log('[ContaoMeilisearch][IndexPageListener] ' . $message . $ctx);
}
public function onIndexPage(string $content, array &$data, array &$set): void
{
$this->debug('Hook start', [
'url' => $data['url'] ?? null,
'protected' => $data['protected'] ?? null,
'checksum' => $data['checksum'] ?? null,
'set_keys' => array_keys($set),
]);
/*
* =====================
* PDF: Reset genau 1× pro Crawl
* =====================
*/
try {
$this->debug('PDF resetTableOnce(): call');
$this->pdfIndexService->resetTableOnce();
$this->debug('PDF resetTableOnce(): ok');
} catch (\Throwable $e) {
error_log('[ContaoMeilisearch] PDF reset failed: ' . $e->getMessage());
$this->debug('PDF resetTableOnce(): failed', [
'error' => $e->getMessage(),
'class' => $e::class,
]);
}
/*
@@ -31,11 +51,24 @@ class IndexPageListener
* SEITEN-METADATEN
* =====================
*/
if (str_contains($content, 'MEILISEARCH_JSON')) {
$hasMeta = str_contains($content, 'MEILISEARCH_JSON');
$this->debug('Meta marker scan', [
'contains_MEILISEARCH_JSON' => $hasMeta,
'content_length' => strlen($content),
]);
if ($hasMeta) {
try {
$parsed = $this->extractMeilisearchJson($content);
$this->debug('extractMeilisearchJson(): done', [
'parsed_is_array' => is_array($parsed),
'parsed_keys' => is_array($parsed) ? array_keys($parsed) : null,
]);
} catch (\Throwable $e) {
error_log('[ContaoMeilisearch] Failed to extract MEILISEARCH_JSON: ' . $e->getMessage());
$this->debug('Failed to extract MEILISEARCH_JSON', [
'error' => $e->getMessage(),
'class' => $e::class,
]);
$parsed = null;
}
@@ -48,6 +81,8 @@ class IndexPageListener
?? $parsed['page']['priority']
?? null;
$this->debug('Meta: priority candidate', ['priority' => $priority]);
if ($priority !== null && $priority !== '') {
$set['priority'] = (int) $priority;
}
@@ -59,6 +94,8 @@ class IndexPageListener
$parsed['page']['keywords'] ?? null,
];
$this->debug('Meta: keyword sources', ['sources' => $keywordSources]);
$keywords = [];
foreach ($keywordSources as $src) {
if (!is_string($src) || trim($src) === '') {
@@ -73,9 +110,17 @@ class IndexPageListener
$set['keywords'] = implode(' ', array_unique($keywords));
}
// IMAGEPATH
if (!empty($parsed['page']['searchimage'])) {
$set['imagepath'] = trim((string) $parsed['page']['searchimage']);
$this->debug('Meta: keywords result', [
'keywords' => $set['keywords'] ?? null,
]);
// IMAGEPATH (UUID)
$searchImage = $parsed['page']['searchimage'] ?? null;
$this->debug('Meta: searchimage candidate', ['searchimage' => $searchImage]);
if (!empty($searchImage)) {
// >>> HINWEIS: falls dein tl_search-Feld "image" heißt, hier auf $set['image'] ändern!
$set['imagepath'] = trim((string) $searchImage);
}
// STARTDATE
@@ -84,6 +129,8 @@ class IndexPageListener
?? $parsed['news']['startDate']
?? null;
$this->debug('Meta: startDate candidate', ['startDate' => $startDate]);
if (is_numeric($startDate) && (int) $startDate > 0) {
$set['startDate'] = (int) $startDate;
}
@@ -97,9 +144,25 @@ class IndexPageListener
$checksumSeed .= '|' . ($set['startDate'] ?? '');
$set['checksum'] = md5($checksumSeed);
$this->debug('Checksum generated', [
'seed_preview' => substr($checksumSeed, 0, 120) . (strlen($checksumSeed) > 120 ? '…' : ''),
'checksum' => $set['checksum'],
]);
} catch (\Throwable $e) {
error_log('[ContaoMeilisearch] Failed to generate checksum: ' . $e->getMessage());
$this->debug('Failed to generate checksum', [
'error' => $e->getMessage(),
'class' => $e::class,
]);
}
$this->debug('Meta: final set snapshot', [
'priority' => $set['priority'] ?? null,
'keywords' => $set['keywords'] ?? null,
'imagepath' => $set['imagepath'] ?? null,
'startDate' => $set['startDate'] ?? null,
'checksum' => $set['checksum'] ?? null,
]);
}
}
@@ -109,17 +172,25 @@ class IndexPageListener
* =====================
*/
if ((int) ($data['protected'] ?? 0) !== 0) {
$this->debug('Abort: protected page', ['protected' => $data['protected'] ?? null]);
return;
}
$indexPdfs = (bool) Config::get('meilisearch_index_pdfs');
$indexOffice = (bool) Config::get('meilisearch_index_office');
$this->debug('File indexing settings', [
'meilisearch_index_pdfs' => $indexPdfs,
'meilisearch_index_office' => $indexOffice,
]);
if (!$indexPdfs && !$indexOffice) {
$this->debug('Abort: file indexing disabled');
return;
}
$links = $this->findAllLinks($content);
$this->debug('Links found', ['count' => count($links)]);
$pdfLinks = [];
$officeLinks = [];
@@ -132,25 +203,45 @@ class IndexPageListener
continue;
}
if (
in_array($type, ['docx', 'xlsx', 'pptx'], true)
&& $indexOffice
) {
if (in_array($type, ['docx', 'xlsx', 'pptx'], true) && $indexOffice) {
$officeLinks[] = $link;
}
}
$this->debug('Indexable file links', [
'pdf' => count($pdfLinks),
'office' => count($officeLinks),
]);
try {
if ($pdfLinks !== []) {
$this->debug('PDF handlePdfLinks(): call', ['count' => count($pdfLinks)]);
$this->pdfIndexService->handlePdfLinks($pdfLinks);
$this->debug('PDF handlePdfLinks(): ok');
}
if ($officeLinks !== []) {
$this->debug('Office handleOfficeLinks(): call', ['count' => count($officeLinks)]);
$this->officeIndexService->handleOfficeLinks($officeLinks);
$this->debug('Office handleOfficeLinks(): ok');
}
} catch (\Throwable $e) {
error_log('[ContaoMeilisearch] File indexing failed: ' . $e->getMessage());
$this->debug('File indexing failed', [
'error' => $e->getMessage(),
'class' => $e::class,
]);
}
$this->debug('Hook end', [
'final_set_keys' => array_keys($set),
'final_set' => [
'priority' => $set['priority'] ?? null,
'keywords' => $set['keywords'] ?? null,
'imagepath' => $set['imagepath'] ?? null,
'startDate' => $set['startDate'] ?? null,
'checksum' => $set['checksum'] ?? null,
],
]);
}
/**