mirror of
https://github.com/nextcloud/server.git
synced 2026-04-05 09:06:35 -04:00
refactor: remove SystemTag logic from Folder into QuerySearchHelper
- adds OC\SystemTag\SystemTagsInFilesDetector where the search logic is moved to Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
7f3725c962
commit
dbfd2f936a
6 changed files with 117 additions and 55 deletions
|
|
@ -27,6 +27,7 @@ declare(strict_types=1);
|
|||
namespace OCA\DAV\SystemTag;
|
||||
|
||||
use OC\SystemTag\SystemTag;
|
||||
use OC\SystemTag\SystemTagsInFilesDetector;
|
||||
use OC\User\NoUserException;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\Files\NotPermittedException;
|
||||
|
|
@ -40,19 +41,22 @@ class SystemTagsInUseCollection extends SimpleCollection {
|
|||
protected IUserSession $userSession;
|
||||
protected IRootFolder $rootFolder;
|
||||
protected string $mediaType;
|
||||
private ISystemTagManager $systemTagManager;
|
||||
protected ISystemTagManager $systemTagManager;
|
||||
protected SystemTagsInFilesDetector $systemTagsInFilesDetector;
|
||||
|
||||
/** @noinspection PhpMissingParentConstructorInspection */
|
||||
public function __construct(
|
||||
IUserSession $userSession,
|
||||
IRootFolder $rootFolder,
|
||||
ISystemTagManager $systemTagManager,
|
||||
SystemTagsInFilesDetector $systemTagsInFilesDetector,
|
||||
string $mediaType = ''
|
||||
) {
|
||||
$this->userSession = $userSession;
|
||||
$this->rootFolder = $rootFolder;
|
||||
$this->systemTagManager = $systemTagManager;
|
||||
$this->mediaType = $mediaType;
|
||||
$this->systemTagsInFilesDetector = $systemTagsInFilesDetector;
|
||||
$this->name = 'systemtags-assigned';
|
||||
if ($this->mediaType != '') {
|
||||
$this->name .= '/' . $this->mediaType;
|
||||
|
|
@ -67,7 +71,7 @@ class SystemTagsInUseCollection extends SimpleCollection {
|
|||
if ($this->mediaType !== '') {
|
||||
throw new NotFound('Invalid media type');
|
||||
}
|
||||
return new self($this->userSession, $this->rootFolder, $this->systemTagManager, $name);
|
||||
return new self($this->userSession, $this->rootFolder, $this->systemTagManager, $this->systemTagsInFilesDetector, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -89,7 +93,7 @@ class SystemTagsInUseCollection extends SimpleCollection {
|
|||
throw new Forbidden('Permission denied to read this collection');
|
||||
}
|
||||
|
||||
$result = $userFolder->getSystemTags($this->mediaType);
|
||||
$result = $this->systemTagsInFilesDetector->detectAssignedSystemTagsIn($userFolder, $this->mediaType);
|
||||
$children = [];
|
||||
foreach ($result as $tagData) {
|
||||
$tag = new SystemTag((string)$tagData['id'], $tagData['name'], (bool)$tagData['visibility'], (bool)$tagData['editable']);
|
||||
|
|
|
|||
|
|
@ -1620,6 +1620,7 @@ return array(
|
|||
'OC\\SystemTag\\SystemTag' => $baseDir . '/lib/private/SystemTag/SystemTag.php',
|
||||
'OC\\SystemTag\\SystemTagManager' => $baseDir . '/lib/private/SystemTag/SystemTagManager.php',
|
||||
'OC\\SystemTag\\SystemTagObjectMapper' => $baseDir . '/lib/private/SystemTag/SystemTagObjectMapper.php',
|
||||
'OC\\SystemTag\\SystemTagsInFilesDetector' => $baseDir . '/lib/private/SystemTag/SystemTagsInFilesDetector.php',
|
||||
'OC\\TagManager' => $baseDir . '/lib/private/TagManager.php',
|
||||
'OC\\Tagging\\Tag' => $baseDir . '/lib/private/Tagging/Tag.php',
|
||||
'OC\\Tagging\\TagMapper' => $baseDir . '/lib/private/Tagging/TagMapper.php',
|
||||
|
|
|
|||
|
|
@ -1653,6 +1653,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
|
|||
'OC\\SystemTag\\SystemTag' => __DIR__ . '/../../..' . '/lib/private/SystemTag/SystemTag.php',
|
||||
'OC\\SystemTag\\SystemTagManager' => __DIR__ . '/../../..' . '/lib/private/SystemTag/SystemTagManager.php',
|
||||
'OC\\SystemTag\\SystemTagObjectMapper' => __DIR__ . '/../../..' . '/lib/private/SystemTag/SystemTagObjectMapper.php',
|
||||
'OC\\SystemTag\\SystemTagsInFilesDetector' => __DIR__ . '/../../..' . '/lib/private/SystemTag/SystemTagsInFilesDetector.php',
|
||||
'OC\\TagManager' => __DIR__ . '/../../..' . '/lib/private/TagManager.php',
|
||||
'OC\\Tagging\\Tag' => __DIR__ . '/../../..' . '/lib/private/Tagging/Tag.php',
|
||||
'OC\\Tagging\\TagMapper' => __DIR__ . '/../../..' . '/lib/private/Tagging/TagMapper.php',
|
||||
|
|
|
|||
|
|
@ -25,13 +25,18 @@
|
|||
*/
|
||||
namespace OC\Files\Cache;
|
||||
|
||||
use OC\Files\Cache\Wrapper\CacheJail;
|
||||
use OC\Files\Node\Root;
|
||||
use OC\Files\Search\QueryOptimizer\QueryOptimizer;
|
||||
use OC\Files\Search\SearchBinaryOperator;
|
||||
use OC\SystemConfig;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\Files\Cache\ICache;
|
||||
use OCP\Files\Cache\ICacheEntry;
|
||||
use OCP\Files\Folder;
|
||||
use OCP\Files\IMimeTypeLoader;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\Files\Mount\IMountPoint;
|
||||
use OCP\Files\Search\ISearchBinaryOperator;
|
||||
use OCP\Files\Search\ISearchQuery;
|
||||
use OCP\IDBConnection;
|
||||
|
|
@ -190,4 +195,38 @@ class QuerySearchHelper {
|
|||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{array<string, ICache>, array<string, IMountPoint>}
|
||||
*/
|
||||
public function getCachesAndMountPointsForSearch(Root $root, string $path, bool $limitToHome = false): array {
|
||||
$rootLength = strlen($path);
|
||||
$mount = $root->getMount($path);
|
||||
$storage = $mount->getStorage();
|
||||
$internalPath = $mount->getInternalPath($path);
|
||||
|
||||
if ($internalPath !== '') {
|
||||
// a temporary CacheJail is used to handle filtering down the results to within this folder
|
||||
$caches = ['' => new CacheJail($storage->getCache(''), $internalPath)];
|
||||
} else {
|
||||
$caches = ['' => $storage->getCache('')];
|
||||
}
|
||||
$mountByMountPoint = ['' => $mount];
|
||||
|
||||
if (!$limitToHome) {
|
||||
/** @var IMountPoint[] $mounts */
|
||||
$mounts = $root->getMountsIn($path);
|
||||
foreach ($mounts as $mount) {
|
||||
$storage = $mount->getStorage();
|
||||
if ($storage) {
|
||||
$relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
|
||||
$caches[$relativeMountPoint] = $storage->getCache('');
|
||||
$mountByMountPoint[$relativeMountPoint] = $mount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [$caches, $mountByMountPoint];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ namespace OC\Files\Node;
|
|||
|
||||
use OC\Files\Cache\QuerySearchHelper;
|
||||
use OC\Files\Search\SearchBinaryOperator;
|
||||
use OC\Files\Cache\Wrapper\CacheJail;
|
||||
use OC\Files\Search\SearchComparison;
|
||||
use OC\Files\Search\SearchOrder;
|
||||
use OC\Files\Search\SearchQuery;
|
||||
|
|
@ -215,37 +214,6 @@ class Folder extends Node implements \OCP\Files\Folder {
|
|||
return new SearchQuery($operator, $limit, $offset, [], $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return list{0: array<string, \OCP\Files\Cache\ICache>, 1: array<string, \OCP\Files\Mount\IMountPoint>}
|
||||
*/
|
||||
protected function getCachesAndMountpointsForSearch(bool $limitToHome = false): array {
|
||||
$rootLength = strlen($this->path);
|
||||
$mount = $this->root->getMount($this->path);
|
||||
$storage = $mount->getStorage();
|
||||
$internalPath = $mount->getInternalPath($this->path);
|
||||
if ($internalPath !== '') {
|
||||
// a temporary CacheJail is used to handle filtering down the results to within this folder
|
||||
$caches = ['' => new CacheJail($storage->getCache(''), $internalPath)];
|
||||
} else {
|
||||
$caches = ['' => $storage->getCache('')];
|
||||
}
|
||||
$mountByMountPoint = ['' => $mount];
|
||||
|
||||
if (!$limitToHome) {
|
||||
$mounts = $this->root->getMountsIn($this->path);
|
||||
foreach ($mounts as $mount) {
|
||||
$storage = $mount->getStorage();
|
||||
if ($storage) {
|
||||
$relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
|
||||
$caches[$relativeMountPoint] = $storage->getCache('');
|
||||
$mountByMountPoint[$relativeMountPoint] = $mount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [$caches, $mountByMountPoint];
|
||||
}
|
||||
|
||||
/**
|
||||
* search for files with the name matching $query
|
||||
*
|
||||
|
|
@ -265,10 +233,9 @@ class Folder extends Node implements \OCP\Files\Folder {
|
|||
throw new \InvalidArgumentException('searching by owner is only allowed in the users home folder');
|
||||
}
|
||||
|
||||
[$caches, $mountByMountPoint] = $this->getCachesAndMountpointsForSearch($limitToHome);
|
||||
|
||||
/** @var QuerySearchHelper $searchHelper */
|
||||
$searchHelper = \OC::$server->get(QuerySearchHelper::class);
|
||||
[$caches, $mountByMountPoint] = $searchHelper->getCachesAndMountPointsForSearch($this->root, $this->path, $limitToHome);
|
||||
$resultsPerCache = $searchHelper->searchInCaches($query, $caches);
|
||||
|
||||
// loop through all results per-cache, constructing the FileInfo object from the CacheEntry and merge them all
|
||||
|
|
@ -337,24 +304,6 @@ class Folder extends Node implements \OCP\Files\Folder {
|
|||
return $this->search($query);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return array<array-key, array{id: int, name: string, visibility: int, editable: int, ref_file_id: int, number_files: int}>
|
||||
*/
|
||||
public function getSystemTags(string $mediaType, int $limit = 0, int $offset = 0): array {
|
||||
// Currently query has to have exactly one search condition. If no media type is provided,
|
||||
// we fall back to the presence of a systemtag.
|
||||
if (empty($mediaType)) {
|
||||
$query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'systemtag', '%'), null, $limit, $offset);
|
||||
} else {
|
||||
$query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mediaType . '/%'), null, $limit, $offset);
|
||||
}
|
||||
[$caches, ] = $this->getCachesAndMountpointsForSearch();
|
||||
/** @var QuerySearchHelper $searchHelper */
|
||||
$searchHelper = \OCP\Server::get(QuerySearchHelper::class);
|
||||
return $searchHelper->findUsedTagsInCaches($query, $caches);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return \OC\Files\Node\Node[]
|
||||
|
|
|
|||
68
lib/private/SystemTag/SystemTagsInFilesDetector.php
Normal file
68
lib/private/SystemTag/SystemTagsInFilesDetector.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OC\SystemTag;
|
||||
|
||||
use OC\Files\Cache\QuerySearchHelper;
|
||||
use OC\Files\Node\Root;
|
||||
use OC\Files\Search\SearchComparison;
|
||||
use OC\Files\Search\SearchQuery;
|
||||
use OCP\Files\Folder;
|
||||
use OCP\Files\Search\ISearchComparison;
|
||||
|
||||
class SystemTagsInFilesDetector {
|
||||
public function __construct(protected QuerySearchHelper $searchHelper) {
|
||||
}
|
||||
|
||||
public function detectAssignedSystemTagsIn(
|
||||
Folder $folder,
|
||||
string $filteredMediaType = '',
|
||||
int $limit = 0,
|
||||
int $offset = 0
|
||||
): array {
|
||||
// Currently query has to have exactly one search condition. If no media type is provided,
|
||||
// we fall back to the presence of a system tag.
|
||||
if (empty($filteredMediaType)) {
|
||||
$query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'systemtag', '%'), $limit, $offset, []);
|
||||
} else {
|
||||
$query = new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $filteredMediaType . '/%'), $limit, $offset, []);
|
||||
}
|
||||
[$caches, ] = $this->searchHelper->getCachesAndMountPointsForSearch(
|
||||
$this->getRootFolder($folder),
|
||||
$folder->getPath(),
|
||||
);
|
||||
return $this->searchHelper->findUsedTagsInCaches($query, $caches);
|
||||
}
|
||||
|
||||
protected function getRootFolder(?Folder $folder): Root {
|
||||
if ($folder instanceof Root) {
|
||||
return $folder;
|
||||
} elseif ($folder === null) {
|
||||
throw new \LogicException('Could not climb up to root folder');
|
||||
}
|
||||
return $this->getRootFolder($folder->getParent());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue