You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.2 KiB

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