ExpireVersions.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud GmbH.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Versions\Command;
  8. use OCA\Files_Versions\Expiration;
  9. use OCA\Files_Versions\Storage;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\ProgressBar;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class ExpireVersions extends Command {
  18. public function __construct(
  19. private IUserManager $userManager,
  20. private Expiration $expiration,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure(): void {
  25. $this
  26. ->setName('versions:expire')
  27. ->setDescription('Expires the users file versions')
  28. ->addArgument(
  29. 'user_id',
  30. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  31. 'expire file versions of the given account(s), if no account is given file versions for all accounts will be expired.'
  32. );
  33. }
  34. protected function execute(InputInterface $input, OutputInterface $output): int {
  35. $maxAge = $this->expiration->getMaxAgeAsTimestamp();
  36. if (!$maxAge) {
  37. $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.");
  38. return self::FAILURE;
  39. }
  40. $users = $input->getArgument('user_id');
  41. if (!empty($users)) {
  42. foreach ($users as $user) {
  43. if (!$this->userManager->userExists($user)) {
  44. $output->writeln("<error>Unknown account $user</error>");
  45. return self::FAILURE;
  46. }
  47. $output->writeln("Remove deleted files of <info>$user</info>");
  48. $userObject = $this->userManager->get($user);
  49. $this->expireVersionsForUser($userObject);
  50. }
  51. return self::SUCCESS;
  52. }
  53. $p = new ProgressBar($output);
  54. $p->start();
  55. $this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
  56. $p->advance();
  57. $this->expireVersionsForUser($user);
  58. });
  59. $p->finish();
  60. $output->writeln('');
  61. return self::SUCCESS;
  62. }
  63. public function expireVersionsForUser(IUser $user): void {
  64. $uid = $user->getUID();
  65. if (!$this->setupFS($uid)) {
  66. return;
  67. }
  68. Storage::expireOlderThanMaxForUser($uid);
  69. }
  70. /**
  71. * Act on behalf on versions item owner
  72. */
  73. protected function setupFS(string $user): bool {
  74. \OC_Util::tearDownFS();
  75. \OC_Util::setupFS($user);
  76. // Check if this user has a version directory
  77. $view = new \OC\Files\View('/' . $user);
  78. if (!$view->is_dir('/files_versions')) {
  79. return false;
  80. }
  81. return true;
  82. }
  83. }