From 76557f4e98bdee5fe267d95f019347a6a697cbd7 Mon Sep 17 00:00:00 2001 From: pawelpawlik Date: Mon, 11 Sep 2023 12:43:13 +0200 Subject: [PATCH] IBX-5385 use contentService to get content list for reindex --- src/bundle/Core/Command/ReindexCommand.php | 41 +++++++++++++++++-- src/bundle/Core/Resources/config/commands.yml | 1 + .../Search/Content/IndexerGateway.php | 12 ------ .../Search/Legacy/Content/IndexerGateway.php | 28 ------------- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/bundle/Core/Command/ReindexCommand.php b/src/bundle/Core/Command/ReindexCommand.php index f038cca0f7..0705a3c7ad 100644 --- a/src/bundle/Core/Command/ReindexCommand.php +++ b/src/bundle/Core/Command/ReindexCommand.php @@ -11,6 +11,10 @@ use const DIRECTORY_SEPARATOR; use Generator; use Ibexa\Contracts\Core\Persistence\Content\Location\Handler; +use Ibexa\Contracts\Core\Repository\ContentService; +use Ibexa\Contracts\Core\Repository\Values\Content\ContentList; +use Ibexa\Contracts\Core\Repository\Values\Content\Query; +use Ibexa\Contracts\Core\Repository\Values\Filter\Filter; use Ibexa\Contracts\Core\Search\Content\IndexerGateway; use Ibexa\Core\Base\Exceptions\InvalidArgumentException; use Ibexa\Core\Search\Common\IncrementalIndexer; @@ -55,6 +59,9 @@ class ReindexCommand extends Command implements BackwardCompatibleCommand /** @var \Ibexa\Contracts\Core\Persistence\Content\Location\Handler */ private $locationHandler; + /** @var \Ibexa\Contracts\Core\Repository\ContentService */ + private $contentService; + public function __construct( $searchIndexer, Handler $locationHandler, @@ -64,6 +71,7 @@ public function __construct( string $env, bool $isDebug, string $projectDir, + ContentService $contentService, string $phpPath = null ) { $this->gateway = $gateway; @@ -75,7 +83,7 @@ public function __construct( $this->env = $env; $this->isDebug = $isDebug; $this->projectDir = $projectDir; - $this->phpPath = $phpPath; + $this->contentService = $contentService; parent::__construct(); } @@ -262,8 +270,14 @@ protected function indexIncrementally( $generator = $this->gateway->getContentInSubtree($location->pathString, $iterationCount); $purge = false; } elseif ($contentType = $input->getOption('content-type')) { - $count = $this->gateway->countContentWithContentTypeIdentifier($contentType); - $generator = $this->gateway->getContentWithContentTypeIdentifier($contentType, $iterationCount); + $filter = new Filter(); + $filter + ->withCriterion( + new Query\Criterion\ContentTypeIdentifier($contentType) + ); + $contentList = $this->contentService->find($filter); + $count = $contentList->getTotalCount(); + $generator = $this->fetchIterationFromContentList($contentList, $iterationCount); $purge = false; } else { $count = $this->gateway->countAllContent(); @@ -463,6 +477,27 @@ public function getDeprecatedAliases(): array { return ['ezplatform:reindex']; } + + private function fetchIterationFromContentList(ContentList $contentList, int $iterationCount): Generator + { + $iterator = $contentList->getIterator(); + do { + $contentIds = []; + for ($i = 0; $i < $iterationCount; ++$i) { + $content = $iterator->current(); + if ($content) { + $contentIds[] = $content->id; + } elseif (empty($contentIds)) { + return; + } else { + break; + } + $iterator->next(); + } + + yield $contentIds; + } while (!empty($content)); + } } class_alias(ReindexCommand::class, 'eZ\Bundle\EzPublishCoreBundle\Command\ReindexCommand'); diff --git a/src/bundle/Core/Resources/config/commands.yml b/src/bundle/Core/Resources/config/commands.yml index fdfe05ed50..ae0854c3dd 100644 --- a/src/bundle/Core/Resources/config/commands.yml +++ b/src/bundle/Core/Resources/config/commands.yml @@ -42,6 +42,7 @@ services: $env: '%kernel.environment%' $projectDir: '%kernel.project_dir%' $isDebug: '%kernel.debug%' + $contentService: '@Ibexa\Contracts\Core\Repository\ContentService' tags: - { name: console.command } diff --git a/src/contracts/Search/Content/IndexerGateway.php b/src/contracts/Search/Content/IndexerGateway.php index 160edfe04d..8310154a9a 100644 --- a/src/contracts/Search/Content/IndexerGateway.php +++ b/src/contracts/Search/Content/IndexerGateway.php @@ -51,18 +51,6 @@ public function getAllContent(int $iterationCount): Generator; * @throws \Doctrine\DBAL\Exception */ public function countAllContent(): int; - - /** - * @throws \Doctrine\DBAL\Exception - * - * @return iterable list of Content IDs for each iteration - */ - public function getContentWithContentTypeIdentifier(string $contentTypeIdentifier, int $iterationCount): iterable; - - /** - * @throws \Doctrine\DBAL\Exception - */ - public function countContentWithContentTypeIdentifier(string $contentTypeIdentifier): int; } class_alias(IndexerGateway::class, 'eZ\Publish\SPI\Search\Content\IndexerGateway'); diff --git a/src/lib/Search/Legacy/Content/IndexerGateway.php b/src/lib/Search/Legacy/Content/IndexerGateway.php index 2cc7e49bda..18f82330e7 100644 --- a/src/lib/Search/Legacy/Content/IndexerGateway.php +++ b/src/lib/Search/Legacy/Content/IndexerGateway.php @@ -144,34 +144,6 @@ private function fetchIteration(ResultStatement $statement, int $iterationCount) yield $contentIds; } while (!empty($contentId)); } - - public function getContentWithContentTypeIdentifier(string $contentTypeIdentifier, int $iterationCount): Generator - { - $query = $this->buildQueryForContentWithContentTypeIdentifier($contentTypeIdentifier); - - yield from $this->fetchIteration($query->execute(), $iterationCount); - } - - public function countContentWithContentTypeIdentifier(string $contentTypeIdentifier): int - { - $query = $this->buildCountingQuery( - $this->buildQueryForContentWithContentTypeIdentifier($contentTypeIdentifier) - ); - - return (int)$query->execute()->fetchOne(); - } - - private function buildQueryForContentWithContentTypeIdentifier(string $contentTypeIdentifier): QueryBuilder - { - return $this->connection->createQueryBuilder() - ->select('DISTINCT c.id') - ->from('ezcontentobject', 'c') - ->innerJoin('c', 'ezcontentclass', 'cc', 'cc.id = c.contentclass_id') - ->where('c.status = :status') - ->andWhere('cc.identifier LIKE :identifier') - ->setParameter('status', ContentInfo::STATUS_PUBLISHED, ParameterType::INTEGER) - ->setParameter('identifier', $contentTypeIdentifier, ParameterType::STRING); - } } class_alias(IndexerGateway::class, 'eZ\Publish\Core\Search\Legacy\Content\IndexerGateway');