ExpireVersions.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. public function __construct(
  38. private IUserManager $userManager,
  39. private Expiration $expiration,
  40. ) {
  41. parent::__construct();
  42. }
  43. protected function configure(): void {
  44. $this
  45. ->setName('versions:expire')
  46. ->setDescription('Expires the users file versions')
  47. ->addArgument(
  48. 'user_id',
  49. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  50. 'expire file versions of the given user(s), if no user is given file versions for all users will be expired.'
  51. );
  52. }
  53. protected function execute(InputInterface $input, OutputInterface $output): int {
  54. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  55. if (!$maxAge) {
  56. $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.");
  57. return self::FAILURE;
  58. }
  59. $users = $input->getArgument('user_id');
  60. if (!empty($users)) {
  61. foreach ($users as $user) {
  62. if (!$this->userManager->userExists($user)) {
  63. $output->writeln("<error>Unknown user $user</error>");
  64. return self::FAILURE;
  65. }
  66. $output->writeln("Remove deleted files of <info>$user</info>");
  67. $userObject = $this->userManager->get($user);
  68. $this->expireVersionsForUser($userObject);
  69. }
  70. return self::SUCCESS;
  71. }
  72. $p = new ProgressBar($output);
  73. $p->start();
  74. $this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
  75. $p->advance();
  76. $this->expireVersionsForUser($user);
  77. });
  78. $p->finish();
  79. $output->writeln('');
  80. return self::SUCCESS;
  81. }
  82. public function expireVersionsForUser(IUser $user): void {
  83. $uid = $user->getUID();
  84. if (!$this->setupFS($uid)) {
  85. return;
  86. }
  87. Storage::expireOlderThanMaxForUser($uid);
  88. }
  89. /**
  90. * Act on behalf on versions item owner
  91. */
  92. protected function setupFS(string $user): bool {
  93. \OC_Util::tearDownFS();
  94. \OC_Util::setupFS($user);
  95. // Check if this user has a version directory
  96. $view = new \OC\Files\View('/' . $user);
  97. if (!$view->is_dir('/files_versions')) {
  98. return false;
  99. }
  100. return true;
  101. }
  102. }