FEATURE: Switch from ttcontent to pages

Also provide search_abstract as new auto added field which is
configurable.
This commit is contained in:
Daniel Siepmann 2017-07-13 12:51:36 +02:00
parent c58e13cdf6
commit b6ab05bac7
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
6 changed files with 187 additions and 13 deletions

View file

@ -20,7 +20,9 @@ namespace Codappix\SearchCore\Domain\Index;
* 02110-1301, USA.
*/
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
use Codappix\SearchCore\Connection\ConnectionInterface;
use \TYPO3\CMS\Core\Utility\GeneralUtility;
abstract class AbstractIndexer implements IndexerInterface
{
@ -29,6 +31,16 @@ abstract class AbstractIndexer implements IndexerInterface
*/
protected $connection;
/**
* @var ConfigurationContainerInterface
*/
protected $configuration;
/**
* @var string
*/
protected $identifier;
/**
* @var \TYPO3\CMS\Core\Log\Logger
*/
@ -44,23 +56,34 @@ abstract class AbstractIndexer implements IndexerInterface
$this->logger = $logManager->getLogger(__CLASS__);
}
public function setIdentifier($identifier)
{
$this->identifier = $identifier;
}
/**
* @param ConnectionInterface $connection
* @param ConfigurationContainerInterface $configuration
*/
public function __construct(ConnectionInterface $connection)
public function __construct(ConnectionInterface $connection, ConfigurationContainerInterface $configuration)
{
$this->connection = $connection;
$this->configuration = $configuration;
}
public function indexAllDocuments()
{
$this->logger->info('Start indexing');
foreach ($this->getRecordGenerator() as $records) {
$this->logger->debug('Index records.', [$records]);
if ($records === null) {
break;
}
foreach ($records as &$record) {
$this->prepareRecord($record);
}
$this->logger->debug('Index records.', [$records]);
$this->connection->addDocuments($this->getDocumentName(), $records);
}
$this->logger->info('Finish indexing');
@ -70,7 +93,10 @@ abstract class AbstractIndexer implements IndexerInterface
{
$this->logger->info('Start indexing single record.', [$identifier]);
try {
$this->connection->addDocument($this->getDocumentName(), $this->getRecord($identifier));
$record = $this->getRecord($identifier);
$this->prepareRecord($record);
$this->connection->addDocument($this->getDocumentName(), $record);
} catch (NoRecordFoundException $e) {
$this->logger->info('Could not index document.', [$e->getMessage()]);
}
@ -92,6 +118,28 @@ abstract class AbstractIndexer implements IndexerInterface
}
}
/**
* @param array &$record
*/
protected function prepareRecord(array &$record)
{
$record['search_abstract'] = '';
$fieldsToUse = GeneralUtility::trimExplode(
',',
$this->configuration->getIfExists('indexing.' . $this->identifier . '.abstractFields')
);
if (!$fieldsToUse) {
return;
}
foreach ($fieldsToUse as $fieldToUse) {
if (isset($record[$fieldToUse]) && trim($record[$fieldToUse])) {
$record['search_abstract'] = trim($record[$fieldToUse]);
break;
}
}
}
/**
* @param int $offset
* @param int $limit

View file

@ -83,17 +83,30 @@ class IndexerFactory implements Singleton
*/
protected function buildIndexer($indexerClass, $identifier)
{
if ($indexerClass === TcaIndexer::class) {
return $this->objectManager->get(
TcaIndexer::class,
$indexer = null;
if (is_subclass_of($indexerClass, TcaIndexer\PagesIndexer::class)
|| $indexerClass === TcaIndexer\PagesIndexer::class
) {
$indexer = $this->objectManager->get(
$indexerClass,
$this->objectManager->get(TcaTableService::class, $identifier),
$this->objectManager->get(TcaTableService::class, 'tt_content')
);
} elseif (is_subclass_of($indexerClass, TcaIndexer::class) || $indexerClass === TcaIndexer::class) {
$indexer = $this->objectManager->get(
$indexerClass,
$this->objectManager->get(TcaTableService::class, $identifier)
);
} elseif (class_exists($indexerClass) && in_array(IndexerInterface::class, class_implements($indexerClass))) {
$indexer = $this->objectManager->get($indexerClass);
}
if (class_exists($indexerClass) && in_array(IndexerInterface::class, class_implements($indexerClass))) {
return $this->objectManager->get($indexerClass);
}
if ($indexer === null) {
throw new NoMatchingIndexerException('Could not find indexer: ' . $indexerClass, 1497341442);
}
$indexer->setIdentifier($identifier);
return $indexer;
}
}

View file

@ -40,4 +40,13 @@ interface IndexerInterface
* @return void
*/
public function indexDocument($identifier);
/**
* Recieves the identifier of the indexer itself.
*
* @param string $identifier
*
* @return void
*/
public function setIdentifier($identifier);
}

