first commit
This commit is contained in:
99
Classes/Domain/Variants/IdBuilder.php
Normal file
99
Classes/Domain/Variants/IdBuilder.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\Domain\Variants;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2017- 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.
|
||||
*
|
||||
* 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 TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* The variantId can be used to group documents by a variantId. This variantId is by default unique per system,
|
||||
* and has the following syntax:
|
||||
*
|
||||
* <SystemHash>/type/uid
|
||||
*
|
||||
* A file from one system will get the same variantId, which could be useful for de-duplication.
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class IdBuilder
|
||||
{
|
||||
|
||||
/**
|
||||
* This method is used to build a variantId.
|
||||
*
|
||||
* By default the variantId is used
|
||||
* @param string $type
|
||||
* @param integer $uid
|
||||
* @return string
|
||||
*/
|
||||
public function buildFromTypeAndUid($type, $uid)
|
||||
{
|
||||
$systemHash = $this->getSystemHash();
|
||||
$variantId = $systemHash . '/' . $type . '/' . $uid;
|
||||
|
||||
$variantId = $this->applyHook($variantId, $systemHash, $type, $uid);
|
||||
return $variantId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies configured postProcessing hooks to build a custom variantId.
|
||||
*
|
||||
* @param string $variantId
|
||||
* @param string $systemHash
|
||||
* @param string $type
|
||||
* @param integer $uid
|
||||
* @return string
|
||||
*/
|
||||
protected function applyHook($variantId, $systemHash, $type, $uid)
|
||||
{
|
||||
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId'])) {
|
||||
return $variantId;
|
||||
}
|
||||
|
||||
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifyVariantId'] as $classReference) {
|
||||
$variantIdModifier = GeneralUtility::makeInstance($classReference);
|
||||
if ($variantIdModifier instanceof IdModifier) {
|
||||
$variantId = $variantIdModifier->modifyVariantId($variantId, $systemHash, $type, $uid);
|
||||
}
|
||||
}
|
||||
|
||||
return $variantId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a system unique hash.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getSystemHash()
|
||||
{
|
||||
if (!isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'])) {
|
||||
throw new \InvalidArgumentException("No sitename set in TYPO3_CONF_VARS|SYS|sitename");
|
||||
}
|
||||
|
||||
$siteName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
|
||||
$systemKey = 'tx_meilisearch' . $siteName;
|
||||
return GeneralUtility::hmac($systemKey);
|
||||
}
|
||||
}
|
45
Classes/Domain/Variants/IdModifier.php
Normal file
45
Classes/Domain/Variants/IdModifier.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\Domain\Variants;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2017- 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.
|
||||
*
|
||||
* 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!
|
||||
***************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* An implementation of this class can be used to modify the variantId.
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
interface IdModifier
|
||||
{
|
||||
|
||||
/**
|
||||
* @param string $variantId
|
||||
* @param string $systemHash
|
||||
* @param string $type
|
||||
* @param integer $uid
|
||||
* @return string
|
||||
*/
|
||||
public function modifyVariantId($variantId, $systemHash, $type, $uid);
|
||||
}
|
125
Classes/Domain/Variants/VariantsProcessor.php
Normal file
125
Classes/Domain/Variants/VariantsProcessor.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\Domain\Variants;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2017- 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.
|
||||
*
|
||||
* 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\Domain\Search\ResultSet\Result\SearchResult;
|
||||
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Result\SearchResultBuilder;
|
||||
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSet;
|
||||
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSetProcessor;
|
||||
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
|
||||
use WapplerSystems\Meilisearch\System\Solr\ResponseAdapter;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Builds the SearchResult objects from the solr response and assigns the created child SearchResult objects (the variants)
|
||||
* to the parent search result object.
|
||||
*/
|
||||
class VariantsProcessor implements SearchResultSetProcessor
|
||||
{
|
||||
/**
|
||||
* @var TypoScriptConfiguration
|
||||
*/
|
||||
protected $typoScriptConfiguration;
|
||||
|
||||
/**
|
||||
* @var SearchResultBuilder
|
||||
*/
|
||||
protected $resultBuilder;
|
||||
|
||||
/**
|
||||
* VariantsProcessor constructor.
|
||||
* @param TypoScriptConfiguration $configuration
|
||||
* @param SearchResultBuilder|null $resultBuilder
|
||||
*/
|
||||
public function __construct(TypoScriptConfiguration $configuration, SearchResultBuilder $resultBuilder = null)
|
||||
{
|
||||
$this->typoScriptConfiguration = $configuration;
|
||||
$this->resultBuilder = $resultBuilder ?? GeneralUtility::makeInstance(SearchResultBuilder::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to add documents to the expanded documents of the SearchResult
|
||||
* when collapsing is configured.
|
||||
*
|
||||
* @param SearchResultSet $resultSet
|
||||
* @return SearchResultSet
|
||||
*/
|
||||
public function process(SearchResultSet $resultSet)
|
||||
{
|
||||
$response = $resultSet->getResponse();
|
||||
// @extensionScannerIgnoreLine
|
||||
if (!is_array($response->response->docs)) {
|
||||
return $resultSet;
|
||||
}
|
||||
|
||||
if (!$this->typoScriptConfiguration->getSearchVariants()) {
|
||||
return $resultSet;
|
||||
}
|
||||
|
||||
$variantsField = $this->typoScriptConfiguration->getSearchVariantsField();
|
||||
foreach ($resultSet->getSearchResults() as $resultDocument) {
|
||||
/** @var $resultDocument SearchResult */
|
||||
$variantId = $resultDocument[$variantsField] ?? null;
|
||||
|
||||
// when there is no value in the collapsing field, we can return
|
||||
if ($variantId === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$resultDocument->setVariantFieldValue($variantId);
|
||||
if (!isset($response->{'expanded'}) || !isset($response->{'expanded'}->{$variantId})) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->buildVariantDocumentAndAssignToParentResult($response, $variantId, $resultDocument);
|
||||
$resultDocument->setVariantsNumFound($response->{'expanded'}->{$variantId}->{'numFound'});
|
||||
}
|
||||
|
||||
return $resultSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the SearchResult of the variant and assigns it to the parent result document.
|
||||
*
|
||||
* @param ResponseAdapter $response
|
||||
* @param string $variantAccessKey
|
||||
* @param SearchResult $resultDocument
|
||||
*/
|
||||
protected function buildVariantDocumentAndAssignToParentResult(ResponseAdapter $response, $variantAccessKey, SearchResult $resultDocument)
|
||||
{
|
||||
foreach ($response->{'expanded'}->{$variantAccessKey}->{'docs'} as $variantDocumentArray) {
|
||||
$fields = get_object_vars($variantDocumentArray);
|
||||
$variantDocument = new SearchResult($fields);
|
||||
|
||||
$variantSearchResult = $this->resultBuilder->fromApacheSolrDocument($variantDocument);
|
||||
$variantSearchResult->setIsVariant(true);
|
||||
$variantSearchResult->setVariantParent($resultDocument);
|
||||
|
||||
$resultDocument->addVariant($variantSearchResult);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user