Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d874fe4274 | |||
| c790a1c312 | |||
| 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\CoreBundle\Framework\ContaoFramework;
|
||||||
use Contao\Database;
|
use Contao\Database;
|
||||||
|
use Contao\System;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
@@ -22,18 +23,18 @@ class MeilisearchFilesParseCommand extends Command
|
|||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
->setName('meilisearch:files:parse')
|
->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(
|
->addOption(
|
||||||
'limit',
|
'limit',
|
||||||
null,
|
null,
|
||||||
InputOption::VALUE_OPTIONAL,
|
InputOption::VALUE_OPTIONAL,
|
||||||
'Maximum number of files to check per run (optional)'
|
'Maximum number of files to check per run'
|
||||||
)
|
)
|
||||||
->addOption(
|
->addOption(
|
||||||
'dry-run',
|
'dry-run',
|
||||||
null,
|
null,
|
||||||
InputOption::VALUE_NONE,
|
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');
|
$dryRun = (bool) $input->getOption('dry-run');
|
||||||
|
|
||||||
// ---- LIMIT: nur wenn explizit gesetzt
|
|
||||||
$limitOption = $input->getOption('limit');
|
$limitOption = $input->getOption('limit');
|
||||||
$limit = $limitOption !== null ? max(1, (int) $limitOption) : null;
|
$limit = $limitOption !== null ? max(1, (int) $limitOption) : null;
|
||||||
|
|
||||||
// ---- Tika URL
|
|
||||||
$tikaUrl = rtrim((string) ($GLOBALS['TL_CONFIG']['meilisearch_tika_url'] ?? ''), '/');
|
$tikaUrl = rtrim((string) ($GLOBALS['TL_CONFIG']['meilisearch_tika_url'] ?? ''), '/');
|
||||||
if ($tikaUrl === '') {
|
if ($tikaUrl === '') {
|
||||||
$output->writeln('<error>Tika URL not configured</error>');
|
$output->writeln('<error>Tika URL not configured</error>');
|
||||||
@@ -57,7 +56,6 @@ class MeilisearchFilesParseCommand extends Command
|
|||||||
|
|
||||||
$db = Database::getInstance();
|
$db = Database::getInstance();
|
||||||
|
|
||||||
// ---- Files laden
|
|
||||||
$sql = "SELECT * FROM tl_search_files ORDER BY tstamp ASC";
|
$sql = "SELECT * FROM tl_search_files ORDER BY tstamp ASC";
|
||||||
if ($limit !== null) {
|
if ($limit !== null) {
|
||||||
$sql .= " LIMIT " . (int) $limit;
|
$sql .= " LIMIT " . (int) $limit;
|
||||||
@@ -77,10 +75,11 @@ class MeilisearchFilesParseCommand extends Command
|
|||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
|
|
||||||
$originalUrl = (string) $file['url'];
|
$originalUrl = (string) $file['url'];
|
||||||
|
$existingTitle = trim((string) ($file['title'] ?? ''));
|
||||||
$normalized = $originalUrl;
|
$normalized = $originalUrl;
|
||||||
|
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
// 1) ?file=files/…
|
// Normalize URL
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
if (str_contains($normalized, '?')) {
|
if (str_contains($normalized, '?')) {
|
||||||
$parts = parse_url($normalized);
|
$parts = parse_url($normalized);
|
||||||
@@ -95,26 +94,20 @@ class MeilisearchFilesParseCommand extends Command
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------
|
|
||||||
// 2) Fragment entfernen
|
|
||||||
// -------------------------------------------------
|
|
||||||
$normalized = strtok($normalized, '#');
|
$normalized = strtok($normalized, '#');
|
||||||
|
|
||||||
// -------------------------------------------------
|
|
||||||
// 3) URL-Decoding
|
|
||||||
// -------------------------------------------------
|
|
||||||
$normalized = rawurldecode($normalized);
|
$normalized = rawurldecode($normalized);
|
||||||
|
|
||||||
// -------------------------------------------------
|
|
||||||
// 4) Nur lokale files/
|
|
||||||
// -------------------------------------------------
|
|
||||||
$normalized = ltrim($normalized, '/');
|
$normalized = ltrim($normalized, '/');
|
||||||
|
|
||||||
if (!str_starts_with($normalized, 'files/')) {
|
if (!str_starts_with($normalized, 'files/')) {
|
||||||
$this->log('Not in files/, skip', ['url' => $originalUrl]);
|
$this->log('Not in files/, skip', ['url' => $originalUrl]);
|
||||||
continue;
|
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)) {
|
if (!is_file($absolutePath)) {
|
||||||
$this->log('File missing, skip', [
|
$this->log('File missing, skip', [
|
||||||
@@ -128,7 +121,7 @@ class MeilisearchFilesParseCommand extends Command
|
|||||||
$checksum = md5($normalized . '|' . $mtime);
|
$checksum = md5($normalized . '|' . $mtime);
|
||||||
|
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
// 5) Skip unchanged
|
// Skip unchanged
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
if ($file['checksum'] === $checksum && !empty($file['text'])) {
|
if ($file['checksum'] === $checksum && !empty($file['text'])) {
|
||||||
continue;
|
continue;
|
||||||
@@ -140,7 +133,7 @@ class MeilisearchFilesParseCommand extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
// 6) MIME-Type
|
// MIME-Type
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
$ext = strtolower(pathinfo($normalized, PATHINFO_EXTENSION));
|
$ext = strtolower(pathinfo($normalized, PATHINFO_EXTENSION));
|
||||||
|
|
||||||
@@ -158,12 +151,12 @@ class MeilisearchFilesParseCommand extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
// 7) Tika parse
|
// Tika BODY (roher Plaintext)
|
||||||
// -------------------------------------------------
|
// -------------------------------------------------
|
||||||
try {
|
try {
|
||||||
$this->log('Parsing file', ['url' => $normalized]);
|
$this->log('Parsing file', ['url' => $normalized]);
|
||||||
|
|
||||||
$response = $client->request(
|
$bodyResponse = $client->request(
|
||||||
'PUT',
|
'PUT',
|
||||||
$tikaUrl . '/tika/main',
|
$tikaUrl . '/tika/main',
|
||||||
[
|
[
|
||||||
@@ -175,14 +168,87 @@ class MeilisearchFilesParseCommand extends Command
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
$text = trim((string) $response->getContent(false));
|
$text = trim((string) $bodyResponse->getContent(false));
|
||||||
|
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$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(
|
$db->prepare(
|
||||||
"UPDATE tl_search_files
|
"UPDATE tl_search_files
|
||||||
SET text = ?, checksum = ?, file_mtime = ?, tstamp = ?
|
SET text = ?, title = ?, checksum = ?, file_mtime = ?, tstamp = ?
|
||||||
WHERE id = ?"
|
WHERE id = ?"
|
||||||
)->execute(
|
)->execute(
|
||||||
$text,
|
$text,
|
||||||
|
$title,
|
||||||
$checksum,
|
$checksum,
|
||||||
$mtime,
|
$mtime,
|
||||||
time(),
|
time(),
|
||||||
@@ -192,14 +258,8 @@ class MeilisearchFilesParseCommand extends Command
|
|||||||
$this->log('File parsed', [
|
$this->log('File parsed', [
|
||||||
'url' => $normalized,
|
'url' => $normalized,
|
||||||
'chars' => mb_strlen($text),
|
'chars' => mb_strlen($text),
|
||||||
|
'title' => $title,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
$this->log('Parse failed', [
|
|
||||||
'url' => $normalized,
|
|
||||||
'error' => $e->getMessage(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->log('Parser finished');
|
$this->log('Parser finished');
|
||||||
|
|||||||
@@ -4,17 +4,17 @@ namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
|||||||
|
|
||||||
use Contao\Config;
|
use Contao\Config;
|
||||||
use Contao\System;
|
use Contao\System;
|
||||||
|
use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchFileHelper;
|
||||||
|
|
||||||
class IndexPageListener
|
class IndexPageListener
|
||||||
{
|
{
|
||||||
public function __construct()
|
public function __construct(
|
||||||
{
|
private readonly MeilisearchFileHelper $fileHelper,
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -76,8 +76,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) === '') {
|
||||||
@@ -92,32 +90,17 @@ 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'] ?? '');
|
||||||
@@ -125,44 +108,23 @@ class IndexPageListener
|
|||||||
$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) {
|
||||||
@@ -174,64 +136,15 @@ 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');
|
|
||||||
$time = time();
|
|
||||||
|
|
||||||
foreach ($fileLinks as $file) {
|
foreach ($fileLinks as $file) {
|
||||||
$url = strtok($file['url'], '#');
|
$this->fileHelper->collect(
|
||||||
|
$file['url'],
|
||||||
$path = parse_url($url, PHP_URL_PATH);
|
$file['type'],
|
||||||
$abs = $path ? TL_ROOT . '/' . ltrim($path, '/') : null;
|
(int) ($data['pid'] ?? 0)
|
||||||
|
|
||||||
$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,
|
|
||||||
],
|
|
||||||
['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
|
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));
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ $GLOBALS['TL_DCA']['tl_search_files'] = [
|
|||||||
'url' => 'unique',
|
'url' => 'unique',
|
||||||
'type' => 'index',
|
'type' => 'index',
|
||||||
'checksum' => '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",
|
'sql' => "mediumtext NULL",
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'uuid' => [
|
||||||
|
'sql' => "binary(16) NULL",
|
||||||
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* md5(url + filemtime)
|
* md5(url + filemtime)
|
||||||
* → erkennt Änderungen zuverlässig
|
* → erkennt Änderungen zuverlässig
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ Contao 5 – Frontend Module Template
|
|||||||
#}
|
#}
|
||||||
|
|
||||||
<!-- indexer::stop -->
|
<!-- indexer::stop -->
|
||||||
|
{% block meilisearch %}
|
||||||
<div
|
<div
|
||||||
id="topsearch"
|
id="topsearch"
|
||||||
class="meilisearch-search"
|
class="meilisearch-search"
|
||||||
@@ -59,10 +60,44 @@ Contao 5 – Frontend Module Template
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script type="module">
|
<script>
|
||||||
import MeiliSearch from 'https://cdn.jsdelivr.net/npm/meilisearch@latest/dist/bundles/meilisearch.esm.js';
|
(function () {
|
||||||
|
const CDN_URLS = [
|
||||||
|
'https://cdn.jsdelivr.net/npm/meilisearch@0.39.0/dist/bundles/meilisearch.umd.min.js',
|
||||||
|
'https://unpkg.com/meilisearch@0.39.0/dist/bundles/meilisearch.umd.min.js'
|
||||||
|
];
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
function loadClient(urls, onDone) {
|
||||||
|
if (typeof MeiliSearch !== 'undefined') {
|
||||||
|
onDone(true, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!urls.length) {
|
||||||
|
onDone(false, 'Alle CDN-Quellen fehlgeschlagen (mögliche CSP-Blockierung von script-src).');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = urls.shift();
|
||||||
|
const script = document.createElement('script');
|
||||||
|
script.src = url;
|
||||||
|
script.async = true;
|
||||||
|
script.crossOrigin = 'anonymous';
|
||||||
|
|
||||||
|
script.onload = () => {
|
||||||
|
if (typeof MeiliSearch !== 'undefined') {
|
||||||
|
onDone(true, null);
|
||||||
|
} else {
|
||||||
|
loadClient(urls, onDone);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
script.onerror = () => loadClient(urls, onDone);
|
||||||
|
|
||||||
|
document.head.appendChild(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
function initSearch() {
|
||||||
|
|
||||||
const wrapper = document.querySelector('.meilisearch-search');
|
const wrapper = document.querySelector('.meilisearch-search');
|
||||||
if (!wrapper) return;
|
if (!wrapper) return;
|
||||||
@@ -95,6 +130,10 @@ Contao 5 – Frontend Module Template
|
|||||||
input.value = '';
|
input.value = '';
|
||||||
results.innerHTML = '';
|
results.innerHTML = '';
|
||||||
clear.classList.add('is-hidden');
|
clear.classList.add('is-hidden');
|
||||||
|
|
||||||
|
// ✅ WICHTIG: Suchmodus verlassen
|
||||||
|
document.body.classList.remove('search-active');
|
||||||
|
|
||||||
input.focus();
|
input.focus();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -216,6 +255,19 @@ Contao 5 – Frontend Module Template
|
|||||||
results.appendChild(node);
|
results.appendChild(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
loadClient([...CDN_URLS], (ok, reason) => {
|
||||||
|
if (!ok) {
|
||||||
|
console.error('[Meilisearch] Browser client konnte nicht geladen werden. ' + reason);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
initSearch();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
{% endblock %}
|
||||||
<!-- indexer::continue -->
|
<!-- 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