Skip to content

Commit

Permalink
Merge pull request #39695 from fsamapoor/refactor_files_version_app_c…
Browse files Browse the repository at this point in the history
…ommands

Refactors files version app commands.
  • Loading branch information
come-nc authored Sep 21, 2023
2 parents ff9de2e + 0273b96 commit 39d4c87
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 102 deletions.
79 changes: 34 additions & 45 deletions apps/files_versions/lib/Command/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,14 @@
use Symfony\Component\Console\Output\OutputInterface;

class CleanUp extends Command {

/** @var IUserManager */
protected $userManager;

/** @var IRootFolder */
protected $rootFolder;

/**
* @param IRootFolder $rootFolder
* @param IUserManager $userManager
*/
public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
public function __construct(
protected IRootFolder $rootFolder,
protected IUserManager $userManager,
) {
parent::__construct();
$this->userManager = $userManager;
$this->rootFolder = $rootFolder;
}

protected function configure() {
protected function configure(): void {
$this
->setName('versions:cleanup')
->setDescription('Delete versions')
Expand All @@ -76,7 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($path) {
if (!preg_match('#^/([^/]+)/files(/.*)?$#', $path, $pathMatches)) {
$output->writeln("<error>Invalid path given</error>");
return 1;
return self::FAILURE;
}

$users = [ $pathMatches[1] ];
Expand All @@ -85,48 +75,47 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (!empty($users)) {
foreach ($users as $user) {
if ($this->userManager->userExists($user)) {
$output->writeln("Delete versions of <info>$user</info>");
$this->deleteVersions($user, $path);
} else {
if (!$this->userManager->userExists($user)) {
$output->writeln("<error>Unknown user $user</error>");
return 1;
return self::FAILURE;
}

$output->writeln("Delete versions of <info>$user</info>");
$this->deleteVersions($user, $path);
}
} else {
$output->writeln('Delete all versions');
foreach ($this->userManager->getBackends() as $backend) {
$name = get_class($backend);
return self::SUCCESS;
}

if ($backend instanceof IUserBackend) {
$name = $backend->getBackendName();
}
$output->writeln('Delete all versions');
foreach ($this->userManager->getBackends() as $backend) {
$name = get_class($backend);

$output->writeln("Delete versions for users on backend <info>$name</info>");

$limit = 500;
$offset = 0;
do {
$users = $backend->getUsers('', $limit, $offset);
foreach ($users as $user) {
$output->writeln(" <info>$user</info>");
$this->deleteVersions($user);
}
$offset += $limit;
} while (count($users) >= $limit);
if ($backend instanceof IUserBackend) {
$name = $backend->getBackendName();
}

$output->writeln("Delete versions for users on backend <info>$name</info>");

$limit = 500;
$offset = 0;
do {
$users = $backend->getUsers('', $limit, $offset);
foreach ($users as $user) {
$output->writeln(" <info>$user</info>");
$this->deleteVersions($user);
}
$offset += $limit;
} while (count($users) >= $limit);
}
return 0;

return self::SUCCESS;
}


/**
* delete versions for the given user
*
* @param string $user
* @param string|null $path
*/
protected function deleteVersions(string $user, string $path = null): void {
protected function deleteVersions(string $user, ?string $path = null): void {
\OC_Util::tearDownFS();
\OC_Util::setupFS($user);

Expand Down
20 changes: 5 additions & 15 deletions apps/files_versions/lib/Command/Expire.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,13 @@
class Expire implements ICommand {
use FileAccess;

/**
* @var string
*/
private $fileName;

/**
* @var string
*/
private $user;

public function __construct(string $user, string $fileName) {
$this->user = $user;
$this->fileName = $fileName;
public function __construct(
private string $user,
private string $fileName,
) {
}

public function handle() {
public function handle(): void {
/** @var IUserManager $userManager */
$userManager = \OC::$server->get(IUserManager::class);
if (!$userManager->userExists($this->user)) {
Expand All @@ -62,7 +53,6 @@ public function handle() {
// In case of external storage and session credentials, the expiration
// fails because the command does not have those credentials

/** @var LoggerInterface */
$logger = \OC::$server->get(LoggerInterface::class);
$logger->warning($e->getMessage(), [
'exception' => $e,
Expand Down
67 changes: 25 additions & 42 deletions apps/files_versions/lib/Command/ExpireVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,14 @@
use Symfony\Component\Console\Output\OutputInterface;

class ExpireVersions extends Command {

/**
* @var Expiration
*/
private $expiration;

/**
* @var IUserManager
*/
private $userManager;

/**
* @param IUserManager $userManager
* @param Expiration $expiration
*/
public function __construct(IUserManager $userManager,
Expiration $expiration) {
public function __construct(
private IUserManager $userManager,
private Expiration $expiration,
) {
parent::__construct();

$this->userManager = $userManager;
$this->expiration = $expiration;
}

protected function configure() {
protected function configure(): void {
$this
->setName('versions:expire')
->setDescription('Expires the users file versions')
Expand All @@ -74,35 +58,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$maxAge = $this->expiration->getMaxAgeAsTimestamp();
if (!$maxAge) {
$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.");
return 1;
return self::FAILURE;
}

$users = $input->getArgument('user_id');
if (!empty($users)) {
foreach ($users as $user) {
if ($this->userManager->userExists($user)) {
$output->writeln("Remove deleted files of <info>$user</info>");
$userObject = $this->userManager->get($user);
$this->expireVersionsForUser($userObject);
} else {
if (!$this->userManager->userExists($user)) {
$output->writeln("<error>Unknown user $user</error>");
return 1;
return self::FAILURE;
}

$output->writeln("Remove deleted files of <info>$user</info>");
$userObject = $this->userManager->get($user);
$this->expireVersionsForUser($userObject);
}
} else {
$p = new ProgressBar($output);
$p->start();
$this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
$p->advance();
$this->expireVersionsForUser($user);
});
$p->finish();
$output->writeln('');
return self::SUCCESS;
}
return 0;

$p = new ProgressBar($output);
$p->start();
$this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
$p->advance();
$this->expireVersionsForUser($user);
});
$p->finish();
$output->writeln('');
return self::SUCCESS;
}

public function expireVersionsForUser(IUser $user) {
public function expireVersionsForUser(IUser $user): void {
$uid = $user->getUID();
if (!$this->setupFS($uid)) {
return;
Expand All @@ -112,10 +97,8 @@ public function expireVersionsForUser(IUser $user) {

/**
* Act on behalf on versions item owner
* @param string $user
* @return boolean
*/
protected function setupFS($user) {
protected function setupFS(string $user): bool {
\OC_Util::tearDownFS();
\OC_Util::setupFS($user);

Expand Down

0 comments on commit 39d4c87

Please sign in to comment.