CleanUp.php 3.8 KB

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