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 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) {
  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. }
  83. }
  84. } elseif ($input->getOption('all-users')) {
  85. $output->writeln('Remove deleted files for all users');
  86. foreach ($this->userManager->getBackends() as $backend) {
  87. $name = get_class($backend);
  88. if ($backend instanceof IUserBackend) {
  89. $name = $backend->getBackendName();
  90. }
  91. $output->writeln("Remove deleted files for users on backend <info>$name</info>");
  92. $limit = 500;
  93. $offset = 0;
  94. do {
  95. $users = $backend->getUsers('', $limit, $offset);
  96. foreach ($users as $user) {
  97. $output->writeln(" <info>$user</info>");
  98. $this->removeDeletedFiles($user);
  99. }
  100. $offset += $limit;
  101. } while (count($users) >= $limit);
  102. }
  103. } else {
  104. throw new InvalidOptionException('Either specify a user_id or --all-users');
  105. }
  106. }
  107. /**
  108. * remove deleted files for the given user
  109. *
  110. * @param string $uid
  111. */
  112. protected function removeDeletedFiles($uid) {
  113. \OC_Util::tearDownFS();
  114. \OC_Util::setupFS($uid);
  115. if ($this->rootFolder->nodeExists('/' . $uid . '/files_trashbin')) {
  116. $this->rootFolder->get('/' . $uid . '/files_trashbin')->delete();
  117. $query = $this->dbConnection->getQueryBuilder();
  118. $query->delete('files_trash')
  119. ->where($query->expr()->eq('user', $query->createParameter('uid')))
  120. ->setParameter('uid', $uid);
  121. $query->execute();
  122. }
  123. }
  124. }