Skip to content

Commit 65cce1c

Browse files
committed
Merge pull request #49 from dantleech/purge-children
Added support for only removing children
2 parents 7b0340f + 87df665 commit 65cce1c

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/PHPCR/Util/Console/Command/PurgeCommand.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ protected function configure()
4949
->setName('phpcr:purge')
5050
->setDescription('Remove content from the repository')
5151
->addArgument('path', InputArgument::OPTIONAL, 'Path of the node to purge', '/')
52-
->addOption('force', null, InputOption::VALUE_OPTIONAL, 'Set to "yes" to bypass the confirmation dialog', "no")
52+
->addOption('force', null, InputOption::VALUE_NONE, 'Use to bypass the confirmation dialog')
53+
->addOption('only-children', null, InputOption::VALUE_NONE, 'Use to only purge children of specified path')
5354
->setHelp(<<<EOF
5455
The <info>phpcr:purge</info> command remove all the non-standard nodes from the content repository
5556
EOF
@@ -65,25 +66,51 @@ protected function execute(InputInterface $input, OutputInterface $output)
6566
$session = $this->getHelper('phpcr')->getSession();
6667

6768
$path = $input->getArgument('path');
68-
$force = $input->hasParameterOption('--force');
69+
$force = $input->getOption('force');
70+
$onlyChildren = $input->getOption('only-children');
6971

70-
if (! $force) {
72+
if (!$force) {
7173
$dialog = new DialogHelper();
7274
$workspaceName = $session->getWorkspace()->getName();
73-
$force = $dialog->askConfirmation($output, "Are you sure you want to purge path '$path' and all its children from the workspace '$workspaceName'? [yes|no]: ", false);
75+
76+
if ($onlyChildren) {
77+
$question =
78+
'Are you sure you want to recursively delete the children of path "%s" '.
79+
'from workspace "%s"';
80+
} else {
81+
$question =
82+
'Are you sure you want to recursively delete the path "%s" '.
83+
'from workspace "%s"';
84+
}
85+
86+
$force = $dialog->askConfirmation($output, sprintf(
87+
'<question>'.$question.' Y/N ?</question>', $path, $workspaceName, false
88+
));
7489
}
7590

7691
if ($force) {
77-
if ('/' === $path) {
78-
NodeHelper::purgeWorkspace($this->getHelper('phpcr')->getSession());
92+
$message = '<comment>></comment> <info>Purging: </info> %s';
93+
94+
if ($onlyChildren) {
95+
$baseNode = $session->getNode($path, 0);
96+
97+
foreach ($baseNode->getNodes() as $childNode) {
98+
$output->writeln(sprintf($message, $childNode->getPath()));
99+
$childNode->remove();
100+
}
79101
} else {
80-
$session->removeItem($path);
102+
$output->writeln(sprintf($message, $path));
103+
104+
if ('/' === $path) {
105+
NodeHelper::purgeWorkspace($this->getHelper('phpcr')->getSession());
106+
} else {
107+
$session->removeItem($path);
108+
}
81109
}
82110

83111
$session->save();
84-
$output->writeln("Done purging '$path' and all its children\n");
85112
} else {
86-
$output->writeln("Aborted\n");
113+
$output->writeln('<error>Aborted</error>');
87114
}
88115

89116
return 0;

0 commit comments

Comments
 (0)