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\cleanup; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use midcom; |
16
|
|
|
use midcom_services_rcs_config; |
17
|
|
|
use midgard\portable\storage\connection; |
18
|
|
|
use Symfony\Component\Console\Output\Output; |
19
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Cleanup the RCS dir |
23
|
|
|
* Searches for RCS files that don't have a corresponding entry in the repligard table |
24
|
|
|
* |
25
|
|
|
* @package midcom.console |
26
|
|
|
*/ |
27
|
|
|
class rcsdir extends Command |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $counter = 0; |
33
|
|
|
|
34
|
|
|
private $findings = [ |
35
|
|
|
'orphaned' => [] |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
protected function configure() |
39
|
|
|
{ |
40
|
|
|
$this->setName('midcom:cleanup:rcsdir') |
41
|
|
|
->setAliases(['rcsdircleanup']) |
42
|
|
|
->setDescription('Cleanup the RCS dir') |
43
|
|
|
->addOption('dry', 'd', InputOption::VALUE_NONE, 'If set, files will not be deleted'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function check_dir(OutputInterface $output, string $outerDir) |
47
|
|
|
{ |
48
|
|
|
$outerDir = rtrim($outerDir, "/"); |
49
|
|
|
$output->write("\x0D"); |
50
|
|
|
$output->write("Start scanning dir: <comment>" . $outerDir . "</comment>"); |
51
|
|
|
$dirs = array_diff(scandir($outerDir), [".", ".."]); |
52
|
|
|
foreach ($dirs as $d) { |
53
|
|
|
if (is_dir($outerDir . "/" . $d)) { |
54
|
|
|
$this->check_dir($output, $outerDir . "/" . $d); |
55
|
|
|
} else { |
56
|
|
|
// got something |
57
|
|
|
$file = $outerDir . "/" . $d; |
58
|
|
|
if (!$this->has_repligard_entry($file)) { |
59
|
|
|
$this->findings['orphaned'][] = $file; |
60
|
|
|
} |
61
|
|
|
$this->counter++; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function has_repligard_entry(string $file) : bool |
67
|
|
|
{ |
68
|
|
|
$guid = preg_replace('/^.+\/(.+?),?v?$/', '$1', $file); |
69
|
|
|
|
70
|
|
|
$repligard_entry = connection::get_em() |
71
|
|
|
->getRepository('midgard:midgard_repligard') |
72
|
|
|
->findOneBy(['guid' => $guid]); |
73
|
|
|
|
74
|
|
|
return !empty($repligard_entry); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function cleanup_file(OutputInterface $output, string $file) |
78
|
|
|
{ |
79
|
|
|
if (unlink($file)) { |
80
|
|
|
$output->writeln("<info>Cleanup OK</info>", Output::VERBOSITY_VERBOSE); |
81
|
|
|
} else { |
82
|
|
|
$output->writeln("<comment>Cleanup FAILED</comment>"); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : int |
87
|
|
|
{ |
88
|
|
|
$config = new midcom_services_rcs_config(midcom::get()->config); |
89
|
|
|
$dir = $config->get_rootdir(); |
90
|
|
|
if (!is_dir($dir)) { |
91
|
|
|
$output->writeln("<comment>Unable to detect RCS dir</comment> $dir"); |
92
|
|
|
return 1; |
93
|
|
|
} |
94
|
|
|
if ($dry = $input->getOption("dry")) { |
95
|
|
|
$output->writeln("<comment>Running in dry mode!</comment>"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->check_dir($output, $dir); |
99
|
|
|
|
100
|
|
|
$output->writeln("\nScanned <info>" . $this->counter . "</info> files"); |
101
|
|
|
$output->writeln("Found <info>" . count($this->findings['orphaned']) . "</info> orphaned files:"); |
102
|
|
|
|
103
|
|
|
if (!$dry) { |
104
|
|
|
$output->writeln("<comment>Deleting orphans</comment>"); |
105
|
|
|
$progress = new ProgressBar($output, count($this->findings['orphaned'])); |
106
|
|
|
$progress->setRedrawFrequency(100); |
107
|
|
|
$progress->start(); |
108
|
|
|
|
109
|
|
|
foreach ($this->findings['orphaned'] as $file) { |
110
|
|
|
$this->cleanup_file($output, $file); |
111
|
|
|
$progress->advance(); |
112
|
|
|
} |
113
|
|
|
$progress->finish(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$output->writeln("\n<comment>Done</comment>"); |
117
|
|
|
return 0; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|