ExpireVersions.php 3.3 KB

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