Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-5385: Added option content-type to reindex command #259

Merged
merged 6 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 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\Content;
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 All @@ -25,6 +29,7 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use Traversable;

class ReindexCommand extends Command implements BackwardCompatibleCommand
{
Expand Down Expand Up @@ -55,6 +60,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;
papcio122 marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(
$searchIndexer,
Handler $locationHandler,
Expand All @@ -64,6 +72,7 @@ public function __construct(
string $env,
bool $isDebug,
string $projectDir,
ContentService $contentService,
string $phpPath = null
) {
$this->gateway = $gateway;
Expand All @@ -75,6 +84,7 @@ public function __construct(
$this->env = $env;
$this->isDebug = $isDebug;
$this->projectDir = $projectDir;
$this->contentService = $contentService;
$this->phpPath = $phpPath;

parent::__construct();
Expand Down Expand Up @@ -128,23 +138,28 @@ protected function configure()
'since',
null,
InputOption::VALUE_OPTIONAL,
'Refresh changes since a time provided in any format understood by DateTime. Implies "no-purge", cannot be combined with "content-ids" or "subtree"'
'Refresh changes since a time provided in any format understood by DateTime. Implies "no-purge", cannot be combined with "content-ids", "subtree", or "content-type"'
)->addOption(
'content-ids',
null,
InputOption::VALUE_OPTIONAL,
'Comma-separated list of content ID\'s to refresh (deleted/updated/added). Implies "no-purge", cannot be combined with "since" or "subtree"'
'Comma-separated list of content ID\'s to refresh (deleted/updated/added). Implies "no-purge", cannot be combined with "since", "subtree", or "content-type"'
)->addOption(
'subtree',
null,
InputOption::VALUE_OPTIONAL,
'Location ID whose subtree will be indexed (including the Location itself). Implies "no-purge", cannot be combined with "since" or "content-ids"'
'Location ID whose subtree will be indexed (including the Location itself). Implies "no-purge", cannot be combined with "since", "content-ids", or "content-type"'
)->addOption(
'processes',
null,
InputOption::VALUE_OPTIONAL,
'Number of child processes to run in parallel for iterations, if set to "auto" it will set to number of CPU cores -1, set to "1" or "0" to disable',
'auto'
)->addOption(
'content-type',
null,
InputOption::VALUE_REQUIRED,
'Content type identifier to refresh (deleted/updated/added). Implies "no-purge", cannot be combined with "since", "subtree", or "content-ids"'
)->setHelp(
<<<EOT
The command <info>%command.name%</info> indexes the current configured database in the configured search engine index.
Expand Down Expand Up @@ -256,6 +271,18 @@ protected function indexIncrementally(
$count = $this->gateway->countContentInSubtree($location->pathString);
$generator = $this->gateway->getContentInSubtree($location->pathString, $iterationCount);
$purge = false;
} elseif ($contentType = $input->getOption('content-type')) {
$filter = new Filter();
$filter
->withCriterion(
new Query\Criterion\ContentTypeIdentifier($contentType)
);
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\ContentList $contentList */
$contentList = $this->contentService->find($filter);
papcio122 marked this conversation as resolved.
Show resolved Hide resolved
$count = $contentList->getTotalCount();
$contentListIterator = $contentList->getIterator();
$generator = $this->fetchIterationFromContentListIterator($contentListIterator, $iterationCount);
$purge = false;
} else {
$count = $this->gateway->countAllContent();
$generator = $this->gateway->getAllContent($iterationCount);
Expand Down Expand Up @@ -454,6 +481,26 @@ public function getDeprecatedAliases(): array
{
return ['ezplatform:reindex'];
}

/**
* @param Traversable<Content> $contentListIterator
* @param int $iterationCount
*
* @return \Generator
papcio122 marked this conversation as resolved.
Show resolved Hide resolved
*/
private function fetchIterationFromContentListIterator(Traversable $contentListIterator, int $iterationCount): Generator
{
$i = 0;
$contentIds = [];
foreach ($contentListIterator as $content) {
$contentIds[] = $content->id;
if ($iterationCount <= $i) {
break;
}
++$i;
}
yield $contentIds;
}
papcio122 marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading