meilisearch/Classes/Domain/Index/PageIndexer/Helper/UriBuilder/AbstractUriStrategy.php

160 lines
6.2 KiB
PHP
Raw Normal View History

2021-04-17 00:26:33 +02:00
<?php declare(strict_types = 1);
namespace WapplerSystems\Meilisearch\Domain\Index\PageIndexer\Helper\UriBuilder;
/***************************************************************
* Copyright notice
*
* (c) 2019 Timo Hund <timo.hund@dkd.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use WapplerSystems\Meilisearch\IndexQueue\Item;
use WapplerSystems\Meilisearch\IndexQueue\PageIndexerDataUrlModifier;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\System\Url\UrlHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
2021-04-17 21:20:54 +02:00
* Implementations of this class are able to build an indexing url for meilisearch page indexing.
2021-04-17 00:26:33 +02:00
*/
abstract class AbstractUriStrategy
{
/**
2021-04-17 21:20:54 +02:00
* @var MeilisearchLogManager|null|object
2021-04-17 00:26:33 +02:00
*/
protected $logger;
/**
* AbstractUriStrategy constructor.
2021-04-17 21:20:54 +02:00
* @param MeilisearchLogManager|null $logger
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
public function __construct(MeilisearchLogManager $logger = null)
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$this->logger = $logger ?? GeneralUtility::makeInstance(MeilisearchLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
2021-04-17 00:26:33 +02:00
}
/**
* @param UrlHelper $urlHelper
* @param array $overrideConfiguration
* @return UrlHelper
*/
protected function applyTypoScriptOverridesOnIndexingUrl(UrlHelper $urlHelper, array $overrideConfiguration = []): UrlHelper
{
// check whether we should use ssl / https
if (!empty($overrideConfiguration['scheme'])) {
$urlHelper->setScheme($overrideConfiguration['scheme']);
}
// overwriting the host
if (!empty($overrideConfiguration['host'])) {
$urlHelper->setHost($overrideConfiguration['host']);
}
// overwriting the port
if (!empty($overrideConfiguration['port'])) {
$urlHelper->setPort($overrideConfiguration['port']);
}
// setting a path if TYPO3 is installed in a sub directory
if (!empty($overrideConfiguration['path'])) {
$urlHelper->setPath($overrideConfiguration['path']);
}
return $urlHelper;
}
/**
* @param Item $item
* @param int $language
* @param string $mountPointParameter
* @param array $options
* @return string
*/
public function getPageIndexingUriFromPageItemAndLanguageId(Item $item, int $language = 0, string $mountPointParameter = '', $options = []): string
{
$pageIndexUri = $this->buildPageIndexingUriFromPageItemAndLanguageId($item, $language, $mountPointParameter);
$urlHelper = GeneralUtility::makeInstance(UrlHelper::class, $pageIndexUri);
$overrideConfiguration = is_array($options['frontendDataHelper.']) ? $options['frontendDataHelper.'] : [];
$urlHelper = $this->applyTypoScriptOverridesOnIndexingUrl($urlHelper, $overrideConfiguration);
$dataUrl = $urlHelper->getUrl();
if (!GeneralUtility::isValidUrl($dataUrl)) {
$this->logger->log(
2021-04-17 21:20:54 +02:00
MeilisearchLogManager::ERROR,
2021-04-17 00:26:33 +02:00
'Could not create a valid URL to get frontend data while trying to index a page.',
[
'item' => (array)$item,
'constructed URL' => $dataUrl,
'scheme' => $urlHelper->getScheme(),
'host' => $urlHelper->getHost(),
'path' => $urlHelper->getPath(),
'page ID' => $item->getRecordUid(),
'indexer options' => $options
]
);
throw new \RuntimeException(
'Could not create a valid URL to get frontend data while trying to index a page. Created URL: ' . $dataUrl,
1311080805
);
}
return $this->applyDataUrlModifier($item, $language, $dataUrl, $urlHelper);
}
/**
* @param Item $item
* @param int $language
* @param string $mountPointParameter
* @return mixed
*/
abstract protected function buildPageIndexingUriFromPageItemAndLanguageId(Item $item, int $language = 0, string $mountPointParameter = '');
/**
* @param Item $item
* @param int $language
* @param string $dataUrl
* @param UrlHelper $urlHelper
* @return string
*/
protected function applyDataUrlModifier(Item $item, int $language, $dataUrl, UrlHelper $urlHelper):string
{
2021-04-17 21:20:54 +02:00
if (empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['IndexQueuePageIndexer']['dataUrlModifier'])) {
2021-04-17 00:26:33 +02:00
return $dataUrl;
}
2021-04-17 21:20:54 +02:00
$dataUrlModifier = GeneralUtility::makeInstance($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['IndexQueuePageIndexer']['dataUrlModifier']);
2021-04-17 00:26:33 +02:00
if (!$dataUrlModifier instanceof PageIndexerDataUrlModifier) {
2021-04-17 21:20:54 +02:00
throw new \RuntimeException($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['IndexQueuePageIndexer']['dataUrlModifier'] . ' is not an implementation of WapplerSystems\Meilisearch\IndexQueue\PageIndexerDataUrlModifier', 1290523345);
2021-04-17 00:26:33 +02:00
}
return $dataUrlModifier->modifyDataUrl($dataUrl,
[
'item' => $item, 'scheme' => $urlHelper->getScheme(), 'host' => $urlHelper->getHost(),
'path' => $urlHelper->getPath(), 'pageId' => $item->getRecordUid(), 'language' => $language
]
);
}
}