add files uuid

This commit is contained in:
Jürgen Mummert
2026-01-12 10:19:48 +01:00
parent 2257178cb6
commit ad532e7b4c
2 changed files with 43 additions and 158 deletions
+21 -158
View File
@@ -4,9 +4,7 @@ namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\Config; use Contao\Config;
use Contao\System; use Contao\System;
use Contao\FilesModel; use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchFileHelper;
use Contao\StringUtil;
use Contao\CoreBundle\Filesystem\VirtualFilesystemInterface;
class IndexPageListener class IndexPageListener
{ {
@@ -16,8 +14,6 @@ class IndexPageListener
private function debug(string $message, array $context = []): void 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) : ''; $ctx = $context ? ' | ' . json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : '';
error_log('[ContaoMeilisearch][IndexPageListener] ' . $message . $ctx); error_log('[ContaoMeilisearch][IndexPageListener] ' . $message . $ctx);
} }
@@ -79,8 +75,6 @@ class IndexPageListener
$parsed['page']['keywords'] ?? null, $parsed['page']['keywords'] ?? null,
]; ];
$this->debug('Meta: keyword sources', ['sources' => $keywordSources]);
$keywords = []; $keywords = [];
foreach ($keywordSources as $src) { foreach ($keywordSources as $src) {
if (!is_string($src) || trim($src) === '') { if (!is_string($src) || trim($src) === '') {
@@ -95,77 +89,41 @@ class IndexPageListener
$set['keywords'] = implode(' ', array_unique($keywords)); $set['keywords'] = implode(' ', array_unique($keywords));
} }
$this->debug('Meta: keywords result', [ // IMAGEPATH
'keywords' => $set['keywords'] ?? null, if (!empty($parsed['page']['searchimage'] ?? null)) {
]); $set['imagepath'] = trim((string) $parsed['page']['searchimage']);
// IMAGEPATH (UUID)
$searchImage = $parsed['page']['searchimage'] ?? null;
$this->debug('Meta: searchimage candidate', ['searchimage' => $searchImage]);
if (!empty($searchImage)) {
$set['imagepath'] = trim((string) $searchImage);
} }
// STARTDATE // STARTDATE
$startDate = if (is_numeric($parsed['event']['startDate'] ?? null)) {
$parsed['event']['startDate'] $set['startDate'] = (int) $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;
} }
// CHECKSUM // CHECKSUM
try { $checksumSeed = (string) ($data['checksum'] ?? '');
$checksumSeed = (string) ($data['checksum'] ?? ''); $checksumSeed .= '|' . ($set['keywords'] ?? '');
$checksumSeed .= '|' . ($set['keywords'] ?? ''); $checksumSeed .= '|' . ($set['priority'] ?? '');
$checksumSeed .= '|' . ($set['priority'] ?? ''); $checksumSeed .= '|' . ($set['imagepath'] ?? '');
$checksumSeed .= '|' . ($set['imagepath'] ?? ''); $checksumSeed .= '|' . ($set['startDate'] ?? '');
$checksumSeed .= '|' . ($set['startDate'] ?? '');
$set['checksum'] = md5($checksumSeed); $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,
]);
}
} }
} }
/* /*
* ===================== * =====================
* DATEI-ERKENNUNG + UPSERT * DATEI-ERKENNUNG (NUR ERKENNUNG!)
* ===================== * =====================
*/ */
if ((int) ($data['protected'] ?? 0) !== 0) { if ((int) ($data['protected'] ?? 0) !== 0) {
$this->debug('Abort: protected page', ['protected' => $data['protected'] ?? null]);
return; return;
} }
$indexFiles = (bool) Config::get('meilisearch_index_files'); if (!Config::get('meilisearch_index_files')) {
$this->debug('File indexing setting', [
'meilisearch_index_files' => $indexFiles,
]);
if (!$indexFiles) {
$this->debug('Abort: file indexing disabled');
return; return;
} }
$links = $this->findAllLinks($content); $links = $this->findAllLinks($content);
$this->debug('Links found', ['count' => count($links)]);
$fileLinks = []; $fileLinks = [];
foreach ($links as $link) { foreach ($links as $link) {
@@ -177,109 +135,18 @@ class IndexPageListener
$this->debug('Indexable file links found', [ $this->debug('Indexable file links found', [
'count' => count($fileLinks), 'count' => count($fileLinks),
'types' => array_count_values(array_column($fileLinks, 'type')),
]); ]);
if ($fileLinks) { if ($fileLinks) {
$db = System::getContainer()->get('database_connection'); /** @var MeilisearchFileHelper $fileHelper */
$time = time(); $fileHelper = System::getContainer()->get(MeilisearchFileHelper::class);
// ✅ Contao 5.x robust: Projektverzeichnis statt TL_ROOT
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
foreach ($fileLinks as $file) { foreach ($fileLinks as $file) {
try { $fileHelper->collect(
$url = strtok($file['url'], '#'); $file['url'],
$file['type'],
$path = parse_url($url, PHP_URL_PATH); (int) ($data['pid'] ?? 0)
$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(),
]);
}
} }
} }
@@ -329,11 +196,7 @@ class IndexPageListener
private function detectIndexableFileType(string $url): ?string private function detectIndexableFileType(string $url): ?string
{ {
$url = strtok($url, '#'); $url = strtok($url, '#');
$parts = parse_url($url); $parts = parse_url($url);
if (!$parts) {
return null;
}
if (!empty($parts['path'])) { if (!empty($parts['path'])) {
$ext = strtolower(pathinfo($parts['path'], PATHINFO_EXTENSION)); $ext = strtolower(pathinfo($parts['path'], PATHINFO_EXTENSION));
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace MummertMedia\ContaoMeilisearchBundle\Service;
class MeilisearchFileHelper
{
public function __construct()
{
}
/**
* Minimal-Methode zum Testen des Aufrufs aus dem IndexPageListener
*/
public function collect(string $url, string $type, int $pageId): void
{
error_log('[ContaoMeilisearch][MeilisearchFileHelper] collect() called | ' . json_encode([
'url' => $url,
'type' => $type,
'pageId' => $pageId,
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}
}