ExpireVersions.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud GmbH.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_Versions\Command;
  25. use OCA\Files_Versions\Expiration;
  26. use OCA\Files_Versions\Storage;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Helper\ProgressBar;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. class ExpireVersions extends Command {
  35. /**
  36. * @var Expiration
  37. */
  38. private $expiration;
  39. /**
  40. * @var IUserManager
  41. */
  42. private $userManager;
  43. /**
  44. * @param IUserManager $userManager
  45. * @param Expiration $expiration
  46. */
  47. public function __construct(IUserManager $userManager,
  48. Expiration $expiration) {
  49. parent::__construct();
  50. $this->userManager = $userManager;
  51. $this->expiration = $expiration;
  52. }
  53. protected function configure() {
  54. $this
  55. ->setName('versions:expire')
  56. ->setDescription('Expires the users file versions')
  57. ->addArgument(
  58. 'user_id',
  59. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  60. 'expire file versions of the given user(s), if no user is given file versions for all users will be expired.'
  61. );
  62. }
  63. protected function execute(InputInterface $input, OutputInterface $output) {
  64. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  65. if (!$maxAge) {
  66. $output->writeln("No expiry configured.");
  67. return;
  68. }
  69. $users = $input->getArgument('user_id');
  70. if (!empty($users)) {
  71. foreach ($users as $user) {
  72. if ($this->userManager->userExists($user)) {
  73. $output->writeln("Remove deleted files of <info>$user</info>");
  74. $userObject = $this->userManager->get($user);
  75. $this->expireVersionsForUser($userObject);
  76. } else {
  77. $output->writeln("<error>Unknown user $user</error>");
  78. }
  79. }
  80. } else {
  81. $p = new ProgressBar($output);
  82. $p->start();
  83. $this->userManager->callForSeenUsers(function(IUser $user) use ($p) {
  84. $p->advance();
  85. $this->expireVersionsForUser($user);
  86. });
  87. $p->finish();
  88. $output->writeln('');
  89. }
  90. }
  91. function expireVersionsForUser(IUser $user) {
  92. $uid = $user->getUID();
  93. if (!$this->setupFS($uid)) {
  94. return;
  95. }
  96. Storage::expireOlderThanMaxForUser($uid);
  97. }
  98. /**
  99. * Act on behalf on versions item owner
  100. * @param string $user
  101. * @return boolean
  102. */
  103. protected function setupFS($user) {
  104. \OC_Util::tearDownFS();
  105. \OC_Util::setupFS($user);
  106. // Check if this user has a version directory
  107. $view = new \OC\Files\View('/' . $user);
  108. if (!$view->is_dir('/files_versions')) {
  109. return false;
  110. }
  111. return true;
  112. }
  113. }