Skip to content

Commit

Permalink
Adding new options to conversion cmd : --dry-run and --contentobject (e…
Browse files Browse the repository at this point in the history
…zsystems#37)

* Added --dry-run option

* Added option --test-content-object=... to ezxmltext:convert-to-richtext CLI
  • Loading branch information
vidarl committed Apr 24, 2018
1 parent 84e0141 commit 4f71a7f
Showing 1 changed file with 74 additions and 19 deletions.
93 changes: 74 additions & 19 deletions bundle/Command/ConvertXmlTextToRichTextCommand.php
Expand Up @@ -11,6 +11,7 @@
use PDO;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use eZ\Publish\Core\FieldType\RichText\Converter\Aggregate;
Expand Down Expand Up @@ -84,16 +85,40 @@ protected function configure()
This is a non-finalized work in progress. ALWAYS make sure you have a restorable backup of your database before using it.
EOT
);
)
->addOption(
'dry-run',
null,
InputOption::VALUE_NONE,
'Run the converter without writing anything to the database'
)
->addOption(
'test-content-object',
null,
InputOption::VALUE_OPTIONAL,
'Test if converting object with the given id succeeds'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->convertFieldDefinitions($output);
$this->convertFields($output);
$dryRun = false;
if ($input->getOption('dry-run')) {
$output->writeln("Running in dry-run mode. No changes will actually be written to database\n");
$dryRun = true;
}

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

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

function convertFieldDefinitions(OutputInterface $output)
function convertFieldDefinitions($dryRun, OutputInterface $output)
{
$query = $this->db->createSelectQuery();
$query->select($query->expr->count('*'));
Expand Down Expand Up @@ -142,22 +167,37 @@ function convertFieldDefinitions(OutputInterface $output)
)
);

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

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

function convertFields(OutputInterface $output)
function convertFields($dryRun, $contentObjectId, OutputInterface $output)
{
$query = $this->db->createSelectQuery();
$query->select($query->expr->count('*'));
$query->from('ezcontentobject_attribute');
$query->where(
$query->expr->eq(
$this->db->quoteIdentifier('data_type_string'),
$query->bindValue('ezxmltext', null, PDO::PARAM_STR)
)
);
if ($contentObjectId === null) {
$query->where(
$query->expr->eq(
$this->db->quoteIdentifier('data_type_string'),
$query->bindValue('ezxmltext', null, PDO::PARAM_STR)
)
);
} else {
$query->where(
$query->expr->eq(
$this->db->quoteIdentifier('contentobject_id'),
$query->bindValue($contentObjectId, null, PDO::PARAM_STR)
),
$query->expr->eq(
$this->db->quoteIdentifier('data_type_string'),
$query->bindValue('ezxmltext', null, PDO::PARAM_STR)
)
);
}

$statement = $query->prepare();
$statement->execute();
Expand All @@ -168,12 +208,25 @@ function convertFields(OutputInterface $output)
$query = $this->db->createSelectQuery();
$query->select('*');
$query->from('ezcontentobject_attribute');
$query->where(
$query->expr->eq(
$this->db->quoteIdentifier('data_type_string'),
$query->bindValue('ezxmltext', null, PDO::PARAM_STR)
)
);
if ($contentObjectId === null) {
$query->where(
$query->expr->eq(
$this->db->quoteIdentifier('data_type_string'),
$query->bindValue('ezxmltext', null, PDO::PARAM_STR)
)
);
} else {
$query->where(
$query->expr->eq(
$this->db->quoteIdentifier('contentobject_id'),
$query->bindValue($contentObjectId, null, PDO::PARAM_STR)
),
$query->expr->eq(
$this->db->quoteIdentifier('data_type_string'),
$query->bindValue('ezxmltext', null, PDO::PARAM_STR)
)
);
}

$statement = $query->prepare();
$statement->execute();
Expand Down Expand Up @@ -209,7 +262,9 @@ function convertFields(OutputInterface $output)
)
)
);
$updateQuery->prepare()->execute();
if (!$dryRun) {
$updateQuery->prepare()->execute();
}

$this->logger->info(
"Converted ezxmltext field #{$row['id']} to richtext",
Expand Down

0 comments on commit 4f71a7f

Please sign in to comment.