Compare commits
243 Commits
b59bda40fc
..
0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 71f8d2cc95 | |||
| 41c9db954d | |||
| 7613d20831 | |||
| 46e5665c36 | |||
| 322628de6c | |||
| b39d24b832 | |||
| 6287ff54de | |||
| e245483b2a | |||
| 5c30eed719 | |||
| 1f6e115ae6 | |||
| ae28c619da | |||
| c2bef6dad5 | |||
| c5ab9f28c8 | |||
| e609dca6c8 | |||
| 9b10b5cc13 | |||
| cf5986e02b | |||
| 44db711c52 | |||
| 13d1e0105c | |||
| 90bd2c77f2 | |||
| 2a2c68cfb1 | |||
| 987441700f | |||
| 04f3e76c8f | |||
| 2a22253f18 | |||
| 0fd119f873 | |||
| 9edb1e4713 | |||
| e4c8473999 | |||
| 5102f98e2b | |||
| 0507d7d992 | |||
| 31def907f6 | |||
| 0cf9703a9b | |||
| b0337dcc6c | |||
| 37b50f9140 | |||
| 1192ff4bf4 | |||
| da9edd59ba | |||
| 186471b68c | |||
| e6cb8a6f41 | |||
| 75f8df5817 | |||
| e5cce34619 | |||
| c645e3caa8 | |||
| 2832da4736 | |||
| 4cf0d7315b | |||
| ff54ed5aa4 | |||
| 97c25c5abd | |||
| 1948227a06 | |||
| 89f12df84a | |||
| 59ff2c3ed0 | |||
| 8d505b70ee | |||
| fe2fd91697 | |||
| 952f2472c4 | |||
| c0c83f2e66 | |||
| be9b679922 | |||
| 76e081e51e | |||
| 131e24fe2e | |||
| cfffdf2323 | |||
| 0366995021 | |||
| dc141b7abb | |||
| 3041ff59b5 | |||
| e61293d5ba | |||
| 60aa99199b | |||
| e8e80ff48b | |||
| 3a94062a7a | |||
| 70321dc497 | |||
| b6a86156d9 | |||
| 03bcc04537 | |||
| 3321049302 | |||
| 459f007f8d | |||
| 2c31526be7 | |||
| 263d6d42ac | |||
| 0ed5f4a51d | |||
| 2c53b7862e | |||
| 973f3f5b3e | |||
| 5be9af1f20 | |||
| bd345252fe | |||
| 5cf81a6a52 | |||
| da379116d1 | |||
| e7e8917d92 | |||
| 542a8fc1a3 | |||
| 6c3bd63073 | |||
| 7e5518888d | |||
| 4ecbc0ab1a | |||
| 5f900ca5eb | |||
| e38ebf8a02 | |||
| a4e18a84fd | |||
| b57828d4e2 | |||
| 1cf780a585 | |||
| fb92fe01e9 | |||
| e11684d4be | |||
| f807f44e12 | |||
| 4cd1913079 | |||
| 640cccc928 | |||
| fa97460877 | |||
| 340e6c7de0 | |||
| d3e1149cec | |||
| e865dc48bd | |||
| b184b5f3c4 | |||
| 2cef524ccf | |||
| d115e63c08 | |||
| 7d31390cdd | |||
| 7b851001f0 | |||
| 66c119ad66 | |||
| c40efd2061 | |||
| 654a1a8ec8 | |||
| 8f3a0ad1b2 | |||
| 7415df53d7 | |||
| 8c82f948b4 | |||
| b334dc46a7 | |||
| 9dd7dbd2b2 | |||
| a83b101ed7 | |||
| 516233c434 | |||
| 854713a461 | |||
| d7b9174f32 | |||
| 5b1d68eb06 | |||
| bbb4d5cc6c | |||
| 6e6f5904d9 | |||
| 85cbf2a235 | |||
| 6d86faa62f | |||
| 798aaadb7e | |||
| 8f3b525563 | |||
| 497b46f113 | |||
| 08c1f8a015 | |||
| b610a1e128 | |||
| c8069b1ce3 | |||
| e3c29d70e9 | |||
| 9831c0c72d | |||
| 693306c03c | |||
| f156f57e2b | |||
| 15d14198fe | |||
| 1ae7f910ca | |||
| ebbfdface7 | |||
| af9d74e746 | |||
| 8caf5afa97 | |||
| 7025d3c013 | |||
| 48d5316085 | |||
| 020e3500da | |||
| 2bf1df13b4 | |||
| 9fa59c4b1a | |||
| 4a09f8530a | |||
| 0cee7b1e7f | |||
| 44b5c9717a | |||
| afdfa5c807 | |||
| e2c7748342 | |||
| cb63b1391e | |||
| b844bc4e3f | |||
| 51dadaa0b6 | |||
| acfb81b132 | |||
| 5a65aa00eb | |||
| c71554b18c | |||
| 9aa3597c2e | |||
| 21708dac75 | |||
| 872a242794 | |||
| 22c6a3e0b6 | |||
| 22c73baa5c | |||
| 093ac084bb | |||
| d77af88480 | |||
| 8507631cd2 | |||
| dbf58d0163 | |||
| b98cc225fc | |||
| f781727aaf | |||
| 2182ac2409 | |||
| 64ebc8e4a5 | |||
| c90d8ed859 | |||
| ca6878c50a | |||
| 8a90d2622d | |||
| d44b84094d | |||
| 33f7b01c69 | |||
| c8ffb6d696 | |||
| c72f5faf32 | |||
| 1c433f3737 | |||
| 405a71a7f6 | |||
| 19add69376 | |||
| fb8cb4f452 | |||
| 1833c2044e | |||
| 50a201a490 | |||
| 86bb2d8e8d | |||
| faaa6fdcac | |||
| 1fd612e530 | |||
| 4488e31a2b | |||
| b6758fc3c2 | |||
| 4555315496 | |||
| d9e0c2ef51 | |||
| 1b0a4f1975 | |||
| 7f70e32995 | |||
| 93ad88e77d | |||
| 477815e964 | |||
| 8108148f24 | |||
| e325ec7a36 | |||
| ab2dc091f7 | |||
| d9706846de | |||
| 01949362ce | |||
| ac1db3ff9a | |||
| f41357bee5 | |||
| 3948d20261 | |||
| b465d5ed84 | |||
| 013f844804 | |||
| 985573ef63 | |||
| f14c1e34ff | |||
| ef9d2e0bac | |||
| e1ba9645b9 | |||
| dc0823b4ef | |||
| cb69f655ce | |||
| e795e5866c | |||
| c7af2566df | |||
| 7c5b171d42 | |||
| c4b1d653d0 | |||
| 6d22552a14 | |||
| 484e3faf16 | |||
| d846d73b26 | |||
| 5de05412ba | |||
| 66014e6bb7 | |||
| a3a65002bb | |||
| d869f1000d | |||
| 3d491b5a0f | |||
| 99d974af17 | |||
| 8a704e965f | |||
| 06dc2a8194 | |||
| d5122e7e9a | |||
| 0ada038d21 | |||
| b6e6aacf83 | |||
| da1bbfa443 | |||
| 03467246a4 | |||
| 1ba82dfb50 | |||
| db2ceb3aeb | |||
| f40c194e13 | |||
| 94c2fa74d6 | |||
| f702e8e07a | |||
| ddf5f7172c | |||
| 6a4c699ae0 | |||
| 99587499d3 | |||
| 2019c59cf8 | |||
| f94a688177 | |||
| 0d6b4a2a9a | |||
| 18145098cb | |||
| 40c7ae9a54 | |||
| 4793b25814 | |||
| 05e87fd592 | |||
| a06a938818 | |||
| 3f703cf5db | |||
| d4ca5ae6de | |||
| 37e77f6bb1 | |||
| 76a56af9f6 | |||
| 796e48c83c | |||
| 2727be2b05 | |||
| aff120670e |
@@ -1,73 +0,0 @@
|
|||||||
# Contao Meilisearch Bundle
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## ✨ Features
|
|
||||||
|
|
||||||
- Integration von **Meilisearch** als externe Suchmaschine
|
|
||||||
- Indexierung von:
|
|
||||||
- Contao-Seiten
|
|
||||||
- Inhaltselementen
|
|
||||||
- **PDF-Dateien**
|
|
||||||
- **Office-Dokumenten** (DOCX, XLSX, PPTX)
|
|
||||||
- Unterstützung für:
|
|
||||||
- Seiten-Prioritäten
|
|
||||||
- Keywords
|
|
||||||
- Vorschaubild
|
|
||||||
- Kompatibel mit:
|
|
||||||
- Contao **4.13**, **5.6** und **5.7**
|
|
||||||
- PHP **8.4**
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## ⏱️ Scheduled Indexing (Cron setup)
|
|
||||||
|
|
||||||
Das Bundle stellt eigene Commands zur Verfügung, um Dateien zu bereinigen und den Meilisearch-Index neu aufzubauen.
|
|
||||||
Für den produktiven Einsatz wird empfohlen, diese Commands regelmäßig per **System-Crontab** auszuführen.
|
|
||||||
|
|
||||||
Das Bundle nutzt **keinen eigenen Contao-Cron**, sondern System-Cronjobs.
|
|
||||||
|
|
||||||
## Verfügbare Commands
|
|
||||||
|
|
||||||
### Datei-Cleanup
|
|
||||||
|
|
||||||
```
|
|
||||||
/vendor/bin/contao-console meilisearch:files:cleanup
|
|
||||||
```
|
|
||||||
|
|
||||||
### Datei-Parsing
|
|
||||||
|
|
||||||
```
|
|
||||||
/vendor/bin/contao-console meilisearch:files:parse
|
|
||||||
```
|
|
||||||
|
|
||||||
### Meilisearch-Index
|
|
||||||
|
|
||||||
```
|
|
||||||
/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
|
|
||||||
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
|
|
||||||
|
|
||||||
```
|
|
||||||
>> var/logs/meilisearch_cron.log 2>&1
|
|
||||||
```
|
|
||||||
|
|
||||||
## Lizenz
|
|
||||||
|
|
||||||
MIT
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "mummert/contao-meilisearch-bundle",
|
"name": "mummert-media/contao-meilisearch-bundle",
|
||||||
"description": "Contao Meilisearch integration bundle",
|
"description": "Contao Meilisearch integration bundle",
|
||||||
"type": "contao-bundle",
|
"type": "contao-bundle",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -8,14 +8,18 @@
|
|||||||
"contao/core-bundle": "^4.13 || ^5.6 || ^5.7",
|
"contao/core-bundle": "^4.13 || ^5.6 || ^5.7",
|
||||||
"contao/calendar-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",
|
"contao/news-bundle": "^4.13 || ^5.6 || ^5.7",
|
||||||
"meilisearch/meilisearch-php": "^1.16"
|
"meilisearch/meilisearch-php": "^1.16",
|
||||||
|
"smalot/pdfparser": "^2.12",
|
||||||
|
"phpoffice/phpword": "^1.4",
|
||||||
|
"phpoffice/phpspreadsheet": "^3.0",
|
||||||
|
"phpoffice/phppresentation": "^1.2"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Mummert\\ContaoMeilisearchBundle\\": "src/"
|
"MummertMedia\\ContaoMeilisearchBundle\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"contao-manager-plugin": "Mummert\\ContaoMeilisearchBundle\\ContaoManager\\Plugin"
|
"contao-manager-plugin": "MummertMedia\\ContaoMeilisearchBundle\\ContaoManager\\Plugin"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
@@ -1,100 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle\Command;
|
|
||||||
|
|
||||||
use Contao\CoreBundle\Framework\ContaoFramework;
|
|
||||||
use Doctrine\DBAL\Connection;
|
|
||||||
use Symfony\Component\Console\Command\Command;
|
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
|
||||||
|
|
||||||
class MeilisearchFilesCleanupCommand extends Command
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
private readonly ContaoFramework $framework,
|
|
||||||
private readonly Connection $connection,
|
|
||||||
) {
|
|
||||||
parent::__construct();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function configure(): void
|
|
||||||
{
|
|
||||||
$this
|
|
||||||
->setName('meilisearch:files:cleanup')
|
|
||||||
->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
|
|
||||||
)
|
|
||||||
->addOption(
|
|
||||||
'dry-run',
|
|
||||||
null,
|
|
||||||
InputOption::VALUE_NONE,
|
|
||||||
'Show how many entries would be removed without deleting them'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
||||||
{
|
|
||||||
$this->framework->initialize();
|
|
||||||
|
|
||||||
$this->log('Cleaner gestartet');
|
|
||||||
|
|
||||||
try {
|
|
||||||
$grace = max(0, (int) $input->getOption('grace'));
|
|
||||||
$dryRun = (bool) $input->getOption('dry-run');
|
|
||||||
$cutoff = time() - $grace;
|
|
||||||
|
|
||||||
if ($dryRun) {
|
|
||||||
$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)',
|
|
||||||
$count,
|
|
||||||
date('Y-m-d H:i:s', $cutoff)
|
|
||||||
);
|
|
||||||
|
|
||||||
$output->writeln('<comment>' . $message . '</comment>');
|
|
||||||
$this->log($message);
|
|
||||||
|
|
||||||
$this->log('Cleaner stopped (dry-run)');
|
|
||||||
return Command::SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
$affected = $this->connection->executeStatement(
|
|
||||||
'DELETE FROM tl_search_files WHERE last_seen < ?',
|
|
||||||
[$cutoff]
|
|
||||||
);
|
|
||||||
|
|
||||||
$message = sprintf(
|
|
||||||
'Removed %d stale file(s) (last_seen < %s)',
|
|
||||||
$affected,
|
|
||||||
date('Y-m-d H:i:s', $cutoff)
|
|
||||||
);
|
|
||||||
|
|
||||||
$output->writeln('<info>' . $message . '</info>');
|
|
||||||
$this->log($message);
|
|
||||||
|
|
||||||
$this->log('Cleaner successfully stopped');
|
|
||||||
return Command::SUCCESS;
|
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
$this->log('Cleaner ERROR: ' . $e->getMessage());
|
|
||||||
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
|
||||||
|
|
||||||
return Command::FAILURE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function log(string $message): void
|
|
||||||
{
|
|
||||||
error_log(sprintf('[%s] %s', date('Y-m-d H:i:s'), $message));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,277 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Mummert\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;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
$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', [
|
|
||||||
'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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle\Command;
|
namespace MummertMedia\ContaoMeilisearchBundle\Command;
|
||||||
|
|
||||||
use Mummert\ContaoMeilisearchBundle\Service\MeilisearchIndexService;
|
use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchIndexService;
|
||||||
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\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
@@ -24,34 +24,12 @@ class MeilisearchIndexCommand extends Command
|
|||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
$this->log('Meilisearch index gestartet');
|
|
||||||
$output->writeln('<info>Meilisearch index started</info>');
|
$output->writeln('<info>Meilisearch index started</info>');
|
||||||
|
|
||||||
try {
|
$this->indexService->run();
|
||||||
$this->indexService->run();
|
|
||||||
|
|
||||||
$this->log('Meilisearch index successfully stopped');
|
$output->writeln('<info>Meilisearch index finished</info>');
|
||||||
$output->writeln('<info>Meilisearch index finished</info>');
|
|
||||||
|
|
||||||
return Command::SUCCESS;
|
return Command::SUCCESS;
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
|
||||||
$this->log('Meilisearch index ERROR: ' . $e->getMessage());
|
|
||||||
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
|
||||||
|
|
||||||
return Command::FAILURE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Einheitliches Logging mit Zeitstempel
|
|
||||||
*/
|
|
||||||
private function log(string $message): void
|
|
||||||
{
|
|
||||||
error_log(sprintf(
|
|
||||||
'[%s] %s',
|
|
||||||
date('Y-m-d H:i:s'),
|
|
||||||
$message
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle\ContaoManager;
|
namespace MummertMedia\ContaoMeilisearchBundle\ContaoManager;
|
||||||
|
|
||||||
use Contao\CalendarBundle\ContaoCalendarBundle;
|
use Contao\CalendarBundle\ContaoCalendarBundle;
|
||||||
use Contao\CoreBundle\ContaoCoreBundle;
|
use Contao\CoreBundle\ContaoCoreBundle;
|
||||||
@@ -8,7 +8,7 @@ use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
|
|||||||
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
|
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
|
||||||
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
|
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
|
||||||
use Contao\NewsBundle\ContaoNewsBundle;
|
use Contao\NewsBundle\ContaoNewsBundle;
|
||||||
use Mummert\ContaoMeilisearchBundle\ContaoMeilisearchBundle;
|
use MummertMedia\ContaoMeilisearchBundle\ContaoMeilisearchBundle;
|
||||||
|
|
||||||
class Plugin implements BundlePluginInterface
|
class Plugin implements BundlePluginInterface
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle;
|
namespace MummertMedia\ContaoMeilisearchBundle;
|
||||||
|
|
||||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle\Controller\FrontendModule;
|
namespace MummertMedia\ContaoMeilisearchBundle\Controller\FrontendModule;
|
||||||
|
|
||||||
use Contao\Config;
|
use Contao\Config;
|
||||||
use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
|
use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
namespace MummertMedia\ContaoMeilisearchBundle\Cron;
|
||||||
|
|
||||||
|
use Contao\CoreBundle\Framework\ContaoFramework;
|
||||||
|
use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchIndexService;
|
||||||
|
|
||||||
|
class MeilisearchIndexCron
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly MeilisearchIndexService $indexService,
|
||||||
|
private readonly ContaoFramework $framework,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function __invoke(): void
|
||||||
|
{
|
||||||
|
// Contao initialisieren (wichtig!)
|
||||||
|
$this->framework->initialize();
|
||||||
|
|
||||||
|
// einmal täglich indexieren
|
||||||
|
$this->indexService->run();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle\DependencyInjection;
|
namespace MummertMedia\ContaoMeilisearchBundle\DependencyInjection;
|
||||||
|
|
||||||
use Symfony\Component\Config\FileLocator;
|
use Symfony\Component\Config\FileLocator;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
|||||||
@@ -1,20 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle\EventListener;
|
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||||
|
|
||||||
use Contao\Config;
|
use Contao\Config;
|
||||||
use Contao\System;
|
use MummertMedia\ContaoMeilisearchBundle\Service\PdfIndexService;
|
||||||
use Mummert\ContaoMeilisearchBundle\Service\MeilisearchFileHelper;
|
use MummertMedia\ContaoMeilisearchBundle\Service\OfficeIndexService;
|
||||||
|
|
||||||
class IndexPageListener
|
class IndexPageListener
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly MeilisearchFileHelper $fileHelper,
|
private readonly PdfIndexService $pdfIndexService,
|
||||||
) {
|
private readonly OfficeIndexService $officeIndexService,
|
||||||
}
|
) {}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -28,6 +30,22 @@ class IndexPageListener
|
|||||||
'set_keys' => array_keys($set),
|
'set_keys' => array_keys($set),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* =====================
|
||||||
|
* PDF: Reset genau 1× pro Crawl
|
||||||
|
* =====================
|
||||||
|
*/
|
||||||
|
try {
|
||||||
|
$this->debug('PDF resetTableOnce(): call');
|
||||||
|
$this->pdfIndexService->resetTableOnce();
|
||||||
|
$this->debug('PDF resetTableOnce(): ok');
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$this->debug('PDF resetTableOnce(): failed', [
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
'class' => $e::class,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* =====================
|
* =====================
|
||||||
* SEITEN-METADATEN
|
* SEITEN-METADATEN
|
||||||
@@ -76,6 +94,8 @@ 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) === '') {
|
||||||
@@ -90,71 +110,143 @@ class IndexPageListener
|
|||||||
$set['keywords'] = implode(' ', array_unique($keywords));
|
$set['keywords'] = implode(' ', array_unique($keywords));
|
||||||
}
|
}
|
||||||
|
|
||||||
// IMAGEPATH
|
$this->debug('Meta: keywords result', [
|
||||||
if (!empty($parsed['page']['searchimage'] ?? null)) {
|
'keywords' => $set['keywords'] ?? 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)) {
|
||||||
|
// >>> HINWEIS: falls dein tl_search-Feld "image" heißt, hier auf $set['image'] ändern!
|
||||||
|
$set['imagepath'] = trim((string) $searchImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
// STARTDATE
|
// STARTDATE
|
||||||
if (is_numeric($parsed['event']['startDate'] ?? null)) {
|
$startDate =
|
||||||
$set['startDate'] = (int) $parsed['event']['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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// CHECKSUM
|
// CHECKSUM
|
||||||
$checksumSeed = (string) ($data['checksum'] ?? '');
|
try {
|
||||||
$checksumSeed .= '|' . ($set['keywords'] ?? '');
|
$checksumSeed = (string) ($data['checksum'] ?? '');
|
||||||
$checksumSeed .= '|' . ($set['priority'] ?? '');
|
$checksumSeed .= '|' . ($set['keywords'] ?? '');
|
||||||
$checksumSeed .= '|' . ($set['imagepath'] ?? '');
|
$checksumSeed .= '|' . ($set['priority'] ?? '');
|
||||||
$checksumSeed .= '|' . ($set['startDate'] ?? '');
|
$checksumSeed .= '|' . ($set['imagepath'] ?? '');
|
||||||
|
$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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$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-ERKENNUNG (NUR ERKENNUNG!)
|
* DATEI-INDEXIERUNG (PDF / OFFICE)
|
||||||
* =====================
|
* =====================
|
||||||
*/
|
*/
|
||||||
if ((int) ($data['protected'] ?? 0) !== 0) {
|
if ((int) ($data['protected'] ?? 0) !== 0) {
|
||||||
|
$this->debug('Abort: protected page', ['protected' => $data['protected'] ?? null]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Config::get('meilisearch_index_files')) {
|
$indexPdfs = (bool) Config::get('meilisearch_index_pdfs');
|
||||||
|
$indexOffice = (bool) Config::get('meilisearch_index_office');
|
||||||
|
|
||||||
|
$this->debug('File indexing settings', [
|
||||||
|
'meilisearch_index_pdfs' => $indexPdfs,
|
||||||
|
'meilisearch_index_office' => $indexOffice,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!$indexPdfs && !$indexOffice) {
|
||||||
|
$this->debug('Abort: file indexing disabled');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$links = $this->findAllLinks($content);
|
$links = $this->findAllLinks($content);
|
||||||
$fileLinks = [];
|
$this->debug('Links found', ['count' => count($links)]);
|
||||||
|
|
||||||
|
$pdfLinks = [];
|
||||||
|
$officeLinks = [];
|
||||||
|
|
||||||
foreach ($links as $link) {
|
foreach ($links as $link) {
|
||||||
$type = $this->detectIndexableFileType($link['url']);
|
$type = $this->detectIndexableFileType($link['url']);
|
||||||
if ($type !== null) {
|
|
||||||
$fileLinks[] = $link + ['type' => $type];
|
if ($type === 'pdf' && $indexPdfs) {
|
||||||
|
$pdfLinks[] = $link;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in_array($type, ['docx', 'xlsx', 'pptx'], true) && $indexOffice) {
|
||||||
|
$officeLinks[] = $link;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->debug('Indexable file links found', [
|
$this->debug('Indexable file links', [
|
||||||
'count' => count($fileLinks),
|
'pdf' => count($pdfLinks),
|
||||||
|
'office' => count($officeLinks),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($fileLinks) {
|
try {
|
||||||
foreach ($fileLinks as $file) {
|
if ($pdfLinks !== []) {
|
||||||
$this->fileHelper->collect(
|
$this->debug('PDF handlePdfLinks(): call', ['count' => count($pdfLinks)]);
|
||||||
$file['url'],
|
$this->pdfIndexService->handlePdfLinks($pdfLinks);
|
||||||
$file['type'],
|
$this->debug('PDF handlePdfLinks(): ok');
|
||||||
(int) ($data['pid'] ?? 0)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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('Hook end', [
|
$this->debug('Hook end', [
|
||||||
'final_set_keys' => array_keys($set),
|
'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,
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* === Hilfsmethoden unverändert === */
|
/**
|
||||||
|
* Extrahiert MEILISEARCH_JSON aus HTML-Kommentar
|
||||||
|
*/
|
||||||
private function extractMeilisearchJson(string $content): ?array
|
private function extractMeilisearchJson(string $content): ?array
|
||||||
{
|
{
|
||||||
if (!preg_match('/<!--\s*MEILISEARCH_JSON\s*(\{.*?\})\s*-->/s', $content, $m)) {
|
if (!preg_match('/<!--\s*MEILISEARCH_JSON\s*(\{.*?\})\s*-->/s', $content, $m)) {
|
||||||
@@ -169,6 +261,9 @@ class IndexPageListener
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sammle alle <a href="…"> Links
|
||||||
|
*/
|
||||||
private function findAllLinks(string $content): array
|
private function findAllLinks(string $content): array
|
||||||
{
|
{
|
||||||
if (!preg_match_all(
|
if (!preg_match_all(
|
||||||
@@ -191,11 +286,20 @@ class IndexPageListener
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ermittelt indexierbaren Dateityp (pdf|docx|xlsx|pptx) oder null
|
||||||
|
*/
|
||||||
private function detectIndexableFileType(string $url): ?string
|
private function detectIndexableFileType(string $url): ?string
|
||||||
{
|
{
|
||||||
|
// Hash entfernen
|
||||||
$url = strtok($url, '#');
|
$url = strtok($url, '#');
|
||||||
$parts = parse_url($url);
|
|
||||||
|
|
||||||
|
$parts = parse_url($url);
|
||||||
|
if (!$parts) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// direkter Pfad (/files/…)
|
||||||
if (!empty($parts['path'])) {
|
if (!empty($parts['path'])) {
|
||||||
$ext = strtolower(pathinfo($parts['path'], PATHINFO_EXTENSION));
|
$ext = strtolower(pathinfo($parts['path'], PATHINFO_EXTENSION));
|
||||||
if (in_array($ext, ['pdf', 'docx', 'xlsx', 'pptx'], true)) {
|
if (in_array($ext, ['pdf', 'docx', 'xlsx', 'pptx'], true)) {
|
||||||
@@ -203,12 +307,18 @@ class IndexPageListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Query-Parameter (Contao 4 + 5)
|
||||||
if (!empty($parts['query'])) {
|
if (!empty($parts['query'])) {
|
||||||
parse_str($parts['query'], $query);
|
parse_str($parts['query'], $query);
|
||||||
|
|
||||||
foreach (['file', 'p', 'f'] as $param) {
|
foreach (['file', 'p', 'f'] as $param) {
|
||||||
if (!empty($query[$param])) {
|
if (!empty($query[$param])) {
|
||||||
$candidate = rawurldecode(html_entity_decode((string) $query[$param], ENT_QUOTES));
|
$candidate = (string) $query[$param];
|
||||||
|
|
||||||
|
// sicher decodieren (Contao 4 + 5)
|
||||||
|
$candidate = html_entity_decode($candidate, ENT_QUOTES);
|
||||||
|
$candidate = rawurldecode($candidate);
|
||||||
|
|
||||||
$ext = strtolower(pathinfo($candidate, PATHINFO_EXTENSION));
|
$ext = strtolower(pathinfo($candidate, PATHINFO_EXTENSION));
|
||||||
|
|
||||||
if (in_array($ext, ['pdf', 'docx', 'xlsx', 'pptx'], true)) {
|
if (in_array($ext, ['pdf', 'docx', 'xlsx', 'pptx'], true)) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle\EventListener;
|
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
|
||||||
|
|
||||||
use Contao\CalendarEventsModel;
|
use Contao\CalendarEventsModel;
|
||||||
use Contao\Config;
|
use Contao\Config;
|
||||||
@@ -16,13 +16,6 @@ class MeilisearchPageMarkerListener
|
|||||||
return $buffer;
|
return $buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ⛔ Marker bereits vorhanden → nichts mehr tun
|
|
||||||
if (str_contains($buffer, '⟦MEILISEARCH_META⟧')
|
|
||||||
|| str_contains($buffer, 'MEILISEARCH_JSON')
|
|
||||||
) {
|
|
||||||
return $buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -2,24 +2,24 @@ services:
|
|||||||
# Alias MUSS vorhanden sein (richtig platziert)
|
# Alias MUSS vorhanden sein (richtig platziert)
|
||||||
Psr\Container\ContainerInterface: '@service_container'
|
Psr\Container\ContainerInterface: '@service_container'
|
||||||
|
|
||||||
Mummert\ContaoMeilisearchBundle\:
|
MummertMedia\ContaoMeilisearchBundle\:
|
||||||
resource: '../../{Command,EventListener,Service}'
|
resource: '../../{Command,Cron,EventListener,Service}'
|
||||||
autowire: true
|
autowire: true
|
||||||
autoconfigure: true
|
autoconfigure: true
|
||||||
|
|
||||||
Mummert\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener:
|
MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener:
|
||||||
autowire: true
|
|
||||||
autoconfigure: false
|
|
||||||
tags:
|
|
||||||
- { name: contao.hook, hook: outputFrontendTemplate, method: onOutputFrontendTemplate }
|
|
||||||
|
|
||||||
Mummert\ContaoMeilisearchBundle\EventListener\IndexPageListener:
|
|
||||||
autowire: true
|
autowire: true
|
||||||
autoconfigure: false
|
autoconfigure: false
|
||||||
tags:
|
tags:
|
||||||
- { name: contao.hook, hook: indexPage, method: onIndexPage }
|
- { name: contao.hook, hook: indexPage, method: onIndexPage }
|
||||||
|
|
||||||
Mummert\ContaoMeilisearchBundle\Controller\FrontendModule\MeilisearchSearchController:
|
MummertMedia\ContaoMeilisearchBundle\Cron\MeilisearchIndexCron:
|
||||||
|
autowire: true
|
||||||
|
autoconfigure: false
|
||||||
|
tags:
|
||||||
|
- { name: contao.cron, interval: daily, method: __invoke }
|
||||||
|
|
||||||
|
MummertMedia\ContaoMeilisearchBundle\Controller\FrontendModule\MeilisearchSearchController:
|
||||||
autowire: true
|
autowire: true
|
||||||
autoconfigure: false
|
autoconfigure: false
|
||||||
tags:
|
tags:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Mummert\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener;
|
use MummertMedia\ContaoMeilisearchBundle\EventListener\MeilisearchPageMarkerListener;
|
||||||
|
|
||||||
|
|
||||||
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
|
$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [
|
||||||
|
|||||||
@@ -2,18 +2,16 @@
|
|||||||
|
|
||||||
use Contao\DC_Table;
|
use Contao\DC_Table;
|
||||||
|
|
||||||
$GLOBALS['TL_DCA']['tl_search_files'] = [
|
$GLOBALS['TL_DCA']['tl_search_pdf'] = [
|
||||||
'config' => [
|
'config' => [
|
||||||
'dataContainer' => DC_Table::class,
|
'dataContainer' => DC_Table::class,
|
||||||
'sql' => [
|
'sql' => [
|
||||||
'keys' => [
|
'keys' => [
|
||||||
'id' => 'primary',
|
'id' => 'primary',
|
||||||
'page_id' => 'index',
|
'checksum' => 'unique',
|
||||||
'url' => 'unique',
|
'page_id' => 'index',
|
||||||
'type' => 'index',
|
'url' => 'index',
|
||||||
'checksum' => 'index',
|
'type' => 'index', // ⬅️ NEU
|
||||||
'uuid' => 'index',
|
|
||||||
'last_seen' => 'index',
|
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
@@ -27,18 +25,10 @@ $GLOBALS['TL_DCA']['tl_search_files'] = [
|
|||||||
'sql' => "int(10) unsigned NOT NULL default 0",
|
'sql' => "int(10) unsigned NOT NULL default 0",
|
||||||
],
|
],
|
||||||
|
|
||||||
/*
|
|
||||||
* Zeitpunkt, wann die Datei zuletzt beim Crawl gesehen wurde
|
|
||||||
* → Basis für Cleanup
|
|
||||||
*/
|
|
||||||
'last_seen' => [ // ⬅️ NEU
|
|
||||||
'sql' => "int(10) unsigned NOT NULL default 0",
|
|
||||||
],
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Dateityp: pdf | docx | xlsx | pptx
|
* Dateityp: pdf | docx | xlsx | pptx
|
||||||
*/
|
*/
|
||||||
'type' => [
|
'type' => [ // ⬅️ NEU
|
||||||
'sql' => "varchar(16) NOT NULL default 'pdf'",
|
'sql' => "varchar(16) NOT NULL default 'pdf'",
|
||||||
],
|
],
|
||||||
|
|
||||||
@@ -64,10 +54,6 @@ $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
|
||||||
@@ -78,7 +64,7 @@ $GLOBALS['TL_DCA']['tl_search_files'] = [
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Herkunftsseite (tl_page.id)
|
* Herkunftsseite (tl_page.id)
|
||||||
* → optional, Debug / Referenz
|
* → Cleanup / Referenz
|
||||||
*/
|
*/
|
||||||
'page_id' => [
|
'page_id' => [
|
||||||
'sql' => "int(10) unsigned NOT NULL default 0",
|
'sql' => "int(10) unsigned NOT NULL default 0",
|
||||||
@@ -4,17 +4,14 @@ use Contao\CoreBundle\DataContainer\PaletteManipulator;
|
|||||||
use Contao\System;
|
use Contao\System;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* -------------------------------------------------
|
|
||||||
* Fields
|
* Fields
|
||||||
* -------------------------------------------------
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_host'] = [
|
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_host'] = [
|
||||||
'inputType' => 'text',
|
'inputType' => 'text',
|
||||||
'eval' => [
|
'eval' => [
|
||||||
'mandatory' => true,
|
'mandatory' => true,
|
||||||
'rgxp' => 'url',
|
'rgxp' => 'url',
|
||||||
'tl_class' => 'w50',
|
'tl_class' => 'w50',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -22,7 +19,7 @@ $GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index'] = [
|
|||||||
'inputType' => 'text',
|
'inputType' => 'text',
|
||||||
'eval' => [
|
'eval' => [
|
||||||
'mandatory' => true,
|
'mandatory' => true,
|
||||||
'tl_class' => 'w50',
|
'tl_class' => 'w50',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -30,7 +27,7 @@ $GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_api_write'] = [
|
|||||||
'inputType' => 'text',
|
'inputType' => 'text',
|
||||||
'eval' => [
|
'eval' => [
|
||||||
'mandatory' => true,
|
'mandatory' => true,
|
||||||
'tl_class' => 'w50',
|
'tl_class' => 'w50',
|
||||||
'hideInput' => true,
|
'hideInput' => true,
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
@@ -39,7 +36,7 @@ $GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_api_search'] = [
|
|||||||
'inputType' => 'text',
|
'inputType' => 'text',
|
||||||
'eval' => [
|
'eval' => [
|
||||||
'mandatory' => true,
|
'mandatory' => true,
|
||||||
'tl_class' => 'w50',
|
'tl_class' => 'w50',
|
||||||
'hideInput' => true,
|
'hideInput' => true,
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
@@ -58,71 +55,50 @@ $GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_imagesize'] = [
|
|||||||
return $options;
|
return $options;
|
||||||
},
|
},
|
||||||
'eval' => [
|
'eval' => [
|
||||||
'tl_class' => 'w50',
|
'tl_class' => 'w50',
|
||||||
'chosen' => true,
|
'chosen' => true,
|
||||||
'includeBlankOption' => true,
|
'includeBlankOption' => true,
|
||||||
],
|
],
|
||||||
|
// 🔥 DAS HAT GEFEHLT
|
||||||
'sql' => "int(10) unsigned NOT NULL default 0",
|
'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'] = [
|
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_fallback_image'] = [
|
||||||
'inputType' => 'fileTree',
|
'inputType' => 'fileTree',
|
||||||
'eval' => [
|
'eval' => [
|
||||||
'filesOnly' => true,
|
'filesOnly' => true,
|
||||||
'fieldType' => 'radio',
|
'fieldType' => 'radio',
|
||||||
'tl_class' => 'w50',
|
'tl_class' => 'w50',
|
||||||
],
|
],
|
||||||
'sql' => "varbinary(16) NULL",
|
'sql' => "varbinary(16) NULL",
|
||||||
];
|
];
|
||||||
|
|
||||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index_past_events'] = [
|
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index_pdfs'] = [
|
||||||
|
'label' => &$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_pdfs'],
|
||||||
'inputType' => 'checkbox',
|
'inputType' => 'checkbox',
|
||||||
'eval' => [
|
'eval' => [
|
||||||
'tl_class' => 'w50 clr',
|
'tl_class' => 'w50',
|
||||||
],
|
],
|
||||||
|
'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'],
|
||||||
* Datei-Indexierung (Tika)
|
|
||||||
* -------------------------------------------------
|
|
||||||
*/
|
|
||||||
|
|
||||||
$GLOBALS['TL_DCA']['tl_settings']['fields']['meilisearch_index_files'] = [
|
|
||||||
'inputType' => 'checkbox',
|
'inputType' => 'checkbox',
|
||||||
'eval' => [
|
'eval' => ['tl_class' => 'w50'],
|
||||||
'tl_class' => 'w50',
|
'sql' => "char(1) NOT NULL default '0'",
|
||||||
'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
|
* Palette
|
||||||
* -------------------------------------------------
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PaletteManipulator::create()
|
PaletteManipulator::create()
|
||||||
->addLegend('meilisearch_legend', null, PaletteManipulator::POSITION_AFTER, true)
|
->addLegend('meilisearch_legend', null, PaletteManipulator::POSITION_AFTER, true)
|
||||||
->addField('meilisearch_host', 'meilisearch_legend')
|
->addField('meilisearch_host', 'meilisearch_legend')
|
||||||
@@ -132,5 +108,6 @@ PaletteManipulator::create()
|
|||||||
->addField('meilisearch_imagesize', 'meilisearch_legend')
|
->addField('meilisearch_imagesize', 'meilisearch_legend')
|
||||||
->addField('meilisearch_fallback_image', 'meilisearch_legend')
|
->addField('meilisearch_fallback_image', 'meilisearch_legend')
|
||||||
->addField('meilisearch_index_past_events', 'meilisearch_legend')
|
->addField('meilisearch_index_past_events', 'meilisearch_legend')
|
||||||
->addField('meilisearch_index_files', 'meilisearch_legend')
|
->addField('meilisearch_index_pdfs', 'meilisearch_legend')
|
||||||
|
->addField('meilisearch_index_office', 'meilisearch_legend')
|
||||||
->applyToPalette('default', 'tl_settings');
|
->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]
|
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_past_events'][1]
|
||||||
= 'Vergangene Kalender-Events werden ebenfalls in Meilisearch indexiert.';
|
= 'Vergangene Kalender-Events werden ebenfalls in Meilisearch indexiert.';
|
||||||
|
|
||||||
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_files'] = [
|
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_pdfs'] = [
|
||||||
'Dateien indexieren',
|
'PDFs indexieren',
|
||||||
'Aktiviert die Indexierung von PDF-Dateien sowie DOCX, XLSX und PPTX.',
|
'Aktiviert die Indexierung von PDF-Dateien für die Suche.',
|
||||||
];
|
];
|
||||||
|
|
||||||
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_tika_url']
|
$GLOBALS['TL_LANG']['tl_settings']['meilisearch_index_office']
|
||||||
= ['Apache Tika URL', 'URL der Apache Tika Instanz (z. B. https://tika.domain.tld).'];
|
= ['Office-Dateien indexieren', 'DOCX, XLSX und PPTX in die Suche aufnehmen.'];
|
||||||
@@ -4,7 +4,6 @@ Contao 5 – Frontend Module Template
|
|||||||
#}
|
#}
|
||||||
|
|
||||||
<!-- indexer::stop -->
|
<!-- indexer::stop -->
|
||||||
{% block meilisearch %}
|
|
||||||
<div
|
<div
|
||||||
id="topsearch"
|
id="topsearch"
|
||||||
class="meilisearch-search"
|
class="meilisearch-search"
|
||||||
@@ -60,44 +59,10 @@ Contao 5 – Frontend Module Template
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script type="module">
|
||||||
(function () {
|
import MeiliSearch from 'https://cdn.jsdelivr.net/npm/meilisearch@latest/dist/bundles/meilisearch.esm.js';
|
||||||
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'
|
|
||||||
];
|
|
||||||
|
|
||||||
function loadClient(urls, onDone) {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
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;
|
||||||
@@ -130,10 +95,6 @@ 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();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -255,19 +216,6 @@ 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,3 @@
|
|||||||
|
Time,Source,URI,"Found on URI","Found on level",Tags,Message
|
||||||
|
"2025-12-28 10:44:12.357228","Contao\CoreBundle\Crawl\Escargot\Subscriber\SearchIndexSubscriber",https://flowerpowerabi.de/,https://flowerpowerabi.de/sitemap.xml,3,,"Forwarded to the search indexer. Was indexed successfully."
|
||||||
|
"2025-12-28 10:44:12.413689","Contao\CoreBundle\Crawl\Escargot\Subscriber\SearchIndexSubscriber",https://flowerpowerabi.de/testseite-fuer-meilisearch,https://flowerpowerabi.de/sitemap.xml,3,,"Forwarded to the search indexer. Was indexed successfully."
|
||||||
|
|
Before Width: | Height: | Size: 1.7 KiB After Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 932 B After Width: | Height: | Size: 932 B |
|
Before Width: | Height: | Size: 937 B After Width: | Height: | Size: 937 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
@@ -1,259 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Mummert\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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle\Service;
|
namespace MummertMedia\ContaoMeilisearchBundle\Service;
|
||||||
|
|
||||||
use Contao\Config;
|
use Contao\Config;
|
||||||
use Contao\CoreBundle\Framework\ContaoFramework;
|
use Contao\CoreBundle\Framework\ContaoFramework;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Mummert\ContaoMeilisearchBundle\Service;
|
namespace MummertMedia\ContaoMeilisearchBundle\Service;
|
||||||
|
|
||||||
use Contao\Config;
|
use Contao\Config;
|
||||||
use Contao\CoreBundle\Framework\ContaoFramework;
|
use Contao\CoreBundle\Framework\ContaoFramework;
|
||||||
@@ -41,8 +41,8 @@ class MeilisearchIndexService
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$host = (string) Config::get('meilisearch_host');
|
$host = (string) Config::get('meilisearch_host');
|
||||||
$apiKey = (string) Config::get('meilisearch_api_write');
|
$apiKey = (string) Config::get('meilisearch_api_write');
|
||||||
$this->indexName = (string) Config::get('meilisearch_index');
|
$this->indexName = (string) Config::get('meilisearch_index');
|
||||||
|
|
||||||
if ($host === '' || $this->indexName === '') {
|
if ($host === '' || $this->indexName === '') {
|
||||||
@@ -72,7 +72,7 @@ class MeilisearchIndexService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->indexTlSearch($index);
|
$this->indexTlSearch($index);
|
||||||
$this->indexTlSearchFiles($index);
|
$this->indexTlSearchPdf($index);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function ensureIndexSettings(Indexes $index): void
|
private function ensureIndexSettings(Indexes $index): void
|
||||||
@@ -80,7 +80,6 @@ class MeilisearchIndexService
|
|||||||
$index->updateSettings([
|
$index->updateSettings([
|
||||||
'searchableAttributes' => ['title', 'keywords', 'text'],
|
'searchableAttributes' => ['title', 'keywords', 'text'],
|
||||||
'sortableAttributes' => ['priority', 'startDate'],
|
'sortableAttributes' => ['priority', 'startDate'],
|
||||||
'filterableAttributes' => ['type', 'filetype'],
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +132,7 @@ class MeilisearchIndexService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tl_search indexieren (Seiten / News / Events)
|
* tl_search indexieren
|
||||||
*/
|
*/
|
||||||
private function indexTlSearch(Indexes $index): void
|
private function indexTlSearch(Indexes $index): void
|
||||||
{
|
{
|
||||||
@@ -165,11 +164,13 @@ class MeilisearchIndexService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$cleanText = $this->stripMeilisearchMeta((string) $row['text']);
|
||||||
|
|
||||||
$doc = [
|
$doc = [
|
||||||
'id' => $type . '_' . $row['id'],
|
'id' => $type . '_' . $row['id'],
|
||||||
'type' => $type,
|
'type' => $type,
|
||||||
'title' => $row['title'],
|
'title' => $row['title'],
|
||||||
'text' => $this->stripMeilisearchMeta((string) $row['text']),
|
'text' => $cleanText,
|
||||||
'url' => $row['url'],
|
'url' => $row['url'],
|
||||||
'protected' => (bool) $row['protected'],
|
'protected' => (bool) $row['protected'],
|
||||||
'checksum' => $row['checksum'],
|
'checksum' => $row['checksum'],
|
||||||
@@ -191,24 +192,31 @@ class MeilisearchIndexService
|
|||||||
$documents[] = $doc;
|
$documents[] = $doc;
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
error_log('[ContaoMeilisearch] Failed to build tl_search document: ' . $e->getMessage());
|
error_log(
|
||||||
|
'[ContaoMeilisearch] Failed to build document for tl_search ID '
|
||||||
|
. ($row['id'] ?? '?') . ': ' . $e->getMessage()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($documents !== []) {
|
if ($documents !== []) {
|
||||||
$index->addDocuments($documents);
|
try {
|
||||||
|
$index->addDocuments($documents);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
error_log('[ContaoMeilisearch] Failed to add tl_search documents: ' . $e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tl_search_files indexieren (PDF / Office)
|
* tl_search_pdf indexieren
|
||||||
*/
|
*/
|
||||||
private function indexTlSearchFiles(Indexes $index): void
|
private function indexTlSearchPdf(Indexes $index): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search_files');
|
$rows = $this->connection->fetchAllAssociative('SELECT * FROM tl_search_pdf');
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
error_log('[ContaoMeilisearch] Failed to read tl_search_files: ' . $e->getMessage());
|
error_log('[ContaoMeilisearch] Failed to read tl_search_pdf: ' . $e->getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,24 +233,30 @@ class MeilisearchIndexService
|
|||||||
: 'pdf';
|
: 'pdf';
|
||||||
|
|
||||||
$documents[] = [
|
$documents[] = [
|
||||||
'id' => 'file_' . $row['id'],
|
'id' => $fileType . '_' . $row['id'],
|
||||||
'type' => 'file',
|
'type' => $fileType,
|
||||||
'filetype' => $fileType,
|
'title' => $row['title'],
|
||||||
'title' => $row['title'] ?: basename($row['url']),
|
'text' => $this->stripMeilisearchMeta((string) $row['text']),
|
||||||
'text' => (string) $row['text'],
|
'url' => $row['url'],
|
||||||
'url' => $row['url'],
|
'checksum' => $row['checksum'],
|
||||||
'checksum' => $row['checksum'],
|
'poster' => self::FILETYPE_ICON_MAP[$fileType]
|
||||||
'poster' => self::FILETYPE_ICON_MAP[$fileType]
|
|
||||||
?? self::FILETYPE_ICON_MAP['pdf'],
|
?? self::FILETYPE_ICON_MAP['pdf'],
|
||||||
];
|
];
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
error_log('[ContaoMeilisearch] Failed to build file document: ' . $e->getMessage());
|
error_log(
|
||||||
|
'[ContaoMeilisearch] Failed to build PDF document for ID '
|
||||||
|
. ($row['id'] ?? '?') . ': ' . $e->getMessage()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($documents !== []) {
|
if ($documents !== []) {
|
||||||
$index->addDocuments($documents);
|
try {
|
||||||
|
$index->addDocuments($documents);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
error_log('[ContaoMeilisearch] Failed to add tl_search_pdf documents: ' . $e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,273 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
// pro Crawl-Durchlauf: doppelte Verarbeitung vermeiden
|
||||||
|
private array $seenThisCrawl = [];
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
foreach ($officeLinks as $row) {
|
||||||
|
$url = (string) ($row['url'] ?? '');
|
||||||
|
$linkText = $row['linkText'] ?? null;
|
||||||
|
|
||||||
|
if ($url === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// innerhalb des Crawls gleiche URL nicht mehrfach parsen
|
||||||
|
$seenKey = md5($url);
|
||||||
|
if (isset($this->seenThisCrawl[$seenKey])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->seenThisCrawl[$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);
|
||||||
|
|
||||||
|
$title = $linkText ?: basename($absolutePath);
|
||||||
|
|
||||||
|
$text = $this->parseOfficeFile($absolutePath, $type);
|
||||||
|
if ($text === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->upsertOffice(
|
||||||
|
$relativePath,
|
||||||
|
$title,
|
||||||
|
$text,
|
||||||
|
$checksum,
|
||||||
|
$mtime,
|
||||||
|
$type
|
||||||
|
);
|
||||||
|
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
error_log(
|
||||||
|
'[ContaoMeilisearch] Office indexing failed for "' . $url . '": ' . $e->getMessage()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{string,string}|null [relativePath, type]
|
||||||
|
*/
|
||||||
|
private function normalizeOfficeUrl(string $url): ?array
|
||||||
|
{
|
||||||
|
$decoded = html_entity_decode($url);
|
||||||
|
$parts = parse_url($decoded);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
): void {
|
||||||
|
try {
|
||||||
|
Database::getInstance()
|
||||||
|
->prepare('
|
||||||
|
INSERT INTO tl_search_pdf
|
||||||
|
(tstamp, type, url, title, text, checksum, file_mtime)
|
||||||
|
VALUES
|
||||||
|
(?, ?, ?, ?, ?, ?, ?)
|
||||||
|
ON DUPLICATE KEY UPDATE
|
||||||
|
tstamp=VALUES(tstamp),
|
||||||
|
type=VALUES(type),
|
||||||
|
url=VALUES(url),
|
||||||
|
title=VALUES(title),
|
||||||
|
text=VALUES(text),
|
||||||
|
file_mtime=VALUES(file_mtime)
|
||||||
|
')
|
||||||
|
->execute(
|
||||||
|
time(),
|
||||||
|
$type,
|
||||||
|
$url,
|
||||||
|
$title,
|
||||||
|
$text,
|
||||||
|
$checksum,
|
||||||
|
$mtime
|
||||||
|
);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
error_log(
|
||||||
|
'[ContaoMeilisearch] Failed to write Office index entry (' . $url . '): ' . $e->getMessage()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 $e) {
|
||||||
|
error_log(
|
||||||
|
'[ContaoMeilisearch] Failed to parse DOCX "' . $absolutePath . '": ' . $e->getMessage()
|
||||||
|
);
|
||||||
|
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 $e) {
|
||||||
|
error_log(
|
||||||
|
'[ContaoMeilisearch] Failed to parse XLSX "' . $absolutePath . '": ' . $e->getMessage()
|
||||||
|
);
|
||||||
|
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 $e) {
|
||||||
|
error_log(
|
||||||
|
'[ContaoMeilisearch] Failed to parse PPTX "' . $absolutePath . '": ' . $e->getMessage()
|
||||||
|
);
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,224 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MummertMedia\ContaoMeilisearchBundle\Service;
|
||||||
|
|
||||||
|
use Contao\Database;
|
||||||
|
use Smalot\PdfParser\Parser;
|
||||||
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||||
|
|
||||||
|
class PdfIndexService
|
||||||
|
{
|
||||||
|
private string $projectDir;
|
||||||
|
|
||||||
|
private bool $didReset = false;
|
||||||
|
private array $seenThisCrawl = [];
|
||||||
|
|
||||||
|
public function __construct(ParameterBagInterface $params)
|
||||||
|
{
|
||||||
|
$this->projectDir = rtrim((string) $params->get('kernel.project_dir'), '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wird aus dem Listener beim ersten Hook-Call pro Crawl aufgerufen.
|
||||||
|
*/
|
||||||
|
public function resetTableOnce(): void
|
||||||
|
{
|
||||||
|
if ($this->didReset) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->didReset = true;
|
||||||
|
$this->seenThisCrawl = [];
|
||||||
|
|
||||||
|
Database::getInstance()->execute('TRUNCATE tl_search_pdf');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<int,array{url:string,linkText:?string}> $pdfLinks
|
||||||
|
*/
|
||||||
|
public function handlePdfLinks(array $pdfLinks): void
|
||||||
|
{
|
||||||
|
foreach ($pdfLinks as $row) {
|
||||||
|
$url = (string) ($row['url'] ?? '');
|
||||||
|
$linkText = $row['linkText'] ?? null;
|
||||||
|
|
||||||
|
if ($url === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// innerhalb eines Crawls doppelte URLs vermeiden
|
||||||
|
$seenKey = md5($url);
|
||||||
|
if (isset($this->seenThisCrawl[$seenKey])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->seenThisCrawl[$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);
|
||||||
|
|
||||||
|
// Titel-Priorität:
|
||||||
|
// 1) Linktext
|
||||||
|
// 2) PDF-Metadaten
|
||||||
|
// 3) Dateiname
|
||||||
|
$pdfMetaTitle = $this->readPdfMetaTitle($absolutePath);
|
||||||
|
$title = $linkText ?: ($pdfMetaTitle ?: basename($absolutePath));
|
||||||
|
|
||||||
|
$text = $this->parsePdf($absolutePath);
|
||||||
|
if ($text === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->upsertPdf(
|
||||||
|
$normalizedPath,
|
||||||
|
$title,
|
||||||
|
$text,
|
||||||
|
$checksum,
|
||||||
|
$mtime
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalizePdfUrl(string $url): ?string
|
||||||
|
{
|
||||||
|
$decoded = html_entity_decode($url);
|
||||||
|
$parts = parse_url($decoded);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
): void {
|
||||||
|
Database::getInstance()
|
||||||
|
->prepare('
|
||||||
|
INSERT INTO tl_search_pdf
|
||||||
|
(tstamp, url, title, text, checksum, file_mtime)
|
||||||
|
VALUES
|
||||||
|
(?, ?, ?, ?, ?, ?)
|
||||||
|
ON DUPLICATE KEY UPDATE
|
||||||
|
tstamp=VALUES(tstamp),
|
||||||
|
url=VALUES(url),
|
||||||
|
title=VALUES(title),
|
||||||
|
text=VALUES(text),
|
||||||
|
file_mtime=VALUES(file_mtime)
|
||||||
|
')
|
||||||
|
->execute(
|
||||||
|
time(),
|
||||||
|
$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) {
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||