Skip to content

Commit

Permalink
EZP-29697: ezxmltext -> richtext conversion : Give user a report of c…
Browse files Browse the repository at this point in the history
…ustom tag usage (ezsystems#80)

* EZP-29697: ezxmltext -> richtext conversion : Give user a report of custom tag usage

* fixup! EZP-29697: ezxmltext -> richtext conversion : Give user a report of custom tag usage

* fixup! EZP-29697: ezxmltext -> richtext conversion : Give user a report of custom tag usage
  • Loading branch information
vidarl authored and andrerom committed Oct 5, 2018
1 parent c750d95 commit 2cedfa1
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
93 changes: 91 additions & 2 deletions bundle/Command/ConvertXmlTextToRichTextCommand.php
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
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;
Expand Down Expand Up @@ -75,13 +76,19 @@ class ConvertXmlTextToRichTextCommand extends ContainerAwareCommand
*/
protected $userLogin;

public function __construct(Connection $dbal, RichTextConverter $converter, LoggerInterface $logger)
/**
* @var string
*/
protected $kernelCacheDir;

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

$this->dbal = $dbal;
$this->logger = $logger;
$this->converter = $converter;
$this->kernelCacheDir = $kernelCacheDir;
$this->logger = $logger;
$this->exportDir = '';
}

Expand Down Expand Up @@ -170,6 +177,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->baseExecute($input, $output, $dryRun);
$this->createCustomTagLog();
if ($dryRun) {
$output->writeln('Running in dry-run mode. No changes will actually be written to database');
if ($this->exportDir !== '') {
Expand Down Expand Up @@ -199,6 +207,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$this->processFields($dryRun, !$input->getOption('disable-duplicate-id-check'), !$input->getOption('disable-id-value-check'), $output);
$this->reportCustomTags($input, $output);
$this->removeCustomTagLog();
}

protected function baseExecute(InputInterface $input, OutputInterface $output, &$dryRun)
Expand Down Expand Up @@ -255,6 +265,84 @@ protected function baseExecute(InputInterface $input, OutputInterface $output, &
$this->converter->setImageContentTypes($imageContentTypeIds);
}

protected function getCustomTagLogFileName()
{
return $this->kernelCacheDir . DIRECTORY_SEPARATOR . 'customtags.log';
}

protected function createCustomTagLog()
{
$this->removeCustomTagLog();
touch($this->getCustomTagLogFileName());
}

protected function writeCustomTagLog()
{
$customTagLog = $this->converter->getCustomTagLog();
if (count($customTagLog[RichTextConverter::INLINE_CUSTOM_TAG]) > 0) {
file_put_contents(
$this->getCustomTagLogFileName(),
RichTextConverter::INLINE_CUSTOM_TAG . ':' . implode(',', $customTagLog[RichTextConverter::INLINE_CUSTOM_TAG]) . PHP_EOL,
FILE_APPEND
);
}
if (count($customTagLog[RichTextConverter::BLOCK_CUSTOM_TAG]) > 0) {
file_put_contents($this->getCustomTagLogFileName(),
RichTextConverter::BLOCK_CUSTOM_TAG . ':' . implode(',', $customTagLog[RichTextConverter::BLOCK_CUSTOM_TAG]) . PHP_EOL,
FILE_APPEND
);
}
}

protected function removeCustomTagLog()
{
$filename = $this->getCustomTagLogFileName();
if (file_exists($filename)) {
unlink($filename);
}
}

protected function reportCustomTags(InputInterface $input, OutputInterface $output)
{
$customTagsFile = file_get_contents($this->getCustomTagLogFileName());
$separator = PHP_EOL;
$line = strtok($customTagsFile, $separator);
$inlines = [];
$blocks = [];

while ($line !== false) {
// line will have format 'inline:customtag1,customtag2'
$lineSplit = explode(':', $line);
switch ($lineSplit[0]) {
case RichTextConverter::INLINE_CUSTOM_TAG:
$inlines = array_merge($inlines, explode(',', $lineSplit[1]));
break;
case RichTextConverter::BLOCK_CUSTOM_TAG:
$blocks = array_merge($blocks, explode(',', $lineSplit[1]));
break;
}

$line = strtok($separator);
}
$inlines = array_unique($inlines, SORT_LOCALE_STRING);
$blocks = array_unique($blocks, SORT_LOCALE_STRING);

$io = new SymfonyStyle($input, $output);
$io->title('Custom tags overview');
$io->text('Below are the list of custom tags found during conversion of ezxmltext fields');
$io->section('Inline custom tags');
$io->listing($inlines);
if (count($inlines) === 0) {
$io->text('No inline custom tags converted');
}

$io->section('Block custom tags');
$io->listing($blocks);
if (count($blocks) === 0) {
$io->text('No block custom tags converted');
}
}

protected function getContentTypeIds($contentTypeIdentifiers)
{
$query = $this->dbal->createQueryBuilder();
Expand Down Expand Up @@ -652,6 +740,7 @@ protected function convertFields($dryRun, $contentId, $checkDuplicateIds, $check
]
);
}
$this->writeCustomTagLog();
}

