Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e757bbb6a | |||
| 9f86a5240d | |||
| 59b261c333 | |||
| 0912738528 | |||
| 8b2c6e6b92 | |||
| ca1305c9c6 | |||
| e6e3e9339a | |||
| b7a5e95c7d | |||
| bc35527f3f | |||
| e04dfb2bd4 | |||
| 6d8d0938f1 | |||
| 8f9c9cea72 | |||
| ad532e7b4c | |||
| 2257178cb6 | |||
| 2f8eddda36 | |||
| 5026f615f2 | |||
| f402e6546a | |||
| 579f58b614 | |||
| 17188537bc | |||
| 3427f6b60b | |||
| 6e41df002e | |||
| 838f574574 | |||
| 8549e4e9da | |||
| 29f7920cb5 | |||
| 0c637c2f92 | |||
| 86b81affdc | |||
| 2d3ddac945 | |||
| 17da2a8434 | |||
| c085911877 | |||
| 40792870bd | |||
| 38372539c2 | |||
| 2bd52f77e0 | |||
| 99ef883da5 |
@@ -4,6 +4,7 @@ namespace MummertMedia\ContaoMeilisearchBundle\Command;
|
||||
|
||||
use Contao\CoreBundle\Framework\ContaoFramework;
|
||||
use Contao\Database;
|
||||
use Contao\System;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
@@ -22,18 +23,18 @@ class MeilisearchFilesParseCommand extends Command
|
||||
{
|
||||
$this
|
||||
->setName('meilisearch:files:parse')
|
||||
->setDescription('Parse indexed files via Apache Tika and store extracted text')
|
||||
->setDescription('Parse indexed files via Apache Tika and enrich tl_search_files')
|
||||
->addOption(
|
||||
'limit',
|
||||
null,
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'Maximum number of files to check per run (optional)'
|
||||
'Maximum number of files to check per run'
|
||||
)
|
||||
->addOption(
|
||||
'dry-run',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'Do not send files to Tika, just show what would be parsed'
|
||||
'Do not send files to Tika'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -44,11 +45,9 @@ class MeilisearchFilesParseCommand extends Command
|
||||
|
||||
$dryRun = (bool) $input->getOption('dry-run');
|
||||
|
||||
// ---- LIMIT: nur wenn explizit gesetzt
|
||||
$limitOption = $input->getOption('limit');
|
||||
$limit = $limitOption !== null ? max(1, (int) $limitOption) : null;
|
||||
|
||||
// ---- Tika URL
|
||||
$tikaUrl = rtrim((string) ($GLOBALS['TL_CONFIG']['meilisearch_tika_url'] ?? ''), '/');
|
||||
if ($tikaUrl === '') {
|
||||
$output->writeln('<error>Tika URL not configured</error>');
|
||||
@@ -57,7 +56,6 @@ class MeilisearchFilesParseCommand extends Command
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// ---- Files laden
|
||||
$sql = "SELECT * FROM tl_search_files ORDER BY tstamp ASC";
|
||||
if ($limit !== null) {
|
||||
$sql .= " LIMIT " . (int) $limit;
|
||||
@@ -76,11 +74,12 @@ class MeilisearchFilesParseCommand extends Command
|
||||
|
||||
foreach ($files as $file) {
|
||||
|
||||
$originalUrl = (string) $file['url'];
|
||||
$normalized = $originalUrl;
|
||||
$originalUrl = (string) $file['url'];
|
||||
$existingTitle = trim((string) ($file['title'] ?? ''));
|
||||
$normalized = $originalUrl;
|
||||
|
||||
// -------------------------------------------------
|
||||
// 1) ?file=files/…
|
||||
// Normalize URL
|
||||
// -------------------------------------------------
|
||||
if (str_contains($normalized, '?')) {
|
||||
$parts = parse_url($normalized);
|
||||
@@ -95,26 +94,20 @@ class MeilisearchFilesParseCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// 2) Fragment entfernen
|
||||
// -------------------------------------------------
|
||||
$normalized = strtok($normalized, '#');
|
||||
|
||||
// -------------------------------------------------
|
||||
// 3) URL-Decoding
|
||||
// -------------------------------------------------
|
||||
$normalized = rawurldecode($normalized);
|
||||
|
||||
// -------------------------------------------------
|
||||
// 4) Nur lokale files/
|
||||
// -------------------------------------------------
|
||||
$normalized = ltrim($normalized, '/');
|
||||
|
||||
if (!str_starts_with($normalized, 'files/')) {
|
||||
$this->log('Not in files/, skip', ['url' => $originalUrl]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$absolutePath = TL_ROOT . '/' . $normalized;
|
||||
$root = defined('TL_ROOT')
|
||||
? TL_ROOT
|
||||
: System::getContainer()->getParameter('kernel.project_dir') . '/public';
|
||||
|
||||
$absolutePath = $root . '/' . $normalized;
|
||||
|
||||
if (!is_file($absolutePath)) {
|
||||
$this->log('File missing, skip', [
|
||||
@@ -128,7 +121,7 @@ class MeilisearchFilesParseCommand extends Command
|
||||
$checksum = md5($normalized . '|' . $mtime);
|
||||
|
||||
// -------------------------------------------------
|
||||
// 5) Skip unchanged
|
||||
// Skip unchanged
|
||||
// -------------------------------------------------
|
||||
if ($file['checksum'] === $checksum && !empty($file['text'])) {
|
||||
continue;
|
||||
@@ -140,7 +133,7 @@ class MeilisearchFilesParseCommand extends Command
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// 6) MIME-Type
|
||||
// MIME-Type
|
||||
// -------------------------------------------------
|
||||
$ext = strtolower(pathinfo($normalized, PATHINFO_EXTENSION));
|
||||
|
||||
@@ -158,12 +151,12 @@ class MeilisearchFilesParseCommand extends Command
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// 7) Tika parse
|
||||
// Tika BODY (roher Plaintext)
|
||||
// -------------------------------------------------
|
||||
try {
|
||||
$this->log('Parsing file', ['url' => $normalized]);
|
||||
|
||||
$response = $client->request(
|
||||
$bodyResponse = $client->request(
|
||||
'PUT',
|
||||
$tikaUrl . '/tika/main',
|
||||
[
|
||||
@@ -175,31 +168,98 @@ class MeilisearchFilesParseCommand extends Command
|
||||
]
|
||||
);
|
||||
|
||||
$text = trim((string) $response->getContent(false));
|
||||
|
||||
$db->prepare(
|
||||
"UPDATE tl_search_files
|
||||
SET text = ?, checksum = ?, file_mtime = ?, tstamp = ?
|
||||
WHERE id = ?"
|
||||
)->execute(
|
||||
$text,
|
||||
$checksum,
|
||||
$mtime,
|
||||
time(),
|
||||
$file['id']
|
||||
);
|
||||
|
||||
$this->log('File parsed', [
|
||||
'url' => $normalized,
|
||||
'chars' => mb_strlen($text),
|
||||
]);
|
||||
$text = trim((string) $bodyResponse->getContent(false));
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
$this->log('Parse failed', [
|
||||
$this->log('Body parse failed', [
|
||||
'url' => $normalized,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// TITLE: keep existing editor-defined title
|
||||
// -------------------------------------------------
|
||||
$title = $existingTitle !== '' ? $existingTitle : null;
|
||||
|
||||
// -------------------------------------------------
|
||||
// Tika METADATA (Title) – only if no existing title
|
||||
// -------------------------------------------------
|
||||
if ($title === null) {
|
||||
try {
|
||||
$metaResponse = $client->request(
|
||||
'PUT',
|
||||
$tikaUrl . '/meta',
|
||||
[
|
||||
'headers' => [
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => $mimeType,
|
||||
],
|
||||
'body' => fopen($absolutePath, 'rb'),
|
||||
]
|
||||
);
|
||||
|
||||
$meta = json_decode($metaResponse->getContent(false), true);
|
||||
|
||||
$rawTitle =
|
||||
$meta['dc:title'][0]
|
||||
?? $meta['pdf:docinfo:title'][0]
|
||||
?? null;
|
||||
|
||||
if ($rawTitle) {
|
||||
$title = html_entity_decode(
|
||||
$rawTitle,
|
||||
ENT_QUOTES | ENT_HTML5,
|
||||
'UTF-8'
|
||||
);
|
||||
}
|
||||
|
||||
} catch (\Throwable) {
|
||||
// Metadata optional
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// TITLE → ASCII SAFE (only if newly generated)
|
||||
// -------------------------------------------------
|
||||
if ($existingTitle === '' && $title) {
|
||||
$title = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $title);
|
||||
$title = preg_replace('/\s+/', ' ', $title);
|
||||
$title = trim($title);
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// FALLBACK: Dateiname (only if still empty)
|
||||
// -------------------------------------------------
|
||||
if (!$title || strlen($title) < 5) {
|
||||
$title = pathinfo($normalized, PATHINFO_FILENAME);
|
||||
$title = str_replace(['_', '-'], ' ', $title);
|
||||
$title = preg_replace('/\s+/', ' ', $title);
|
||||
$title = trim($title);
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// Store result
|
||||
// -------------------------------------------------
|
||||
$db->prepare(
|
||||
"UPDATE tl_search_files
|
||||
SET text = ?, title = ?, checksum = ?, file_mtime = ?, tstamp = ?
|
||||
WHERE id = ?"
|
||||
)->execute(
|
||||
$text,
|
||||
$title,
|
||||
$checksum,
|
||||
$mtime,
|
||||
time(),
|
||||
$file['id']
|
||||
);
|
||||
|
||||
$this->log('File parsed', [
|
||||
'url' => $normalized,
|
||||
'chars' => mb_strlen($text),
|
||||
'title' => $title,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->log('Parser finished');
|
||||
|
||||
@@ -4,17 +4,17 @@ namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\Config;
|
||||
use Contao\System;
|
||||
use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchFileHelper;
|
||||
|
||||
class IndexPageListener
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
public function __construct(
|
||||
private readonly MeilisearchFileHelper $fileHelper,
|
||||
) {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -76,8 +76,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) === '') {
|
||||
@@ -92,77 +90,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) {
|
||||
@@ -174,64 +136,15 @@ 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();
|
||||
|
||||
foreach ($fileLinks as $file) {
|
||||
$url = strtok($file['url'], '#');
|
||||
|
||||
$path = parse_url($url, PHP_URL_PATH);
|
||||
$abs = $path ? TL_ROOT . '/' . ltrim($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]
|
||||
$this->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,
|
||||
],
|
||||
['id' => $existing['id']]
|
||||
);
|
||||
|
||||
$this->debug('File updated', [
|
||||
'url' => $url,
|
||||
'checksum' => $checksum,
|
||||
]);
|
||||
} 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,
|
||||
]
|
||||
);
|
||||
|
||||
$this->debug('File inserted', [
|
||||
'url' => $url,
|
||||
'checksum' => $checksum,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,11 +194,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));
|
||||
|
||||
@@ -12,7 +12,8 @@ $GLOBALS['TL_DCA']['tl_search_files'] = [
|
||||
'url' => 'unique',
|
||||
'type' => 'index',
|
||||
'checksum' => 'index',
|
||||
'last_seen' => 'index', // ⬅️ NEU (für Cleanup-Performance)
|
||||
'uuid' => 'index',
|
||||
'last_seen' => 'index',
|
||||
],
|
||||
],
|
||||
],
|
||||
@@ -63,6 +64,10 @@ $GLOBALS['TL_DCA']['tl_search_files'] = [
|
||||
'sql' => "mediumtext NULL",
|
||||
],
|
||||
|
||||
'uuid' => [
|
||||
'sql' => "binary(16) NULL",
|
||||
],
|
||||
|
||||
/*
|
||||
* md5(url + filemtime)
|
||||
* → erkennt Änderungen zuverlässig
|
||||
|
||||
@@ -4,6 +4,7 @@ Contao 5 – Frontend Module Template
|
||||
#}
|
||||
|
||||
<!-- indexer::stop -->
|
||||
{% block meilisearch %}
|
||||
<div
|
||||
id="topsearch"
|
||||
class="meilisearch-search"
|
||||
@@ -95,6 +96,10 @@ Contao 5 – Frontend Module Template
|
||||
input.value = '';
|
||||
results.innerHTML = '';
|
||||
clear.classList.add('is-hidden');
|
||||
|
||||
// ✅ WICHTIG: Suchmodus verlassen
|
||||
document.body.classList.remove('search-active');
|
||||
|
||||
input.focus();
|
||||
});
|
||||
|
||||
@@ -218,4 +223,5 @@ Contao 5 – Frontend Module Template
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
<!-- indexer::continue -->
|
||||
@@ -0,0 +1,259 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\Service;
|
||||
|
||||
use Contao\FilesModel;
|
||||
use Contao\StringUtil;
|
||||
use Contao\System;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
class MeilisearchFileHelper
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Connection $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Zentrale Datei-Verarbeitung
|
||||
*/
|
||||
public function collect(string $url, string $type, int $pageId): void
|
||||
{
|
||||
$this->log('collect() start', [
|
||||
'url' => $url,
|
||||
'type' => $type,
|
||||
'pageId' => $pageId,
|
||||
]);
|
||||
|
||||
// -------------------------------------------------
|
||||
// 1. URL normalisieren
|
||||
// -------------------------------------------------
|
||||
$cleanUrl = strtok($url, '#');
|
||||
$parts = parse_url($cleanUrl);
|
||||
|
||||
if (!$parts) {
|
||||
$this->log('Invalid URL, skip');
|
||||
return;
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// 2. Externe Datei? → skip
|
||||
// -------------------------------------------------
|
||||
if (!empty($parts['host'])) {
|
||||
$currentRequest = System::getContainer()
|
||||
->get('request_stack')
|
||||
->getCurrentRequest();
|
||||
|
||||
$pageHost = $currentRequest
|
||||
? parse_url($currentRequest->getSchemeAndHttpHost(), PHP_URL_HOST)
|
||||
: null;
|
||||
|
||||
if ($pageHost && $parts['host'] !== $pageHost) {
|
||||
$this->log('External file detected, skip', [
|
||||
'host' => $parts['host'],
|
||||
]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// 3. Pfad-Kandidaten sammeln (ohne Annahmen!)
|
||||
// -------------------------------------------------
|
||||
$query = [];
|
||||
if (!empty($parts['query'])) {
|
||||
parse_str($parts['query'], $query);
|
||||
}
|
||||
|
||||
$pathCandidates = [];
|
||||
|
||||
// direkter Pfad
|
||||
if (!empty($parts['path'])) {
|
||||
$pathCandidates[] = $parts['path'];
|
||||
}
|
||||
|
||||
// Download-Parameter
|
||||
foreach (['file', 'f', 'p'] as $param) {
|
||||
if (!empty($query[$param])) {
|
||||
$pathCandidates[] = $query[$param];
|
||||
}
|
||||
}
|
||||
|
||||
// normalisieren
|
||||
$pathCandidates = array_values(array_unique(array_filter(array_map(
|
||||
static function ($candidate) {
|
||||
$candidate = rawurldecode(html_entity_decode((string) $candidate, ENT_QUOTES));
|
||||
return ltrim($candidate, '/') ?: null;
|
||||
},
|
||||
$pathCandidates
|
||||
))));
|
||||
|
||||
$this->log('Path candidates (normalized)', [
|
||||
'candidates' => $pathCandidates,
|
||||
]);
|
||||
|
||||
// -------------------------------------------------
|
||||
// 4. FilesModel (DBAFS) auflösen → UUID
|
||||
// -------------------------------------------------
|
||||
$fileModel = null;
|
||||
|
||||
foreach ($pathCandidates as $candidate) {
|
||||
|
||||
// 1) direkt
|
||||
$model = FilesModel::findByPath($candidate);
|
||||
if ($model && $model->uuid) {
|
||||
$fileModel = $model;
|
||||
$this->log('Resolved via FilesModel (direct)', [
|
||||
'candidate' => $candidate,
|
||||
'path' => $model->path,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
|
||||
// 2) fallback: files/ davor
|
||||
if (!str_starts_with($candidate, 'files/')) {
|
||||
$model = FilesModel::findByPath('files/' . $candidate);
|
||||
if ($model && $model->uuid) {
|
||||
$fileModel = $model;
|
||||
$this->log('Resolved via FilesModel (files/ prefix)', [
|
||||
'candidate' => $candidate,
|
||||
'path' => $model->path,
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$fileModel) {
|
||||
$this->log('No Contao file model found, skip', [
|
||||
'candidates' => $pathCandidates,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$normalizedPath = (string) $fileModel->path;
|
||||
$uuidBin = $fileModel->uuid;
|
||||
$uuid = StringUtil::binToUuid($uuidBin);
|
||||
$canonicalUrl = '/' . ltrim($normalizedPath, '/');
|
||||
|
||||
$this->log('UUID resolved', [
|
||||
'path' => $canonicalUrl,
|
||||
'uuid' => $uuid,
|
||||
]);
|
||||
|
||||
// -------------------------------------------------
|
||||
// 5. Datei im Filesystem prüfen
|
||||
// -------------------------------------------------
|
||||
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
||||
$abs = $projectDir . '/public/' . $normalizedPath;
|
||||
|
||||
if (!is_file($abs)) {
|
||||
$this->log('Resolved model but file missing on filesystem, skip', [
|
||||
'path' => $normalizedPath,
|
||||
'abs' => $abs,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// 6. Redaktionellen Titel aus tl_files.meta
|
||||
// -------------------------------------------------
|
||||
$title = null;
|
||||
$meta = StringUtil::deserialize($fileModel->meta, true);
|
||||
|
||||
// 1) bevorzugte Sprache (falls vorhanden)
|
||||
$lang = $GLOBALS['TL_LANGUAGE'] ?? null;
|
||||
if ($lang && !empty($meta[$lang]['title'])) {
|
||||
$title = trim((string) $meta[$lang]['title']);
|
||||
}
|
||||
|
||||
// 2) Fallback: erste verfügbare Sprache
|
||||
if ($title === null && is_array($meta)) {
|
||||
foreach ($meta as $langKey => $langMeta) {
|
||||
if (!empty($langMeta['title'])) {
|
||||
$title = trim((string) $langMeta['title']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($title) {
|
||||
$this->log('Title resolved from tl_files', [
|
||||
'title' => $title,
|
||||
]);
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// 7. Datei-Infos
|
||||
// -------------------------------------------------
|
||||
$mtime = filemtime($abs) ?: 0;
|
||||
$checksum = md5($normalizedPath . '|' . $mtime);
|
||||
$now = time();
|
||||
|
||||
// -------------------------------------------------
|
||||
// 8. Upsert über UUID
|
||||
// -------------------------------------------------
|
||||
$existing = $this->connection->fetchAssociative(
|
||||
'SELECT id FROM tl_search_files WHERE uuid = ?',
|
||||
[$uuidBin]
|
||||
);
|
||||
|
||||
if ($existing) {
|
||||
$data = [
|
||||
'tstamp' => $now,
|
||||
'last_seen' => $now,
|
||||
'type' => $type,
|
||||
'url' => $canonicalUrl,
|
||||
'page_id' => $pageId,
|
||||
'file_mtime' => $mtime,
|
||||
'checksum' => $checksum,
|
||||
];
|
||||
|
||||
if ($title !== null) {
|
||||
$data['title'] = $title;
|
||||
}
|
||||
|
||||
$this->connection->update(
|
||||
'tl_search_files',
|
||||
$data,
|
||||
['id' => $existing['id']]
|
||||
);
|
||||
|
||||
$this->log('File updated by UUID', [
|
||||
'uuid' => $uuid,
|
||||
]);
|
||||
} else {
|
||||
$this->connection->insert(
|
||||
'tl_search_files',
|
||||
[
|
||||
'tstamp' => $now,
|
||||
'last_seen' => $now,
|
||||
'type' => $type,
|
||||
'url' => $canonicalUrl,
|
||||
'title' => $title ?? basename($normalizedPath),
|
||||
'page_id' => $pageId,
|
||||
'file_mtime' => $mtime,
|
||||
'checksum' => $checksum,
|
||||
'uuid' => $uuidBin,
|
||||
]
|
||||
);
|
||||
|
||||
$this->log('File inserted by UUID', [
|
||||
'uuid' => $uuid,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->log('collect() end');
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// Logging
|
||||
// -------------------------------------------------
|
||||
private function log(string $message, array $context = []): void
|
||||
{
|
||||
$ctx = $context
|
||||
? ' | ' . json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
|
||||
: '';
|
||||
|
||||
error_log('[ContaoMeilisearch][MeilisearchFileHelper] ' . $message . $ctx);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user