add files uuid
This commit is contained in:
@@ -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,32 +89,17 @@ 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'] ?? '');
|
||||
@@ -128,44 +107,23 @@ 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) {
|
||||
$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) {
|
||||
$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]
|
||||
$fileHelper->collect(
|
||||
$file['url'],
|
||||
$file['type'],
|
||||
(int) ($data['pid'] ?? 0)
|
||||
);
|
||||
|
||||
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
|
||||
{
|
||||
$url = strtok($url, '#');
|
||||
|
||||
$parts = parse_url($url);
|
||||
if (!$parts) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!empty($parts['path'])) {
|
||||
$ext = strtolower(pathinfo($parts['path'], PATHINFO_EXTENSION));
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user