2016-12-09 13:19:35 +01:00
|
|
|
<?php
|
|
|
|
namespace Leonmrni\SearchCore\Domain\Index;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
|
|
|
|
use Leonmrni\SearchCore\Connection\ConnectionInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will index the given table using configuration from TCA.
|
|
|
|
*/
|
|
|
|
class TcaIndexer implements IndexerInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ConnectionInterface
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TcaIndexer\TcaTableService
|
|
|
|
*/
|
|
|
|
protected $tcaTableService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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__);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-09 14:07:38 +01:00
|
|
|
* @param TcaIndexer\TcaTableService $tcaTableService
|
2016-12-09 13:19:35 +01:00
|
|
|
* @param ConnectionInterface $connection
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
TcaIndexer\TcaTableService $tcaTableService,
|
|
|
|
ConnectionInterface $connection
|
|
|
|
) {
|
|
|
|
$this->tcaTableService = $tcaTableService;
|
|
|
|
$this->connection = $connection;
|
|
|
|
}
|
|
|
|
|
2016-12-15 09:17:58 +01:00
|
|
|
public function indexAllDocuments()
|
2016-12-09 13:19:35 +01:00
|
|
|
{
|
|
|
|
$this->logger->info('Start indexing');
|
|
|
|
foreach ($this->getRecordGenerator() as $records) {
|
|
|
|
$this->logger->debug('Index records.', [$records]);
|
2016-12-13 10:38:49 +01:00
|
|
|
if ($records === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:19:35 +01:00
|
|
|
$this->connection->addDocuments($this->tcaTableService->getTableName(), $records);
|
|
|
|
}
|
|
|
|
$this->logger->info('Finish indexing');
|
|
|
|
}
|
|
|
|
|
2016-12-15 09:17:58 +01:00
|
|
|
public function indexDocument($identifier)
|
2016-12-12 13:33:07 +01:00
|
|
|
{
|
|
|
|
$this->logger->info('Start indexing single record.', [$identifier]);
|
2016-12-15 09:17:58 +01:00
|
|
|
$this->connection->addDocument($this->tcaTableService->getTableName(), $this->getRecord($identifier));
|
2016-12-12 13:33:07 +01:00
|
|
|
$this->logger->info('Finish indexing');
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:19:35 +01:00
|
|
|
/**
|
2016-12-09 14:07:38 +01:00
|
|
|
* @return \Generator
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
|
|
|
protected function getRecordGenerator()
|
|
|
|
{
|
|
|
|
$offset = 0;
|
|
|
|
// TODO: Make configurable.
|
|
|
|
$limit = 50;
|
|
|
|
|
|
|
|
while (($records = $this->getRecords($offset, $limit)) !== []) {
|
|
|
|
yield $records;
|
|
|
|
$offset += $limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $offset
|
|
|
|
* @param int $limit
|
2016-12-13 10:38:49 +01:00
|
|
|
* @return array|null
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
|
|
|
protected function getRecords($offset, $limit)
|
|
|
|
{
|
|
|
|
$records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
|
|
|
|
$this->tcaTableService->getFields(),
|
2016-12-13 10:38:49 +01:00
|
|
|
$this->tcaTableService->getTableClause(),
|
2016-12-09 13:19:35 +01:00
|
|
|
$this->tcaTableService->getWhereClause(),
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
(int) $offset . ',' . (int) $limit
|
|
|
|
);
|
2016-12-15 11:32:41 +01:00
|
|
|
$this->tcaTableService->filterRecordsByRootLineBlacklist($records);
|
2016-12-09 13:19:35 +01:00
|
|
|
|
|
|
|
foreach ($records as &$record) {
|
|
|
|
$this->tcaTableService->prepareRecord($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $records;
|
|
|
|
}
|
2016-12-12 13:33:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $identifier
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getRecord($identifier)
|
|
|
|
{
|
|
|
|
$record = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
|
|
|
|
$this->tcaTableService->getFields(),
|
2016-12-13 10:38:49 +01:00
|
|
|
$this->tcaTableService->getTableClause(),
|
|
|
|
$this->tcaTableService->getWhereClause()
|
|
|
|
. ' AND ' . $this->tcaTableService->getTableName() . '.uid = ' . (int) $identifier
|
2016-12-12 13:33:07 +01:00
|
|
|
);
|
|
|
|
$this->tcaTableService->prepareRecord($record);
|
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|