Skip to content

Commit

Permalink
IBX-5385 use contentService to get content list for reindex
Browse files Browse the repository at this point in the history
  • Loading branch information
papcio122 committed Sep 11, 2023
1 parent 032693f commit 6f02412
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 43 deletions.
41 changes: 38 additions & 3 deletions src/bundle/Core/Command/ReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -64,6 +71,7 @@ public function __construct(
string $env,
bool $isDebug,
string $projectDir,
ContentService $contentService,
string $phpPath = null
) {
$this->gateway = $gateway;
Expand All @@ -75,7 +83,7 @@ public function __construct(
$this->env = $env;
$this->isDebug = $isDebug;
$this->projectDir = $projectDir;
$this->phpPath = $phpPath;
$this->contentService = $contentService;

parent::__construct();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
1 change: 1 addition & 0 deletions src/bundle/Core/Resources/config/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
12 changes: 0 additions & 12 deletions src/contracts/Search/Content/IndexerGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,6 @@ public function getAllContent(int $iterationCount): Generator;
* @throws \Doctrine\DBAL\Exception
*/
public function countAllContent(): int;

/**
* @throws \Doctrine\DBAL\Exception
*
* @return iterable<int> 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');
28 changes: 0 additions & 28 deletions src/lib/Search/Legacy/Content/IndexerGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit 6f02412

Please sign in to comment.