From ad532e7b4cbc47adfed30c57d97fbd9316d24deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Mummert?= Date: Mon, 12 Jan 2026 10:19:48 +0100 Subject: [PATCH] add files uuid --- src/EventListener/IndexPageListener.php | 179 +++--------------------- src/Service/MeilisearchFileHelper.php | 22 +++ 2 files changed, 43 insertions(+), 158 deletions(-) create mode 100644 src/Service/MeilisearchFileHelper.php diff --git a/src/EventListener/IndexPageListener.php b/src/EventListener/IndexPageListener.php index 9bb6c40..9fb2eb4 100644 --- a/src/EventListener/IndexPageListener.php +++ b/src/EventListener/IndexPageListener.php @@ -4,9 +4,7 @@ namespace MummertMedia\ContaoMeilisearchBundle\EventListener; use Contao\Config; use Contao\System; -use Contao\FilesModel; -use Contao\StringUtil; -use Contao\CoreBundle\Filesystem\VirtualFilesystemInterface; +use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchFileHelper; class IndexPageListener { @@ -16,8 +14,6 @@ class IndexPageListener 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); } @@ -79,8 +75,6 @@ class IndexPageListener $parsed['page']['keywords'] ?? null, ]; - $this->debug('Meta: keyword sources', ['sources' => $keywordSources]); - $keywords = []; foreach ($keywordSources as $src) { if (!is_string($src) || trim($src) === '') { @@ -95,77 +89,41 @@ class IndexPageListener $set['keywords'] = implode(' ', array_unique($keywords)); } - $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)) { - $set['imagepath'] = trim((string) $searchImage); + // IMAGEPATH + if (!empty($parsed['page']['searchimage'] ?? null)) { + $set['imagepath'] = trim((string) $parsed['page']['searchimage']); } // STARTDATE - $startDate = - $parsed['event']['startDate'] - ?? $parsed['news']['startDate'] - ?? null; - - $this->debug('Meta: startDate candidate', ['startDate' => $startDate]); - - if (is_numeric($startDate) && (int) $startDate > 0) { - $set['startDate'] = (int) $startDate; + if (is_numeric($parsed['event']['startDate'] ?? null)) { + $set['startDate'] = (int) $parsed['event']['startDate']; } // CHECKSUM - try { - $checksumSeed = (string) ($data['checksum'] ?? ''); - $checksumSeed .= '|' . ($set['keywords'] ?? ''); - $checksumSeed .= '|' . ($set['priority'] ?? ''); - $checksumSeed .= '|' . ($set['imagepath'] ?? ''); - $checksumSeed .= '|' . ($set['startDate'] ?? ''); + $checksumSeed = (string) ($data['checksum'] ?? ''); + $checksumSeed .= '|' . ($set['keywords'] ?? ''); + $checksumSeed .= '|' . ($set['priority'] ?? ''); + $checksumSeed .= '|' . ($set['imagepath'] ?? ''); + $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) { - $this->debug('Failed to generate checksum', [ - 'error' => $e->getMessage(), - 'class' => $e::class, - ]); - } + $set['checksum'] = md5($checksumSeed); } } /* * ===================== - * DATEI-ERKENNUNG + UPSERT + * DATEI-ERKENNUNG (NUR ERKENNUNG!) * ===================== */ if ((int) ($data['protected'] ?? 0) !== 0) { - $this->debug('Abort: protected page', ['protected' => $data['protected'] ?? null]); return; } - $indexFiles = (bool) Config::get('meilisearch_index_files'); - - $this->debug('File indexing setting', [ - 'meilisearch_index_files' => $indexFiles, - ]); - - if (!$indexFiles) { - $this->debug('Abort: file indexing disabled'); + if (!Config::get('meilisearch_index_files')) { return; } $links = $this->findAllLinks($content); - $this->debug('Links found', ['count' => count($links)]); - $fileLinks = []; foreach ($links as $link) { @@ -177,109 +135,18 @@ class IndexPageListener $this->debug('Indexable file links found', [ 'count' => count($fileLinks), - 'types' => array_count_values(array_column($fileLinks, 'type')), ]); if ($fileLinks) { - $db = System::getContainer()->get('database_connection'); - $time = time(); - - // ✅ Contao 5.x robust: Projektverzeichnis statt TL_ROOT - $projectDir = System::getContainer()->getParameter('kernel.project_dir'); + /** @var MeilisearchFileHelper $fileHelper */ + $fileHelper = System::getContainer()->get(MeilisearchFileHelper::class); foreach ($fileLinks as $file) { - try { - $url = strtok($file['url'], '#'); - - $path = parse_url($url, PHP_URL_PATH); - $path = $path ? ltrim($path, '/') : null; - - // --------------------------------------------- - // UUID aus Pfad ermitteln (Contao 4.13 + 5.x) - // --------------------------------------------- - $uuid = null; - $uuidBin = null; - - if ($path && str_starts_with($path, 'files/')) { - if (interface_exists(VirtualFilesystemInterface::class)) { - try { - $vfs = System::getContainer()->get(VirtualFilesystemInterface::class); - $uuid = $vfs->pathToUuid($path); - } catch (\Throwable) { - $uuid = null; - } - } - - if (!$uuid) { - $fileModel = FilesModel::findByPath($path); - if ($fileModel) { - $uuid = $fileModel->uuid; - } - } - - if ($uuid) { - $uuidBin = StringUtil::uuidToBin($uuid); - } - } - - $abs = $path ? $projectDir . '/public/' . $path : null; - - $mtime = ($abs && is_file($abs)) ? filemtime($abs) : 0; - $checksum = md5($url . '|' . $mtime); - - $existing = $db->fetchAssociative( - 'SELECT id, checksum FROM tl_search_files WHERE url = ?', - [$url] - ); - - if ($existing) { - $db->update( - 'tl_search_files', - [ - 'tstamp' => $time, - 'last_seen' => $time, - 'page_id' => (int) ($data['pid'] ?? 0), - 'file_mtime' => $mtime, - 'checksum' => $checksum, - 'uuid' => $uuidBin, // ⬅️ NEU - ], - ['id' => $existing['id']] - ); - - $this->debug('File updated', [ - 'url' => $url, - 'uuid' => $uuid, - ]); - } else { - $db->insert( - 'tl_search_files', - [ - 'tstamp' => $time, - 'last_seen' => $time, - 'type' => $file['type'], - 'url' => $url, - 'title' => $file['linkText'] ?? basename($url), - 'page_id' => (int) ($data['pid'] ?? 0), - 'file_mtime' => $mtime, - 'checksum' => $checksum, - 'uuid' => $uuidBin, // ⬅️ NEU - ] - ); - - $this->debug('File inserted', [ - 'url' => $url, - 'uuid' => $uuid, - ]); - } - } catch (\Throwable $e) { - $this->debug('File upsert FAILED', [ - 'url' => $file['url'] ?? null, - 'type' => $file['type'] ?? null, - 'error' => $e->getMessage(), - 'class' => $e::class, - 'code' => $e->getCode(), - ]); - } + $fileHelper->collect( + $file['url'], + $file['type'], + (int) ($data['pid'] ?? 0) + ); } } @@ -329,11 +196,7 @@ class IndexPageListener private function detectIndexableFileType(string $url): ?string { $url = strtok($url, '#'); - $parts = parse_url($url); - if (!$parts) { - return null; - } if (!empty($parts['path'])) { $ext = strtolower(pathinfo($parts['path'], PATHINFO_EXTENSION)); diff --git a/src/Service/MeilisearchFileHelper.php b/src/Service/MeilisearchFileHelper.php new file mode 100644 index 0000000..9c9467a --- /dev/null +++ b/src/Service/MeilisearchFileHelper.php @@ -0,0 +1,22 @@ + $url, + 'type' => $type, + 'pageId' => $pageId, + ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); + } +} \ No newline at end of file