* Using the TrashManager allows access to all deleted files
* Add 'scope' parameter to choose where to restore from (user or groupfolders)
* Add 'restore-from' and 'restore-to' date parameters to filter files to be
restored by their deletion date
* Add 'dry-run' flag to be able to see which files would be restored and being
able to adjust the filter parameters accordingly
Signed-off-by: GitHub <noreply@github.com>
if ($restoreFrom !== null and $restoreTo !== null and $restoreFrom > $restoreTo) {
throw new InvalidOptionException('restore-from must be before restore-to');
}
$output->writeln("Successfully restored <info>$count</info> out of <info>$trashCount</info> files.");
return [
$this->parseScope($input->getOption('scope')),
$restoreFrom,
$restoreTo,
$input->getOption('dry-run')
];
}
/**
* @param string $scope
* @return int
*/
protected function parseScope(string $scope): int {
switch ($scope) {
case 'user':
return self::SCOPE_USER;
case 'groupfolders':
return self::SCOPE_GROUPFOLDERS;
case 'all':
return self::SCOPE_ALL;
default:
throw new InvalidOptionException("Invalid scope '$scope'");
}
}
/**
* @param string|null $timestamp
* @return int|null
*/
protected function parseTimestamp(?string $timestamp): ?int {
if ($timestamp === null) {
return null;
}
$timestamp = strtotime($timestamp);
if ($timestamp === false) {
throw new InvalidOptionException("Invalid timestamp '$timestamp'");
}
return $timestamp;
}
/**
* @param ITrashItem[] $trashItem
* @param int $scope
* @param int|null $restoreFrom
* @param int|null $restoreTo
* @param OutputInterface $output
* @return ITrashItem[]
*/
protected function filterTrashItems(array $trashItems, int $scope, ?int $restoreFrom, ?int $restoreTo, OutputInterface $output): array {
$filteredTrashItems = [];
foreach ($trashItems as $trashItem) {
// Check scope with exact class names
if ($scope === self::SCOPE_USER && get_class($trashItem) !== \OCA\Files_Trashbin\Trash\TrashItem::class) {
$output->writeln("Skipping <info>" . $trashItem->getName() . "</info> because it is not a user trash item", OutputInterface::VERBOSITY_VERBOSE);
continue;
}
if ($scope === self::SCOPE_GROUPFOLDERS && get_class($trashItem) !== \OCA\GroupFolders\Trash\GroupTrashItem::class) {
$output->writeln("Skipping <info>" . $trashItem->getName() . "</info> because it is not a groupfolders trash item", OutputInterface::VERBOSITY_VERBOSE);
continue;
}
// Check left timestamp boundary
if ($restoreFrom !== null && $trashItem->getDeletedTime() <= $restoreFrom) {
$output->writeln("Skipping <info>" . $trashItem->getName() . "</info> because it was deleted before the restore-from timestamp", OutputInterface::VERBOSITY_VERBOSE);
continue;
}
// Check right timestamp boundary
if ($restoreTo !== null && $trashItem->getDeletedTime() >= $restoreTo) {
$output->writeln("Skipping <info>" . $trashItem->getName() . "</info> because it was deleted after the restore-to timestamp", OutputInterface::VERBOSITY_VERBOSE);