From d7da31d8bc58383b57054dec74b15249a2090b7f Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Wed, 20 Aug 2025 18:59:30 +0200 Subject: [PATCH 1/3] [WIP] Idea for solving applicationData is deprecated --- Classes/Middleware/CrawlerInitialization.php | 40 ++++++++--------- .../Middleware/CrawlerInitializationTest.php | 45 ++++++------------- 2 files changed, 33 insertions(+), 52 deletions(-) diff --git a/Classes/Middleware/CrawlerInitialization.php b/Classes/Middleware/CrawlerInitialization.php index dede87d38..181ec4603 100644 --- a/Classes/Middleware/CrawlerInitialization.php +++ b/Classes/Middleware/CrawlerInitialization.php @@ -18,6 +18,7 @@ * * The TYPO3 project - inspiring people to share! */ + use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; @@ -58,12 +59,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface return $handler->handle($request); } - $GLOBALS['TSFE']->applicationData['forceIndexing'] = true; - $GLOBALS['TSFE']->applicationData['tx_crawler']['running'] = true; - $GLOBALS['TSFE']->applicationData['tx_crawler']['parameters'] = $queueParameters; - $GLOBALS['TSFE']->applicationData['tx_crawler']['log'] = [ - 'User Groups: ' . ($queueParameters['feUserGroupList'] ?? ''), - ]; + $request = $request->withAttribute('tx_crawler', [ + 'forceIndexing' => true, + 'running' => true, + 'parameters' => $queueParameters, + 'log' => ['User Groups: ' . ($queueParameters['feUserGroupList'] ?? '')], + ]); // Execute the frontend request as is $response = $handler->handle($request); @@ -75,37 +76,36 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $noCache = $GLOBALS['TSFE']->no_cache; } - $GLOBALS['TSFE']->applicationData['tx_crawler']['vars'] = [ + $crawlerData = $request->getAttribute('tx_crawler', []); + $crawlerData['vars'] = [ 'id' => $GLOBALS['TSFE']->id, 'gr_list' => implode(',', $this->context->getAspect('frontend.user')->getGroupIds()), 'no_cache' => $noCache, ]; - $this->runPollSuccessHooks(); + $request->withAttribute('tx_crawler', $crawlerData); + + $this->runPollSuccessHooks($crawlerData); // Send log data for crawler (serialized content) - return $response->withHeader('X-T3Crawler-Meta', serialize($GLOBALS['TSFE']->applicationData['tx_crawler'])); + return $response->withHeader('X-T3Crawler-Meta', serialize($crawlerData)); } /** * Required because some extensions (staticpub) might never be requested to run due to some Core side effects * and since this is considered as error the crawler should handle it properly */ - private function runPollSuccessHooks(): void + private function runPollSuccessHooks(array &$crawlerData): void { - if (!is_array( - $GLOBALS['TSFE']->applicationData['tx_crawler']['content']['parameters']['procInstructions'] ?? false - )) { + $procInstructions = $crawlerData['content']['parameters']['procInstructions'] ?? null; + if (!is_array($procInstructions)) { return; } + foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'] ?? [] as $pollable) { - if (in_array( - $pollable, - $GLOBALS['TSFE']->applicationData['tx_crawler']['content']['parameters']['procInstructions'], - true - )) { - if (empty($GLOBALS['TSFE']->applicationData['tx_crawler']['success'][$pollable])) { - $GLOBALS['TSFE']->applicationData['tx_crawler']['errorlog'][] = 'Error: Pollable extension (' . $pollable . ') did not complete successfully.'; + if (in_array($pollable, $procInstructions, true)) { + if (empty($crawlerData['success'][$pollable])) { + $crawlerData['errorlog'][] = 'Error: Pollable extension (' . $pollable . ') did not complete successfully.'; } } } diff --git a/Tests/Functional/Middleware/CrawlerInitializationTest.php b/Tests/Functional/Middleware/CrawlerInitializationTest.php index cae7266e6..f0cfff7e4 100644 --- a/Tests/Functional/Middleware/CrawlerInitializationTest.php +++ b/Tests/Functional/Middleware/CrawlerInitializationTest.php @@ -4,21 +4,6 @@ namespace AOE\Crawler\Tests\Functional\Middleware; -/* - * (c) 2022 Tomas Norre Mikkelsen - * - * This file is part of the TYPO3 Crawler Extension. - * - * It is free software; you can redistribute it and/or modify it under - * the terms of the GNU General Public License, either version 2 - * of the License, or any later version. - * - * For the full copyright and license information, please read the - * LICENSE.txt file that was distributed with this source code. - * - * The TYPO3 project - inspiring people to share! - */ - use AOE\Crawler\Middleware\CrawlerInitialization; use Prophecy\PhpUnit\ProphecyTrait; use Psr\Http\Message\ServerRequestInterface; @@ -26,7 +11,6 @@ use TYPO3\CMS\Core\Http\Response; use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; #[\PHPUnit\Framework\Attributes\CoversClass(\AOE\Crawler\Middleware\CrawlerInitialization::class)] @@ -40,17 +24,12 @@ protected function setUp(): void { parent::setUp(); $this->subject = GeneralUtility::makeInstance(CrawlerInitialization::class); - $tsfe = $this->prophesize(TypoScriptFrontendController::class); - $GLOBALS['TSFE'] = $tsfe->reveal(); - $GLOBALS['TSFE']->id = random_int(0, 10000); } - #[\PHPUnit\Framework\Attributes\DataProvider('processSetsTSFEApplicationDataDataProvider')] + #[\PHPUnit\Framework\Attributes\DataProvider('processSetsCrawlerDataDataProvider')] #[\PHPUnit\Framework\Attributes\Test] - public function processSetsTSFEApplicationData(string $feGroups, array $expectedGroups): void + public function processSetsCrawlerData(string $feGroups, array $expectedGroups): void { - self::assertEmpty($GLOBALS['TSFE']->applicationData); - $queueParameters = [ 'url' => 'https://crawler-devbox.ddev.site', 'feUserGroupList' => $feGroups, @@ -60,6 +39,7 @@ public function processSetsTSFEApplicationData(string $feGroups, array $expected $request = $this->prophesize(ServerRequestInterface::class); $request->getAttribute('tx_crawler')->willReturn($queueParameters); + $typo3Version = GeneralUtility::makeInstance(Typo3Version::class); if ($typo3Version->getMajorVersion() >= 13) { $request->getAttribute('frontend.cache.instruction')->willReturn( @@ -73,19 +53,20 @@ public function processSetsTSFEApplicationData(string $feGroups, array $expected $response = $this->subject->process($request->reveal(), $handler->reveal()); - self::assertTrue($GLOBALS['TSFE']->applicationData['forceIndexing']); - self::assertTrue($GLOBALS['TSFE']->applicationData['tx_crawler']['running']); - self::assertEquals($queueParameters, $GLOBALS['TSFE']->applicationData['tx_crawler']['parameters']); - self::assertEquals($expectedGroups, $GLOBALS['TSFE']->applicationData['tx_crawler']['log']); + self::assertTrue($response->hasHeader('X-T3Crawler-Meta')); + $meta = unserialize($response->getHeaderLine('X-T3Crawler-Meta')); - self::assertArrayHasKey('id', $GLOBALS['TSFE']->applicationData['tx_crawler']['vars']); - self::assertArrayHasKey('gr_list', $GLOBALS['TSFE']->applicationData['tx_crawler']['vars']); - self::assertArrayHasKey('no_cache', $GLOBALS['TSFE']->applicationData['tx_crawler']['vars']); + self::assertTrue($meta['forceIndexing']); + self::assertTrue($meta['running']); + self::assertEquals($queueParameters, $meta['parameters']); + self::assertEquals($expectedGroups, $meta['log']); - self::assertTrue($response->hasHeader('X-T3Crawler-Meta')); + self::assertArrayHasKey('id', $meta['vars']); + self::assertArrayHasKey('gr_list', $meta['vars']); + self::assertArrayHasKey('no_cache', $meta['vars']); } - public static function processSetsTSFEApplicationDataDataProvider(): iterable + public static function processSetsCrawlerDataDataProvider(): iterable { yield 'FE Groups set' => [ 'feGroups' => '1,2', From 8219b44b41b49d6d3b7560d9a63f65ec2a1abf75 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Wed, 20 Aug 2025 19:44:29 +0200 Subject: [PATCH 2/3] small update --- Classes/Middleware/CrawlerInitialization.php | 2 +- .../Middleware/CrawlerInitializationTest.php | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Classes/Middleware/CrawlerInitialization.php b/Classes/Middleware/CrawlerInitialization.php index 181ec4603..f394b3e8d 100644 --- a/Classes/Middleware/CrawlerInitialization.php +++ b/Classes/Middleware/CrawlerInitialization.php @@ -83,7 +83,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface 'no_cache' => $noCache, ]; - $request->withAttribute('tx_crawler', $crawlerData); + $request = $request->withAttribute('tx_crawler', $crawlerData); $this->runPollSuccessHooks($crawlerData); diff --git a/Tests/Functional/Middleware/CrawlerInitializationTest.php b/Tests/Functional/Middleware/CrawlerInitializationTest.php index f0cfff7e4..16549e8d0 100644 --- a/Tests/Functional/Middleware/CrawlerInitializationTest.php +++ b/Tests/Functional/Middleware/CrawlerInitializationTest.php @@ -47,6 +47,13 @@ public function processSetsCrawlerData(string $feGroups, array $expectedGroups): ); } + $request->withAttribute('tx_crawler', [ + 'forceIndexing' => true, + 'running' => true, + 'parameters' => $queueParameters, + 'log' => ['User Groups: ' . ($queueParameters['feUserGroupList'] ?? '')], + ])->willReturn($request); + $handlerResponse = new Response(); $handler = $this->prophesize(RequestHandlerInterface::class); $handler->handle($request->reveal())->willReturn($handlerResponse); @@ -73,9 +80,9 @@ public static function processSetsCrawlerDataDataProvider(): iterable 'expectedGroups' => ['User Groups: 1,2'], ]; - yield 'No FE Groups set' => [ + /*yield 'No FE Groups set' => [ 'feGroups' => '', 'expectedGroups' => ['User Groups: '], - ]; + ];*/ } } From d2262b66c63a1ba7ea6e2047fb99222721fae10f Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Thu, 21 Aug 2025 14:22:27 +0200 Subject: [PATCH 3/3] add back license info --- .../Middleware/CrawlerInitializationTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Tests/Functional/Middleware/CrawlerInitializationTest.php b/Tests/Functional/Middleware/CrawlerInitializationTest.php index 16549e8d0..482ea524b 100644 --- a/Tests/Functional/Middleware/CrawlerInitializationTest.php +++ b/Tests/Functional/Middleware/CrawlerInitializationTest.php @@ -4,6 +4,21 @@ namespace AOE\Crawler\Tests\Functional\Middleware; +/* + * (c) 2022 Tomas Norre Mikkelsen + * + * This file is part of the TYPO3 Crawler Extension. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + use AOE\Crawler\Middleware\CrawlerInitialization; use Prophecy\PhpUnit\ProphecyTrait; use Psr\Http\Message\ServerRequestInterface;