2017-07-04 12:12:36 +02:00
|
|
|
<?php
|
2017-07-06 23:48:47 +02:00
|
|
|
namespace Codappix\SearchCore\Domain\Index;
|
2017-07-04 12:12:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
|
|
*
|
|
|
|
* This program 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2017-07-13 12:51:36 +02:00
|
|
|
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
|
2017-12-19 17:02:18 +01:00
|
|
|
use Codappix\SearchCore\Configuration\InvalidArgumentException as InvalidConfigurationArgumentException;
|
2017-07-06 23:48:47 +02:00
|
|
|
use Codappix\SearchCore\Connection\ConnectionInterface;
|
2017-12-19 17:02:18 +01:00
|
|
|
use Codappix\SearchCore\DataProcessing\ProcessorInterface;
|
2017-07-13 12:51:36 +02:00
|
|
|
use \TYPO3\CMS\Core\Utility\GeneralUtility;
|
2017-07-04 12:12:36 +02:00
|
|
|
|
|
|
|
abstract class AbstractIndexer implements IndexerInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ConnectionInterface
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
2017-07-13 12:51:36 +02:00
|
|
|
/**
|
|
|
|
* @var ConfigurationContainerInterface
|
|
|
|
*/
|
|
|
|
protected $configuration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-07-27 16:31:37 +02:00
|
|
|
protected $identifier = '';
|
2017-07-13 12:51:36 +02:00
|
|
|
|
2017-07-04 12:12:36 +02:00
|
|
|
/**
|
|
|
|
* @var \TYPO3\CMS\Core\Log\Logger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inject log manager to get concrete logger from it.
|
|
|
|
*
|
|
|
|
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
|
|
|
|
*/
|
|
|
|
public function injectLogger(\TYPO3\CMS\Core\Log\LogManager $logManager)
|
|
|
|
{
|
|
|
|
$this->logger = $logManager->getLogger(__CLASS__);
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:51:36 +02:00
|
|
|
public function setIdentifier($identifier)
|
|
|
|
{
|
|
|
|
$this->identifier = $identifier;
|
|
|
|
}
|
|
|
|
|
2017-07-04 12:12:36 +02:00
|
|
|
/**
|
|
|
|
* @param ConnectionInterface $connection
|
2017-07-13 12:51:36 +02:00
|
|
|
* @param ConfigurationContainerInterface $configuration
|
2017-07-04 12:12:36 +02:00
|
|
|
*/
|
2017-07-13 12:51:36 +02:00
|
|
|
public function __construct(ConnectionInterface $connection, ConfigurationContainerInterface $configuration)
|
2017-07-04 12:12:36 +02:00
|
|
|
{
|
|
|
|
$this->connection = $connection;
|
2017-07-13 12:51:36 +02:00
|
|
|
$this->configuration = $configuration;
|
2017-07-04 12:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function indexAllDocuments()
|
|
|
|
{
|
|
|
|
$this->logger->info('Start indexing');
|
|
|
|
foreach ($this->getRecordGenerator() as $records) {
|
|
|
|
if ($records === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:51:36 +02:00
|
|
|
foreach ($records as &$record) {
|
|
|
|
$this->prepareRecord($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->debug('Index records.', [$records]);
|
2017-07-04 12:12:36 +02:00
|
|
|
$this->connection->addDocuments($this->getDocumentName(), $records);
|
|
|
|
}
|
|
|
|
$this->logger->info('Finish indexing');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function indexDocument($identifier)
|
|
|
|
{
|
|
|
|
$this->logger->info('Start indexing single record.', [$identifier]);
|
|
|
|
try {
|
2017-07-13 12:51:36 +02:00
|
|
|
$record = $this->getRecord($identifier);
|
|
|
|
$this->prepareRecord($record);
|
|
|
|
|
|
|
|
$this->connection->addDocument($this->getDocumentName(), $record);
|
2017-07-04 12:12:36 +02:00
|
|
|
} catch (NoRecordFoundException $e) {
|
|
|
|
$this->logger->info('Could not index document.', [$e->getMessage()]);
|
|
|
|
}
|
|
|
|
$this->logger->info('Finish indexing');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Generator
|
|
|
|
*/
|
|
|
|
protected function getRecordGenerator()
|
|
|
|
{
|
|
|
|
$offset = 0;
|
2017-07-20 09:48:44 +02:00
|
|
|
$limit = $this->getLimit();
|
2017-07-04 12:12:36 +02:00
|
|
|
|
|
|
|
while (($records = $this->getRecords($offset, $limit)) !== []) {
|
|
|
|
yield $records;
|
|
|
|
$offset += $limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:51:36 +02:00
|
|
|
/**
|
|
|
|
* @param array &$record
|
|
|
|
*/
|
|
|
|
protected function prepareRecord(array &$record)
|
|
|
|
{
|
2017-07-27 13:00:51 +02:00
|
|
|
try {
|
2017-12-19 17:02:18 +01:00
|
|
|
foreach ($this->configuration->get('indexing.' . $this->identifier . '.dataProcessing') as $configuration) {
|
|
|
|
$dataProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($configuration['_typoScriptNodeValue']);
|
|
|
|
if ($dataProcessor instanceof ProcessorInterface) {
|
|
|
|
$record = $dataProcessor->processRecord($record, $configuration);
|
2017-07-27 13:00:51 +02:00
|
|
|
}
|
2017-07-13 12:51:36 +02:00
|
|
|
}
|
2017-12-19 17:02:18 +01:00
|
|
|
} catch (InvalidConfigurationArgumentException $e) {
|
|
|
|
// Nothing to do.
|
2017-07-13 12:51:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 09:48:44 +02:00
|
|
|
/**
|
|
|
|
* Returns the limit to use to fetch records.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getLimit()
|
|
|
|
{
|
|
|
|
// TODO: Make configurable.
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
2017-07-04 12:12:36 +02:00
|
|
|
/**
|
|
|
|
* @param int $offset
|
|
|
|
* @param int $limit
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
abstract protected function getRecords($offset, $limit);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $identifier
|
|
|
|
* @return array
|
|
|
|
* @throws NoRecordFoundException If record could not be found.
|
|
|
|
*/
|
|
|
|
abstract protected function getRecord($identifier);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract protected function getDocumentName();
|
|
|
|
}
|