Skip to content

Commit

Permalink
NGSTACK-811 assing tag to content in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
Marijan Klaric committed Jul 23, 2024
1 parent 723eded commit c2ccf4d
Showing 1 changed file with 51 additions and 37 deletions.
88 changes: 51 additions & 37 deletions bundle/Command/TagContentByTypesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,59 +64,73 @@ protected function execute(InputInterface $input, OutputInterface $output): int
new Criterion\ContentTypeIdentifier($this->getContentTypes()),
]);

$searchResults = $this->repository->getSearchService()->findContent($query);
$totalCount = $searchResults->totalCount;
$batchSize = 50;

if ($this->input->getOption('field-identifiers') === null) {
$fieldIdentifiers = $this->configResolver->getParameter('tag_command_default_field_identifiers', 'ngsite');
} else {
$fieldIdentifiers = $this->getFieldIdentifiers();
}
$searchResults = $this->repository->getSearchService()->findContent($query);
$totalResults = $searchResults->totalCount;

$this->style->newLine();
$this->style->progressStart($totalCount);
$this->style->progressStart($totalResults);

Check failure on line 73 in bundle/Command/TagContentByTypesCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $max of method Symfony\Component\Console\Style\SymfonyStyle::progressStart() expects int, int|null given.

foreach ($searchResults->searchHits as $searchHit) {
$result = $this->repository->sudo(
function () use ($searchHit, $fieldIdentifiers): ?int {
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */
$content = $searchHit->valueObject;
$this->repository->beginTransaction();

$contentDraft = $this->repository->getContentService()->createContentDraft($content->contentInfo);
$contentUpdateStruct = $this->repository->getContentService()->newContentUpdateStruct();
for ($offset = 0; $offset < $totalResults; $offset += $batchSize) {
$query->offset = $offset;
$query->limit = $batchSize;
$searchResults = $this->repository->getSearchService()->findContent($query);

foreach ($fieldIdentifiers as $fieldIdentifier) {
if (!$this->hasField($content, $fieldIdentifier)) {
continue;
}
if ($this->input->getOption('field-identifiers') === null) {
$fieldIdentifiers = $this->configResolver->getParameter('tag_command_default_field_identifiers', 'ngsite');
} else {
$fieldIdentifiers = $this->getFieldIdentifiers();
}

if (!$content->getField($fieldIdentifier)->value instanceof TagFieldValue) {
$this->style->error(sprintf('Field with identifier %s must be a type of eztags', $fieldIdentifier));
return Command::FAILURE;
}
$this->repository->beginTransaction();

$alreadyAssignedTags = $content->getFieldValue($fieldIdentifier)->tags;
$tagsToAssign = array_filter($alreadyAssignedTags, fn ($alreadyAssignedTag) => $this->getTag()->id !== $alreadyAssignedTag->id);
$tagsToAssign[] = $this->getTag();
$contentUpdateStruct->setField($fieldIdentifier, new TagFieldValue($tagsToAssign));
foreach ($searchResults->searchHits as $searchHit) {
$result = $this->repository->sudo(
function () use ($searchHit, $fieldIdentifiers): ?int {
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content $content */
$content = $searchHit->valueObject;

break;
}
$contentDraft = $this->repository->getContentService()->createContentDraft($content->contentInfo);
$contentUpdateStruct = $this->repository->getContentService()->newContentUpdateStruct();

$this->repository->getContentService()->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
$this->repository->getContentService()->publishVersion($contentDraft->versionInfo);
foreach ($fieldIdentifiers as $fieldIdentifier) {
if (!$this->hasField($content, $fieldIdentifier)) {
continue;
}

$this->style->progressAdvance();
if (!$content->getField($fieldIdentifier)->value instanceof TagFieldValue) {

Check failure on line 104 in bundle/Command/TagContentByTypesCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot access property $value on Ibexa\Contracts\Core\Repository\Values\Content\Field|null.
$this->style->error(sprintf('Field with identifier %s must be a type of eztags', $fieldIdentifier));
return Command::FAILURE;
}

return null;
}
);
$alreadyAssignedTags = $content->getFieldValue($fieldIdentifier)->tags;
$tagsToAssign = array_filter($alreadyAssignedTags, fn ($alreadyAssignedTag) => $this->getTag()->id !== $alreadyAssignedTag->id);
$tagsToAssign[] = $this->getTag();
$contentUpdateStruct->setField($fieldIdentifier, new TagFieldValue($tagsToAssign));

if ($result === Command::FAILURE) {
return Command::FAILURE;
break;
}

$this->repository->getContentService()->updateContent($contentDraft->versionInfo, $contentUpdateStruct);
$this->repository->getContentService()->publishVersion($contentDraft->versionInfo);

$this->style->progressAdvance();

return null;
}
);

if ($result === Command::FAILURE) {
return Command::FAILURE;
}
}
}

$this->repository->commit();

$this->style->progressFinish();
$this->style->success('Tags assigned successfully');

Expand Down

0 comments on commit c2ccf4d

Please sign in to comment.