forked from ezsystems/ezplatform-xmltext-fieldtype
/
ConvertXmlTextToRichTextCommand.php
603 lines (526 loc) · 20.7 KB
/
ConvertXmlTextToRichTextCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzPlatformXmlTextFieldTypeBundle\Command;
use DOMDocument;
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;
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;
class ConvertXmlTextToRichTextCommand extends ContainerAwareCommand
{
const MAX_OBJECTS_PER_CHILD = 1000;
const DEFAULT_REPOSITORY_USER = 'admin';
/**
* @var \Doctrine\DBAL\Connection
*/
private $dbal;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* @var RichTextConverter
*/
private $converter;
/**
* @var array.
*/
protected $imageContentTypeIdentifiers;
/**
* @var array
*/
protected $processes = [];
/**
* @var int
*/
protected $maxConcurrency;
/**
* @var string
*/
private $phpPath;
/**
* @var string
*/
protected $userLogin;
public function __construct(Connection $dbal, RichTextConverter $converter, LoggerInterface $logger)
{
parent::__construct();
$this->dbal = $dbal;
$this->logger = $logger;
$this->converter = $converter;
}
protected function configure()
{
$this
->setName('ezxmltext:convert-to-richtext')
->setDescription(<<< EOT
Converts XmlText fields from eZ Publish Platform to RichText fields.
== WARNING ==
ALWAYS make sure you have a restorable backup of your database before using this!
EOT
)
->addOption(
'concurrency',
null,
InputOption::VALUE_OPTIONAL,
'Number of child processes to use when converting fields.',
1
)
->addOption(
'dry-run',
null,
InputOption::VALUE_NONE,
'Run the converter without writing anything to the database'
)
->addOption(
'disable-duplicate-id-check',
null,
InputOption::VALUE_NONE,
'Disable the check for duplicate html ids in every attribute. This might decrease execution time on large databases'
)
->addOption(
'disable-id-value-check',
null,
InputOption::VALUE_NONE,
'Disable the check for non-validating id/name values. This might decrease execution time on large databases'
)
->addOption(
'test-content-object',
null,
InputOption::VALUE_OPTIONAL,
'Test if converting object with the given id succeeds'
)
->addOption(
'image-content-types',
null,
InputOption::VALUE_OPTIONAL,
'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',
null,
InputOption::VALUE_NONE,
"Use this option to ensure that embedded images in a database are tagget correctly so that the editor will detect them as such.\n
This option is needed if you have an existing ezplatform database which was converted with an earlier version of\n
'ezxmltext:convert-to-richtext' which did not convert embedded images correctly."
)
->addOption(
'user',
'u',
InputOption::VALUE_OPTIONAL,
'eZ Platform username (with Role containing at least Content policies: read, versionread)',
self::DEFAULT_REPOSITORY_USER
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->baseExecute($input, $output, $dryRun);
if ($dryRun) {
$output->writeln("Running in dry-run mode. No changes will actually be written to database\n");
}
$testContentId = $input->getOption('test-content-object');
if ($testContentId !== null && $this->maxConcurrency !== 1) {
throw new RuntimeException('Multi concurrency is not supported together with the --test-content-object option');
}
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, $testContentId, $output);
return;
}
if ($testContentId === null) {
$this->convertFieldDefinitions($dryRun, $output);
} else {
$dryRun = true;
$this->convertFields($dryRun, $testContentId, !$input->getOption('disable-duplicate-id-check'), !$input->getOption('disable-id-value-check'), null, null);
return;
}
$this->processFields($dryRun, !$input->getOption('disable-duplicate-id-check'), !$input->getOption('disable-id-value-check'), $output);
}
protected function baseExecute(InputInterface $input, OutputInterface $output, &$dryRun)
{
$this->userLogin = $input->getOption('user');
$this->login();
$dryRun = false;
if ($input->getOption('dry-run')) {
$dryRun = true;
}
$this->maxConcurrency = (int) $input->getOption('concurrency');
if ($this->maxConcurrency < 1) {
throw new RuntimeException('Invalid value for "--concurrency" given');
}
if ($input->getOption('fix-embedded-images-only') && $this->maxConcurrency !== 1) {
throw new RuntimeException('Multi concurrency is not supported together with the --fix-embedded-images-only option');
}
if ($input->getOption('image-content-types')) {
$this->imageContentTypeIdentifiers = explode(',', $input->getOption('image-content-types'));
} else {
$this->imageContentTypeIdentifiers = ['image'];
}
$imageContentTypeIds = $this->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))));
}
$this->converter->setImageContentTypes($imageContentTypeIds);
}
protected function getContentTypeIds($contentTypeIdentifiers)
{
$query = $this->dbal->createQueryBuilder();
$query->select('c.identifier, c.id')
->from('ezcontentclass', 'c')
->where(
$query->expr()->in(
'c.identifier',
':contentTypeIdentifiers'
)
)
->setParameter(':contentTypeIdentifiers', $contentTypeIdentifiers, Connection::PARAM_STR_ARRAY);
$statement = $query->execute();
return array_map('reset', $statement->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_COLUMN));
}
protected function login()
{
$userService = $this->getContainer()->get('ezpublish.api.service.user');
$repository = $this->getContainer()->get('ezpublish.api.repository');
$permissionResolver = $repository->getPermissionResolver();
$permissionResolver->setCurrentUserReference($userService->loadUserByLogin($this->userLogin));
}
protected function fixEmbeddedImages($dryRun, $contentId, OutputInterface $output)
{
$count = $this->getRowCountOfContentObjectAttributes('ezrichtext', $contentId);
$output->writeln("Found $count field rows to convert.");
$offset = 0;
$totalCount = 0;
do {
$limit = self::MAX_OBJECTS_PER_CHILD;
$statement = $this->getFieldRows('ezrichtext', $contentId, $offset, $limit);
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
if (empty($row['data_text'])) {
$inputValue = Value::EMPTY_VALUE;
} else {
$inputValue = $row['data_text'];
}
try {
$xmlDoc = $this->createDocument($inputValue);
} catch (RuntimeException $e) {
$this->logger->info(
$e->getMessage(),
[
'original' => $inputValue,
]
);
continue;
}
$updatedCount = $this->converter->tagEmbeddedImages($xmlDoc, $row['id']);
if ($updatedCount > 0) {
++$totalCount;
}
$converted = $xmlDoc->saveXML();
if ($updatedCount === 0) {
$this->logger->info(
"No embedded image(s) in ezrichtext field #{$row['id']} needed to be updated",
[
'original' => $inputValue,
]
);
} else {
$this->updateFieldRow($dryRun, $row['id'], $row['version'], $converted);
$this->logger->info(
"Updated $updatedCount embded image(s) in ezrichtext field #{$row['id']}",
[
'original' => $inputValue,
'converted' => $converted,
]
);
}
}
$offset += self::MAX_OBJECTS_PER_CHILD;
} while ($offset + self::MAX_OBJECTS_PER_CHILD <= $count);
$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', 'a')
->set('a.data_type_string', ':newdatatypestring')
// was tagPreset in ezxmltext, unused in RichText
->set('a.data_text2', ':datatext2')
->where(
$updateQuery->expr()->eq(
'a.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', 'a')
->set('a.data_type_string', ':datatypestring')
->set('a.data_text', ':datatext')
->where(
$updateQuery->expr()->eq(
'a.id',
':id'
)
)
->andWhere(
$updateQuery->expr()->eq(
'a.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) {
$this->waitForChild($output);
}
}
protected function waitForChild(OutputInterface $output)
{
$childEnded = false;
while (!$childEnded) {
foreach ($this->processes as $pid => $p) {
$process = $p['process'];
if (!$process->isRunning()) {
$output->write($process->getIncrementalOutput());
$output->write($process->getIncrementalErrorOutput());
$childEnded = true;
$exitStatus = $process->getExitCode();
if ($exitStatus !== 0) {
throw new RuntimeException(sprintf('Child process (offset=%s, limit=%s) ended with status code %d. Terminating', $p['offset'], $p['limit'], $exitStatus));
}
unset($this->processes[$pid]);
break;
}
$output->write($process->getIncrementalOutput());
$output->write($process->getIncrementalErrorOutput());
}
sleep(1);
}
return;
}
private function createChildProcess($dryRun, $checkDuplicateIds, $checkIdValues, $offset, $limit, OutputInterface $output)
{
$arguments = [
file_exists('bin/console') ? 'bin/console' : 'app/console',
'ezxmltext:convert-to-richtext-sub-process',
"--offset=$offset",
"--limit=$limit",
'--image-content-types=' . implode(',', $this->imageContentTypeIdentifiers),
"--user=$this->userLogin",
];
$memoryLimit = ini_get('memory_limit');
if ($memoryLimit !== null) {
array_unshift($arguments, "memory_limit=$memoryLimit");
array_unshift($arguments, '-d');
}
if ($dryRun) {
$arguments[] = '--dry-run';
}
if (!$checkDuplicateIds) {
$arguments[] = '--disable-duplicate-id-check';
}
if (!$checkIdValues) {
$arguments[] = '--disable-id-value-check';
}
if ($output->isVerbose()) {
$arguments[] = '-v';
} elseif ($output->isVeryVerbose()) {
$arguments[] = '-vv';
} elseif ($output->isDebug()) {
$arguments[] = '-vvv';
}
$process = new ProcessBuilder($arguments);
$process->setTimeout(null);
$process->setPrefix($this->getPhpPath());
$p = $process->getProcess();
$p->start();
return $p;
}
private function getPhpPath()
{
if ($this->phpPath) {
return $this->phpPath;
}
$phpFinder = new PhpExecutableFinder();
$this->phpPath = $phpFinder->find();
if (!$this->phpPath) {
throw new \RuntimeException(
'The php executable could not be found, it\'s needed for executing parable sub processes, so add it to your PATH environment variable and try again'
);
}
return $this->phpPath;
}
protected function convertFields($dryRun, $contentId, $checkDuplicateIds, $checkIdValues, $offset, $limit)
{
$statement = $this->getFieldRows('ezxmltext', $contentId, $offset, $limit);
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
if (empty($row['data_text'])) {
$inputValue = Value::EMPTY_VALUE;
} else {
$inputValue = $row['data_text'];
}
try {
$xmlDoc = $this->createDocument($inputValue);
} catch (RuntimeException $e) {
$this->logger->info(
$e->getMessage(),
[
'original' => $inputValue,
]
);
}
$converted = $this->converter->convert($xmlDoc, $checkDuplicateIds, $checkIdValues, $row['id']);
$this->updateFieldRow($dryRun, $row['id'], $row['version'], $converted);
$this->logger->info(
"Converted ezxmltext field #{$row['id']} to richtext",
[
'original' => $inputValue,
'converted' => $converted,
]
);
}
}
protected function processFields($dryRun, $checkDuplicateIds, $checkIdValues, OutputInterface $output)
{
$count = $this->getRowCountOfContentObjectAttributes('ezxmltext', null);
$output->writeln("Found $count field rows to convert.");
$offset = 0;
$fork = $this->maxConcurrency > 1;
do {
$limit = self::MAX_OBJECTS_PER_CHILD;
if ($fork) {
$this->waitForAvailableProcessSlot($output);
$process = $this->createChildProcess($dryRun, $checkDuplicateIds, $checkIdValues, $offset, $limit, $output);
$this->processes[$process->getPid()] = ['offset' => $offset, 'limit' => $limit, 'process' => $process];
} else {
$this->convertFields($dryRun, null, $checkDuplicateIds, $checkIdValues, $offset, $limit);
}
$offset += self::MAX_OBJECTS_PER_CHILD;
} while ($offset + self::MAX_OBJECTS_PER_CHILD <= $count);
while (count($this->processes) > 0) {
$this->waitForChild($output);
}
$output->writeln("Converted $count ezxmltext fields to richtext");
}
protected function createDocument($xmlString)
{
$document = new DOMDocument();
$document->preserveWhiteSpace = false;
$document->formatOutput = false;
// In dev mode, symfony may throw Symfony\Component\Debug\Exception\ContextErrorException
try {
$result = $document->loadXml($xmlString);
if ($result === false) {
throw new RuntimeException('Unable to parse ezxmltext. Invalid XML format');
}
} catch (ContextErrorException $e) {
throw new RuntimeException($e->getMessage(), $e->getCode());
}
return $document;
}
}