CleanUp.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Liam Dennehy <liam@wiemax.net>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Trashbin\Command;
  27. use OCP\Files\IRootFolder;
  28. use OCP\IDBConnection;
  29. use OCP\IUserBackend;
  30. use OCP\IUserManager;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Exception\InvalidOptionException;
  33. use Symfony\Component\Console\Input\InputArgument;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Input\InputOption;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. class CleanUp extends Command {
  38. /** @var IUserManager */
  39. protected $userManager;
  40. /** @var IRootFolder */
  41. protected $rootFolder;
  42. /** @var \OCP\IDBConnection */
  43. protected $dbConnection;
  44. /**
  45. * @param IRootFolder $rootFolder
  46. * @param IUserManager $userManager
  47. * @param IDBConnection $dbConnection
  48. */
  49. public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IDBConnection $dbConnection) {
  50. parent::__construct();
  51. $this->userManager = $userManager;
  52. $this->rootFolder = $rootFolder;
  53. $this->dbConnection = $dbConnection;
  54. }
  55. protected function configure() {
  56. $this
  57. ->setName('trashbin:cleanup')
  58. ->setDescription('Remove deleted files')
  59. ->addArgument(
  60. 'user_id',
  61. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  62. 'remove deleted files of the given user(s)'
  63. )
  64. ->addOption(
  65. 'all-users',
  66. null,
  67. InputOption::VALUE_NONE,
  68. 'run action on all users'
  69. );
  70. }
  71. protected function execute(InputInterface $input, OutputInterface $output): int {
  72. $users = $input->getArgument('user_id');
  73. if ((!empty($users)) and ($input->getOption('all-users'))) {
  74. throw new InvalidOptionException('Either specify a user_id or --all-users');
  75. } elseif (!empty($users)) {
  76. foreach ($users as $user) {
  77. if ($this->userManager->userExists($user)) {
  78. $output->writeln("Remove deleted files of <info>$user</info>");
  79. $this->removeDeletedFiles($user);
  80. } else {
  81. $output->writeln("<error>Unknown user $user</error>");
  82. return 1;
  83. }
  84. }
  85. } elseif ($input->getOption('all-users')) {
  86. $output->writeln('Remove deleted files for all users');
  87. foreach ($this->userManager->getBackends() as $backend) {
  88. $name = get_class($backend);
  89. if ($backend instanceof IUserBackend) {
  90. $name = $backend->getBackendName();
  91. }
  92. $output->writeln("Remove deleted files for users on backend <info>$name</info>");
  93. $limit = 500;
  94. $offset = 0;
  95. do {
  96. $users = $backend->getUsers('', $limit, $offset);
  97. foreach ($users as $user) {
  98. $output->writeln(" <info>$user</info>");
  99. $this->removeDeletedFiles($user);
  100. }
  101. $offset += $limit;
  102. } while (count($users) >= $limit);
  103. }
  104. } else {
  105. throw new InvalidOptionException('Either specify a user_id or --all-users');
  106. }
  107. return 0;
  108. }
  109. /**
  110. * remove deleted files for the given user
  111. *
  112. * @param string $uid
  113. */
  114. protected function removeDeletedFiles($uid) {
  115. \OC_Util::tearDownFS();
  116. \OC_Util::setupFS($uid);
  117. if ($this->rootFolder->nodeExists('/' . $uid . '/files_trashbin')) {
  118. $this->rootFolder->get('/' . $uid . '/files_trashbin')->delete();
  119. $query = $this->dbConnection->getQueryBuilder();
  120. $query->delete('files_trash')
  121. ->where($query->expr()->eq('user', $query->createParameter('uid')))
  122. ->setParameter('uid', $uid);
  123. $query->execute();
  124. }
  125. }
  126. }