Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions apps/dav/lib/SystemTag/SystemTagPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
*/
private $server;

/** @var array<int, string[]> */
/** @var array<string, list<string>> */
private array $cachedTagMappings = [];
/** @var array<string, ISystemTag> */
private array $cachedTags = [];
Expand Down Expand Up @@ -210,17 +210,17 @@ private function preloadCollection(
}

if ($collection instanceof Directory
&& !isset($this->cachedTagMappings[$collection->getId()])
&& !isset($this->cachedTagMappings[(string)$collection->getId()])
&& $propFind->getStatus(
self::SYSTEM_TAGS_PROPERTYNAME
) !== null) {
$fileIds = [$collection->getId()];
$fileIds = [(string)$collection->getId()];

// note: pre-fetching only supported for depth <= 1
$folderContent = $collection->getChildren();
foreach ($folderContent as $info) {
if ($info instanceof Node) {
$fileIds[] = $info->getId();
$fileIds[] = (string)$info->getId();
}
}

Expand All @@ -231,7 +231,7 @@ private function preloadCollection(

// also cache the ones that were not found
foreach ($emptyFileIds as $fileId) {
$this->cachedTagMappings[$fileId] = [];
$this->cachedTagMappings[(string)$fileId] = [];
}
}
}
Expand Down Expand Up @@ -350,10 +350,10 @@ private function propfindForFile(PropFind $propFind, Node $node): void {
* @return ISystemTag[]
*/
private function getTagsForFile(int $fileId, ?IUser $user): array {
if (isset($this->cachedTagMappings[$fileId])) {
$tagIds = $this->cachedTagMappings[$fileId];
if (isset($this->cachedTagMappings[(string)$fileId])) {
$tagIds = $this->cachedTagMappings[(string)$fileId];
} else {
$tags = $this->tagMapper->getTagIdsForObjects([$fileId], 'files');
$tags = $this->tagMapper->getTagIdsForObjects([(string)$fileId], 'files');
$fileTags = current($tags);
if ($fileTags) {
$tagIds = $fileTags;
Expand All @@ -362,13 +362,10 @@ private function getTagsForFile(int $fileId, ?IUser $user): array {
}
}

$tags = array_filter(array_map(function (string $tagId) {
return $this->cachedTags[$tagId] ?? null;
}, $tagIds));
$tags = array_filter(array_map(
fn (string $tagId): ?ISystemTag => $this->cachedTags[$tagId] ?? null, $tagIds));

$uncachedTagIds = array_filter($tagIds, function (string $tagId): bool {
return !isset($this->cachedTags[$tagId]);
});
$uncachedTagIds = array_filter($tagIds, fn (string $tagId): bool => !isset($this->cachedTags[$tagId]));

if (count($uncachedTagIds)) {
$retrievedTags = $this->tagManager->getTagsByIds($uncachedTagIds);
Expand Down
7 changes: 4 additions & 3 deletions apps/systemtags/lib/Command/Files/DeleteAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ public function execute(InputInterface $input, OutputInterface $output): int {
$targetInput = $input->getArgument('target');
$targetNode = $this->fileUtils->getNode($targetInput);

if (! $targetNode) {
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()]);
$targetNodeId = (string)$targetNode->getId();
$tags = $this->systemTagObjectMapper->getTagIdsForObjects([$targetNodeId], 'files');
$this->systemTagObjectMapper->unassignTags($targetNodeId, 'files', $tags[$targetNodeId]);
$output->writeln('<info>all tags removed.</info>');

return 0;
Expand Down
2 changes: 1 addition & 1 deletion apps/systemtags/lib/Search/TagSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
$searchResultEntry = new SearchResultEntry(
$thumbnailUrl,
$result->getName(),
$this->formatSubline($query, $matchedTags[$nodeId]),
$this->formatSubline($query, $matchedTags[(string)$nodeId]),
$this->urlGenerator->getAbsoluteURL($link),
$result->getMimetype() === FileInfo::MIMETYPE_FOLDER
? 'icon-folder'
Expand Down
22 changes: 11 additions & 11 deletions lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use OCP\SystemTag\TagAssignedEvent;
use OCP\SystemTag\TagNotFoundException;
use OCP\SystemTag\TagUnassignedEvent;
use Override;

class SystemTagObjectMapper implements ISystemTagObjectMapper {
public const RELATION_TABLE = 'systemtag_object_mapping';
Expand All @@ -31,9 +32,7 @@ public function __construct(
) {
}

/**
* {@inheritdoc}
*/
#[Override]
public function getTagIdsForObjects($objIds, string $objectType): array {
if (!\is_array($objIds)) {
$objIds = [$objIds];
Expand All @@ -59,7 +58,7 @@ public function getTagIdsForObjects($objIds, string $objectType): array {
$result = $query->executeQuery();
while ($row = $result->fetch()) {
$objectId = $row['objectid'];
$mapping[$objectId][] = $row['systemtagid'];
$mapping[$objectId][] = (string)$row['systemtagid'];
}

$result->closeCursor();
Expand Down Expand Up @@ -108,9 +107,7 @@ public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0,
return $objectIds;
}

/**
* {@inheritdoc}
*/
#[Override]
public function assignTags(string $objId, string $objectType, $tagIds): void {
if (!\is_array($tagIds)) {
$tagIds = [$tagIds];
Expand Down Expand Up @@ -168,18 +165,18 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
return;
}

$tagsAssigned = array_map(static fn (string $tagId): int => (int)$tagId, $tagsAssigned);

$this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent(
MapperEvent::EVENT_ASSIGN,
$objectType,
$objId,
$tagsAssigned
$tagsAssigned,
));
$this->dispatcher->dispatchTyped(new TagAssignedEvent($objectType, [$objId], $tagsAssigned));
}

/**
* {@inheritdoc}
*/
#[Override]
public function unassignTags(string $objId, string $objectType, $tagIds): void {
if (!\is_array($tagIds)) {
$tagIds = [$tagIds];
Expand All @@ -199,6 +196,9 @@ public function unassignTags(string $objId, string $objectType, $tagIds): void {

$this->updateEtagForTags($tagIds);

// convert ids to int because the event uses ints
$tagIds = array_map(static fn (string $tagId): int => (int)$tagId, $tagIds);

$this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent(
MapperEvent::EVENT_UNASSIGN,
$objectType,
Expand Down
32 changes: 19 additions & 13 deletions lib/public/SystemTag/ISystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,36 @@
*/
namespace OCP\SystemTag;

use OCP\AppFramework\Attribute\Consumable;

/**
* Public interface to access and manage system-wide tags.
*
* @since 9.0.0
*/
#[Consumable(since: '9.0.0')]
interface ISystemTagObjectMapper {
/**
* Get a list of tag ids for the given object ids.
*
* This returns an array that maps object id to tag ids
*
* ```
* [
* 1 => array('id1', 'id2'),
* 2 => array('id3', 'id2'),
* 3 => array('id5'),
* 4 => array()
* 1 => ['id1', 'id2'],
* 2 => ['id3', 'id2'],
* 3 => ['id5'],
* 4 => []
* ]
* ```
*
* Untagged objects will have an empty array associated.
*
* @param string|array $objIds object ids
* @param string|list<string> $objIds object ids
* @param string $objectType object type
*
* @return array with object id as key and an array
* of tag ids as value
* @return array<string, list<string>> with object id as key and an array
* of tag ids as value
*
* @since 9.0.0
*/
Expand All @@ -40,12 +46,12 @@ public function getTagIdsForObjects($objIds, string $objectType): array;
/**
* Get a list of objects tagged with $tagIds.
*
* @param string|array $tagIds Tag id or array of tag ids.
* @param string|list<string> $tagIds Tag id or array of tag ids.
* @param string $objectType object type
* @param int $limit Count of object ids you want to get
* @param string $offset The last object id you already received
*
* @return string[] array of object ids or empty array if none found
* @return list<string> array of object ids or empty array if none found
*
* @throws TagNotFoundException if at least one of the
* given tags does not exist
Expand All @@ -66,7 +72,7 @@ public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0,
*
* @param string $objId object id
* @param string $objectType object type
* @param string|array $tagIds tag id or array of tag ids to assign
* @param string|list<string> $tagIds tag id or array of tag ids to assign
*
* @throws TagNotFoundException if at least one of the
* given tags does not exist
Expand All @@ -85,7 +91,7 @@ public function assignTags(string $objId, string $objectType, $tagIds);
*
* @param string $objId object id
* @param string $objectType object type
* @param string|array $tagIds tag id or array of tag ids to unassign
* @param string|list<string> $tagIds tag id or array of tag ids to unassign
*
* @throws TagNotFoundException if at least one of the
* given tags does not exist
Expand All @@ -97,7 +103,7 @@ public function unassignTags(string $objId, string $objectType, $tagIds);
/**
* Checks whether the given objects have the given tag.
*
* @param string|array $objIds object ids
* @param string|list<string> $objIds object ids
* @param string $objectType object type
* @param string $tagId tag id to check
* @param bool $all true to check that ALL objects have the tag assigned,
Expand Down Expand Up @@ -128,7 +134,7 @@ public function getAvailableObjectTypes(): array;
*
* @param string $tagId tag id
* @param string $objectType object type
* @param string[] $objectIds list of object ids
* @param list<string> $objectIds list of object ids
*
* @throws TagNotFoundException if the tag does not exist
* @since 31.0.0
Expand Down
Loading