2016-12-09 11:47:28 +01:00
|
|
|
<?php
|
2017-07-06 23:48:47 +02:00
|
|
|
namespace Codappix\SearchCore\Hook;
|
2016-12-09 11:47:28 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-07-06 23:48:47 +02:00
|
|
|
use Codappix\SearchCore\Configuration\NoConfigurationException;
|
|
|
|
use Codappix\SearchCore\Domain\Service\DataHandler as OwnDataHandler;
|
2016-12-09 11:47:28 +01:00
|
|
|
use TYPO3\CMS\Backend\Utility\BackendUtility;
|
|
|
|
use TYPO3\CMS\Core\DataHandling\DataHandler as CoreDataHandler;
|
|
|
|
use TYPO3\CMS\Core\Log\LogManager;
|
|
|
|
use TYPO3\CMS\Core\Log\Logger;
|
|
|
|
use TYPO3\CMS\Core\SingletonInterface as Singleton;
|
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for TYPO3 Hooks to internal API.
|
|
|
|
*/
|
|
|
|
class DataHandler implements Singleton
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var OwnDataHandler
|
|
|
|
*/
|
|
|
|
protected $dataHandler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \TYPO3\CMS\Core\Log\Logger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dependency injection as TYPO3 doesn't provide it on it's own.
|
|
|
|
* Still you can submit your own dataHandler.
|
|
|
|
*/
|
|
|
|
public function __construct(OwnDataHandler $dataHandler = null, Logger $logger = null)
|
|
|
|
{
|
2018-03-06 17:40:49 +01:00
|
|
|
if ($dataHandler === null) {
|
2017-01-12 14:21:41 +01:00
|
|
|
try {
|
2018-03-06 17:40:49 +01:00
|
|
|
$dataHandler = GeneralUtility::makeInstance(ObjectManager::class)
|
2017-01-12 14:21:41 +01:00
|
|
|
->get(OwnDataHandler::class);
|
|
|
|
} catch (NoConfigurationException $e) {
|
|
|
|
// We have no configuration. That's fine, hooks will not be
|
|
|
|
// executed due to check for existing DataHandler.
|
|
|
|
}
|
2016-12-09 11:47:28 +01:00
|
|
|
}
|
2018-03-06 17:40:49 +01:00
|
|
|
$this->dataHandler = $dataHandler;
|
2016-12-09 11:47:28 +01:00
|
|
|
|
2018-03-06 17:40:49 +01:00
|
|
|
if ($logger === null) {
|
|
|
|
$logger = GeneralUtility::makeInstance(LogManager::class)
|
2016-12-09 11:47:28 +01:00
|
|
|
->getLogger(__CLASS__);
|
|
|
|
}
|
2018-03-06 17:40:49 +01:00
|
|
|
$this->logger = $logger;
|
2016-12-09 11:47:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by CoreDataHandler on deletion of records.
|
|
|
|
*/
|
2018-03-16 17:37:19 +01:00
|
|
|
public function processCmdmap_deleteAction(string $table, string $uid) : bool
|
2016-12-09 11:47:28 +01:00
|
|
|
{
|
2017-01-12 14:21:41 +01:00
|
|
|
if (! $this->shouldProcessHookForTable($table)) {
|
|
|
|
$this->logger->debug('Delete not processed.', [$table, $uid]);
|
2016-12-12 13:33:07 +01:00
|
|
|
return false;
|
2016-12-09 11:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-03-06 17:40:49 +01:00
|
|
|
$this->dataHandler->delete($table, (string) $uid);
|
2016-12-12 18:02:52 +01:00
|
|
|
return true;
|
2016-12-09 11:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-02-22 21:55:52 +01:00
|
|
|
public function processDatamap_afterAllOperations(CoreDataHandler $dataHandler)
|
2016-12-09 11:47:28 +01:00
|
|
|
{
|
2018-02-22 21:55:52 +01:00
|
|
|
foreach ($dataHandler->datamap as $table => $record) {
|
|
|
|
$uid = key($record);
|
|
|
|
$fieldData = current($record);
|
|
|
|
|
2018-03-06 17:58:19 +01:00
|
|
|
if (isset($fieldData['uid'])) {
|
|
|
|
$uid = $fieldData['uid'];
|
2018-02-22 21:55:52 +01:00
|
|
|
} elseif (isset($dataHandler->substNEWwithIDs[$uid])) {
|
|
|
|
$uid = $dataHandler->substNEWwithIDs[$uid];
|
|
|
|
}
|
|
|
|
|
2018-03-16 17:37:19 +01:00
|
|
|
if (!is_numeric($uid) || $uid <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-02-22 21:55:52 +01:00
|
|
|
$this->processRecord($table, $uid);
|
2016-12-09 11:47:28 +01:00
|
|
|
}
|
2018-02-22 21:55:52 +01:00
|
|
|
}
|
2016-12-09 11:47:28 +01:00
|
|
|
|
2018-03-08 20:43:30 +01:00
|
|
|
public function clearCachePostProc(array $parameters, CoreDataHandler $dataHandler)
|
|
|
|
{
|
|
|
|
$pageUid = 0;
|
|
|
|
|
|
|
|
// If editor uses "small page blizzard"
|
|
|
|
if (isset($parameters['cacheCmd']) && is_numeric($parameters['cacheCmd'])) {
|
|
|
|
$pageUid = $parameters['cacheCmd'];
|
|
|
|
}
|
|
|
|
// If records were changed
|
|
|
|
if (isset($parameters['uid_page']) && is_numeric($parameters['uid_page'])) {
|
|
|
|
$pageUid = $parameters['uid_page'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($pageUid > 0) {
|
|
|
|
$this->processRecord('pages', (int) $pageUid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 21:55:52 +01:00
|
|
|
protected function processRecord(string $table, int $uid) : bool
|
|
|
|
{
|
|
|
|
if (! $this->shouldProcessHookForTable($table)) {
|
|
|
|
$this->logger->debug('Indexing of record not processed.', [$table, $uid]);
|
|
|
|
return false;
|
2016-12-09 11:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-02-22 21:55:52 +01:00
|
|
|
$record = $this->getRecord($table, $uid);
|
|
|
|
if ($record !== null) {
|
|
|
|
$this->dataHandler->update($table, $record);
|
2016-12-15 14:02:40 +01:00
|
|
|
return true;
|
2016-12-09 11:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-02-22 21:55:52 +01:00
|
|
|
$this->logger->debug('Indexing of record not processed, as he was not found in Database.', [$table, $uid]);
|
2016-12-15 14:02:40 +01:00
|
|
|
return false;
|
2016-12-09 11:47:28 +01:00
|
|
|
}
|
|
|
|
|
2018-03-06 17:40:49 +01:00
|
|
|
protected function shouldProcessHookForTable(string $table) : bool
|
2017-01-12 14:21:41 +01:00
|
|
|
{
|
|
|
|
if ($this->dataHandler === null) {
|
|
|
|
$this->logger->debug('Datahandler could not be setup.');
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-06 17:58:19 +01:00
|
|
|
if (! $this->dataHandler->supportsTable($table)) {
|
2017-01-12 14:21:41 +01:00
|
|
|
$this->logger->debug('Table is not allowed.', [$table]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:47:28 +01:00
|
|
|
/**
|
|
|
|
* Wrapper to allow unit testing.
|
|
|
|
*
|
2018-03-06 17:40:49 +01:00
|
|
|
* @return array|null
|
2016-12-09 11:47:28 +01:00
|
|
|
*/
|
2018-03-06 17:40:49 +01:00
|
|
|
protected function getRecord(string $table, int $uid)
|
2016-12-09 11:47:28 +01:00
|
|
|
{
|
|
|
|
return BackendUtility::getRecord($table, $uid);
|
|
|
|
}
|
|
|
|
}
|