Fix the reindex command

This commit is contained in:
Alexander Bigga 2021-11-24 21:31:56 +01:00
parent c0dd3409ad
commit d13a97018b
7 changed files with 73 additions and 116 deletions

View File

@ -12,11 +12,14 @@
namespace Kitodo\Dlf\Command;
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
use Symfony\Component\Console\Command\Command;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Object\ObjectManager;
/**
* Base class for CLI Command classes.
@ -29,18 +32,35 @@ use TYPO3\CMS\Core\Database\Connection;
class BaseCommand extends Command
{
/**
* Return starting point for indexing command.
* @var DocumentRepository
*/
protected $documentRepository;
/**
* Initialize the documentRepository based on the given storagePid.
*
* @param string|string[]|bool|null $inputPid possible pid
*
* @return int starting point for indexing command
* @return int|bool validated storagePid
*/
protected function getStartingPoint($inputPid): int
protected function initializeDocumentRepository($storagePid)
{
if (MathUtility::canBeInterpretedAsInteger($inputPid)) {
return MathUtility::forceIntegerInRange((int) $inputPid, 0);
if (MathUtility::canBeInterpretedAsInteger($storagePid)) {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$configurationManager = $objectManager->get(ConfigurationManager::class);
$frameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
$frameworkConfiguration['persistence']['storagePid'] = MathUtility::forceIntegerInRange((int) $storagePid, 0);
$configurationManager->setConfiguration($frameworkConfiguration);
$this->documentRepository = $objectManager->get(
DocumentRepository::class
);
} else {
return false;
}
return 0;
return MathUtility::forceIntegerInRange((int) $storagePid, 0);
}
/**

View File

@ -95,7 +95,7 @@ class HarvestCommand extends BaseCommand
* @param InputInterface $input The input parameters
* @param OutputInterface $output The Symfony interface for outputs on console
*
* @return void
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
@ -104,10 +104,8 @@ class HarvestCommand extends BaseCommand
$io = new SymfonyStyle($input, $output);
$io->title($this->getDescription());
$startingPoint = 0;
if (MathUtility::canBeInterpretedAsInteger($input->getOption('pid'))) {
$startingPoint = MathUtility::forceIntegerInRange((int) $input->getOption('pid'), 0);
}
$startingPoint = $this->initializeDocumentRepository($input->getOption('pid'));
if ($startingPoint == 0) {
$io->error('ERROR: No valid PID (' . $startingPoint . ') given.');
exit(1);
@ -254,6 +252,8 @@ class HarvestCommand extends BaseCommand
}
$io->success('All done!');
return 0;
}
/**

View File

@ -88,7 +88,7 @@ class IndexCommand extends BaseCommand
$io = new SymfonyStyle($input, $output);
$io->title($this->getDescription());
$startingPoint = $this->getStartingPoint($input->getOption('pid'));
$startingPoint = $this->initializeDocumentRepository($input->getOption('pid'));
if ($startingPoint == 0) {
$io->error('ERROR: No valid PID (' . $startingPoint . ') given.');
@ -164,6 +164,7 @@ class IndexCommand extends BaseCommand
}
$io->success('All done!');
return 0;
}
}

View File

@ -87,7 +87,7 @@ class ReindexCommand extends BaseCommand
* @param InputInterface $input The input parameters
* @param OutputInterface $output The Symfony interface for outputs on console
*
* @return void
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
@ -96,9 +96,9 @@ class ReindexCommand extends BaseCommand
$io = new SymfonyStyle($input, $output);
$io->title($this->getDescription());
$startingPoint = $this->getStartingPoint($input->getOption('pid'));
$startingPoint = $this->initializeDocumentRepository($input->getOption('pid'));
if ($startingPoint == 0) {
if ($startingPoint === false) {
$io->error('ERROR: No valid PID (' . $startingPoint . ') given.');
exit(1);
}
@ -141,7 +141,7 @@ class ReindexCommand extends BaseCommand
if (!empty($input->getOption('all'))) {
// Get all documents.
$documents = $this->getAllDocuments($startingPoint);
$documents = $this->documentRepository->findAll();
} elseif (
!empty($input->getOption('coll'))
&& !is_array($input->getOption('coll'))
@ -151,14 +151,15 @@ class ReindexCommand extends BaseCommand
$io->error('ERROR: Parameter --coll|-c is not a valid comma-separated list of collection UIDs.');
exit(1);
}
$documents = $this->getDocumentsToProceed($input->getOption('coll'), $startingPoint);
// Get all documents of given collections.
$documents = $this->documentRepository->findAllByCollectionsLimited(GeneralUtility::intExplode(',', $input->getOption('coll'), true), 0);
} else {
$io->error('ERROR: One of parameters --all|-a or --coll|-c must be given.');
exit(1);
}
foreach ($documents as $id => $document) {
$doc = Document::getInstance($document, ['storagePid' => $startingPoint], true);
$doc = Document::getInstance($document->getUid(), ['storagePid' => $startingPoint], true);
if ($doc->ready) {
if ($dryRun) {
$io->writeln('DRY RUN: Would index ' . $id . '/' . count($documents) . ' ' . $doc->uid . ' ("' . $doc->location . '") on PID ' . $startingPoint . ' and Solr core ' . $solrCoreUid . '.');
@ -179,102 +180,7 @@ class ReindexCommand extends BaseCommand
}
$io->success('All done!');
}
/**
* Fetches all documents with given collection.
*
* @param string $collId A comma separated list of collection UIDs
* @param int $pageId The PID of the collections' documents
*
* @return array Array of documents to index
*/
protected function getDocumentsToProceed(string $collIds, int $pageId): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$documents = [];
$result = $queryBuilder
->select('tx_dlf_documents.uid')
->from('tx_dlf_documents')
->join(
'tx_dlf_documents',
'tx_dlf_relations',
'tx_dlf_relations_joins',
$queryBuilder->expr()->eq(
'tx_dlf_relations_joins.uid_local',
'tx_dlf_documents.uid'
)
)
->join(
'tx_dlf_relations_joins',
'tx_dlf_collections',
'tx_dlf_collections_join',
$queryBuilder->expr()->eq(
'tx_dlf_relations_joins.uid_foreign',
'tx_dlf_collections_join.uid'
)
)
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->in(
'tx_dlf_collections_join.uid',
$queryBuilder->createNamedParameter(
GeneralUtility::intExplode(',', $collIds, true),
Connection::PARAM_INT_ARRAY
)
),
$queryBuilder->expr()->eq(
'tx_dlf_collections_join.pid',
$queryBuilder->createNamedParameter((int) $pageId, Connection::PARAM_INT)
),
$queryBuilder->expr()->eq(
'tx_dlf_relations_joins.ident',
$queryBuilder->createNamedParameter('docs_colls')
)
)
)
->groupBy('tx_dlf_documents.uid')
->orderBy('tx_dlf_documents.uid', 'ASC')
->execute();
while ($record = $result->fetch()) {
$documents[] = $record['uid'];
}
return $documents;
}
/**
* Fetches all documents of given page.
*
* @param int $pageId The documents' PID
*
* @return array Array of documents to index
*/
protected function getAllDocuments(int $pageId): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_documents');
$documents = [];
$pageId = (int) $pageId;
$result = $queryBuilder
->select('uid')
->from('tx_dlf_documents')
->where(
$queryBuilder->expr()->eq(
'tx_dlf_documents.pid',
$queryBuilder->createNamedParameter($pageId, Connection::PARAM_INT)
)
)
->orderBy('tx_dlf_documents.uid', 'ASC')
->execute();
while ($record = $result->fetch()) {
$documents[] = $record['uid'];
}
return $documents;
return 0;
}
}

