CleanUp.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 OCA\Files_Versions\Db\VersionsMapper;
  27. use OCP\Files\IRootFolder;
  28. use OCP\IUserBackend;
  29. use OCP\IUserManager;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class CleanUp extends Command {
  36. /** @var IUserManager */
  37. protected $userManager;
  38. /** @var IRootFolder */
  39. protected $rootFolder;
  40. /**
  41. * @param IRootFolder $rootFolder
  42. * @param IUserManager $userManager
  43. */
  44. public function __construct(
  45. IRootFolder $rootFolder,
  46. IUserManager $userManager,
  47. protected VersionsMapper $versionMapper,
  48. ) {
  49. parent::__construct();
  50. $this->userManager = $userManager;
  51. $this->rootFolder = $rootFolder;
  52. }
  53. protected function configure() {
  54. $this
  55. ->setName('versions:cleanup')
  56. ->setDescription('Delete versions')
  57. ->addArgument(
  58. 'user_id',
  59. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  60. 'delete versions of the given user(s), if no user is given all versions will be deleted'
  61. )
  62. ->addOption(
  63. 'path',
  64. 'p',
  65. InputOption::VALUE_REQUIRED,
  66. 'only delete versions of this path, e.g. --path="/alice/files/Music"'
  67. );
  68. }
  69. protected function execute(InputInterface $input, OutputInterface $output): int {
  70. $users = $input->getArgument('user_id');
  71. $path = $input->getOption('path');
  72. if ($path) {
  73. if (!preg_match('#^/([^/]+)/files(/.*)?$#', $path, $pathMatches)) {
  74. $output->writeln("<error>Invalid path given</error>");
  75. return 1;
  76. }
  77. $users = [ $pathMatches[1] ];
  78. $path = trim($pathMatches[2], '/');
  79. }
  80. if (!empty($users)) {
  81. foreach ($users as $user) {
  82. if ($this->userManager->userExists($user)) {
  83. $output->writeln("Delete versions of <info>$user</info>");
  84. $this->deleteVersions($user, $path);
  85. } else {
  86. $output->writeln("<error>Unknown user $user</error>");
  87. return 1;
  88. }
  89. }
  90. } else {
  91. $output->writeln('Delete all versions');
  92. foreach ($this->userManager->getBackends() as $backend) {
  93. $name = get_class($backend);
  94. if ($backend instanceof IUserBackend) {
  95. $name = $backend->getBackendName();
  96. }
  97. $output->writeln("Delete versions for users on backend <info>$name</info>");
  98. $limit = 500;
  99. $offset = 0;
  100. do {
  101. $users = $backend->getUsers('', $limit, $offset);
  102. foreach ($users as $user) {
  103. $output->writeln(" <info>$user</info>");
  104. $this->deleteVersions($user);
  105. }
  106. $offset += $limit;
  107. } while (count($users) >= $limit);
  108. }
  109. }
  110. return 0;
  111. }
  112. /**
  113. * delete versions for the given user
  114. *
  115. * @param string $user
  116. * @param string|null $path
  117. */
  118. protected function deleteVersions(string $user, string $path = null): void {
  119. \OC_Util::tearDownFS();
  120. \OC_Util::setupFS($user);
  121. $userHomeStorageId = $this->rootFolder->getUserFolder($user)->getStorage()->getCache()->getNumericStorageId();
  122. $this->versionMapper->deleteAllVersionsForUser($userHomeStorageId, $path);
  123. $fullPath = '/' . $user . '/files_versions' . ($path ? '/' . $path : '');
  124. if ($this->rootFolder->nodeExists($fullPath)) {
  125. $this->rootFolder->get($fullPath)->delete();
  126. }
  127. }
  128. }