-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(systemtags): add commands to manage tags on files
Resolve #32735 Signed-off-by: schaarsc <schaarsc@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Core\Command\SystemTag\Files; | ||
|
||
use OC\Core\Command\Info\FileUtils; | ||
use OCP\SystemTag\ISystemTagManager; | ||
use OCP\SystemTag\ISystemTagObjectMapper; | ||
use OCP\SystemTag\TagAlreadyExistsException; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class Add extends Command { | ||
|
||
public function __construct( | ||
private FileUtils $fileUtils, | ||
private ISystemTagManager $systemTagManager, | ||
private ISystemTagObjectMapper $systemTagObjectMapper, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void { | ||
$this->setName('tag:files:add') | ||
->setDescription('Add a system-tag to a file or folder') | ||
->addArgument('target', InputArgument::REQUIRED, 'file id or path') | ||
->addArgument('tags', InputArgument::REQUIRED, 'Name of the tag(s) to add, comma separated') | ||
->addArgument('access', InputArgument::REQUIRED, 'access level of the tag (public, restricted or invisible)'); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int { | ||
$targetInput = $input->getArgument('target'); | ||
$tagsInput = $input->getArgument('tags'); | ||
|
||
if ($tagsInput === '') { | ||
$output->writeln('<error>`tags` can\'t be empty</error>'); | ||
return 3; | ||
} | ||
|
||
$tagNameArray = explode(',', $tagsInput); | ||
|
||
$access = $input->getArgument('access'); | ||
switch ($access) { | ||
case 'public': | ||
$userVisible = true; | ||
$userAssignable = true; | ||
break; | ||
case 'restricted': | ||
$userVisible = true; | ||
$userAssignable = false; | ||
break; | ||
case 'invisible': | ||
$userVisible = false; | ||
$userAssignable = false; | ||
break; | ||
default: | ||
$output->writeln('<error>`access` property is invalid</error>'); | ||
return 1; | ||
} | ||
|
||
$targetNode = $this->fileUtils->getNode($targetInput); | ||
|
||
if (! $targetNode) { | ||
$output->writeln("<error>file $targetInput not found</error>"); | ||
return 1; | ||
} | ||
|
||
foreach ($tagNameArray as $tagName) { | ||
try { | ||
$tag = $this->systemTagManager->createTag($tagName, $userVisible, $userAssignable); | ||
$output->writeln("<info>$access</info> tag named <info>$tagName</info> created."); | ||
} catch (TagAlreadyExistsException $e) { | ||
$tag = $this->systemTagManager->getTag($tagName, $userVisible, $userAssignable); | ||
} | ||
|
||
$this->systemTagObjectMapper->assignTags((string)$targetNode->getId(), 'files', $tag->getId()); | ||
$output->writeln("<info>$access</info> tag named <info>$tagName</info> added."); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Core\Command\SystemTag\Files; | ||
|
||
use OC\Core\Command\Info\FileUtils; | ||
use OCP\SystemTag\ISystemTagManager; | ||
use OCP\SystemTag\ISystemTagObjectMapper; | ||
use OCP\SystemTag\TagNotFoundException; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class Delete extends Command { | ||
|
||
public function __construct( | ||
private FileUtils $fileUtils, | ||
private ISystemTagManager $systemTagManager, | ||
private ISystemTagObjectMapper $systemTagObjectMapper, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void { | ||
$this->setName('tag:files:delete') | ||
->setDescription('Delete a system-tag from a file or folder') | ||
->addArgument('target', InputArgument::REQUIRED, 'file id or path') | ||
->addArgument('tags', InputArgument::REQUIRED, 'Name of the tag(s) to delete, comma separated') | ||
->addArgument('access', InputArgument::REQUIRED, 'access level of the tag (public, restricted or invisible)'); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int { | ||
$targetInput = $input->getArgument('target'); | ||
$tagsInput = $input->getArgument('tags'); | ||
|
||
if ($tagsInput === '') { | ||
$output->writeln('<error>`tags` can\'t be empty</error>'); | ||
return 3; | ||
} | ||
|
||
$tagNameArray = explode(',', $tagsInput); | ||
|
||
$access = $input->getArgument('access'); | ||
switch ($access) { | ||
case 'public': | ||
$userVisible = true; | ||
$userAssignable = true; | ||
break; | ||
case 'restricted': | ||
$userVisible = true; | ||
$userAssignable = false; | ||
break; | ||
case 'invisible': | ||
$userVisible = false; | ||
$userAssignable = false; | ||
break; | ||
default: | ||
$output->writeln('<error>`access` property is invalid</error>'); | ||
return 1; | ||
} | ||
|
||
$targetNode = $this->fileUtils->getNode($targetInput); | ||
|
||
if (! $targetNode) { | ||
$output->writeln("<error>file $targetInput not found</error>"); | ||
return 1; | ||
} | ||
|
||
foreach ($tagNameArray as $tagName) { | ||
try { | ||
$tag = $this->systemTagManager->getTag($tagName, $userVisible, $userAssignable); | ||
$this->systemTagObjectMapper->unassignTags((string)$targetNode->getId(), 'files', $tag->getId()); | ||
$output->writeln("<info>$access</info> tag named <info>$tagName</info> removed."); | ||
} catch (TagNotFoundException $e) { | ||
$output->writeln("<info>$access</info> tag named <info>$tagName</info> does not exist!"); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Core\Command\SystemTag\Files; | ||
|
||
use OC\Core\Command\Info\FileUtils; | ||
use OCP\SystemTag\ISystemTagObjectMapper; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class DeleteAll extends Command { | ||
|
||
public function __construct( | ||
private FileUtils $fileUtils, | ||
private ISystemTagObjectMapper $systemTagObjectMapper, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void { | ||
$this->setName('tag:files:delete-all') | ||
->setDescription('Delete all system-tags from a file or folder') | ||
->addArgument('target', InputArgument::REQUIRED, 'file id or path'); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int { | ||
$targetInput = $input->getArgument('target'); | ||
$targetNode = $this->fileUtils->getNode($targetInput); | ||
|
||
if (! $targetNode) { | ||
$output->writeln("<error>file $targetInput not found</error>"); | ||
return 1; | ||
} | ||
|
||
$tags = $this->systemTagObjectMapper->getTagIdsForObjects($targetNode->getId(), 'files'); | ||
$this->systemTagObjectMapper->unassignTags((string)$targetNode->getId(), 'files', $tags[$targetNode->getId()]); | ||
$output->writeln('<info>all tags removed.</info>'); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters