From 89d9f0d276ba41fd40f20730886224582a6ecb54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20K=C3=A4hm?= Date: Fri, 7 Jun 2024 13:43:11 +0200 Subject: [PATCH] [TASK] fix PhpStan errors for TYPO3 13 --- ...rPageDocumentIsCreatedForIndexingEvent.php | 14 ++++- ...oreDocumentIsProcessedForIndexingEvent.php | 11 +++- .../BeforeDocumentsAreIndexedEvent.php | 11 +++- ...ageDocumentIsProcessedForIndexingEvent.php | 58 +------------------ Classes/IndexQueue/AbstractIndexer.php | 12 ++-- .../IndexQueue/FrontendHelper/PageIndexer.php | 46 +++++++++++++-- Classes/IndexQueue/PageIndexerRequest.php | 4 +- Classes/Middleware/SolrRoutingMiddleware.php | 2 +- Classes/Report/SolrConfigurationStatus.php | 11 +--- ...QueueWorkerTaskAdditionalFieldProvider.php | 6 +- ...QueueWorkerTaskAdditionalFieldProvider.php | 9 +-- ...timizeIndexTaskAdditionalFieldProvider.php | 18 +++--- .../ReIndexTaskAdditionalFieldProvider.php | 21 ++++--- .../FrequentlySearchedViewHelper.php | 22 +++++-- 14 files changed, 134 insertions(+), 111 deletions(-) diff --git a/Classes/Event/Indexing/AfterPageDocumentIsCreatedForIndexingEvent.php b/Classes/Event/Indexing/AfterPageDocumentIsCreatedForIndexingEvent.php index a5262eab85..046b8141e4 100644 --- a/Classes/Event/Indexing/AfterPageDocumentIsCreatedForIndexingEvent.php +++ b/Classes/Event/Indexing/AfterPageDocumentIsCreatedForIndexingEvent.php @@ -20,8 +20,10 @@ use ApacheSolrForTypo3\Solr\IndexQueue\Item; use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; +use Psr\Http\Message\ServerRequestInterface; use TYPO3\CMS\Core\Site\Entity\Site; use TYPO3\CMS\Core\Site\Entity\SiteLanguage; +use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; /** @@ -39,7 +41,7 @@ public function __construct( private readonly Item $indexQueueItem, private readonly array $record, private readonly TypoScriptFrontendController $tsfe, - private readonly TypoScriptConfiguration $configuration + private readonly TypoScriptConfiguration $configuration, ) {} public function getDocument(): Document @@ -62,14 +64,20 @@ public function getIndexingConfigurationName(): string return $this->indexQueueItem->getIndexingConfigurationName(); } + /** + * @throws ContentRenderingException + */ public function getSite(): Site { - return $this->tsfe->getSite(); + return clone $this->tsfe->cObj->getRequest()->getAttribute('site'); } + /** + * @throws ContentRenderingException + */ public function getSiteLanguage(): SiteLanguage { - return $this->tsfe->getLanguage(); + return $this->getSite()->getAttribute('language'); } public function getRecord(): array diff --git a/Classes/Event/Indexing/BeforeDocumentIsProcessedForIndexingEvent.php b/Classes/Event/Indexing/BeforeDocumentIsProcessedForIndexingEvent.php index cb40e41c92..39c8b7f663 100644 --- a/Classes/Event/Indexing/BeforeDocumentIsProcessedForIndexingEvent.php +++ b/Classes/Event/Indexing/BeforeDocumentIsProcessedForIndexingEvent.php @@ -21,6 +21,7 @@ use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; use TYPO3\CMS\Core\Site\Entity\Site; use TYPO3\CMS\Core\Site\Entity\SiteLanguage; +use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; /** @@ -45,14 +46,20 @@ public function __construct( $this->documents[] = $this->document; } + /** + * @throws ContentRenderingException + */ public function getSite(): Site { - return $this->tsfe->getSite(); + return clone $this->tsfe->cObj->getRequest()->getAttribute('site'); } + /** + * @throws ContentRenderingException + */ public function getSiteLanguage(): SiteLanguage { - return $this->tsfe->getLanguage(); + return clone $this->getSite()->getAttribute('language'); } public function getIndexQueueItem(): Item diff --git a/Classes/Event/Indexing/BeforeDocumentsAreIndexedEvent.php b/Classes/Event/Indexing/BeforeDocumentsAreIndexedEvent.php index 718a9fa47a..686c61d39f 100644 --- a/Classes/Event/Indexing/BeforeDocumentsAreIndexedEvent.php +++ b/Classes/Event/Indexing/BeforeDocumentsAreIndexedEvent.php @@ -21,6 +21,7 @@ use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; use TYPO3\CMS\Core\Site\Entity\Site; use TYPO3\CMS\Core\Site\Entity\SiteLanguage; +use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; /** @@ -39,14 +40,20 @@ public function __construct( private readonly TypoScriptFrontendController $tsfe, ) {} + /** + * @throws ContentRenderingException + */ public function getSite(): Site { - return $this->tsfe->getSite(); + return clone $this->tsfe->cObj->getRequest()->getAttribute('site'); } + /** + * @throws ContentRenderingException + */ public function getSiteLanguage(): SiteLanguage { - return $this->tsfe->getLanguage(); + return clone $this->getSite()->getAttribute('language'); } public function getIndexQueueItem(): Item diff --git a/Classes/Event/Indexing/BeforePageDocumentIsProcessedForIndexingEvent.php b/Classes/Event/Indexing/BeforePageDocumentIsProcessedForIndexingEvent.php index 28d30e5673..fe20faaf4a 100644 --- a/Classes/Event/Indexing/BeforePageDocumentIsProcessedForIndexingEvent.php +++ b/Classes/Event/Indexing/BeforePageDocumentIsProcessedForIndexingEvent.php @@ -19,8 +19,7 @@ use ApacheSolrForTypo3\Solr\IndexQueue\Item; use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; -use TYPO3\CMS\Core\Site\Entity\Site; -use TYPO3\CMS\Core\Site\Entity\SiteLanguage; +use Psr\Http\Message\ServerRequestInterface; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; /** @@ -29,59 +28,6 @@ * Previously used with * $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['Indexer']['indexPageAddDocuments'] */ -class BeforePageDocumentIsProcessedForIndexingEvent +class BeforePageDocumentIsProcessedForIndexingEvent extends BeforeDocumentIsProcessedForIndexingEvent { - /** - * @var Document[] - */ - private array $documents = []; - - public function __construct( - private readonly Document $document, - private readonly Item $indexQueueItem, - private readonly TypoScriptFrontendController $tsfe, - ) { - $this->documents[] = $this->document; - } - - public function getSite(): Site - { - return $this->tsfe->getSite(); - } - - public function getSiteLanguage(): SiteLanguage - { - return $this->tsfe->getLanguage(); - } - - public function getIndexQueueItem(): Item - { - return $this->indexQueueItem; - } - - public function getDocument(): Document - { - return $this->document; - } - - /** - * @param Document[] $documents - */ - public function addDocuments(array $documents): void - { - $this->documents = array_merge($this->documents, $documents); - } - - /** - * @return Document[] - */ - public function getDocuments(): array - { - return $this->documents; - } - - public function getTsfe(): TypoScriptFrontendController - { - return clone $this->tsfe; - } } diff --git a/Classes/IndexQueue/AbstractIndexer.php b/Classes/IndexQueue/AbstractIndexer.php index 69bb9d9aa3..ae0962b7a5 100644 --- a/Classes/IndexQueue/AbstractIndexer.php +++ b/Classes/IndexQueue/AbstractIndexer.php @@ -22,7 +22,9 @@ use ApacheSolrForTypo3\Solr\ContentObject\Relation; use ApacheSolrForTypo3\Solr\FrontendEnvironment\Tsfe; use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; +use ApacheSolrForTypo3\Solr\System\Util\ArrayAccessor; use TYPO3\CMS\Core\Core\Environment; +use TYPO3\CMS\Core\TypoScript\FrontendTypoScript; use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\MathUtility; @@ -120,7 +122,7 @@ protected function resolveFieldValue( array $indexingConfiguration, string $solrFieldName, array $data, - TypoScriptFrontendController $tsfe + TypoScriptFrontendController $tsfe, ): mixed { if (isset($indexingConfiguration[$solrFieldName . '.'])) { // configuration found => need to resolve a cObj @@ -156,10 +158,12 @@ protected function resolveFieldValue( 1 )); - // @todo: this must be solved differently - $typoScriptParser = GeneralUtility::makeInstance(TypoScriptParser::class); + /** @var ?FrontendTypoScript $frontendTypoScript */ + $frontendTypoScript = $tsfe->cObj->getRequest()->getAttribute('frontend.typoscript'); + $configurationAccess = new ArrayAccessor($frontendTypoScript?->getSetupArray(), '.', true); // $name and $conf is loaded with the referenced values. - [$name, $conf] = $typoScriptParser->getVal($referencedTsPath, $GLOBALS['TYPO3_REQUEST']->getAttribute('frontend.typoscript')?->getSetupArray()); + $name = $configurationAccess->get($referencedTsPath); + $conf = $configurationAccess->get($referencedTsPath . '.'); // need to change directory to make IMAGE content objects work in BE context // see http://blog.netzelf.de/lang/de/tipps-und-tricks/tslib_cobj-image-im-backend diff --git a/Classes/IndexQueue/FrontendHelper/PageIndexer.php b/Classes/IndexQueue/FrontendHelper/PageIndexer.php index f19d00cd61..be8578452e 100644 --- a/Classes/IndexQueue/FrontendHelper/PageIndexer.php +++ b/Classes/IndexQueue/FrontendHelper/PageIndexer.php @@ -21,18 +21,20 @@ use ApacheSolrForTypo3\Solr\Event\Indexing\AfterPageDocumentIsCreatedForIndexingEvent; use ApacheSolrForTypo3\Solr\Event\Indexing\BeforeDocumentsAreIndexedEvent; use ApacheSolrForTypo3\Solr\Event\Indexing\BeforePageDocumentIsProcessedForIndexingEvent; -use ApacheSolrForTypo3\Solr\Exception; +use ApacheSolrForTypo3\Solr\Exception as SolrException; use ApacheSolrForTypo3\Solr\FieldProcessor\Service; use ApacheSolrForTypo3\Solr\IndexQueue\Item; use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequest; use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerResponse; use ApacheSolrForTypo3\Solr\IndexQueue\Queue; +use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException; use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; use ApacheSolrForTypo3\Solr\System\Logging\DebugWriter; use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection; use ApacheSolrForTypo3\Solr\Util; +use Doctrine\DBAL\Exception as DBALException; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Log\LogLevel; @@ -200,9 +202,16 @@ public function __invoke(AfterCacheableContentIsGeneratedEvent $event): void /** * Index item + * + * @throws DBALException + * @throws SolrException + * @throws Throwable */ - protected function index(Item $indexQueueItem, ServerRequestInterface $request, TypoScriptFrontendController $tsfe): void - { + protected function index( + Item $indexQueueItem, + ServerRequestInterface $request, + TypoScriptFrontendController $tsfe, + ): void { /** @var PageArguments $pageArguments */ $pageArguments = $request->getAttribute('routing'); $pageInformation = $request->getAttribute('frontend.page.information'); @@ -218,7 +227,12 @@ protected function index(Item $indexQueueItem, ServerRequestInterface $request, $this->getAccessRootline(), $pageInformation->getMountPoint() ); - $document = $this->substitutePageDocument($document, $pageInformation->getPageRecord(), $indexQueueItem, $tsfe); + $document = $this->substitutePageDocument( + $document, + $pageInformation->getPageRecord(), + $indexQueueItem, + $tsfe, + ); $this->responseData['pageIndexed'] = (int)$this->indexPage($document, $indexQueueItem, $tsfe); $this->responseData['originalPageDocument'] = (array)$document; @@ -236,6 +250,11 @@ protected function index(Item $indexQueueItem, ServerRequestInterface $request, /** * Gets the solr connection to use for indexing the page based on the * Index Queue item's properties. + * + * @throws DBALException + * @throws NoSolrConnectionFoundException + * @throws SolrException + * @throws Throwable */ protected function getSolrConnection(Item $indexQueueItem, SiteLanguage $siteLanguage, bool $logExceptions): SolrConnection { @@ -243,7 +262,7 @@ protected function getSolrConnection(Item $indexQueueItem, SiteLanguage $siteLan try { $solrConnection = $connectionManager->getConnectionByRootPageId($indexQueueItem->getRootPageUid(), $siteLanguage->getLanguageId()); if (!$solrConnection->getWriteService()->ping()) { - throw new Exception( + throw new SolrException( 'Could not connect to Solr server.', 1323946472 ); @@ -269,9 +288,12 @@ protected function getSolrConnection(Item $indexQueueItem, SiteLanguage $siteLan /** * This method retrieves the item from the index queue, that is indexed in this request. + * + * @throws DBALException */ protected function getIndexQueueItem(): ?Item { + /** @var Queue $indexQueue */ $indexQueue = GeneralUtility::makeInstance(Queue::class); return $indexQueue->getItem($this->request->getParameter('item')); } @@ -289,7 +311,13 @@ protected function substitutePageDocument( Item $indexQueueItem, TypoScriptFrontendController $tsfe, ): Document { - $event = new AfterPageDocumentIsCreatedForIndexingEvent($pageDocument, $indexQueueItem, $pageRecord, $tsfe, $this->configuration); + $event = new AfterPageDocumentIsCreatedForIndexingEvent( + $pageDocument, + $indexQueueItem, + $pageRecord, + $tsfe, + $this->configuration, + ); $event = $this->getEventDispatcher()->dispatch($event); return $event->getDocument(); } @@ -309,6 +337,9 @@ protected function getPageDocument(TypoScriptFrontendController $tsfe, PageInfor * Indexes a page. * * @return bool TRUE after successfully indexing the page, FALSE on error + * + * @throws DBALException + * @throws SolrException */ protected function indexPage( Document $pageDocument, @@ -336,6 +367,9 @@ protected function indexPage( * care of manipulating fields as defined in the field's configuration. * * @param Document[] $documents An array of documents to manipulate + * + * @throws SolrException + * @throws DBALException */ protected function processDocuments(array $documents): void { diff --git a/Classes/IndexQueue/PageIndexerRequest.php b/Classes/IndexQueue/PageIndexerRequest.php index 92a927dd63..77ead5f281 100644 --- a/Classes/IndexQueue/PageIndexerRequest.php +++ b/Classes/IndexQueue/PageIndexerRequest.php @@ -366,11 +366,11 @@ protected function getUrl(string $url, array $headers, float $timeout): Response 'options' => $options, ] ); - } finally { + } /* @todo: fix that properly or remove */ /*finally { if (isset($originalBackendUser)) { $GLOBALS['BE_USER'] = $originalBackendUser; } - } + }*/ $response->getBody()->rewind(); return $response; } diff --git a/Classes/Middleware/SolrRoutingMiddleware.php b/Classes/Middleware/SolrRoutingMiddleware.php index 1c74c98443..a769bbd9d2 100644 --- a/Classes/Middleware/SolrRoutingMiddleware.php +++ b/Classes/Middleware/SolrRoutingMiddleware.php @@ -301,7 +301,7 @@ protected function retrievePageInformation(UriInterface $uri, Site $site): array sprintf( $message, $path, - $this->language->getTwoLetterIsoCode(), + $this->language->getLocale()->getLanguageCode(), $uri->getPath() ) ); diff --git a/Classes/Report/SolrConfigurationStatus.php b/Classes/Report/SolrConfigurationStatus.php index 7ba1bd37b5..f34ff543cc 100644 --- a/Classes/Report/SolrConfigurationStatus.php +++ b/Classes/Report/SolrConfigurationStatus.php @@ -149,18 +149,15 @@ protected function getRootPagesWithIndexingOff(): array if ($solrIsEnabledAndIndexingDisabled) { $rootPagesWithIndexingOff[] = $rootPage; } - /** @phpstan-ignore-next-line */ } catch (RuntimeException) { $rootPagesWithIndexingOff[] = $rootPage; - /** @phpstan-ignore-next-line */ - } catch (ServiceUnavailableException $sue) { + } /** @noinspection PhpRedundantCatchClauseInspection */ catch (ServiceUnavailableException $sue) { if ($sue->getCode() == 1294587218) { // No TypoScript template found, continue with next site $rootPagesWithIndexingOff[] = $rootPage; continue; } - /** @phpstan-ignore-next-line */ - } catch (SiteNotFoundException $sue) { + } /** @noinspection PhpRedundantCatchClauseInspection */ catch (SiteNotFoundException $sue) { if ($sue->getCode() == 1521716622) { // No site found, continue with next site $rootPagesWithIndexingOff[] = $rootPage; @@ -188,8 +185,6 @@ protected function getRootPages(): array /** * Checks if the solr plugin is enabled with plugin.tx_solr.enabled. - * - * @throws DBALException */ protected function getIsSolrEnabled(int $pageUid): bool { @@ -198,8 +193,6 @@ protected function getIsSolrEnabled(int $pageUid): bool /** * Checks if the indexing is enabled with config.index_enable - * - * @throws DBALException */ protected function getIsIndexingEnabled(int $pageUid): bool { diff --git a/Classes/Task/EventQueueWorkerTaskAdditionalFieldProvider.php b/Classes/Task/EventQueueWorkerTaskAdditionalFieldProvider.php index 30e366d9cd..c5ea1f6dd6 100644 --- a/Classes/Task/EventQueueWorkerTaskAdditionalFieldProvider.php +++ b/Classes/Task/EventQueueWorkerTaskAdditionalFieldProvider.php @@ -19,8 +19,8 @@ use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider; use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController; +use TYPO3\CMS\Scheduler\SchedulerManagementAction; use TYPO3\CMS\Scheduler\Task\AbstractTask; -use TYPO3\CMS\Scheduler\Task\Enumeration\Action; /** * Additional field provider for the index queue worker task @@ -49,11 +49,11 @@ public function getAdditionalFields( if ($task !== null && !($task instanceof EventQueueWorkerTask)) { return $additionalFields; } - if ($schedulerModule->getCurrentAction()->equals(Action::ADD)) { + if ($schedulerModule->getCurrentAction() === SchedulerManagementAction::ADD) { $taskInfo['solr_eventqueueworkertask_limit'] = EventQueueWorkerTask::DEFAULT_PROCESSING_LIMIT; } - if ($schedulerModule->getCurrentAction()->equals(Action::EDIT)) { + if ($schedulerModule->getCurrentAction() === SchedulerManagementAction::EDIT) { $taskInfo['solr_eventqueueworkertask_limit'] = $task->getLimit(); } diff --git a/Classes/Task/IndexQueueWorkerTaskAdditionalFieldProvider.php b/Classes/Task/IndexQueueWorkerTaskAdditionalFieldProvider.php index 3920b7a2ea..ebc02c7c05 100644 --- a/Classes/Task/IndexQueueWorkerTaskAdditionalFieldProvider.php +++ b/Classes/Task/IndexQueueWorkerTaskAdditionalFieldProvider.php @@ -20,13 +20,14 @@ use ApacheSolrForTypo3\Solr\Backend\SiteSelectorField; use ApacheSolrForTypo3\Solr\Domain\Site\Exception\UnexpectedTYPO3SiteInitializationException; use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository; +use ApacheSolrForTypo3\Solr\Exception\InvalidArgumentException; use Doctrine\DBAL\Exception as DBALException; use LogicException; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider; use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController; +use TYPO3\CMS\Scheduler\SchedulerManagementAction; use TYPO3\CMS\Scheduler\Task\AbstractTask; -use TYPO3\CMS\Scheduler\Task\Enumeration\Action; /** * Additional field provider for the index queue worker task @@ -49,6 +50,7 @@ public function __construct( * For each field it provides an associative sub-array with the following: * * @throws DBALException + * @throws InvalidArgumentException * @throws UnexpectedTYPO3SiteInitializationException */ public function getAdditionalFields( @@ -63,14 +65,13 @@ public function getAdditionalFields( return $additionalFields; } - $currentAction = $schedulerModule->getCurrentAction(); - if ($currentAction->equals(Action::ADD)) { + if ($schedulerModule->getCurrentAction() === SchedulerManagementAction::ADD) { $taskInfo['site'] = null; $taskInfo['documentsToIndexLimit'] = 50; $taskInfo['forcedWebRoot'] = ''; } - if ($currentAction->equals(Action::EDIT)) { + if ($schedulerModule->getCurrentAction() === SchedulerManagementAction::EDIT) { $taskInfo['site'] = $this->siteRepository->getSiteByRootPageId((int)$task->getRootPageId()); $taskInfo['documentsToIndexLimit'] = $task->getDocumentsToIndexLimit(); $taskInfo['forcedWebRoot'] = $task->getForcedWebRoot(); diff --git a/Classes/Task/OptimizeIndexTaskAdditionalFieldProvider.php b/Classes/Task/OptimizeIndexTaskAdditionalFieldProvider.php index 26b52222d9..ada90e1113 100644 --- a/Classes/Task/OptimizeIndexTaskAdditionalFieldProvider.php +++ b/Classes/Task/OptimizeIndexTaskAdditionalFieldProvider.php @@ -21,6 +21,7 @@ use ApacheSolrForTypo3\Solr\Backend\SiteSelectorField; use ApacheSolrForTypo3\Solr\Domain\Site\Site; use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository; +use ApacheSolrForTypo3\Solr\Exception\InvalidArgumentException; use Doctrine\DBAL\Exception as DBALException; use LogicException; use Throwable; @@ -31,8 +32,8 @@ use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider; use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController; +use TYPO3\CMS\Scheduler\SchedulerManagementAction; use TYPO3\CMS\Scheduler\Task\AbstractTask; -use TYPO3\CMS\Scheduler\Task\Enumeration\Action; /** * Adds additional field to specify the Solr server to initialize the index queue for @@ -83,6 +84,7 @@ public function __construct() * Initializes this instance and the necessary objects. * * @throws DBALException + * @throws InvalidArgumentException */ protected function initialize( SchedulerModuleController $schedulerModule, @@ -93,9 +95,7 @@ protected function initialize( $this->schedulerModule = $schedulerModule; $this->taskInformation = $taskInfo; - $currentAction = $schedulerModule->getCurrentAction(); - - if ($currentAction->equals(Action::EDIT)) { + if ($schedulerModule->getCurrentAction() === SchedulerManagementAction::EDIT) { $this->site = $this->siteRepository->getSiteByRootPageId((int)$task->getRootPageId()); } } @@ -204,13 +204,13 @@ public function validateAdditionalFields( * class matches. * * @param array $submittedData array containing the data submitted by the user - * @param OptimizeIndexTask $task reference to the current task object + * @param OptimizeIndexTask|AbstractTask|AbstractSolrTask $task reference to the current task object */ public function saveAdditionalFields( array $submittedData, - OptimizeIndexTask|AbstractTask $task + AbstractTask|OptimizeIndexTask|AbstractSolrTask $task ): void { - if (!$this->isTaskInstanceofOptimizeIndexTask($task)) { + if (!$this->isTaskInstanceofOptimizeIndexTask($task) || !$task instanceof AbstractSolrTask) { return; } @@ -220,6 +220,10 @@ public function saveAdditionalFields( if (!empty($submittedData['cores'])) { $cores = $submittedData['cores']; } + + if (!$task instanceof OptimizeIndexTask) { + return; + } $task->setCoresToOptimizeIndex($cores); } diff --git a/Classes/Task/ReIndexTaskAdditionalFieldProvider.php b/Classes/Task/ReIndexTaskAdditionalFieldProvider.php index 40f067ad72..d455c3a490 100644 --- a/Classes/Task/ReIndexTaskAdditionalFieldProvider.php +++ b/Classes/Task/ReIndexTaskAdditionalFieldProvider.php @@ -22,6 +22,7 @@ use ApacheSolrForTypo3\Solr\Domain\Site\Exception\UnexpectedTYPO3SiteInitializationException; use ApacheSolrForTypo3\Solr\Domain\Site\Site; use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository; +use ApacheSolrForTypo3\Solr\Exception\InvalidArgumentException; use Doctrine\DBAL\Exception as DBALException; use LogicException; use TYPO3\CMS\Backend\Form\Exception as BackendFormException; @@ -29,8 +30,8 @@ use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider; use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController; +use TYPO3\CMS\Scheduler\SchedulerManagementAction; use TYPO3\CMS\Scheduler\Task\AbstractTask; -use TYPO3\CMS\Scheduler\Task\Enumeration\Action; /** * Adds additional field to specify the Solr server to initialize the index queue for @@ -64,6 +65,7 @@ public function __construct() * Initialize object * * @throws DBALException + * @throws InvalidArgumentException */ protected function initialize( array $taskInfo, @@ -75,9 +77,7 @@ protected function initialize( $this->task = $task; $this->schedulerModule = $schedulerModule; - $currentAction = $schedulerModule->getCurrentAction(); - - if ($currentAction->equals(Action::EDIT)) { + if ($schedulerModule->getCurrentAction() === SchedulerManagementAction::EDIT) { $this->site = $this->siteRepository->getSiteByRootPageId((int)$task->getRootPageId()); } } @@ -97,9 +97,10 @@ protected function initialize( * @throws BackendFormException * @throws UnexpectedTYPO3SiteInitializationException * @throws DBALException + * @throws InvalidArgumentException * - * @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection * @noinspection PhpMissingReturnTypeInspection + * @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection */ public function getAdditionalFields( array &$taskInfo, @@ -185,13 +186,13 @@ public function validateAdditionalFields( * class matches. * * @param array $submittedData array containing the data submitted by the user - * @param ReIndexTask $task reference to the current task object + * @param AbstractTask|AbstractSolrTask|ReIndexTask $task reference to the current task object */ public function saveAdditionalFields( array $submittedData, - ReIndexTask|AbstractTask $task + AbstractTask|AbstractSolrTask|ReIndexTask $task ): void { - if (!$this->isTaskInstanceofReIndexTask($task)) { + if (!$this->isTaskInstanceofReIndexTask($task) || !$task instanceof AbstractSolrTask) { return; } @@ -201,6 +202,10 @@ public function saveAdditionalFields( if (!empty($submittedData['indexingConfigurations'])) { $indexingConfigurations = $submittedData['indexingConfigurations']; } + + if (!$task instanceof ReIndexTask) { + return; + } $task->setIndexingConfigurationsToReIndex($indexingConfigurations); } diff --git a/Classes/ViewHelpers/FrequentlySearchedViewHelper.php b/Classes/ViewHelpers/FrequentlySearchedViewHelper.php index b0171ce5de..44e6b53282 100644 --- a/Classes/ViewHelpers/FrequentlySearchedViewHelper.php +++ b/Classes/ViewHelpers/FrequentlySearchedViewHelper.php @@ -18,6 +18,7 @@ namespace ApacheSolrForTypo3\Solr\ViewHelpers; use ApacheSolrForTypo3\Solr\Domain\Search\FrequentSearches\FrequentSearchesService; +use ApacheSolrForTypo3\Solr\Exception as SolrException; use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager; use Closure; use Doctrine\DBAL\Exception as DBALException; @@ -26,6 +27,7 @@ use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException as AspectNotFoundExceptionAlias; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext; use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; /** @@ -51,9 +53,13 @@ class FrequentlySearchedViewHelper extends AbstractSolrViewHelper * * @throws AspectNotFoundExceptionAlias * @throws DBALException + * @throws SolrException */ - public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) - { + public static function renderStatic( + array $arguments, + Closure $renderChildrenClosure, + RenderingContextInterface|RenderingContext $renderingContext + ) { $cache = self::getInitializedCache(); /** @var ConfigurationManager $configurationManager */ $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class); @@ -65,6 +71,13 @@ public static function renderStatic(array $arguments, Closure $renderChildrenClo $cache, ); + if (!$renderingContext instanceof RenderingContext) { + throw new SolrException( + 'Solr rendering context must be an instance of RenderingContext', + 1717760054, + ); + } + $frequentSearches = $frequentSearchesService->getFrequentSearchTerms($renderingContext->getRequest()); $minimumSize = $typoScriptConfiguration->getSearchFrequentSearchesMinSize(); $maximumSize = $typoScriptConfiguration->getSearchFrequentSearchesMaxSize(); @@ -83,8 +96,9 @@ protected static function getInitializedCache(): ?FrontendInterface { $cacheIdentifier = 'tx_solr'; try { - /** @var FrontendInterface $cacheInstance */ - $cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache($cacheIdentifier); + /** @var CacheManager $cacheManager */ + $cacheManager = GeneralUtility::makeInstance(CacheManager::class); + $cacheInstance = $cacheManager->getCache($cacheIdentifier); } catch (NoSuchCacheException) { return null; }