1
0

CleanUp.php 4.1 KB

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