Skip to content

Commit

Permalink
NGSTACK-842: add support for processing items modified since number o…
Browse files Browse the repository at this point in the history
…f days
  • Loading branch information
pspanja committed Jun 28, 2024
1 parent c261c7b commit c2928af
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions bundle/Command/ScheduledVisibilityUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;

use Throwable;

use function count;
use function sprintf;

Expand All @@ -48,13 +48,21 @@ protected function configure(): void
$this->setDescription(
'Updates content visibility based on publish_from and publish_to attributes and configuration.',
);

$this->addOption(
'limit',
'l',
InputOption::VALUE_OPTIONAL,
'Number of content objects to process in a single iteration',
1024,
);

$this->addOption(
'since',
null,
InputOption::VALUE_OPTIONAL,
'Process Content Items modified since the given number of days',
);
}

protected function initialize(InputInterface $input, OutputInterface $output): void
Expand Down Expand Up @@ -95,7 +103,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::FAILURE;
}

$pager = $this->getPager($allContentTypes, $allowedContentTypes);
$since = $input->getOption('since');
$since = $since === null ? $since : (int) $since;
$pager = $this->getPager($allContentTypes, $allowedContentTypes, $since);

if ($pager->getNbResults() === 0) {
$this->style->info('No content found');
Expand Down Expand Up @@ -178,29 +188,46 @@ private function processResults(array $results, ProgressBar $progressBar): void
}
}

private function getQueryBuilder(): QueryBuilder
private function getQueryBuilder(?int $since): QueryBuilder
{
$query = $this->connection->createQueryBuilder();
$query
->select('id', 'initial_language_id')
->from('ezcontentobject')
->where('published != :unpublished')

->orderBy('id', 'ASC')
->setParameter('unpublished', 0);

$this->applySince($query, $since);

return $query;
}

private function applySince(QueryBuilder $query, ?int $since): void
{
if ($since === null) {
return;
}

$since = time() - ($since * 86400);

$query
->andWhere($query->expr()->gte('modified', ':modified'))
->setParameter('modified', $since)
;
}

private function applyContentTypeLimit(QueryBuilder $query, array $contentTypeIds): void
{
$query->where(
$query->expr()->in('contentclass_id', ':content_type_ids'),
)->setParameter('content_type_ids', $contentTypeIds, Connection::PARAM_INT_ARRAY);
}

private function getPager(bool $allContentTypes, array $allowedContentTypes): Pagerfanta
private function getPager(bool $allContentTypes, array $allowedContentTypes, ?int $since): Pagerfanta
{
$query = $this->getQueryBuilder();
$query = $this->getQueryBuilder($since);

$contentTypeIds = [];
if (!$allContentTypes && count($allowedContentTypes) > 0) {
Expand All @@ -225,13 +252,15 @@ private function getPager(bool $allContentTypes, array $allowedContentTypes): Pa
$this->applyContentTypeLimit($query, $contentTypeIds);
}

$countQueryBuilderModifier = function (QueryBuilder $queryBuilder) use ($contentTypeIds): void {
$countQueryBuilderModifier = function (QueryBuilder $queryBuilder) use ($contentTypeIds, $since): void {
$queryBuilder->select('COUNT(id) AS total_results')
->from('ezcontentobject')
->where('published != :unpublished')
->setParameter('unpublished', 0)
->setMaxResults(1);

$this->applySince($queryBuilder, $since);

if (count($contentTypeIds) > 0) {
$this->applyContentTypeLimit($queryBuilder, $contentTypeIds);
}
Expand Down

0 comments on commit c2928af

Please sign in to comment.