Skip to content

Commit

Permalink
IBX-5385 add option content-type to reindex command
Browse files Browse the repository at this point in the history
  • Loading branch information
papcio122 committed Aug 9, 2023
1 parent 6588c99 commit 978462f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/bundle/Core/Command/ReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,29 @@ 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 +262,10 @@ protected function indexIncrementally(
$count = $this->gateway->countContentInSubtree($location->pathString);
$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);
$purge = false;
} else {
$count = $this->gateway->countAllContent();
$generator = $this->gateway->getAllContent($iterationCount);
Expand Down
13 changes: 13 additions & 0 deletions src/contracts/Search/Content/IndexerGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ 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: 28 additions & 0 deletions src/lib/Search/Legacy/Content/IndexerGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,34 @@ 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 978462f

Please sign in to comment.