From c8870d0756c10b75ec52b29329bdd9170892bd10 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Mon, 7 Jun 2021 00:32:27 +0200 Subject: [PATCH 1/2] Add occ versions:cleanup --path option Signed-off-by: Daniel Rudolf --- apps/files_versions/lib/Command/CleanUp.php | 33 +++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/apps/files_versions/lib/Command/CleanUp.php b/apps/files_versions/lib/Command/CleanUp.php index b51959e953254..1f171fea7abba 100644 --- a/apps/files_versions/lib/Command/CleanUp.php +++ b/apps/files_versions/lib/Command/CleanUp.php @@ -5,6 +5,7 @@ * @author Björn Schießle * @author Christoph Wurst * @author Joas Schilling + * @author Daniel Rudolf * * @license AGPL-3.0 * @@ -29,6 +30,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class CleanUp extends Command { @@ -57,17 +59,35 @@ protected function configure() { 'user_id', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'delete versions of the given user(s), if no user is given all versions will be deleted' + ) + ->addOption( + 'path', + 'p', + InputOption::VALUE_REQUIRED, + 'only delete versions of this path, e.g. --path="/alice/files_versions/Music"' ); } protected function execute(InputInterface $input, OutputInterface $output): int { $users = $input->getArgument('user_id'); + + $path = $input->getOption('path'); + if ($path) { + if (!preg_match('#^/([^/]+)/files_versions(/.*)?$#', $path, $pathMatches)) { + $output->writeln("Invalid path given"); + return 1; + } + + $users = [ $pathMatches[1] ]; + $path = trim($pathMatches[2], '/'); + } + if (!empty($users)) { foreach ($users as $user) { if ($this->userManager->userExists($user)) { $output->writeln("Delete versions of $user"); - $this->deleteVersions($user); + $this->deleteVersions($user, $path); } else { $output->writeln("Unknown user $user"); return 1; @@ -103,13 +123,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** * delete versions for the given user * - * @param string $user + * @param string $user + * @param string|null $path */ - protected function deleteVersions($user) { + protected function deleteVersions(string $user, string $path = null): void { \OC_Util::tearDownFS(); \OC_Util::setupFS($user); - if ($this->rootFolder->nodeExists('/' . $user . '/files_versions')) { - $this->rootFolder->get('/' . $user . '/files_versions')->delete(); + + $fullPath = '/' . $user . '/files_versions' . ($path ? '/' . $path : ''); + if ($this->rootFolder->nodeExists($fullPath)) { + $this->rootFolder->get($fullPath)->delete(); } } } From 54cb4ac840b873677e2689b59230c3d3bab6993b Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Thu, 7 Oct 2021 19:18:03 +0200 Subject: [PATCH 2/2] Improve occ versions:cleanup --path option Signed-off-by: Daniel Rudolf --- apps/files_versions/lib/Command/CleanUp.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/files_versions/lib/Command/CleanUp.php b/apps/files_versions/lib/Command/CleanUp.php index 1f171fea7abba..d7bb4caa483ff 100644 --- a/apps/files_versions/lib/Command/CleanUp.php +++ b/apps/files_versions/lib/Command/CleanUp.php @@ -64,7 +64,7 @@ protected function configure() { 'path', 'p', InputOption::VALUE_REQUIRED, - 'only delete versions of this path, e.g. --path="/alice/files_versions/Music"' + 'only delete versions of this path, e.g. --path="/alice/files/Music"' ); } @@ -74,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $path = $input->getOption('path'); if ($path) { - if (!preg_match('#^/([^/]+)/files_versions(/.*)?$#', $path, $pathMatches)) { + if (!preg_match('#^/([^/]+)/files(/.*)?$#', $path, $pathMatches)) { $output->writeln("Invalid path given"); return 1; }