1
0

CleanUp.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Daniel Rudolf <nextcloud.com@daniel-rudolf.de>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Versions\Command;
  26. use OCP\Files\IRootFolder;
  27. use OCP\IUserBackend;
  28. use OCP\IUserManager;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class CleanUp extends Command {
  35. /** @var IUserManager */
  36. protected $userManager;
  37. /** @var IRootFolder */
  38. protected $rootFolder;
  39. /**
  40. * @param IRootFolder $rootFolder
  41. * @param IUserManager $userManager
  42. */
  43. public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
  44. parent::__construct();
  45. $this->userManager = $userManager;
  46. $this->rootFolder = $rootFolder;
  47. }
  48. protected function configure() {
  49. $this
  50. ->setName('versions:cleanup')
  51. ->setDescription('Delete versions')
  52. ->addArgument(
  53. 'user_id',
  54. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  55. 'delete versions of the given user(s), if no user is given all versions will be deleted'
  56. )
  57. ->addOption(
  58. 'path',
  59. 'p',
  60. InputOption::VALUE_REQUIRED,
  61. 'only delete versions of this path, e.g. --path="/alice/files/Music"'
  62. );
  63. }
  64. protected function execute(InputInterface $input, OutputInterface $output): int {
  65. $users = $input->getArgument('user_id');
  66. $path = $input->getOption('path');
  67. if ($path) {
  68. if (!preg_match('#^/([^/]+)/files(/.*)?$#', $path, $pathMatches)) {
  69. $output->writeln("<error>Invalid path given</error>");
  70. return 1;
  71. }
  72. $users = [ $pathMatches[1] ];
  73. $path = trim($pathMatches[2], '/');
  74. }
  75. if (!empty($users)) {
  76. foreach ($users as $user) {
  77. if ($this->userManager->userExists($user)) {
  78. $output->writeln("Delete versions of <info>$user</info>");
  79. $this->deleteVersions($user, $path);
  80. } else {
  81. $output->writeln("<error>Unknown user $user</error>");
  82. return 1;
  83. }
  84. }
  85. } else {
  86. $output->writeln('Delete all versions');
  87. foreach ($this->userManager->getBackends() as $backend) {
  88. $name = get_class($backend);
  89. if ($backend instanceof IUserBackend) {
  90. $name = $backend->getBackendName();
  91. }
  92. $output->writeln("Delete versions for users on backend <info>$name</info>");
  93. $limit = 500;
  94. $offset = 0;
  95. do {
  96. $users = $backend->getUsers('', $limit, $offset);
  97. foreach ($users as $user) {
  98. $output->writeln(" <info>$user</info>");
  99. $this->deleteVersions($user);
  100. }
  101. $offset += $limit;
  102. } while (count($users) >= $limit);
  103. }
  104. }
  105. return 0;
  106. }
  107. /**
  108. * delete versions for the given user
  109. *
  110. * @param string $user
  111. * @param string|null $path
  112. */
  113. protected function deleteVersions(string $user, string $path = null): void {
  114. \OC_Util::tearDownFS();
  115. \OC_Util::setupFS($user);
  116. $fullPath = '/' . $user . '/files_versions' . ($path ? '/' . $path : '');
  117. if ($this->rootFolder->nodeExists($fullPath)) {
  118. $this->rootFolder->get($fullPath)->delete();
  119. }
  120. }
  121. }