diff --git a/apps/dav/lib/Files/FileSearchBackend.php b/apps/dav/lib/Files/FileSearchBackend.php index 1c9f2fdc618f3..3b77046a5052e 100644 --- a/apps/dav/lib/Files/FileSearchBackend.php +++ b/apps/dav/lib/Files/FileSearchBackend.php @@ -160,50 +160,63 @@ public function preloadPropertyFor(array $nodes, array $requestProperties): void * @return SearchResult[] */ public function search(Query $search): array { - $scopes = []; - foreach ($search->from as $scope) { - if ($scope->path === null) { - throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead'); - } - $node = $this->tree->getNodeForPath($scope->path); - if (!$node instanceof Directory) { - throw new \InvalidArgumentException('Search is only supported on directories'); - } + switch (count($search->from)) { + case 0: + throw new \InvalidArgumentException('You need to specify a scope for the search.'); + break; + case 1: + $query = $this->transformQuery($search); + $scope = $search->from[0]; + + /** @var Folder $folder $results */ + $results = $folder->search($query); + break; + default: + $scopes = []; + foreach ($search->from as $scope) { + if ($scope->path === null) { + throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead'); + } + $node = $this->tree->getNodeForPath($scope->path); + if (!$node instanceof Directory) { + throw new \InvalidArgumentException('Search is only supported on directories'); + } - $fileInfo = $node->getFileInfo(); - /** @var Folder $folder */ - $folder = $this->rootFolder->get($fileInfo->getPath()); - $folderStorage = $folder->getStorage(); - if ($folderStorage->instanceOfStorage(Jail::class)) { - /** @var Jail $folderStorage */ - $internalPath = $folderStorage->getUnjailedPath($folder->getInternalPath()); - } else { - $internalPath = $folder->getInternalPath(); - } + $fileInfo = $node->getFileInfo(); + /** @var Folder $folder */ + $folder = $this->rootFolder->get($fileInfo->getPath()); + $folderStorage = $folder->getStorage(); + if ($folderStorage->instanceOfStorage(Jail::class)) { + /** @var Jail $folderStorage */ + $internalPath = $folderStorage->getUnjailedPath($folder->getInternalPath()); + } else { + $internalPath = $folder->getInternalPath(); + } - $scopes[] = new SearchBinaryOperator( - ISearchBinaryOperator::OPERATOR_AND, - [ - new SearchComparison( - ISearchComparison::COMPARE_EQUAL, - 'storage', - $folderStorage->getCache()->getNumericStorageId(), - '' - ), - new SearchComparison( - ISearchComparison::COMPARE_LIKE, - 'path', - $internalPath . '/%', - '' - ), - ] - ); - } + $scopes[] = new SearchBinaryOperator( + ISearchBinaryOperator::OPERATOR_AND, + [ + new SearchComparison( + ISearchComparison::COMPARE_EQUAL, + 'storage', + $folderStorage->getCache()->getNumericStorageId(), + '' + ), + new SearchComparison( + ISearchComparison::COMPARE_LIKE, + 'path', + $internalPath . '/%', + '' + ), + ] + ); + } - $scopeOperators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $scopes); - $query = $this->transformQuery($search, $scopeOperators); - $userFolder = $this->rootFolder->getUserFolder($this->user->getUID()); - $results = $userFolder->search($query); + $scopeOperators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $scopes); + $query = $this->transformQuery($search, $scopeOperators); + $userFolder = $this->rootFolder->getUserFolder($this->user->getUID()); + $results = $userFolder->search($query); + } /** @var SearchResult[] $nodes */ $nodes = array_map(function (Node $node) { @@ -318,7 +331,7 @@ private function getHrefForNode(Node $node) { * * @return ISearchQuery */ - private function transformQuery(Query $query, SearchBinaryOperator $scopeOperators): ISearchQuery { + private function transformQuery(Query $query, ?SearchBinaryOperator $scopeOperators): ISearchQuery { $orders = array_map(function (Order $order): ISearchOrder { $direction = $order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING; if (str_starts_with($order->property->name, FilesPlugin::FILE_METADATA_PREFIX)) { @@ -348,7 +361,11 @@ private function transformQuery(Query $query, SearchBinaryOperator $scopeOperato /** @var SearchBinaryOperator|SearchComparison */ $queryOperators = $this->transformSearchOperation($query->where); - $operators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [$queryOperators, $scopeOperators]); + if ($scopeOperators === null) { + $operators = $queryOperators; + } else { + $operators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [$queryOperators, $scopeOperators]); + } return new SearchQuery( $operators,