ExpireVersions.php 2.7 KB

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