CleanUp.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Versions\Command;
  8. use OCA\Files_Versions\Db\VersionsMapper;
  9. use OCP\Files\IRootFolder;
  10. use OCP\IUserBackend;
  11. use OCP\IUserManager;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class CleanUp extends Command {
  18. public function __construct(
  19. protected IRootFolder $rootFolder,
  20. protected IUserManager $userManager,
  21. protected VersionsMapper $versionMapper,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure(): void {
  26. $this
  27. ->setName('versions:cleanup')
  28. ->setDescription('Delete versions')
  29. ->addArgument(
  30. 'user_id',
  31. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  32. 'delete versions of the given user(s), if no user is given all versions will be deleted'
  33. )
  34. ->addOption(
  35. 'path',
  36. 'p',
  37. InputOption::VALUE_REQUIRED,
  38. 'only delete versions of this path, e.g. --path="/alice/files/Music"'
  39. );
  40. }
  41. protected function execute(InputInterface $input, OutputInterface $output): int {
  42. $users = $input->getArgument('user_id');
  43. $path = $input->getOption('path');
  44. if ($path) {
  45. if (!preg_match('#^/([^/]+)/files(/.*)?$#', $path, $pathMatches)) {
  46. $output->writeln('<error>Invalid path given</error>');
  47. return self::FAILURE;
  48. }
  49. $users = [ $pathMatches[1] ];
  50. $path = trim($pathMatches[2], '/');
  51. }
  52. if (!empty($users)) {
  53. foreach ($users as $user) {
  54. if (!$this->userManager->userExists($user)) {
  55. $output->writeln("<error>Unknown user $user</error>");
  56. return self::FAILURE;
  57. }
  58. $output->writeln("Delete versions of <info>$user</info>");
  59. $this->deleteVersions($user, $path);
  60. }
  61. return self::SUCCESS;
  62. }
  63. $output->writeln('Delete all versions');
  64. foreach ($this->userManager->getBackends() as $backend) {
  65. $name = get_class($backend);
  66. if ($backend instanceof IUserBackend) {
  67. $name = $backend->getBackendName();
  68. }
  69. $output->writeln("Delete versions for users on backend <info>$name</info>");
  70. $limit = 500;
  71. $offset = 0;
  72. do {
  73. $users = $backend->getUsers('', $limit, $offset);
  74. foreach ($users as $user) {
  75. $output->writeln(" <info>$user</info>");
  76. $this->deleteVersions($user);
  77. }
  78. $offset += $limit;
  79. } while (count($users) >= $limit);
  80. }
  81. return self::SUCCESS;
  82. }
  83. /**
  84. * delete versions for the given user
  85. */
  86. protected function deleteVersions(string $user, ?string $path = null): void {
  87. \OC_Util::tearDownFS();
  88. \OC_Util::setupFS($user);
  89. $userHomeStorageId = $this->rootFolder->getUserFolder($user)->getStorage()->getCache()->getNumericStorageId();
  90. $this->versionMapper->deleteAllVersionsForUser($userHomeStorageId, $path);
  91. $fullPath = '/' . $user . '/files_versions' . ($path ? '/' . $path : '');
  92. if ($this->rootFolder->nodeExists($fullPath)) {
  93. $this->rootFolder->get($fullPath)->delete();
  94. }
  95. }
  96. }