ExpireVersions.php 3.3 KB

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