Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

CleanUpRelationTypeEqZeroCommand.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage;
8
9 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10 use Symfony\Component\Console\Helper\Table;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Question\ConfirmationQuestion;
15 use eZ\Publish\Core\Persistence\Legacy\Handler as LegacyStorageEngine;
16 use RuntimeException;
17
18 class CleanUpRelationTypeEqZeroCommand extends ContainerAwareCommand
19 {
20 protected function configure()
21 {
22 $this
23 ->setName('ezpublish:update:legacy_storage_clean_up_relation_type_eq_zero')
24 ->setDescription('Clean up invalid relation type in Legacy Storage database')
25 ->addArgument('action', InputArgument::REQUIRED, 'Action: ' . implode(', ', $this->getAvailableActions()))
26 ->setHelp(
27 <<<EOT
28 The command <info>%command.name%</info> cleans up corrupted relations in Legacy Storage database. See: https://jira.ez.no/browse/EZP-27254
29
30 <warning>During the script execution the database should not be modified.
31
32 To avoid surprises you are advised to create a backup or execute a dry run:
33
34 %command.name% dry-run
35
36 before proceeding with actual update.</warning>
37
38 Since this script can potentially run for a very long time, to avoid memory
39 exhaustion run it in production environment using the <info>--env=prod</info> switch.
40 EOT
41 );
42 }
43
44 protected function execute(InputInterface $input, OutputInterface $output)
45 {
46 $action = $input->getArgument('action');
47
48 if (!$this->isValidAction($action)) {
49 throw new RuntimeException(
50 "Action '{$action}' is not supported, use one of: " .
51 implode(', ', $this->getAvailableActions())
52 );
53 }
54
55 $this->checkStorage();
56
57 $totalCount = $this->getTotalCount();
58 $output->writeln('Found total relations to delete: ' . $totalCount);
59 if ($totalCount == 0) {
60 $output->writeln('Nothing to process, exiting.');
61
62 return;
63 }
64
65 if ($this->confirmExecution($input, $output)) {
66 switch ($action) {
67 case 'fix':
68 $this->executeFix();
69 break;
70 case 'list':
71 case 'dry-run':
72 $this->executeList($output, $totalCount);
73 break;
74 }
75 }
76
77 $output->writeln('');
78 }
79
83 protected function checkStorage()
84 {
85 $storageEngine = $this->getContainer()->get('ezpublish.api.storage_engine');
86
87 if (!$storageEngine instanceof LegacyStorageEngine) {
88 throw new RuntimeException(
89 'Expected to find Legacy Storage Engine but found something else.'
90 );
91 }
92 }
93
94 protected function confirmExecution(InputInterface $input, OutputInterface $output)
95 {
96 $question = new ConfirmationQuestion('<question>Are you sure you want to proceed?</question> ', false);
97
98 return $this
99 ->getHelper('question')
100 ->ask($input, $output, $question);
101 }
102
103 protected function executeFix()
104 {
106 $conn = $this->getContainer()->get('ezpublish.connection');
107 $conn->exec('DELETE FROM ezcontentobject_link WHERE relation_type = 0');
108 }
109
110 protected function executeList(OutputInterface $output, $totalCount)
111 {
112 $table = new Table($output);
113 $table->setHeaders([
114 'ID',
115 'FROM_CONTENTOBEJCT_ID',
116 'FROM_CONTENTOBJECT_VERSION',
117 'TO_CONTENTOBJECT_ID',
118 'RELATION_TYPE',
119 ]);
120 $table->addRows($this->getCorruptedRelations());
121 $table->render();
122 }
123
124 protected function getCorruptedRelations()
125 {
126 $sql = 'SELECT id, from_contentobject_id, from_contentobject_version, to_contentobject_id,'
127 . 'relation_type FROM ezcontentobject_link WHERE relation_type = 0';
128
130 $conn = $this->getContainer()->get('ezpublish.connection');
131
132 $stmt = $conn->prepare($sql);
133 $stmt->execute();
134
135 return $stmt->fetchAll(\PDO::FETCH_NUM);
136 }
137
138 protected function getTotalCount()
139 {
141 $conn = $this->getContainer()->get('ezpublish.connection');
142
143 $stmt = $conn->prepare('SELECT COUNT(id) FROM ezcontentobject_link WHERE relation_type = 0');
144 $stmt->execute();
145
146 return (int)$stmt->fetchColumn();
147 }
148
149 protected function getAvailableActions()
150 {
151 return ['fix', 'list', 'dry-run'];
152 }
153
154 protected function isValidAction($action)
155 {
156 return in_array($action, $this->getAvailableActions());
157 }
158 }
eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\CleanUpRelationTypeEqZeroCommand
Definition: CleanUpRelationTypeEqZeroCommand.php:19
eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\CleanUpRelationTypeEqZeroCommand\execute
execute(InputInterface $input, OutputInterface $output)
Definition: CleanUpRelationTypeEqZeroCommand.php:44
eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\CleanUpRelationTypeEqZeroCommand\getAvailableActions
getAvailableActions()
Definition: CleanUpRelationTypeEqZeroCommand.php:149
eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\CleanUpRelationTypeEqZeroCommand\isValidAction
isValidAction($action)
Definition: CleanUpRelationTypeEqZeroCommand.php:154
eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\CleanUpRelationTypeEqZeroCommand\executeList
executeList(OutputInterface $output, $totalCount)
Definition: CleanUpRelationTypeEqZeroCommand.php:110
eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\CleanUpRelationTypeEqZeroCommand\confirmExecution
confirmExecution(InputInterface $input, OutputInterface $output)
Definition: CleanUpRelationTypeEqZeroCommand.php:94
eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\CleanUpRelationTypeEqZeroCommand\checkStorage
checkStorage()
Checks that configured storage engine is Legacy Storage Engine.
Definition: CleanUpRelationTypeEqZeroCommand.php:83
eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\CleanUpRelationTypeEqZeroCommand\configure
configure()
Definition: CleanUpRelationTypeEqZeroCommand.php:20
eZ\Publish\Core\Persistence\Legacy\Handler
The main handler for Legacy Storage Engine.
Definition: Core/Persistence/Legacy/Handler.php:32
eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage
Definition: CleanUpRelationTypeEqZeroCommand.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.