View file

@ -20,6 +20,7 @@ namespace Codappix\SearchCore\Domain\Index;
* 02110-1301, USA.
*/
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
use Codappix\SearchCore\Connection\ConnectionInterface;
/**
@ -35,13 +36,16 @@ class TcaIndexer extends AbstractIndexer
/**
* @param TcaIndexer\TcaTableService $tcaTableService
* @param ConnectionInterface $connection
* @param ConfigurationContainerInterface $configuration
*/
public function __construct(
TcaIndexer\TcaTableService $tcaTableService,
ConnectionInterface $connection
ConnectionInterface $connection,
ConfigurationContainerInterface $configuration
) {
$this->tcaTableService = $tcaTableService;
$this->connection = $connection;
$this->configuration = $configuration;
}
/**

View file

@ -0,0 +1,93 @@
<?php
namespace Codappix\SearchCore\Domain\Index\TcaIndexer;
/*
* 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.
*/
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface;
use Codappix\SearchCore\Connection\ConnectionInterface;
use Codappix\SearchCore\Domain\Index\TcaIndexer;
/**
* Specific indexer for Pages, will basically add content of page.
*/
class PagesIndexer extends TcaIndexer
{
/**
* @var TcaTableService
*/
protected $contentTableService;
/**
* @param TcaTableService $tcaTableService
* @param TcaTableService $tcaTableService
* @param ConnectionInterface $connection
* @param ConfigurationContainerInterface $configuration
*/
public function __construct(
TcaTableService $tcaTableService,
TcaTableService $contentTableService,
ConnectionInterface $connection,
ConfigurationContainerInterface $configuration
) {
$this->tcaTableService = $tcaTableService;
$this->contentTableService = $contentTableService;
$this->connection = $connection;
$this->configuration = $configuration;
}
/**
* @param array &$record
*/
protected function prepareRecord(array &$record)
{
parent::prepareRecord($record);
$record['content'] = $this->fetchContentForPage($record['uid']);
}
/**
* @param int $uid
* @return string
*/
protected function fetchContentForPage($uid)
{
$contentElements = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
$this->contentTableService->getFields(),
$this->contentTableService->getTableClause(),
$this->contentTableService->getWhereClause() .
sprintf(' AND %s.pid = %u', $this->contentTableService->getTableName(), $uid)
);
if ($contentElements === null) {
$this->logger->debug('No content for page ' . $uid);
return '';
}
$this->logger->debug('Fetched content for page ' . $uid);
$content = [];
foreach ($contentElements as $contentElement) {
$content[] = $contentElement['bodytext'];
}
// Remove Tags.
// Interpret escaped new lines and special chars.
// Trim, e.g. trailing or leading new lines.
return trim(stripcslashes(strip_tags(implode(' ', $content))));
}
}

View file

@ -102,6 +102,10 @@ class TcaTableService
*/
public function getTableClause()
{
if ($this->tableName === 'pages') {
return $this->tableName;
}
return $this->tableName . ' LEFT JOIN pages on ' . $this->tableName . '.pid = pages.uid';
}
@ -145,12 +149,15 @@ class TcaTableService
$whereClause = '1=1'
. BackendUtility::BEenableFields($this->tableName)
. BackendUtility::deleteClause($this->tableName)
. BackendUtility::BEenableFields('pages')
. BackendUtility::deleteClause('pages')
. ' AND pages.no_search = 0'
;
if ($this->tableName !== 'pages') {
$whereClause .= BackendUtility::BEenableFields('pages')
. BackendUtility::deleteClause('pages')
;
}
$userDefinedWhere = $this->configuration->getIfExists('indexing.' . $this->getTableName() . '.additionalWhereClause');
if (is_string($userDefinedWhere)) {
$whereClause .= ' AND ' . $userDefinedWhere;