Skip to content

Commit

Permalink
Moved commands' db query functions to dedicated Gateway service
Browse files Browse the repository at this point in the history
  • Loading branch information
vidarl committed Oct 9, 2018
1 parent 0c3743f commit 0097608
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 280 deletions.
165 changes: 12 additions & 153 deletions bundle/Command/ConvertXmlTextToRichTextCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use eZ\Publish\Core\FieldType\XmlText\Value;
use eZ\Publish\Core\FieldType\XmlText\Converter\RichText as RichTextConverter;
use Doctrine\DBAL\Connection;
use Symfony\Component\Debug\Exception\ContextErrorException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\ProcessBuilder;
Expand All @@ -27,9 +26,9 @@ class ConvertXmlTextToRichTextCommand extends ContainerAwareCommand
const DEFAULT_REPOSITORY_USER = 'admin';

/**
* @var \Doctrine\DBAL\Connection
* @var Gateway
*/
private $dbal;
private $gateway;

/**
* @var \Psr\Log\LoggerInterface
Expand Down Expand Up @@ -81,11 +80,11 @@ class ConvertXmlTextToRichTextCommand extends ContainerAwareCommand
*/
protected $kernelCacheDir;

public function __construct(Connection $dbal, RichTextConverter $converter, $kernelCacheDir, LoggerInterface $logger)
public function __construct(Gateway $gateway, RichTextConverter $converter, $kernelCacheDir, LoggerInterface $logger)
{
parent::__construct();

$this->dbal = $dbal;
$this->gateway = $gateway;
$this->converter = $converter;
$this->kernelCacheDir = $kernelCacheDir;
$this->logger = $logger;
Expand Down Expand Up @@ -198,7 +197,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if ($testContentId === null) {
$this->convertFieldDefinitions($dryRun, $output);
$this->gateway->convertFieldDefinitions($dryRun, $output);
} else {
$dryRun = true;
$this->convertFields($dryRun, $testContentId, !$input->getOption('disable-duplicate-id-check'), !$input->getOption('disable-id-value-check'), null, null);
Expand Down Expand Up @@ -258,7 +257,7 @@ protected function baseExecute(InputInterface $input, OutputInterface $output, &
} else {
$this->imageContentTypeIdentifiers = ['image'];
}
$imageContentTypeIds = $this->getContentTypeIds($this->imageContentTypeIdentifiers);
$imageContentTypeIds = $this->gateway->getContentTypeIds($this->imageContentTypeIdentifiers);
if (count($imageContentTypeIds) !== count($this->imageContentTypeIdentifiers)) {
throw new RuntimeException('Unable to lookup all content type identifiers, not found: ' . implode(',', array_diff($this->imageContentTypeIdentifiers, array_keys($imageContentTypeIds))));
}
Expand Down Expand Up @@ -378,7 +377,7 @@ protected function login()

protected function fixEmbeddedImages($dryRun, $contentId, OutputInterface $output)
{
$count = $this->getRowCountOfContentObjectAttributes('ezrichtext', $contentId);
$count = $this->gateway->getRowCountOfContentObjectAttributes('ezrichtext', $contentId);

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

Expand All @@ -387,7 +386,7 @@ protected function fixEmbeddedImages($dryRun, $contentId, OutputInterface $outpu
do {
$limit = self::MAX_OBJECTS_PER_CHILD;

$statement = $this->getFieldRows('ezrichtext', $contentId, $offset, $limit);
$statement = $this->gateway->getFieldRows('ezrichtext', $contentId, $offset, $limit);
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
if (empty($row['data_text'])) {
$inputValue = Value::EMPTY_VALUE;
Expand Down Expand Up @@ -420,7 +419,7 @@ protected function fixEmbeddedImages($dryRun, $contentId, OutputInterface $outpu
]
);
} else {
$this->updateFieldRow($dryRun, $row['id'], $row['version'], $converted);
$this->gateway->updateFieldRow($dryRun, $row['id'], $row['version'], $converted);

$this->logger->info(
"Updated $updatedCount embded image(s) in ezrichtext field #{$row['id']}",
Expand All @@ -437,146 +436,6 @@ protected function fixEmbeddedImages($dryRun, $contentId, OutputInterface $outpu
$output->writeln("Updated ezembed tags in $totalCount field(s)");
}

protected function convertFieldDefinitions($dryRun, OutputInterface $output)
{
$query = $this->dbal->createQueryBuilder();
$query->select('count(a.id)')
->from('ezcontentclass_attribute', 'a')
->where(
$query->expr()->eq(
'a.data_type_string',
':datatypestring'
)
)
->setParameter(':datatypestring', 'ezxmltext');

$statement = $query->execute();
$count = (int) $statement->fetchColumn();

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

$updateQuery = $this->dbal->createQueryBuilder();
$updateQuery->update('ezcontentclass_attribute')
->set('data_type_string', ':newdatatypestring')
// was tagPreset in ezxmltext, unused in RichText
->set('data_text2', ':datatext2')
->where(
$updateQuery->expr()->eq(
'data_type_string',
':olddatatypestring'
)
)
->setParameters([
':newdatatypestring' => 'ezrichtext',
':datatext2' => null,
':olddatatypestring' => 'ezxmltext',
]);

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

$output->writeln("Converted $count ezxmltext field definitions to ezrichtext");
}

protected function getRowCountOfContentObjectAttributes($datatypeString, $contentId)
{
$query = $this->dbal->createQueryBuilder();
$query->select('count(a.id)')
->from('ezcontentobject_attribute', 'a')
->where(
$query->expr()->eq(
'a.data_type_string',
':datatypestring'
)
)
->setParameter(':datatypestring', $datatypeString);

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

$statement = $query->execute();

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

/**
* Get the specified field rows.
* Note that if $contentId !== null, then $offset and $limit will be ignored.
*
* @param $datatypeString
* @param $contentId
* @param $offset
* @param $limit
* @return \Doctrine\DBAL\Driver\Statement|int
*/
protected function getFieldRows($datatypeString, $contentId, $offset, $limit)
{
$query = $this->dbal->createQueryBuilder();
$query->select('a.*')
->from('ezcontentobject_attribute', 'a')
->where(
$query->expr()->eq(
'a.data_type_string',
':datatypestring'
)
)
->orderBy('a.id')
->setParameter(':datatypestring', $datatypeString);

if ($contentId === null) {
$query->setFirstResult($offset)
->setMaxResults($limit);
} else {
$query->andWhere(
$query->expr()->eq(
'a.contentobject_id',
':contentid'
)
)
->setParameter(':contentid', $contentId);
}

return $query->execute();
}

protected function updateFieldRow($dryRun, $id, $version, $datatext)
{
$updateQuery = $this->dbal->createQueryBuilder();
$updateQuery->update('ezcontentobject_attribute')
->set('data_type_string', ':datatypestring')
->set('data_text', ':datatext')
->where(
$updateQuery->expr()->eq(
'id',
':id'
)
)
->andWhere(
$updateQuery->expr()->eq(
'version',
':version'
)
)
->setParameters([
':datatypestring' => 'ezrichtext',
':datatext' => $datatext,
':id' => $id,
':version' => $version,
]);

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

protected function waitForAvailableProcessSlot(OutputInterface $output)
{
if (count($this->processes) >= $this->maxConcurrency) {
Expand Down Expand Up @@ -709,7 +568,7 @@ protected function dumpOnErrors($errors, $dataText, $contentobjectId, $contentob

protected function convertFields($dryRun, $contentId, $checkDuplicateIds, $checkIdValues, $offset, $limit)
{
$statement = $this->getFieldRows('ezxmltext', $contentId, $offset, $limit);
$statement = $this->gateway->getFieldRows('ezxmltext', $contentId, $offset, $limit);
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
if (empty($row['data_text'])) {
$inputValue = Value::EMPTY_VALUE;
Expand All @@ -730,7 +589,7 @@ protected function convertFields($dryRun, $contentId, $checkDuplicateIds, $check
$converted = $this->converter->convert($xmlDoc, $checkDuplicateIds, $checkIdValues, $row['id']);
$this->dumpOnErrors($this->converter->getErrors(), $row['data_text'], $row['contentobject_id'], $row['id'], $row['version'], $row['language_code']);

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

$this->logger->info(
"Converted ezxmltext field #{$row['id']} to richtext",
Expand All @@ -745,7 +604,7 @@ protected function convertFields($dryRun, $contentId, $checkDuplicateIds, $check

protected function processFields($dryRun, $checkDuplicateIds, $checkIdValues, OutputInterface $output)
{
$count = $this->getRowCountOfContentObjectAttributes('ezxmltext', null);
$count = $this->gateway->getRowCountOfContentObjectAttributes('ezxmltext', null);
$output->writeln("Found $count field rows to convert.");

$offset = 0;
Expand Down
Loading

0 comments on commit 0097608

Please sign in to comment.