diff --git a/src/EventListener/IndexPageListener.php b/src/EventListener/IndexPageListener.php index 2921abd..828b1d1 100644 --- a/src/EventListener/IndexPageListener.php +++ b/src/EventListener/IndexPageListener.php @@ -7,8 +7,12 @@ use MummertMedia\ContaoMeilisearchBundle\Service\PdfIndexService; class IndexPageListener { + public function __construct( + private PdfIndexService $pdfIndexService + ) {} public function onIndexPage(string $content, array &$data, array &$set): void { + $this->pdfIndexService->startCrawl(); // Marker vorhanden? if (!str_contains($content, 'MEILISEARCH_JSON')) { return; @@ -100,11 +104,9 @@ class IndexPageListener $pdfLinks = $this->findPdfLinks($content); if ($pdfLinks !== []) { + $this->pdfIndexService->startCrawl(); error_log('PDF gefunden'); - - /** @var PdfIndexService $service */ - $service = System::getContainer()->get(PdfIndexService::class); - $service->handlePdfLinks($pdfLinks); + $this->pdfIndexService->handlePdfLinks($pdfLinks); } } diff --git a/src/EventListener/PdfCleanupListener.php b/src/EventListener/PdfCleanupListener.php new file mode 100644 index 0000000..71aa7a6 --- /dev/null +++ b/src/EventListener/PdfCleanupListener.php @@ -0,0 +1,18 @@ +pdfIndexService->cleanupRemovedPdfs(); + } +} \ No newline at end of file diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 94ee6f7..639fedc 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -9,4 +9,8 @@ services: # IndexPageListener als Contao-Hook registrieren MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener: tags: - - { name: contao.hook, hook: indexPage, method: onIndexPage } \ No newline at end of file + - { name: contao.hook, hook: indexPage, method: onIndexPage } + + MummertMedia\ContaoMeilisearchBundle\EventListener\PdfCleanupListener: + tags: + - { name: kernel.event_listener, event: terminal42.escargot.last_chunk, method: onLastChunk } \ No newline at end of file diff --git a/src/Service/PdfIndexService.php b/src/Service/PdfIndexService.php index 9d6dd82..478a558 100644 --- a/src/Service/PdfIndexService.php +++ b/src/Service/PdfIndexService.php @@ -8,6 +8,29 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; class PdfIndexService { + + private int $crawlStart = 0; + + public function startCrawl(): void + { + if ($this->crawlStart === 0) { + $this->crawlStart = time(); + error_log('PDF Crawl Start: ' . $this->crawlStart); + } + } + + public function cleanupRemovedPdfs(): void + { + if ($this->crawlStart === 0) { + return; + } + + Database::getInstance() + ->prepare('DELETE FROM tl_search_pdf WHERE tstamp < ?') + ->execute($this->crawlStart); + + error_log('PDF Cleanup abgeschlossen'); + } private string $projectDir; public function __construct(ParameterBagInterface $params)