Skip to content

Commit 0e6b256

Browse files
committed
fixup! feat(dav): Support multiple scopes in DAV search
1 parent 817ac06 commit 0e6b256

File tree

1 file changed

+60
-43
lines changed

1 file changed

+60
-43
lines changed

apps/dav/lib/Files/FileSearchBackend.php

+60-43
Original file line numberDiff line numberDiff line change
@@ -160,50 +160,63 @@ public function preloadPropertyFor(array $nodes, array $requestProperties): void
160160
* @return SearchResult[]
161161
*/
162162
public function search(Query $search): array {
163-
$scopes = [];
164-
foreach ($search->from as $scope) {
165-
if ($scope->path === null) {
166-
throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead');
167-
}
168-
$node = $this->tree->getNodeForPath($scope->path);
169-
if (!$node instanceof Directory) {
170-
throw new \InvalidArgumentException('Search is only supported on directories');
171-
}
163+
switch (count($search->from)) {
164+
case 0:
165+
throw new \InvalidArgumentException('You need to specify a scope for the search.');
166+
break;
167+
case 1:
168+
$query = $this->transformQuery($search);
169+
$scope = $search->from[0];
170+
171+
/** @var Folder $folder $results */
172+
$results = $folder->search($query);
173+
break;
174+
default:
175+
$scopes = [];
176+
foreach ($search->from as $scope) {
177+
if ($scope->path === null) {
178+
throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead');
179+
}
180+
$node = $this->tree->getNodeForPath($scope->path);
181+
if (!$node instanceof Directory) {
182+
throw new \InvalidArgumentException('Search is only supported on directories');
183+
}
172184

173-
$fileInfo = $node->getFileInfo();
174-
/** @var Folder $folder */
175-
$folder = $this->rootFolder->get($fileInfo->getPath());
176-
$folderStorage = $folder->getStorage();
177-
if ($folderStorage->instanceOfStorage(Jail::class)) {
178-
/** @var Jail $folderStorage */
179-
$internalPath = $folderStorage->getUnjailedPath($folder->getInternalPath());
180-
} else {
181-
$internalPath = $folder->getInternalPath();
182-
}
185+
$fileInfo = $node->getFileInfo();
186+
/** @var Folder $folder */
187+
$folder = $this->rootFolder->get($fileInfo->getPath());
188+
$folderStorage = $folder->getStorage();
189+
if ($folderStorage->instanceOfStorage(Jail::class)) {
190+
/** @var Jail $folderStorage */
191+
$internalPath = $folderStorage->getUnjailedPath($folder->getInternalPath());
192+
} else {
193+
$internalPath = $folder->getInternalPath();
194+
}
183195

184-
$scopes[] = new SearchBinaryOperator(
185-
ISearchBinaryOperator::OPERATOR_AND,
186-
[
187-
new SearchComparison(
188-
ISearchComparison::COMPARE_EQUAL,
189-
'storage',
190-
$folderStorage->getCache()->getNumericStorageId(),
191-
''
192-
),
193-
new SearchComparison(
194-
ISearchComparison::COMPARE_LIKE,
195-
'path',
196-
$internalPath . '/%',
197-
''
198-
),
199-
]
200-
);
201-
}
196+
$scopes[] = new SearchBinaryOperator(
197+
ISearchBinaryOperator::OPERATOR_AND,
198+
[
199+
new SearchComparison(
200+
ISearchComparison::COMPARE_EQUAL,
201+
'storage',
202+
$folderStorage->getCache()->getNumericStorageId(),
203+
''
204+
),
205+
new SearchComparison(
206+
ISearchComparison::COMPARE_LIKE,
207+
'path',
208+
$internalPath . '/%',
209+
''
210+
),
211+
]
212+
);
213+
}
202214

203-
$scopeOperators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $scopes);
204-
$query = $this->transformQuery($search, $scopeOperators);
205-
$userFolder = $this->rootFolder->getUserFolder($this->user->getUID());
206-
$results = $userFolder->search($query);
215+
$scopeOperators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $scopes);
216+
$query = $this->transformQuery($search, $scopeOperators);
217+
$userFolder = $this->rootFolder->getUserFolder($this->user->getUID());
218+
$results = $userFolder->search($query);
219+
}
207220

208221
/** @var SearchResult[] $nodes */
209222
$nodes = array_map(function (Node $node) {
@@ -318,7 +331,7 @@ private function getHrefForNode(Node $node) {
318331
*
319332
* @return ISearchQuery
320333
*/
321-
private function transformQuery(Query $query, SearchBinaryOperator $scopeOperators): ISearchQuery {
334+
private function transformQuery(Query $query, ?SearchBinaryOperator $scopeOperators): ISearchQuery {
322335
$orders = array_map(function (Order $order): ISearchOrder {
323336
$direction = $order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING;
324337
if (str_starts_with($order->property->name, FilesPlugin::FILE_METADATA_PREFIX)) {
@@ -348,7 +361,11 @@ private function transformQuery(Query $query, SearchBinaryOperator $scopeOperato
348361

349362
/** @var SearchBinaryOperator|SearchComparison */
350363
$queryOperators = $this->transformSearchOperation($query->where);
351-
$operators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [$queryOperators, $scopeOperators]);
364+
if ($scopeOperators === null) {
365+
$operators = $queryOperators;
366+
} else {
367+
$operators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [$queryOperators, $scopeOperators]);
368+
}
352369

353370
return new SearchQuery(
354371
$operators,

0 commit comments

Comments
 (0)