protected function processFields($dryRun, $checkDuplicateIds, $checkIdValues, OutputInterface $output)
Expand Down
1 change: 1 addition & 0 deletions bundle/Resources/config/services.yml
Expand Up @@ -12,6 +12,7 @@ services:
arguments:
- "@ezpublish.persistence.connection"
- "@ezxmltext.richtext_converter"
- "%kernel.cache_dir%"
- "@?logger"
tags:
- { name: console.command }
Expand Down
30 changes: 27 additions & 3 deletions lib/FieldType/XmlText/Converter/RichText.php
Expand Up @@ -24,6 +24,8 @@

class RichText implements Converter
{
const INLINE_CUSTOM_TAG = 'inline';
const BLOCK_CUSTOM_TAG = 'block';
/**
* @var \eZ\Publish\Core\FieldType\RichText\Converter
*/
Expand Down Expand Up @@ -55,6 +57,11 @@ class RichText implements Converter
*/
private $errors;

/**
* @var []
*/
private $customTagsLog;

/**
* RichText constructor.
* @param null $apiRepository
Expand All @@ -69,6 +76,21 @@ public function __construct($apiRepository = null, LoggerInterface $logger = nul
$this->styleSheets = null;
$this->validator = null;
$this->converter = null;
$this->customTagsLog = [self::INLINE_CUSTOM_TAG => [], self::BLOCK_CUSTOM_TAG => []];
}

/**
* @param string $customTagType
* @param string $customTagName
*/
protected function logCustomTag($customTagType, $customTagName)
{
$this->customTagsLog[$customTagType][] = $customTagName;
}

public function getCustomTagLog()
{
return $this->customTagsLog;
}

/**
Expand Down Expand Up @@ -553,10 +575,12 @@ protected function writeWarningOnNonSupportedCustomTags(DOMDocument $document, $

if (!$blockCustomTag) {
$this->log(LogLevel::WARNING, "Inline custom tag '$customTagName' not supported by editor at the moment. You'll not be able to edit content correctly in editor where contentobject_attribute.id=$contentFieldId");
}

if ($parent->localName === 'section') {
$this->logCustomTag(self::INLINE_CUSTOM_TAG, $customTagName);
} elseif ($parent->localName === 'section') {
$this->log(LogLevel::WARNING, "Custom tag '$customTagName' converted to block custom tag. It might have been inline custom tag in legacy DB where contentobject_attribute.id=$contentFieldId");
$this->logCustomTag(self::BLOCK_CUSTOM_TAG, $customTagName);
} else {
$this->logCustomTag(self::BLOCK_CUSTOM_TAG, $customTagName);
}
}
}
Expand Down

0 comments on commit 2cedfa1

Please sign in to comment.