Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0c637c2f92 | |||
| 86b81affdc | |||
| 2d3ddac945 | |||
| 17da2a8434 | |||
| c085911877 | |||
| 40792870bd | |||
| 38372539c2 | |||
| 2bd52f77e0 | |||
| 99ef883da5 | |||
| 56d806c579 | |||
| 2989d205d7 | |||
| 02b1657f19 | |||
| f1c864dfca | |||
| 5cd8286286 | |||
| 0fa0642618 | |||
| 874ed0e656 | |||
| b91281614b | |||
| 4c1b4ac4b7 | |||
| 1f75418d9b | |||
| 278ae9f36f | |||
| 17ecdaec17 | |||
| 8b22467799 | |||
| cd9b918aff | |||
| 8d4af1f61d |
@@ -2,6 +2,7 @@
|
||||
|
||||
Eine schlanke Schnittstelle zwischen **Contao CMS (4.13 / 5.6 / 5.7 ready) unter PHP 8.4** und einer **selbst gehosteten Meilisearch-Instanz**.
|
||||
Das Bundle erweitert den Contao-Suchindex um strukturierte Daten und ermöglicht eine performante, moderne Volltextsuche.
|
||||
Das Parsen von Dateien erfolgt über eine Apache-Tika-Instanz, welche extern bereitgestellt werden muss.
|
||||
|
||||
---
|
||||
|
||||
@@ -38,29 +39,27 @@ Das Bundle nutzt **keinen eigenen Contao-Cron**, sondern System-Cronjobs.
|
||||
/vendor/bin/contao-console meilisearch:files:cleanup
|
||||
```
|
||||
|
||||
### Datei-Parsing
|
||||
|
||||
```
|
||||
/vendor/bin/contao-console meilisearch:files:parse
|
||||
```
|
||||
|
||||
### Meilisearch-Index
|
||||
|
||||
```
|
||||
/vendor/bin/contao-console meilisearch:index
|
||||
```
|
||||
|
||||
## Empfohlene Reihenfolge
|
||||
|
||||
1. Datei-Cleanup
|
||||
`/vendor/bin/contao-console meilisearch:files:cleanup`
|
||||
|
||||
2. Contao-Crawl (ca. 1 Minute später)
|
||||
`/vendor/bin/contao-console contao:crawl`
|
||||
|
||||
3. Meilisearch-Index (ca. 15 Minuten später)
|
||||
`/vendor/bin/contao-console meilisearch:index`
|
||||
|
||||
## Beispiel Crontab
|
||||
|
||||
```
|
||||
0 5 * * * /usr/bin/php8.4 /path/to/project/vendor/bin/contao-console meilisearch:files:cleanup
|
||||
1 5 * * * /usr/bin/php8.4 /path/to/project/vendor/bin/contao-console contao:crawl
|
||||
15 5 * * * /usr/bin/php8.4 /path/to/project/vendor/bin/contao-console meilisearch:index
|
||||
10 5 * * * /usr/bin/php8.4 /path/to/project/vendor/bin/contao-console meilisearch:files:parse
|
||||
20 5 * * * /usr/bin/php8.4 /path/to/project/vendor/bin/contao-console meilisearch:index
|
||||
```
|
||||
|
||||
## Logging
|
||||
|
||||
+1
-5
@@ -8,11 +8,7 @@
|
||||
"contao/core-bundle": "^4.13 || ^5.6 || ^5.7",
|
||||
"contao/calendar-bundle": "^4.13 || ^5.6 || ^5.7",
|
||||
"contao/news-bundle": "^4.13 || ^5.6 || ^5.7",
|
||||
"meilisearch/meilisearch-php": "^1.16",
|
||||
"smalot/pdfparser": "^2.12",
|
||||
"phpoffice/phpword": "^1.4",
|
||||
"phpoffice/phpspreadsheet": "^3.0",
|
||||
"phpoffice/phppresentation": "^1.2"
|
||||
"meilisearch/meilisearch-php": "^1.16"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\Command;
|
||||
|
||||
use Contao\CoreBundle\Framework\ContaoFramework;
|
||||
use Contao\Database;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
@@ -13,6 +13,7 @@ class MeilisearchFilesCleanupCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ContaoFramework $framework,
|
||||
private readonly Connection $connection,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
@@ -21,13 +22,13 @@ class MeilisearchFilesCleanupCommand extends Command
|
||||
{
|
||||
$this
|
||||
->setName('meilisearch:files:cleanup')
|
||||
->setDescription('Remove stale indexed files (PDF, DOCX, XLSX, PPTX) from tl_search_pdf')
|
||||
->setDescription('Remove stale indexed files from tl_search_files')
|
||||
->addOption(
|
||||
'grace',
|
||||
null,
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'Grace period in seconds (files newer than now-grace are kept)',
|
||||
86400 // 24 Stunden
|
||||
86400
|
||||
)
|
||||
->addOption(
|
||||
'dry-run',
|
||||
@@ -49,10 +50,10 @@ class MeilisearchFilesCleanupCommand extends Command
|
||||
$cutoff = time() - $grace;
|
||||
|
||||
if ($dryRun) {
|
||||
$count = Database::getInstance()
|
||||
->prepare('SELECT COUNT(*) AS cnt FROM tl_search_pdf WHERE last_seen < ?')
|
||||
->execute($cutoff)
|
||||
->cnt;
|
||||
$count = $this->connection->fetchOne(
|
||||
'SELECT COUNT(*) FROM tl_search_files WHERE last_seen < ?',
|
||||
[$cutoff]
|
||||
);
|
||||
|
||||
$message = sprintf(
|
||||
'[DRY-RUN] %d stale file(s) would be removed (last_seen < %s)',
|
||||
@@ -63,14 +64,14 @@ class MeilisearchFilesCleanupCommand extends Command
|
||||
$output->writeln('<comment>' . $message . '</comment>');
|
||||
$this->log($message);
|
||||
|
||||
$this->log('Cleaner successfully stopped');
|
||||
$this->log('Cleaner stopped (dry-run)');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$affected = Database::getInstance()
|
||||
->prepare('DELETE FROM tl_search_pdf WHERE last_seen < ?')
|
||||
->execute($cutoff)
|
||||
->affectedRows;
|
||||
$affected = $this->connection->executeStatement(
|
||||
'DELETE FROM tl_search_files WHERE last_seen < ?',
|
||||
[$cutoff]
|
||||
);
|
||||
|
||||
$message = sprintf(
|
||||
'Removed %d stale file(s) (last_seen < %s)',
|
||||
@@ -92,15 +93,8 @@ class MeilisearchFilesCleanupCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Einheitliches Logging mit Zeitstempel
|
||||
*/
|
||||
private function log(string $message): void
|
||||
{
|
||||
error_log(sprintf(
|
||||
'[%s] %s',
|
||||
date('Y-m-d H:i:s'),
|
||||
$message
|
||||
));
|
||||
error_log(sprintf('[%s] %s', date('Y-m-d H:i:s'), $message));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\Command;
|
||||
|
||||
use Contao\CoreBundle\Framework\ContaoFramework;
|
||||
use Contao\Database;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\HttpClient\HttpClient;
|
||||
|
||||
class MeilisearchFilesParseCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ContaoFramework $framework,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setName('meilisearch:files:parse')
|
||||
->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'
|
||||
)
|
||||
->addOption(
|
||||
'dry-run',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'Do not send files to Tika'
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->framework->initialize();
|
||||
$this->log('Parser gestartet');
|
||||
|
||||
$dryRun = (bool) $input->getOption('dry-run');
|
||||
|
||||
$limitOption = $input->getOption('limit');
|
||||
$limit = $limitOption !== null ? max(1, (int) $limitOption) : null;
|
||||
|
||||
$tikaUrl = rtrim((string) ($GLOBALS['TL_CONFIG']['meilisearch_tika_url'] ?? ''), '/');
|
||||
if ($tikaUrl === '') {
|
||||
$output->writeln('<error>Tika URL not configured</error>');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
$sql = "SELECT * FROM tl_search_files ORDER BY tstamp ASC";
|
||||
if ($limit !== null) {
|
||||
$sql .= " LIMIT " . (int) $limit;
|
||||
}
|
||||
|
||||
$files = $db->query($sql)->fetchAllAssoc();
|
||||
|
||||
if (!$files) {
|
||||
$this->log('No files to parse');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$client = HttpClient::create([
|
||||
'timeout' => 180,
|
||||
]);
|
||||
|
||||
foreach ($files as $file) {
|
||||
|
||||
$originalUrl = (string) $file['url'];
|
||||
$existingTitle = trim((string) ($file['title'] ?? ''));
|
||||
$normalized = $originalUrl;
|
||||
|
||||
// -------------------------------------------------
|
||||
// Normalize URL
|
||||
// -------------------------------------------------
|
||||
if (str_contains($normalized, '?')) {
|
||||
$parts = parse_url($normalized);
|
||||
if (!empty($parts['query'])) {
|
||||
parse_str($parts['query'], $query);
|
||||
if (!empty($query['file'])) {
|
||||
$normalized = (string) $query['file'];
|
||||
} else {
|
||||
$this->log('Not a direct file url, skip', ['url' => $originalUrl]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$normalized = strtok($normalized, '#');
|
||||
$normalized = rawurldecode($normalized);
|
||||
$normalized = ltrim($normalized, '/');
|
||||
|
||||
if (!str_starts_with($normalized, 'files/')) {
|
||||
$this->log('Not in files/, skip', ['url' => $originalUrl]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$absolutePath = TL_ROOT . '/' . $normalized;
|
||||
|
||||
if (!is_file($absolutePath)) {
|
||||
$this->log('File missing, skip', [
|
||||
'url' => $originalUrl,
|
||||
'path' => $absolutePath,
|
||||
]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$mtime = filemtime($absolutePath) ?: 0;
|
||||
$checksum = md5($normalized . '|' . $mtime);
|
||||
|
||||
// -------------------------------------------------
|
||||
// Skip unchanged
|
||||
// -------------------------------------------------
|
||||
if ($file['checksum'] === $checksum && !empty($file['text'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($dryRun) {
|
||||
$output->writeln('[DRY-RUN] Would parse: ' . $normalized);
|
||||
continue;
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// MIME-Type
|
||||
// -------------------------------------------------
|
||||
$ext = strtolower(pathinfo($normalized, PATHINFO_EXTENSION));
|
||||
|
||||
$mimeType = match ($ext) {
|
||||
'pdf' => 'application/pdf',
|
||||
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||
default => null,
|
||||
};
|
||||
|
||||
if ($mimeType === null) {
|
||||
$this->log('Unsupported file type, skip', ['url' => $normalized]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// -------------------------------------------------
|
||||
// Tika BODY (roher Plaintext)
|
||||
// -------------------------------------------------
|
||||
try {
|
||||
$this->log('Parsing file', ['url' => $normalized]);
|
||||
|
||||
$bodyResponse = $client->request(
|
||||
'PUT',
|
||||
$tikaUrl . '/tika/main',
|
||||
[
|
||||
'headers' => [
|
||||
'Accept' => 'text/plain',
|
||||
'Content-Type' => $mimeType,
|
||||
],
|
||||
'body' => fopen($absolutePath, 'rb'),
|
||||
]
|
||||
);
|
||||
|
||||
$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(
|
||||
"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');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function log(string $message, array $context = []): void
|
||||
{
|
||||
$ctx = $context
|
||||
? ' | ' . json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
|
||||
: '';
|
||||
|
||||
error_log('[MeilisearchFilesParse] ' . $message . $ctx);
|
||||
}
|
||||
}
|
||||
@@ -3,21 +3,22 @@
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||
|
||||
use Contao\Config;
|
||||
use MummertMedia\ContaoMeilisearchBundle\Service\PdfIndexService;
|
||||
use MummertMedia\ContaoMeilisearchBundle\Service\OfficeIndexService;
|
||||
use Contao\System;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
class IndexPageListener
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PdfIndexService $pdfIndexService,
|
||||
private readonly OfficeIndexService $officeIndexService,
|
||||
) {}
|
||||
private readonly Connection $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -103,7 +104,6 @@ class IndexPageListener
|
||||
$this->debug('Meta: searchimage candidate', ['searchimage' => $searchImage]);
|
||||
|
||||
if (!empty($searchImage)) {
|
||||
// >>> HINWEIS: falls dein tl_search-Feld "image" heißt, hier auf $set['image'] ändern!
|
||||
$set['imagepath'] = trim((string) $searchImage);
|
||||
}
|
||||
|
||||
@@ -139,20 +139,12 @@ class IndexPageListener
|
||||
'class' => $e::class,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->debug('Meta: final set snapshot', [
|
||||
'priority' => $set['priority'] ?? null,
|
||||
'keywords' => $set['keywords'] ?? null,
|
||||
'imagepath' => $set['imagepath'] ?? null,
|
||||
'startDate' => $set['startDate'] ?? null,
|
||||
'checksum' => $set['checksum'] ?? null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* =====================
|
||||
* DATEI-INDEXIERUNG (PDF / OFFICE)
|
||||
* DATEI-ERKENNUNG + UPSERT
|
||||
* =====================
|
||||
*/
|
||||
if ((int) ($data['protected'] ?? 0) !== 0) {
|
||||
@@ -160,15 +152,13 @@ class IndexPageListener
|
||||
return;
|
||||
}
|
||||
|
||||
$indexPdfs = (bool) Config::get('meilisearch_index_pdfs');
|
||||
$indexOffice = (bool) Config::get('meilisearch_index_office');
|
||||
$indexFiles = (bool) Config::get('meilisearch_index_files');
|
||||
|
||||
$this->debug('File indexing settings', [
|
||||
'meilisearch_index_pdfs' => $indexPdfs,
|
||||
'meilisearch_index_office' => $indexOffice,
|
||||
$this->debug('File indexing setting', [
|
||||
'meilisearch_index_files' => $indexFiles,
|
||||
]);
|
||||
|
||||
if (!$indexPdfs && !$indexOffice) {
|
||||
if (!$indexFiles) {
|
||||
$this->debug('Abort: file indexing disabled');
|
||||
return;
|
||||
}
|
||||
@@ -176,61 +166,93 @@ class IndexPageListener
|
||||
$links = $this->findAllLinks($content);
|
||||
$this->debug('Links found', ['count' => count($links)]);
|
||||
|
||||
$pdfLinks = [];
|
||||
$officeLinks = [];
|
||||
$fileLinks = [];
|
||||
|
||||
foreach ($links as $link) {
|
||||
$type = $this->detectIndexableFileType($link['url']);
|
||||
|
||||
if ($type === 'pdf' && $indexPdfs) {
|
||||
$pdfLinks[] = $link;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_array($type, ['docx', 'xlsx', 'pptx'], true) && $indexOffice) {
|
||||
$officeLinks[] = $link;
|
||||
if ($type !== null) {
|
||||
$fileLinks[] = $link + ['type' => $type];
|
||||
}
|
||||
}
|
||||
|
||||
$this->debug('Indexable file links', [
|
||||
'pdf' => count($pdfLinks),
|
||||
'office' => count($officeLinks),
|
||||
$this->debug('Indexable file links found', [
|
||||
'count' => count($fileLinks),
|
||||
'types' => array_count_values(array_column($fileLinks, 'type')),
|
||||
]);
|
||||
|
||||
try {
|
||||
if ($pdfLinks !== []) {
|
||||
$this->debug('PDF handlePdfLinks(): call', ['count' => count($pdfLinks)]);
|
||||
$this->pdfIndexService->handlePdfLinks($pdfLinks);
|
||||
$this->debug('PDF handlePdfLinks(): ok');
|
||||
}
|
||||
if ($fileLinks) {
|
||||
|
||||
if ($officeLinks !== []) {
|
||||
$this->debug('Office handleOfficeLinks(): call', ['count' => count($officeLinks)]);
|
||||
$this->officeIndexService->handleOfficeLinks($officeLinks);
|
||||
$this->debug('Office handleOfficeLinks(): ok');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->debug('File indexing failed', [
|
||||
'error' => $e->getMessage(),
|
||||
'class' => $e::class,
|
||||
$this->debug('About to use Doctrine DBAL connection');
|
||||
|
||||
$db = $this->connection;
|
||||
|
||||
$this->debug('Doctrine DBAL connection ready', [
|
||||
'driver' => get_class($db->getDriver()),
|
||||
]);
|
||||
|
||||
$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]
|
||||
);
|
||||
|
||||
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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->debug('Hook end', [
|
||||
'final_set_keys' => array_keys($set),
|
||||
'final_set' => [
|
||||
'priority' => $set['priority'] ?? null,
|
||||
'keywords' => $set['keywords'] ?? null,
|
||||
'imagepath' => $set['imagepath'] ?? null,
|
||||
'startDate' => $set['startDate'] ?? null,
|
||||
'checksum' => $set['checksum'] ?? null,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extrahiert MEILISEARCH_JSON aus HTML-Kommentar
|
||||
*/
|
||||
/* === Hilfsmethoden unverändert === */
|
||||
|
||||
private function extractMeilisearchJson(string $content): ?array
|
||||
{
|
||||
if (!preg_match('/<!--\s*MEILISEARCH_JSON\s*(\{.*?\})\s*-->/s', $content, $m)) {
|
||||
@@ -245,9 +267,6 @@ class IndexPageListener
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sammle alle <a href="…"> Links
|
||||
*/
|
||||
private function findAllLinks(string $content): array
|
||||
{
|
||||
if (!preg_match_all(
|
||||
@@ -270,12 +289,8 @@ class IndexPageListener
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermittelt indexierbaren Dateityp (pdf|docx|xlsx|pptx) oder null
|
||||
*/
|
||||
private function detectIndexableFileType(string $url): ?string
|
||||
{
|
||||
// Hash entfernen
|
||||
$url = strtok($url, '#');
|
||||
|
||||
$parts = parse_url($url);
|
||||
@@ -283,7 +298,6 @@ class IndexPageListener
|
||||
return null;
|
||||
}
|
||||
|
||||
// direkter Pfad (/files/…)
|
||||
if (!empty($parts['path'])) {
|
||||
$ext = strtolower(pathinfo($parts['path'], PATHINFO_EXTENSION));
|
||||
if (in_array($ext, ['pdf', 'docx', 'xlsx', 'pptx'], true)) {
|
||||
@@ -291,18 +305,12 @@ class IndexPageListener
|
||||
}
|
||||
}
|
||||
|
||||
// Query-Parameter (Contao 4 + 5)
|
||||
if (!empty($parts['query'])) {
|
||||
parse_str($parts['query'], $query);
|
||||
|
||||
foreach (['file', 'p', 'f'] as $param) {
|
||||
if (!empty($query[$param])) {
|
||||
$candidate = (string) $query[$param];
|
||||
|
||||
// sicher decodieren (Contao 4 + 5)
|
||||
$candidate = html_entity_decode($candidate, ENT_QUOTES);
|
||||
$candidate = rawurldecode($candidate);
|
||||
|
||||
$candidate = rawurldecode(html_entity_decode((string) $query[$param], ENT_QUOTES));
|
||||
$ext = strtolower(pathinfo($candidate, PATHINFO_EXTENSION));
|
||||
|
||||
if (in_array($ext, ['pdf', 'docx', 'xlsx', 'pptx'], true)) {
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
use Contao\DC_Table;
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_search_pdf'] = [
|
||||
$GLOBALS['TL_DCA']['tl_search_files'] = [
|
||||
'config' => [
|
||||
'dataContainer' => DC_Table::class,
|
||||
'sql' => [
|
||||
@@ -4,14 +4,17 @@ use Contao\CoreBundle\DataContainer\PaletteManipulator;
|
||||
use Contao\System;
|
||||
|
||||
/**
|
||||
* -------------------------------------------------
|
||||
* Fields
|
||||
* -------------------------------------------------
|
||||
*/
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_host'] = [
|
||||
'inputType' => 'text',
|
||||
'eval' => [
|
||||
'mandatory' => true,
|
||||
'rgxp' => 'url',
|
||||
'tl_class' => 'w50',
|
||||
'rgxp' => 'url',
|
||||
'tl_class' => 'w50',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -19,7 +22,7 @@ $GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index'] = [
|
||||
'inputType' => 'text',
|
||||
'eval' => [
|
||||
'mandatory' => true,
|
||||
'tl_class' => 'w50',
|
||||
'tl_class' => 'w50',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -27,7 +30,7 @@ $GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_api_write'] = [
|
||||
'inputType' => 'text',
|
||||
'eval' => [
|
||||
'mandatory' => true,
|
||||
'tl_class' => 'w50',
|
||||
'tl_class' => 'w50',
|
||||
'hideInput' => true,
|
||||
],
|
||||
];
|
||||
@@ -36,7 +39,7 @@ $GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_api_search'] = [
|
||||
'inputType' => 'text',
|
||||
'eval' => [
|
||||
'mandatory' => true,
|
||||
'tl_class' => 'w50',
|
||||
'tl_class' => 'w50',
|
||||
'hideInput' => true,
|
||||
],
|
||||
];
|
||||
@@ -55,50 +58,71 @@ $GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_imagesize'] = [
|
||||
return $options;
|
||||
},
|
||||
'eval' => [
|
||||
'tl_class' => 'w50',
|
||||
'chosen' => true,
|
||||
'tl_class' => 'w50',
|
||||
'chosen' => true,
|
||||
'includeBlankOption' => true,
|
||||
],
|
||||
// 🔥 DAS HAT GEFEHLT
|
||||
'sql' => "int(10) unsigned NOT NULL default 0",
|
||||
];
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index_past_events'] = [
|
||||
'inputType' => 'checkbox',
|
||||
'eval' => [
|
||||
'tl_class' => 'w50 clr',
|
||||
],
|
||||
];
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_fallback_image'] = [
|
||||
'inputType' => 'fileTree',
|
||||
'eval' => [
|
||||
'filesOnly' => true,
|
||||
'fieldType' => 'radio',
|
||||
'tl_class' => 'w50',
|
||||
'tl_class' => 'w50',
|
||||
],
|
||||
'sql' => "varbinary(16) NULL",
|
||||
];
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index_pdfs'] = [
|
||||
'label' => &$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_pdfs'],
|
||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index_past_events'] = [
|
||||
'inputType' => 'checkbox',
|
||||
'eval' => [
|
||||
'tl_class' => 'w50',
|
||||
'eval' => [
|
||||
'tl_class' => 'w50 clr',
|
||||
],
|
||||
'sql' => "char(1) NOT NULL default '1'",
|
||||
];
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index_office'] = [
|
||||
'label' => &$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_office'],
|
||||
'inputType' => 'checkbox',
|
||||
'eval' => ['tl_class' => 'w50'],
|
||||
'sql' => "char(1) NOT NULL default '0'",
|
||||
];
|
||||
|
||||
/**
|
||||
* Palette
|
||||
* -------------------------------------------------
|
||||
* Datei-Indexierung (Tika)
|
||||
* -------------------------------------------------
|
||||
*/
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index_files'] = [
|
||||
'inputType' => 'checkbox',
|
||||
'eval' => [
|
||||
'tl_class' => 'w50',
|
||||
'submitOnChange' => true,
|
||||
],
|
||||
'sql' => "char(1) NOT NULL default '0'",
|
||||
];
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_tika_url'] = [
|
||||
'inputType' => 'text',
|
||||
'eval' => [
|
||||
'rgxp' => 'url',
|
||||
'mandatory' => true,
|
||||
'tl_class' => 'w50 clr',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* -------------------------------------------------
|
||||
* Selector / Subpalette
|
||||
* -------------------------------------------------
|
||||
*/
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_settings']['palettes']['__selector__'][] = 'meilisearch_index_files';
|
||||
|
||||
$GLOBALS['TL_DCA']['tl_settings']['subpalettes']['meilisearch_index_files']
|
||||
= 'meilisearch_tika_url';
|
||||
|
||||
/**
|
||||
* -------------------------------------------------
|
||||
* Palette
|
||||
* -------------------------------------------------
|
||||
*/
|
||||
|
||||
PaletteManipulator::create()
|
||||
->addLegend('meilisearch_legend', null, PaletteManipulator::POSITION_AFTER, true)
|
||||
->addField('meilisearch_host', 'meilisearch_legend')
|
||||
@@ -108,6 +132,5 @@ PaletteManipulator::create()
|
||||
->addField('meilisearch_imagesize', 'meilisearch_legend')
|
||||
->addField('meilisearch_fallback_image', 'meilisearch_legend')
|
||||
->addField('meilisearch_index_past_events', 'meilisearch_legend')
|
||||
->addField('meilisearch_index_pdfs', 'meilisearch_legend')
|
||||
->addField('meilisearch_index_office', 'meilisearch_legend')
|
||||
->addField('meilisearch_index_files', 'meilisearch_legend')
|
||||
->applyToPalette('default', 'tl_settings');
|
||||
@@ -28,10 +28,10 @@ $GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_past_events'][0]
|
||||
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_past_events'][1]
|
||||
= 'Vergangene Kalender-Events werden ebenfalls in Meilisearch indexiert.';
|
||||
|
||||
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_pdfs'] = [
|
||||
'PDFs indexieren',
|
||||
'Aktiviert die Indexierung von PDF-Dateien für die Suche.',
|
||||
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_files'] = [
|
||||
'Dateien indexieren',
|
||||
'Aktiviert die Indexierung von PDF-Dateien sowie DOCX, XLSX und PPTX.',
|
||||
];
|
||||
|
||||
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_office']
|
||||
= ['Office-Dateien indexieren', 'DOCX, XLSX und PPTX in die Suche aufnehmen.'];
|
||||
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_tika_url']
|
||||
= ['Apache Tika URL', 'URL der Apache Tika Instanz (z. B. https://tika.domain.tld).'];
|
||||
@@ -4,6 +4,7 @@ Contao 5 – Frontend Module Template
|
||||
#}
|
||||
|
||||
<!-- indexer::stop -->
|
||||
{% block meilisearch %}
|
||||
<div
|
||||
id="topsearch"
|
||||
class="meilisearch-search"
|
||||
@@ -218,4 +219,5 @@ Contao 5 – Frontend Module Template
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
<!-- indexer::continue -->
|
||||
@@ -41,8 +41,8 @@ class MeilisearchIndexService
|
||||
return;
|
||||
}
|
||||
|
||||
$host = (string) Config::get('meilisearch_host');
|
||||
$apiKey = (string) Config::get('meilisearch_api_write');
|
||||
$host = (string) Config::get('meilisearch_host');
|
||||
$apiKey = (string) Config::get('meilisearch_api_write');
|
||||
$this->indexName = (string) Config::get('meilisearch_index');
|
||||
|
||||
if ($host === '' || $this->indexName === '') {
|
||||
@@ -72,7 +72,7 @@ class MeilisearchIndexService
|
||||
}
|
||||
|
||||
$this->indexTlSearch($index);
|
||||
$this->indexTlSearchPdf($index);
|
||||
$this->indexTlSearchFiles($index);
|
||||
}
|
||||
|
||||
private function ensureIndexSettings(Indexes $index): void
|
||||
@@ -80,6 +80,7 @@ class MeilisearchIndexService
|
||||
$index->updateSettings([
|
||||
'searchableAttributes' => ['title', 'keywords', 'text'],
|
||||
'sortableAttributes' => ['priority', 'startDate'],
|
||||
'filterableAttributes' => ['type', 'filetype'],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -132,7 +133,7 @@ class MeilisearchIndexService
|
||||
}
|
||||
|
||||
/**
|
||||
* tl_search indexieren
|
||||
* tl_search indexieren (Seiten / News / Events)
|
||||
*/
|
||||
private function indexTlSearch(Indexes $index): void
|
||||
{
|
||||
@@ -164,13 +165,11 @@ class MeilisearchIndexService
|
||||
}
|
||||
}
|
||||
|
||||
$cleanText = $this->stripMeilisearchMeta((string) $row['text']);
|
||||
|
||||
$doc = [
|
||||
'id' => $type . '_' . $row['id'],
|
||||
'type' => $type,
|
||||
'title' => $row['title'],
|
||||
'text' => $cleanText,
|
||||
'text' => $this->stripMeilisearchMeta((string) $row['text']),
|
||||
'url' => $row['url'],
|
||||
'protected' => (bool) $row['protected'],
|
||||
'checksum' => $row['checksum'],
|
||||
@@ -192,31 +191,24 @@ class MeilisearchIndexService
|
||||
$documents[] = $doc;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
error_log(
|
||||
'[ContaoMeilisearch] Failed to build document for tl_search ID '
|
||||
. ($row['id'] ?? '?') . ': ' . $e->getMessage()
|
||||
);
|
||||
error_log('[ContaoMeilisearch] Failed to build tl_search document: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if ($documents !== []) {
|
||||
try {
|
||||
$index->addDocuments($documents);
|
||||
} catch (\Throwable $e) {
|
||||
error_log('[ContaoMeilisearch] Failed to add tl_search documents: ' . $e->getMessage());
|
||||
}
|
||||
$index->addDocuments($documents);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* tl_search_pdf indexieren
|
||||
* tl_search_files indexieren (PDF / Office)
|
||||
*/
|
||||
private function indexTlSearchPdf(Indexes $index): void
|
||||
private function indexTlSearchFiles(Indexes $index): void
|
||||
{
|
||||
try {
|
||||
$rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search_pdf');
|
||||
$rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search_files');
|
||||
} catch (\Throwable $e) {
|
||||
error_log('[ContaoMeilisearch] Failed to read tl_search_pdf: ' . $e->getMessage());
|
||||
error_log('[ContaoMeilisearch] Failed to read tl_search_files: ' . $e->getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -233,30 +225,24 @@ class MeilisearchIndexService
|
||||
: 'pdf';
|
||||
|
||||
$documents[] = [
|
||||
'id' => $fileType . '_' . $row['id'],
|
||||
'type' => $fileType,
|
||||
'title' => $row['title'],
|
||||
'text' => $this->stripMeilisearchMeta((string) $row['text']),
|
||||
'url' => $row['url'],
|
||||
'checksum' => $row['checksum'],
|
||||
'poster' => self::FILETYPE_ICON_MAP[$fileType]
|
||||
'id' => 'file_' . $row['id'],
|
||||
'type' => 'file',
|
||||
'filetype' => $fileType,
|
||||
'title' => $row['title'] ?: basename($row['url']),
|
||||
'text' => (string) $row['text'],
|
||||
'url' => $row['url'],
|
||||
'checksum' => $row['checksum'],
|
||||
'poster' => self::FILETYPE_ICON_MAP[$fileType]
|
||||
?? self::FILETYPE_ICON_MAP['pdf'],
|
||||
];
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
error_log(
|
||||
'[ContaoMeilisearch] Failed to build PDF document for ID '
|
||||
. ($row['id'] ?? '?') . ': ' . $e->getMessage()
|
||||
);
|
||||
error_log('[ContaoMeilisearch] Failed to build file document: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if ($documents !== []) {
|
||||
try {
|
||||
$index->addDocuments($documents);
|
||||
} catch (\Throwable $e) {
|
||||
error_log('[ContaoMeilisearch] Failed to add tl_search_pdf documents: ' . $e->getMessage());
|
||||
}
|
||||
$index->addDocuments($documents);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,276 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\Service;
|
||||
|
||||
use Contao\Database;
|
||||
use PhpOffice\PhpWord\IOFactory as WordIOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory as SpreadsheetIOFactory;
|
||||
use PhpOffice\PhpPresentation\IOFactory as PresentationIOFactory;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
|
||||
class OfficeIndexService
|
||||
{
|
||||
private string $projectDir;
|
||||
|
||||
public function __construct(ParameterBagInterface $params)
|
||||
{
|
||||
$this->projectDir = rtrim((string) $params->get('kernel.project_dir'), '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int,array{url:string,linkText:?string}> $officeLinks
|
||||
*/
|
||||
public function handleOfficeLinks(array $officeLinks): void
|
||||
{
|
||||
// Dedupe nur pro Aufruf (nicht "pro Crawl")
|
||||
$seen = [];
|
||||
$now = time();
|
||||
|
||||
foreach ($officeLinks as $row) {
|
||||
$url = (string) ($row['url'] ?? '');
|
||||
$linkText = $row['linkText'] ?? null;
|
||||
|
||||
if ($url === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// doppelte URLs pro Aufruf vermeiden
|
||||
$seenKey = md5($url);
|
||||
if (isset($seen[$seenKey])) {
|
||||
continue;
|
||||
}
|
||||
$seen[$seenKey] = true;
|
||||
|
||||
$normalized = $this->normalizeOfficeUrl($url);
|
||||
if ($normalized === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
[$relativePath, $type] = $normalized;
|
||||
|
||||
$absolutePath = $this->getAbsolutePath($relativePath);
|
||||
if (!is_file($absolutePath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$mtime = (int) (filemtime($absolutePath) ?: 0);
|
||||
$checksum = md5($relativePath . '|' . $mtime);
|
||||
|
||||
// existiert bereits?
|
||||
$existing = Database::getInstance()
|
||||
->prepare('SELECT checksum FROM tl_search_pdf WHERE url=? LIMIT 1')
|
||||
->execute($relativePath)
|
||||
->fetchAssoc();
|
||||
|
||||
$needsParse = !$existing || ($existing['checksum'] ?? '') !== $checksum;
|
||||
|
||||
// Titel-Priorität:
|
||||
// 1) Linktext
|
||||
// 2) Dateiname
|
||||
$title = $linkText ?: basename($absolutePath);
|
||||
$text = '';
|
||||
|
||||
if ($needsParse) {
|
||||
$text = $this->parseOfficeFile($absolutePath, $type);
|
||||
if ($text === '') {
|
||||
// Parsing fehlgeschlagen → nichts überschreiben
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$this->upsertOffice(
|
||||
$relativePath,
|
||||
$title,
|
||||
$text, // kann '' sein → SQL überschreibt dann nicht
|
||||
$checksum,
|
||||
$mtime,
|
||||
$type,
|
||||
$now
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{string,string}|null [relativePath, type]
|
||||
*/
|
||||
private function normalizeOfficeUrl(string $url): ?array
|
||||
{
|
||||
$decoded = html_entity_decode($url);
|
||||
$parts = parse_url($decoded);
|
||||
|
||||
if (!$parts) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 1) files/... (ohne führenden Slash)
|
||||
if (!empty($parts['path']) && str_starts_with($parts['path'], 'files/')) {
|
||||
$ext = strtolower(pathinfo($parts['path'], PATHINFO_EXTENSION));
|
||||
if (in_array($ext, ['docx', 'xlsx', 'pptx'], true)) {
|
||||
return ['/' . $parts['path'], $ext];
|
||||
}
|
||||
}
|
||||
|
||||
// 2) /files/...
|
||||
if (!empty($parts['path']) && str_starts_with($parts['path'], '/files/')) {
|
||||
$ext = strtolower(pathinfo($parts['path'], PATHINFO_EXTENSION));
|
||||
if (in_array($ext, ['docx', 'xlsx', 'pptx'], true)) {
|
||||
return [$parts['path'], $ext];
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($parts['query'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
parse_str($parts['query'], $query);
|
||||
|
||||
// 3) Contao 4: ?file=files/...
|
||||
if (!empty($query['file'])) {
|
||||
$file = urldecode((string) $query['file']);
|
||||
$file = ltrim($file, '/');
|
||||
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
||||
|
||||
if (
|
||||
str_starts_with($file, 'files/') &&
|
||||
in_array($ext, ['docx', 'xlsx', 'pptx'], true)
|
||||
) {
|
||||
return ['/' . $file, $ext];
|
||||
}
|
||||
}
|
||||
|
||||
// 4) Contao 5: ?p=...
|
||||
if (!empty($query['p'])) {
|
||||
$p = urldecode((string) $query['p']);
|
||||
$ext = strtolower(pathinfo($p, PATHINFO_EXTENSION));
|
||||
|
||||
if (in_array($ext, ['docx', 'xlsx', 'pptx'], true)) {
|
||||
return ['/files/' . ltrim($p, '/'), $ext];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getAbsolutePath(string $relativePath): string
|
||||
{
|
||||
return $this->projectDir . '/' . ltrim($relativePath, '/');
|
||||
}
|
||||
|
||||
private function upsertOffice(
|
||||
string $url,
|
||||
string $title,
|
||||
string $text,
|
||||
string $checksum,
|
||||
int $mtime,
|
||||
string $type,
|
||||
int $now
|
||||
): void {
|
||||
Database::getInstance()
|
||||
->prepare('
|
||||
INSERT INTO tl_search_pdf
|
||||
(tstamp, last_seen, type, url, title, text, checksum, file_mtime)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
tstamp = VALUES(tstamp),
|
||||
last_seen = VALUES(last_seen),
|
||||
type = VALUES(type),
|
||||
url = VALUES(url),
|
||||
title = VALUES(title),
|
||||
checksum = VALUES(checksum),
|
||||
file_mtime = VALUES(file_mtime),
|
||||
text = IF(VALUES(text) = "" OR VALUES(text) IS NULL, text, VALUES(text))
|
||||
')
|
||||
->execute(
|
||||
$now,
|
||||
$now,
|
||||
$type,
|
||||
$url,
|
||||
$title,
|
||||
$text,
|
||||
$checksum,
|
||||
$mtime
|
||||
);
|
||||
}
|
||||
|
||||
private function parseOfficeFile(string $absolutePath, string $type): string
|
||||
{
|
||||
return match ($type) {
|
||||
'docx' => $this->parseDocx($absolutePath),
|
||||
'xlsx' => $this->parseXlsx($absolutePath),
|
||||
'pptx' => $this->parsePptx($absolutePath),
|
||||
default => '',
|
||||
};
|
||||
}
|
||||
|
||||
private function parseDocx(string $absolutePath): string
|
||||
{
|
||||
try {
|
||||
$phpWord = WordIOFactory::load($absolutePath);
|
||||
$text = '';
|
||||
|
||||
foreach ($phpWord->getSections() as $section) {
|
||||
foreach ($section->getElements() as $element) {
|
||||
if (method_exists($element, 'getText')) {
|
||||
$text .= ' ' . $element->getText();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->cleanText($text);
|
||||
} catch (\Throwable) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
private function parseXlsx(string $absolutePath): string
|
||||
{
|
||||
try {
|
||||
$spreadsheet = SpreadsheetIOFactory::load($absolutePath);
|
||||
$text = '';
|
||||
|
||||
foreach ($spreadsheet->getAllSheets() as $sheet) {
|
||||
foreach ($sheet->toArray() as $row) {
|
||||
$text .= ' ' . implode(' ', array_filter($row, 'is_scalar'));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->cleanText($text);
|
||||
} catch (\Throwable) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
private function parsePptx(string $absolutePath): string
|
||||
{
|
||||
try {
|
||||
$presentation = PresentationIOFactory::load($absolutePath);
|
||||
$text = '';
|
||||
|
||||
foreach ($presentation->getAllSlides() as $slide) {
|
||||
foreach ($slide->getShapeCollection() as $shape) {
|
||||
if (method_exists($shape, 'getPlainText')) {
|
||||
$text .= ' ' . $shape->getPlainText();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->cleanText($text);
|
||||
} catch (\Throwable) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
private function cleanText(string $text): string
|
||||
{
|
||||
if (class_exists(\Normalizer::class)) {
|
||||
$text = \Normalizer::normalize($text, \Normalizer::FORM_C) ?? $text;
|
||||
}
|
||||
|
||||
$text = str_replace(["\r\n", "\r"], "\n", $text);
|
||||
$text = preg_replace('/[^\p{L}\p{N}\p{P}\p{Z}\n]/u', ' ', $text);
|
||||
$text = preg_replace('/\s+/u', ' ', $text);
|
||||
|
||||
return trim(mb_substr($text, 0, 20000));
|
||||
}
|
||||
}
|
||||
@@ -1,236 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MummertMedia\ContaoMeilisearchBundle\Service;
|
||||
|
||||
use Contao\Database;
|
||||
use Smalot\PdfParser\Parser;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
|
||||
class PdfIndexService
|
||||
{
|
||||
private string $projectDir;
|
||||
|
||||
public function __construct(ParameterBagInterface $params)
|
||||
{
|
||||
$this->projectDir = rtrim((string) $params->get('kernel.project_dir'), '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int,array{url:string,linkText:?string}> $pdfLinks
|
||||
*/
|
||||
public function handlePdfLinks(array $pdfLinks): void
|
||||
{
|
||||
// Dedupe nur pro Aufruf (nicht "pro Crawl")
|
||||
$seen = [];
|
||||
$now = time();
|
||||
|
||||
foreach ($pdfLinks as $row) {
|
||||
$url = (string) ($row['url'] ?? '');
|
||||
$linkText = $row['linkText'] ?? null;
|
||||
|
||||
if ($url === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// doppelte URLs pro Aufruf vermeiden
|
||||
$seenKey = md5($url);
|
||||
if (isset($seen[$seenKey])) {
|
||||
continue;
|
||||
}
|
||||
$seen[$seenKey] = true;
|
||||
|
||||
$normalizedPath = $this->normalizePdfUrl($url);
|
||||
if ($normalizedPath === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$absolutePath = $this->getAbsolutePath($normalizedPath);
|
||||
if (!is_file($absolutePath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$mtime = (int) (filemtime($absolutePath) ?: 0);
|
||||
$checksum = md5($normalizedPath . '|' . $mtime);
|
||||
|
||||
// existiert bereits?
|
||||
$existing = Database::getInstance()
|
||||
->prepare('SELECT checksum FROM tl_search_pdf WHERE url=? LIMIT 1')
|
||||
->execute($normalizedPath)
|
||||
->fetchAssoc();
|
||||
|
||||
$needsParse = !$existing || ($existing['checksum'] ?? '') !== $checksum;
|
||||
|
||||
// Titel-Priorität:
|
||||
// 1) Linktext
|
||||
// 2) PDF-Metadaten
|
||||
// 3) Dateiname
|
||||
$title = $linkText ?: basename($absolutePath);
|
||||
$text = '';
|
||||
|
||||
if ($needsParse) {
|
||||
$pdfMetaTitle = $this->readPdfMetaTitle($absolutePath);
|
||||
$title = $linkText ?: ($pdfMetaTitle ?: basename($absolutePath));
|
||||
|
||||
$text = $this->parsePdf($absolutePath);
|
||||
if ($text === '') {
|
||||
// wenn parsing fehlschlägt, NICHT überschreiben
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$this->upsertPdf(
|
||||
$normalizedPath,
|
||||
$title,
|
||||
$text, // kann '' sein → wird in SQL nicht überschrieben
|
||||
$checksum,
|
||||
$mtime,
|
||||
$now
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizePdfUrl(string $url): ?string
|
||||
{
|
||||
$decoded = html_entity_decode($url);
|
||||
$parts = parse_url($decoded);
|
||||
|
||||
if (!$parts) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 1) files/...pdf (ohne führenden Slash)
|
||||
if (
|
||||
!empty($parts['path'])
|
||||
&& str_starts_with($parts['path'], 'files/')
|
||||
&& str_ends_with(strtolower($parts['path']), '.pdf')
|
||||
) {
|
||||
return '/' . $parts['path'];
|
||||
}
|
||||
|
||||
// 2) /files/...pdf
|
||||
if (
|
||||
!empty($parts['path'])
|
||||
&& str_starts_with($parts['path'], '/files/')
|
||||
&& str_ends_with(strtolower($parts['path']), '.pdf')
|
||||
) {
|
||||
return $parts['path'];
|
||||
}
|
||||
|
||||
if (empty($parts['query'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
parse_str($parts['query'], $query);
|
||||
|
||||
// 3) Contao 4: ?file=files/...
|
||||
if (!empty($query['file'])) {
|
||||
$file = urldecode((string) $query['file']);
|
||||
$file = ltrim($file, '/');
|
||||
|
||||
if (
|
||||
str_starts_with($file, 'files/')
|
||||
&& str_ends_with(strtolower($file), '.pdf')
|
||||
) {
|
||||
return '/' . $file;
|
||||
}
|
||||
}
|
||||
|
||||
// 4) Contao 5: ?p=...
|
||||
if (!empty($query['p'])) {
|
||||
$p = urldecode((string) $query['p']);
|
||||
return '/files/' . ltrim($p, '/');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getAbsolutePath(string $relativePath): string
|
||||
{
|
||||
return $this->projectDir . '/' . ltrim($relativePath, '/');
|
||||
}
|
||||
|
||||
private function upsertPdf(
|
||||
string $url,
|
||||
string $title,
|
||||
string $text,
|
||||
string $checksum,
|
||||
int $mtime,
|
||||
int $now
|
||||
): void {
|
||||
Database::getInstance()
|
||||
->prepare('
|
||||
INSERT INTO tl_search_pdf
|
||||
(tstamp, last_seen, type, url, title, text, checksum, file_mtime)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
tstamp = VALUES(tstamp),
|
||||
last_seen = VALUES(last_seen),
|
||||
type = VALUES(type),
|
||||
url = VALUES(url),
|
||||
title = VALUES(title),
|
||||
checksum = VALUES(checksum),
|
||||
file_mtime = VALUES(file_mtime),
|
||||
text = IF(VALUES(text) = "" OR VALUES(text) IS NULL, text, VALUES(text))
|
||||
')
|
||||
->execute(
|
||||
$now,
|
||||
$now,
|
||||
'pdf',
|
||||
$url,
|
||||
$title,
|
||||
$text,
|
||||
$checksum,
|
||||
$mtime
|
||||
);
|
||||
}
|
||||
|
||||
private function parsePdf(string $absolutePath): string
|
||||
{
|
||||
try {
|
||||
$parser = new Parser();
|
||||
$pdf = $parser->parseFile($absolutePath);
|
||||
$text = $this->cleanPdfContent($pdf->getText());
|
||||
|
||||
return mb_substr($text, 0, 20000);
|
||||
} catch (\Throwable) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
private function readPdfMetaTitle(string $absolutePath): ?string
|
||||
{
|
||||
try {
|
||||
$parser = new Parser();
|
||||
$pdf = $parser->parseFile($absolutePath);
|
||||
$details = $pdf->getDetails();
|
||||
|
||||
foreach (['Title', 'title'] as $key) {
|
||||
if (!empty($details[$key]) && is_string($details[$key])) {
|
||||
$t = trim($details[$key]);
|
||||
if ($t !== '') {
|
||||
return $t;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function cleanPdfContent(string $text): string
|
||||
{
|
||||
if (class_exists(\Normalizer::class)) {
|
||||
$text = \Normalizer::normalize($text, \Normalizer::FORM_C) ?? $text;
|
||||
}
|
||||
|
||||
$text = str_replace(["\r\n", "\r"], "\n", $text);
|
||||
$text = preg_replace('/[^\p{L}\p{N}\p{P}\p{Z}\n]/u', ' ', $text);
|
||||
$text = preg_replace('/(?<=\p{L})\s+(?=\p{L})/u', ' ', $text);
|
||||
$text = preg_replace('/\s+/u', ' ', $text);
|
||||
|
||||
return trim($text);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user