View File

@ -123,7 +123,7 @@ class DocumentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
/**
* Finds all documents for the given collections
*
* @param array $collections separated by comma
* @param array $collections
* @param int $limit
*
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
@ -147,7 +147,10 @@ class DocumentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
$query->logicalAnd($constraints)
);
}
$query->setLimit((int) $limit);
if ($limit > 0) {
$query->setLimit((int) $limit);
}
return $query->execute();
}

View File

@ -15,6 +15,10 @@
* is the name of the command (to be called as the first argument after "typo3").
* Required parameter is the "class" of the command which needs to be a subclass
* of \Symfony\Component\Console\Command\Command.
*
* This file is deprecated in TYPO3 v10 and will be removed in TYPO3 v11.
* See Deprecation: #89139 - Console Commands configuration format Commands.php
* https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/10.3/Deprecation-89139-ConsoleCommandsConfigurationFormatCommandsPhp.html
*/
return [
'kitodo:harvest' => [

View File

@ -0,0 +1,23 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false
Kitodo\Dlf\:
resource: '../Classes/*'
Kitodo\Dlf\Command\HarvestCommand:
tags:
- name: 'console.command'
command: 'kitodo:harvest'
Kitodo\Dlf\Command\IndexCommand:
tags:
- name: 'console.command'
command: 'kitodo:index'
Kitodo\Dlf\Command\ReindexCommand:
tags:
- name: 'console.command'
command: 'kitodo:reindex'