1
0

ExpireVersions.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud GmbH.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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_Versions\Command;
  27. use OCA\Files_Versions\Expiration;
  28. use OCA\Files_Versions\Storage;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Helper\ProgressBar;
  33. use Symfony\Component\Console\Input\InputArgument;
  34. use Symfony\Component\Console\Input\InputInterface;
  35. use Symfony\Component\Console\Output\OutputInterface;
  36. class ExpireVersions extends Command {
  37. /**
  38. * @var Expiration
  39. */
  40. private $expiration;
  41. /**
  42. * @var IUserManager
  43. */
  44. private $userManager;
  45. /**
  46. * @param IUserManager $userManager
  47. * @param Expiration $expiration
  48. */
  49. public function __construct(IUserManager $userManager,
  50. Expiration $expiration) {
  51. parent::__construct();
  52. $this->userManager = $userManager;
  53. $this->expiration = $expiration;
  54. }
  55. protected function configure() {
  56. $this
  57. ->setName('versions:expire')
  58. ->setDescription('Expires the users file versions')
  59. ->addArgument(
  60. 'user_id',
  61. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  62. 'expire file versions of the given user(s), if no user is given file versions for all users will be expired.'
  63. );
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  67. if (!$maxAge) {
  68. $output->writeln("Auto expiration is configured - expiration will be handled automatically according to the expiration patterns detailed at the following link https://docs.nextcloud.com/server/latest/admin_manual/configuration_files/file_versioning.html.");
  69. return 1;
  70. }
  71. $users = $input->getArgument('user_id');
  72. if (!empty($users)) {
  73. foreach ($users as $user) {
  74. if ($this->userManager->userExists($user)) {
  75. $output->writeln("Remove deleted files of <info>$user</info>");
  76. $userObject = $this->userManager->get($user);
  77. $this->expireVersionsForUser($userObject);
  78. } else {
  79. $output->writeln("<error>Unknown user $user</error>");
  80. return 1;
  81. }
  82. }
  83. } else {
  84. $p = new ProgressBar($output);
  85. $p->start();
  86. $this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
  87. $p->advance();
  88. $this->expireVersionsForUser($user);
  89. });
  90. $p->finish();
  91. $output->writeln('');
  92. }
  93. return 0;
  94. }
  95. public function expireVersionsForUser(IUser $user) {
  96. $uid = $user->getUID();
  97. if (!$this->setupFS($uid)) {
  98. return;
  99. }
  100. Storage::expireOlderThanMaxForUser($uid);
  101. }
  102. /**
  103. * Act on behalf on versions item owner
  104. * @param string $user
  105. * @return boolean
  106. */
  107. protected function setupFS($user) {
  108. \OC_Util::tearDownFS();
  109. \OC_Util::setupFS($user);
  110. // Check if this user has a version directory
  111. $view = new \OC\Files\View('/' . $user);
  112. if (!$view->is_dir('/files_versions')) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. }