1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package midcom.console |
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace midcom\console\command; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use midgard\portable\storage\connection; |
15
|
|
|
use PDO; |
16
|
|
|
use Symfony\Component\Console\Question\Question; |
17
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
18
|
|
|
use midgard\portable\api\dbobject; |
19
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Clean up repligard table |
23
|
|
|
* |
24
|
|
|
* @package midcom.console |
25
|
|
|
*/ |
26
|
|
|
#[AsCommand( |
27
|
|
|
name: 'midcom:repligard', |
28
|
|
|
description: 'Clean up repligard table', |
29
|
|
|
aliases: ['repligard'] |
30
|
|
|
)] |
31
|
|
|
class repligard extends Command |
32
|
|
|
{ |
33
|
|
|
private PDO $db; |
34
|
|
|
|
35
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : int |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
|
|
$this->db = connection::get_em()->getConnection()->getNativeConnection(); |
39
|
|
|
} catch (\Exception) { |
40
|
|
|
$this->db = $this->create_connection($input, $output); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$result = $this->_run('SELECT COUNT(guid) FROM repligard WHERE object_action=2')->fetchColumn(); |
44
|
|
|
if ($result > 0) { |
45
|
|
|
$output->writeln('Found <info>' . $result . '</info> entries for purged objects'); |
46
|
|
|
if ($this->_confirm($input, $output)) { |
47
|
|
|
$result = $this->_run('DELETE FROM repligard WHERE object_action=2', 'exec'); |
48
|
|
|
$output->writeln('Deleted <comment>' . $result . '</comment> rows'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$result = $this->_run('SELECT DISTINCT typename FROM repligard'); |
53
|
|
|
foreach ($result->fetchAll(PDO::FETCH_COLUMN, 0) as $typename) { |
54
|
|
|
if (!is_a($typename, dbobject::class, true)) { |
55
|
|
|
$result = $this->_run('SELECT COUNT(guid) FROM repligard WHERE typename="' . $typename . '"'); |
56
|
|
|
$output->writeln('Found <info>' . $result->fetchColumn() . '</info> entries for nonexistent type <comment>' . $typename . '</comment>'); |
57
|
|
|
if ($this->_confirm($input, $output)) { |
58
|
|
|
$result = $this->_run('DELETE FROM repligard WHERE typename="' . $typename . '"', 'exec'); |
59
|
|
|
$output->writeln('Deleted <comment>' . $result . '</comment> rows'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
return Command::SUCCESS; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function create_connection(InputInterface $input, OutputInterface $output) : PDO |
67
|
|
|
{ |
68
|
|
|
$config = \midgard_connection::get_instance()->config; |
69
|
|
|
$defaults = [ |
70
|
|
|
'username' => $config->dbuser, |
71
|
|
|
'password' => $config->dbpass, |
72
|
|
|
'host' => $config->host, |
73
|
|
|
'dbtype' => $config->dbtype, |
74
|
|
|
'dbname' => $config->database |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
$dialog = $this->getHelperSet()->get('question'); |
78
|
|
|
|
79
|
|
|
$host = $dialog->ask($input, $output, new Question('<question>DB host:</question> [' . $defaults['host'] . ']', $defaults['host'])); |
|
|
|
|
80
|
|
|
$dbtype = $dialog->ask($input, $output, new Question('<question>DB type:</question> [' . $defaults['dbtype'] . ']', $defaults['dbtype'])); |
81
|
|
|
$dbname = $dialog->ask($input, $output, new Question('<question>DB name:</question> [' . $defaults['dbname'] . ']', $defaults['dbname'])); |
82
|
|
|
|
83
|
|
|
if (empty($defaults['username'])) { |
84
|
|
|
$username = $dialog->ask($input, $output, new Question('<question>DB Username:</question> ')); |
85
|
|
|
$pw_question = new Question('<question>DB Password:</question> '); |
86
|
|
|
$pw_question->setHidden(true); |
87
|
|
|
$pw_question->setHiddenFallback(false); |
88
|
|
|
$password = $dialog->ask($input, $output, $pw_question); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$dsn = strtolower($dbtype) . ':host=' . $host . ';dbname=' . $dbname; |
92
|
|
|
return new PDO($dsn, $username, $password); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function _confirm(InputInterface $input, OutputInterface $output) : bool |
96
|
|
|
{ |
97
|
|
|
$options = [true => 'y', false => 'N']; |
98
|
|
|
$question = '<question>Delete all rows? [' . implode('|', $options). ']</question> '; |
99
|
|
|
$question = new ConfirmationQuestion($question, false); |
100
|
|
|
$dialog = $this->getHelperSet()->get('question'); |
101
|
|
|
return $dialog->ask($input, $output, $question); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function _run(string $stmt, $command = 'query') |
105
|
|
|
{ |
106
|
|
|
$result = $this->db->$command($stmt); |
107
|
|
|
if ($result === false) { |
108
|
|
|
throw new \RuntimeException(implode("\n", $this->db->errorInfo())); |
109
|
|
|
} |
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|