CleanUp.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_Versions\Command;
  25. use OCP\Files\IRootFolder;
  26. use OCP\IUserBackend;
  27. use OCP\IUserManager;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class CleanUp extends Command {
  33. /** @var IUserManager */
  34. protected $userManager;
  35. /** @var IRootFolder */
  36. protected $rootFolder;
  37. /**
  38. * @param IRootFolder $rootFolder
  39. * @param IUserManager $userManager
  40. */
  41. public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
  42. parent::__construct();
  43. $this->userManager = $userManager;
  44. $this->rootFolder = $rootFolder;
  45. }
  46. protected function configure() {
  47. $this
  48. ->setName('versions:cleanup')
  49. ->setDescription('Delete versions')
  50. ->addArgument(
  51. 'user_id',
  52. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  53. 'delete versions of the given user(s), if no user is given all versions will be deleted'
  54. );
  55. }
  56. protected function execute(InputInterface $input, OutputInterface $output): int {
  57. $users = $input->getArgument('user_id');
  58. if (!empty($users)) {
  59. foreach ($users as $user) {
  60. if ($this->userManager->userExists($user)) {
  61. $output->writeln("Delete versions of <info>$user</info>");
  62. $this->deleteVersions($user);
  63. } else {
  64. $output->writeln("<error>Unknown user $user</error>");
  65. return 1;
  66. }
  67. }
  68. } else {
  69. $output->writeln('Delete all versions');
  70. foreach ($this->userManager->getBackends() as $backend) {
  71. $name = get_class($backend);
  72. if ($backend instanceof IUserBackend) {
  73. $name = $backend->getBackendName();
  74. }
  75. $output->writeln("Delete versions for users on backend <info>$name</info>");
  76. $limit = 500;
  77. $offset = 0;
  78. do {
  79. $users = $backend->getUsers('', $limit, $offset);
  80. foreach ($users as $user) {
  81. $output->writeln(" <info>$user</info>");
  82. $this->deleteVersions($user);
  83. }
  84. $offset += $limit;
  85. } while (count($users) >= $limit);
  86. }
  87. }
  88. return 0;
  89. }
  90. /**
  91. * delete versions for the given user
  92. *
  93. * @param string $user
  94. */
  95. protected function deleteVersions($user) {
  96. \OC_Util::tearDownFS();
  97. \OC_Util::setupFS($user);
  98. if ($this->rootFolder->nodeExists('/' . $user . '/files_versions')) {
  99. $this->rootFolder->get('/' . $user . '/files_versions')->delete();
  100. }
  101. }
  102. }