-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
new occ commands to manage system-tags for files #48277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sorbaugh
merged 1 commit into
nextcloud:master
from
schaarsc:feature/32735-occ-add-tag-to-file-master
Apr 24, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 OCA\SystemTags\Command\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 hidden or 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 OCA\SystemTags\Command\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 hidden or 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 OCA\SystemTags\Command\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; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if FileUtils is recommended to be used in apps even if they're in server