Skip to content

Commit

Permalink
Ensured consistent naming of variables
Browse files Browse the repository at this point in the history
  • Loading branch information
vidarl committed May 3, 2018
1 parent 773d778 commit ac7d7a5
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 57 deletions.
75 changes: 51 additions & 24 deletions bundle/Command/ConvertXmlTextToRichTextCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PDO;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -75,7 +76,7 @@ protected function configure()
'image-content-types',
null,
InputOption::VALUE_OPTIONAL,
'Comma separated list of content types which are considered as images when converting embedded tags. Default value is 27'
'Comma separated list of content type identifiers which are considered as images when converting embedded tags. Default value is image'
)
->addOption(
'fix-embedded-images-only',
Expand All @@ -96,34 +97,59 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dryRun = true;
}

$testContentObjectId = $input->getOption('test-content-object');
$testContentId = $input->getOption('test-content-object');

if ($input->getOption('image-content-types')) {
$contentTypes = explode(',', $input->getOption('image-content-types'));
$contentTypeIdentifiers = explode(',', $input->getOption('image-content-types'));
} else {
$contentTypes = array(27);
$contentTypeIdentifiers = array('image');
}
$this->converter->setImageContentTypes($contentTypes);
$contentTypeIds = $this->getContentTypeIds($contentTypeIdentifiers);
if (count($contentTypeIds) !== count($contentTypeIdentifiers)) {
throw new RuntimeException('Unable to lookup all content type identifiers, found : ' . implode(',', $contentTypeIds));
}
$this->converter->setImageContentTypes($contentTypeIds);

if ($input->getOption('fix-embedded-images-only')) {
$output->writeln("Fixing embedded images only. No other changes are done to the database\n");
$this->fixEmbeddedImages($dryRun, $testContentObjectId, $output);
$this->fixEmbeddedImages($dryRun, $testContentId, $output);

return;
}

if ($testContentObjectId === null) {
if ($testContentId === null) {
$this->convertFieldDefinitions($dryRun, $output);
} else {
$dryRun = true;
}

$this->convertFields($dryRun, $testContentObjectId, !$input->getOption('disable-duplicate-id-check'), $output);
$this->convertFields($dryRun, $testContentId, !$input->getOption('disable-duplicate-id-check'), $output);
}

protected function getContentTypeIds($contentTypeIdentifiers)
{
$query = $this->dbal->createQueryBuilder();

$query->select('c.id')
->from('ezcontentclass', 'c')
->where(
$query->expr()->in(
'c.identifier',
':contentTypeIdentifiers'
)
)
->setParameter(':contentTypeIdentifiers', $contentTypeIdentifiers, Connection::PARAM_STR_ARRAY);

$statement = $query->execute();

return $statement->fetchAll(PDO::FETCH_COLUMN);
}

protected function loginAsAdmin()
{
$userService = $this->getContainer()->get('ezpublish.api.service.user');
$permissionResolver = $this->getContainer()->get('date_based_publisher.permission_resolver');
$repository = $this->getContainer()->get('ezpublish.api.repository');
$permissionResolver = $repository->getPermissionResolver();
$permissionResolver->setCurrentUserReference($userService->loadUserByLogin('admin'));
}

Expand Down Expand Up @@ -154,18 +180,17 @@ protected function fixEmbeddedImages($dryRun, $contentId, OutputInterface $outpu
$this->logger->info(
"No embedded image(s) in ezrichtext field #{$row['id']} needed to be updated",
[
'original' => $inputValue
'original' => $inputValue,
]
);

} else {
$this->updateFieldRow($dryRun, $row['id'], $row['version'], $converted);

$this->logger->info(
"Updated $count embded image(s) in ezrichtext field #{$row['id']}",
[
'original' => $inputValue,
'converted' => $converted
'converted' => $converted,
]
);
}
Expand Down Expand Up @@ -216,7 +241,7 @@ protected function convertFieldDefinitions($dryRun, OutputInterface $output)
$output->writeln("Converted $count ezxmltext field definitions to ezrichtext");
}

protected function getRowCountOfContentObjectAttributes($datatypeString, $contentObjectId)
protected function getRowCountOfContentObjectAttributes($datatypeString, $contentId)
{
$query = $this->dbal->createQueryBuilder();
$query->select('count(a.id)')
Expand All @@ -227,19 +252,20 @@ protected function getRowCountOfContentObjectAttributes($datatypeString, $conten
':datatypestring'
)
)
->setParameter(':datatypestring',$datatypeString);
->setParameter(':datatypestring', $datatypeString);

if ($contentObjectId !== null) {
if ($contentId !== null) {
$query->andWhere(
$query->expr()->eq(
'a.contentobject_id',
':contentobjectid'
':contentid'
)
)
->setParameter(':contentobjectid', $contentObjectId);
->setParameter(':contentid', $contentId);
}

$statement = $query->execute();

return (int) $statement->fetchColumn();
}

Expand All @@ -259,17 +285,18 @@ protected function getFieldRows($datatypeString, $contentId)
':datatypestring'
)
)
->setParameter(':datatypestring',$datatypeString);
->setParameter(':datatypestring', $datatypeString);

if ($contentId !== null) {
$query->andWhere(
$query->expr()->eq(
'a.contentobject_id',
':contentobjectid'
':contentid'
)
)
->setParameter(':contentobjectid', $contentId);
->setParameter(':contentid', $contentId);
}

return $query->execute();
}

Expand All @@ -295,21 +322,21 @@ protected function updateFieldRow($dryRun, $id, $version, $datatext)
':datatypestring' => 'ezrichtext',
':datatext' => $datatext,
':id' => $id,
':version' => $version
':version' => $version,
));

if (!$dryRun) {
$updateQuery->execute();
}
}

protected function convertFields($dryRun, $contentObjectId, $checkDuplicateIds, OutputInterface $output)
protected function convertFields($dryRun, $contentId, $checkDuplicateIds, OutputInterface $output)
{
$count = $this->getRowCountOfContentObjectAttributes('ezxmltext', $contentObjectId);
$count = $this->getRowCountOfContentObjectAttributes('ezxmltext', $contentId);

$output->writeln("Found $count field rows to convert.");

$statement = $this->getFieldRows('ezxmltext', $contentObjectId);
$statement = $this->getFieldRows('ezxmltext', $contentId);

while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
if (empty($row['data_text'])) {
Expand Down
2 changes: 1 addition & 1 deletion bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ services:
ezxmltext.richtext_converter:
class: eZ\Publish\Core\FieldType\XmlText\Converter\RichText
arguments:
- "@?logger"
- "@ezpublish.api.repository"
- "@?logger"
Loading

0 comments on commit ac7d7a5

Please sign in to comment.