1
0

CleanUp.php 3.1 KB

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