diff options
author | Morris Jobke <hey@morrisjobke.de> | 2021-03-22 21:48:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-22 21:48:51 +0100 |
commit | 1c074e76028b65c4158097a35254fbf7ebe8749c (patch) | |
tree | cbfa973fdad69df0f06347673575b9481bab09de /lib | |
parent | 1eb084cfd37875adfe37301dcef7801634f7e28a (diff) | |
parent | b38618c8139a8017590ea59064184307a93f9808 (diff) | |
download | nextcloud-server-1c074e76028b65c4158097a35254fbf7ebe8749c.tar.gz nextcloud-server-1c074e76028b65c4158097a35254fbf7ebe8749c.zip |
Merge pull request #26198 from nextcloud/unified-search-node
Handle limit offset and sorting in files search
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Files/Cache/Cache.php | 12 | ||||
-rw-r--r-- | lib/private/Files/Node/Folder.php | 179 | ||||
-rw-r--r-- | lib/private/Files/Search/SearchOrder.php | 25 | ||||
-rw-r--r-- | lib/private/Files/Search/SearchQuery.php | 8 | ||||
-rw-r--r-- | lib/private/Search/Provider/File.php | 48 | ||||
-rw-r--r-- | lib/private/Search/Result/File.php | 7 | ||||
-rw-r--r-- | lib/public/Files/Search/ISearchOrder.php | 12 | ||||
-rw-r--r-- | lib/public/Files/Search/ISearchQuery.php | 2 |
8 files changed, 203 insertions, 90 deletions
diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index a18fddcbdeb..59e50164ef6 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -198,10 +198,10 @@ class Cache implements ICache { } $data['permissions'] = (int)$data['permissions']; if (isset($data['creation_time'])) { - $data['creation_time'] = (int) $data['creation_time']; + $data['creation_time'] = (int)$data['creation_time']; } if (isset($data['upload_time'])) { - $data['upload_time'] = (int) $data['upload_time']; + $data['upload_time'] = (int)$data['upload_time']; } return new CacheEntry($data); } @@ -841,6 +841,10 @@ class Cache implements ICache { $query->whereStorageId(); if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) { + $user = $searchQuery->getUser(); + if ($user === null) { + throw new \InvalidArgumentException("Searching by tag requires the user to be set in the query"); + } $query ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( @@ -848,7 +852,7 @@ class Cache implements ICache { $builder->expr()->eq('tagmap.categoryid', 'tag.id') )) ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files'))) - ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID()))); + ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($user->getUID()))); } $searchExpr = $this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation()); @@ -1031,7 +1035,7 @@ class Cache implements ICache { return null; } - return (string) $path; + return (string)$path; } /** diff --git a/lib/private/Files/Node/Folder.php b/lib/private/Files/Node/Folder.php index fb0748465da..36c4cbe58bc 100644 --- a/lib/private/Files/Node/Folder.php +++ b/lib/private/Files/Node/Folder.php @@ -32,15 +32,24 @@ namespace OC\Files\Node; use OC\DB\QueryBuilder\Literal; +use OC\Files\Search\SearchBinaryOperator; +use OC\Files\Search\SearchComparison; +use OC\Files\Search\SearchQuery; use OC\Files\Storage\Wrapper\Jail; +use OC\Files\Storage\Storage; use OCA\Files_Sharing\SharedStorage; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\Files\Cache\ICacheEntry; use OCP\Files\Config\ICachedMountInfo; use OCP\Files\FileInfo; use OCP\Files\Mount\IMountPoint; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\Files\Search\ISearchBinaryOperator; +use OCP\Files\Search\ISearchComparison; +use OCP\Files\Search\ISearchOperator; use OCP\Files\Search\ISearchQuery; +use OCP\IUserManager; class Folder extends Node implements \OCP\Files\Folder { /** @@ -96,8 +105,8 @@ class Folder extends Node implements \OCP\Files\Folder { /** * get the content of this directory * - * @throws \OCP\Files\NotFoundException * @return Node[] + * @throws \OCP\Files\NotFoundException */ public function getDirectoryListing() { $folderContent = $this->view->getDirectoryContent($this->path); @@ -200,6 +209,17 @@ class Folder extends Node implements \OCP\Files\Folder { throw new NotPermittedException('No create permission for path'); } + private function queryFromOperator(ISearchOperator $operator, string $uid = null): ISearchQuery { + if ($uid === null) { + $user = null; + } else { + /** @var IUserManager $userManager */ + $userManager = \OC::$server->query(IUserManager::class); + $user = $userManager->get($uid); + } + return new SearchQuery($operator, 0, 0, [], $user); + } + /** * search for files with the name matching $query * @@ -208,45 +228,27 @@ class Folder extends Node implements \OCP\Files\Folder { */ public function search($query) { if (is_string($query)) { - return $this->searchCommon('search', ['%' . $query . '%']); - } else { - return $this->searchCommon('searchQuery', [$query]); + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%')); } - } - /** - * search for files by mimetype - * - * @param string $mimetype - * @return Node[] - */ - public function searchByMime($mimetype) { - return $this->searchCommon('searchByMime', [$mimetype]); - } - - /** - * search for files by tag - * - * @param string|int $tag name or tag id - * @param string $userId owner of the tags - * @return Node[] - */ - public function searchByTag($tag, $userId) { - return $this->searchCommon('searchByTag', [$tag, $userId]); - } - - /** - * @param string $method cache method - * @param array $args call args - * @return \OC\Files\Node\Node[] - */ - private function searchCommon($method, $args) { - $limitToHome = ($method === 'searchQuery')? $args[0]->limitToHome(): false; + // Limit+offset for queries with ordering + // + // Because we currently can't do ordering between the results from different storages in sql + // The only way to do ordering is requesting the $limit number of entries from all storages + // sorting them and returning the first $limit entries. + // + // For offset we have the same problem, we don't know how many entries from each storage should be skipped + // by a given $offset, so instead we query $offset + $limit from each storage and return entries $offset..($offset+$limit) + // after merging and sorting them. + // + // This is suboptimal but because limit and offset tend to be fairly small in real world use cases it should + // still be significantly better than disabling paging altogether + + $limitToHome = $query->limitToHome(); if ($limitToHome && count(explode('/', $this->path)) !== 3) { throw new \InvalidArgumentException('searching by owner is only allows on the users home folder'); } - $files = []; $rootLength = strlen($this->path); $mount = $this->root->getMount($this->path); $storage = $mount->getStorage(); @@ -255,45 +257,106 @@ class Folder extends Node implements \OCP\Files\Folder { if ($internalPath !== '') { $internalPath = $internalPath . '/'; } - $internalRootLength = strlen($internalPath); + + $subQueryLimit = $query->getLimit() > 0 ? $query->getLimit() + $query->getOffset() : 0; + $rootQuery = new SearchQuery( + new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [ + new SearchComparison(ISearchComparison::COMPARE_LIKE, 'path', $internalPath . '%'), + $query->getSearchOperation(), + ] + ), + $subQueryLimit, + 0, + $query->getOrder(), + $query->getUser() + ); + + $files = []; $cache = $storage->getCache(''); - $results = call_user_func_array([$cache, $method], $args); + $results = $cache->searchQuery($rootQuery); foreach ($results as $result) { - if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) { - $result['internalPath'] = $result['path']; - $result['path'] = substr($result['path'], $internalRootLength); - $result['storage'] = $storage; - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); - } + $files[] = $this->cacheEntryToFileInfo($mount, '', $internalPath, $result); } if (!$limitToHome) { $mounts = $this->root->getMountsIn($this->path); foreach ($mounts as $mount) { + $subQuery = new SearchQuery( + $query->getSearchOperation(), + $subQueryLimit, + 0, + $query->getOrder(), + $query->getUser() + ); + $storage = $mount->getStorage(); if ($storage) { $cache = $storage->getCache(''); $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); - $results = call_user_func_array([$cache, $method], $args); + $results = $cache->searchQuery($subQuery); foreach ($results as $result) { - $result['internalPath'] = $result['path']; - $result['path'] = $relativeMountPoint . $result['path']; - $result['storage'] = $storage; - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, - $result['internalPath'], $result, $mount); + $files[] = $this->cacheEntryToFileInfo($mount, $relativeMountPoint, '', $result); } } } } + $order = $query->getOrder(); + if ($order) { + usort($files, function (FileInfo $a,FileInfo $b) use ($order) { + foreach ($order as $orderField) { + $cmp = $orderField->sortFileInfo($a, $b); + if ($cmp !== 0) { + return $cmp; + } + } + return 0; + }); + } + $files = array_values(array_slice($files, $query->getOffset(), $query->getLimit() > 0 ? $query->getLimit() : null)); + return array_map(function (FileInfo $file) { return $this->createNode($file->getPath(), $file); }, $files); } + private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, string $trimRoot, ICacheEntry $cacheEntry): FileInfo { + $trimLength = strlen($trimRoot); + $cacheEntry['internalPath'] = $cacheEntry['path']; + $cacheEntry['path'] = $appendRoot . substr($cacheEntry['path'], $trimLength); + return new \OC\Files\FileInfo($this->path . '/' . $cacheEntry['path'], $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount); + } + + /** + * search for files by mimetype + * + * @param string $mimetype + * @return Node[] + */ + public function searchByMime($mimetype) { + if (strpos($mimetype, '/') === false) { + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%')); + } else { + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype)); + } + return $this->search($query); + } + + /** + * search for files by tag + * + * @param string|int $tag name or tag id + * @param string $userId owner of the tags + * @return Node[] + */ + public function searchByTag($tag, $userId) { + $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', $tag), $userId); + return $this->search($query); + } + /** * @param int $id * @return \OC\Files\Node\Node[] @@ -320,7 +383,7 @@ class Folder extends Node implements \OCP\Files\Folder { if (count($mountsContainingFile) === 0) { if ($user === $this->getAppDataDirectoryName()) { - return $this->getByIdInRootMount((int) $id); + return $this->getByIdInRootMount((int)$id); } return []; } @@ -383,11 +446,11 @@ class Folder extends Node implements \OCP\Files\Folder { return [$this->root->createNode( $absolutePath, new \OC\Files\FileInfo( - $absolutePath, - $mount->getStorage(), - $cacheEntry->getPath(), - $cacheEntry, - $mount + $absolutePath, + $mount->getStorage(), + $cacheEntry->getPath(), + $cacheEntry, + $mount ))]; } @@ -518,10 +581,10 @@ class Folder extends Node implements \OCP\Files\Folder { $query->andWhere($storageFilters); $query->andWhere($builder->expr()->orX( - // handle non empty folders separate - $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), - $builder->expr()->eq('f.size', new Literal(0)) - )) + // handle non empty folders separate + $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), + $builder->expr()->eq('f.size', new Literal(0)) + )) ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_versions/%'))) ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_trashbin/%'))) ->orderBy('f.mtime', 'DESC') diff --git a/lib/private/Files/Search/SearchOrder.php b/lib/private/Files/Search/SearchOrder.php index 4bff8ba1b6c..deb73baa4c0 100644 --- a/lib/private/Files/Search/SearchOrder.php +++ b/lib/private/Files/Search/SearchOrder.php @@ -23,6 +23,7 @@ namespace OC\Files\Search; +use OCP\Files\FileInfo; use OCP\Files\Search\ISearchOrder; class SearchOrder implements ISearchOrder { @@ -55,4 +56,28 @@ class SearchOrder implements ISearchOrder { public function getField() { return $this->field; } + + public function sortFileInfo(FileInfo $a, FileInfo $b): int { + $cmp = $this->sortFileInfoNoDirection($a, $b); + return $cmp * ($this->direction === ISearchOrder::DIRECTION_ASCENDING ? 1 : -1); + } + + private function sortFileInfoNoDirection(FileInfo $a, FileInfo $b): int { + switch ($this->field) { + case 'name': + return $a->getName() <=> $b->getName(); + case 'mimetype': + return $a->getMimetype() <=> $b->getMimetype(); + case 'mtime': + return $a->getMtime() <=> $b->getMtime(); + case 'size': + return $a->getSize() <=> $b->getSize(); + case 'fileid': + return $a->getId() <=> $b->getId(); + case 'permissions': + return $a->getPermissions() <=> $b->getPermissions(); + default: + return 0; + } + } } diff --git a/lib/private/Files/Search/SearchQuery.php b/lib/private/Files/Search/SearchQuery.php index b7b8b801104..091fe57d21b 100644 --- a/lib/private/Files/Search/SearchQuery.php +++ b/lib/private/Files/Search/SearchQuery.php @@ -37,7 +37,7 @@ class SearchQuery implements ISearchQuery { private $offset; /** @var ISearchOrder[] */ private $order; - /** @var IUser */ + /** @var ?IUser */ private $user; private $limitToHome; @@ -48,7 +48,7 @@ class SearchQuery implements ISearchQuery { * @param int $limit * @param int $offset * @param array $order - * @param IUser $user + * @param ?IUser $user * @param bool $limitToHome */ public function __construct( @@ -56,7 +56,7 @@ class SearchQuery implements ISearchQuery { int $limit, int $offset, array $order, - IUser $user, + ?IUser $user = null, bool $limitToHome = false ) { $this->searchOperation = $searchOperation; @@ -96,7 +96,7 @@ class SearchQuery implements ISearchQuery { } /** - * @return IUser + * @return ?IUser */ public function getUser() { return $this->user; diff --git a/lib/private/Search/Provider/File.php b/lib/private/Search/Provider/File.php index 4125b1f7d70..d42d57b8003 100644 --- a/lib/private/Search/Provider/File.php +++ b/lib/private/Search/Provider/File.php @@ -29,7 +29,14 @@ namespace OC\Search\Provider; -use OC\Files\Filesystem; +use OC\Files\Search\SearchComparison; +use OC\Files\Search\SearchOrder; +use OC\Files\Search\SearchQuery; +use OCP\Files\FileInfo; +use OCP\Files\IRootFolder; +use OCP\Files\Search\ISearchComparison; +use OCP\Files\Search\ISearchOrder; +use OCP\IUserSession; use OCP\Search\PagedProvider; /** @@ -48,35 +55,38 @@ class File extends PagedProvider { * @deprecated 20.0.0 */ public function search($query, int $limit = null, int $offset = null) { - if ($offset === null) { - $offset = 0; + /** @var IRootFolder $rootFolder */ + $rootFolder = \OC::$server->query(IRootFolder::class); + /** @var IUserSession $userSession */ + $userSession = \OC::$server->query(IUserSession::class); + $user = $userSession->getUser(); + if (!$user) { + return []; } - \OC_Util::setupFS(); - $files = Filesystem::search($query); + $userFolder = $rootFolder->getUserFolder($user->getUID()); + $fileQuery = new SearchQuery( + new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%'), + (int)$limit, + (int)$offset, + [ + new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'), + ], + $user + ); + $files = $userFolder->search($fileQuery); $results = []; - if ($limit !== null) { - $files = array_slice($files, $offset, $offset + $limit); - } // edit results foreach ($files as $fileData) { - // skip versions - if (strpos($fileData['path'], '_versions') === 0) { - continue; - } - // skip top-level folder - if ($fileData['name'] === 'files' && $fileData['parent'] === -1) { - continue; - } // create audio result - if ($fileData['mimepart'] === 'audio') { + if ($fileData->getMimePart() === 'audio') { $result = new \OC\Search\Result\Audio($fileData); } // create image result - elseif ($fileData['mimepart'] === 'image') { + elseif ($fileData->getMimePart() === 'image') { $result = new \OC\Search\Result\Image($fileData); } // create folder result - elseif ($fileData['mimetype'] === 'httpd/unix-directory') { + elseif ($fileData->getMimetype() === FileInfo::MIMETYPE_FOLDER) { $result = new \OC\Search\Result\Folder($fileData); } // or create file result diff --git a/lib/private/Search/Result/File.php b/lib/private/Search/Result/File.php index 33e1e97f471..c3b0c4e3751 100644 --- a/lib/private/Search/Result/File.php +++ b/lib/private/Search/Result/File.php @@ -97,14 +97,13 @@ class File extends \OCP\Search\Result { public function __construct(FileInfo $data) { $path = $this->getRelativePath($data->getPath()); - $info = pathinfo($path); $this->id = $data->getId(); - $this->name = $info['basename']; + $this->name = $data->getName(); $this->link = \OC::$server->getURLGenerator()->linkToRoute( 'files.view.index', [ - 'dir' => $info['dirname'], - 'scrollto' => $info['basename'], + 'dir' => dirname($path), + 'scrollto' => $data->getName(), ] ); $this->permissions = $data->getPermissions(); diff --git a/lib/public/Files/Search/ISearchOrder.php b/lib/public/Files/Search/ISearchOrder.php index fb0137c1bac..8237b1861ea 100644 --- a/lib/public/Files/Search/ISearchOrder.php +++ b/lib/public/Files/Search/ISearchOrder.php @@ -24,6 +24,8 @@ namespace OCP\Files\Search; +use OCP\Files\FileInfo; + /** * @since 12.0.0 */ @@ -46,4 +48,14 @@ interface ISearchOrder { * @since 12.0.0 */ public function getField(); + + /** + * Apply the sorting on 2 FileInfo objects + * + * @param FileInfo $a + * @param FileInfo $b + * @return int -1 if $a < $b, 0 if $a = $b, 1 if $a > $b (for ascending, reverse for descending) + * @since 22.0.0 + */ + public function sortFileInfo(FileInfo $a, FileInfo $b): int; } diff --git a/lib/public/Files/Search/ISearchQuery.php b/lib/public/Files/Search/ISearchQuery.php index 4d866f8d7b6..dd7c901f7f5 100644 --- a/lib/public/Files/Search/ISearchQuery.php +++ b/lib/public/Files/Search/ISearchQuery.php @@ -62,7 +62,7 @@ interface ISearchQuery { /** * The user that issued the search * - * @return IUser + * @return ?IUser * @since 12.0.0 */ public function getUser(); |