|
<?php declare(strict_types=1); |
|
namespace My\Site\Query\Modifier; |
|
|
|
use ApacheSolrForTypo3\Solr\ConnectionManager; |
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder; |
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query; |
|
use ApacheSolrForTypo3\Solr\Query\Modifier\Modifier; |
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
|
use ApacheSolrForTypo3\Solr\Util; |
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
|
|
/** |
|
* Adds shard query parameter |
|
* |
|
* http://localhost:8983/solr/core_de/select?shards=localhost:8983/solr/core_de,localhost:8983/solr/core_en |
|
*/ |
|
class Shard implements Modifier |
|
{ |
|
/** |
|
* @var QueryBuilder |
|
*/ |
|
protected $queryBuilder; |
|
|
|
/** |
|
* @var ConnectionManager |
|
*/ |
|
protected $connectionManager; |
|
|
|
/** |
|
* @var TypoScriptConfiguration |
|
*/ |
|
protected $typoScriptConfiguration = null; |
|
|
|
public function __construct(QueryBuilder $builder = null) |
|
{ |
|
$this->queryBuilder = $builder ?? GeneralUtility::makeInstance(QueryBuilder::class); |
|
$this->typoScriptConfiguration = Util::getSolrConfiguration(); |
|
$this->connectionManager = GeneralUtility::makeInstance(ConnectionManager::class); |
|
} |
|
|
|
public function modifyQuery(Query $query): Query |
|
{ |
|
$languages = GeneralUtility::trimExplode(',', |
|
$this->typoScriptConfiguration->getValueByPath('plugin.tx_solr.search.query.shard.languages') ?? ''); |
|
if (count($languages) > 0) { |
|
$shards = []; |
|
foreach ($languages as $language) { |
|
$connection = $this->connectionManager->getConnectionByPageId($GLOBALS['TSFE']->id, (integer)$language); |
|
$node = $connection->getNode('read'); |
|
$shards[] = rtrim($node->__toString(), '/'); |
|
} |
|
$query->addParam('shards', implode(',', $shards)); |
|
} |
|
|
|
return $query; |
|
} |
|
} |