aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/Files/FileSearchBackend.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/dav/lib/Files/FileSearchBackend.php')
-rw-r--r--apps/dav/lib/Files/FileSearchBackend.php310
1 files changed, 209 insertions, 101 deletions
diff --git a/apps/dav/lib/Files/FileSearchBackend.php b/apps/dav/lib/Files/FileSearchBackend.php
index 50ebed541ee..eb548bbd55c 100644
--- a/apps/dav/lib/Files/FileSearchBackend.php
+++ b/apps/dav/lib/Files/FileSearchBackend.php
@@ -1,50 +1,39 @@
<?php
+
/**
- * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
- *
- * @author Christian <16852529+cviereck@users.noreply.github.com>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @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 <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
-
namespace OCA\DAV\Files;
use OC\Files\Search\SearchBinaryOperator;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchOrder;
use OC\Files\Search\SearchQuery;
+use OC\Files\Storage\Wrapper\Jail;
use OC\Files\View;
use OCA\DAV\Connector\Sabre\CachingTree;
use OCA\DAV\Connector\Sabre\Directory;
+use OCA\DAV\Connector\Sabre\File;
use OCA\DAV\Connector\Sabre\FilesPlugin;
+use OCA\DAV\Connector\Sabre\Server;
use OCA\DAV\Connector\Sabre\TagsPlugin;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
+use OCP\Files\Search\ISearchBinaryOperator;
+use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchOperator;
use OCP\Files\Search\ISearchOrder;
use OCP\Files\Search\ISearchQuery;
+use OCP\FilesMetadata\IFilesMetadataManager;
+use OCP\FilesMetadata\IMetadataQuery;
+use OCP\FilesMetadata\Model\IMetadataValueWrapper;
use OCP\IUser;
use OCP\Share\IManager;
use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\INode;
use SearchDAV\Backend\ISearchBackend;
use SearchDAV\Backend\SearchPropertyDefinition;
use SearchDAV\Backend\SearchResult;
@@ -54,49 +43,27 @@ use SearchDAV\Query\Order;
use SearchDAV\Query\Query;
class FileSearchBackend implements ISearchBackend {
- /** @var CachingTree */
- private $tree;
-
- /** @var IUser */
- private $user;
-
- /** @var IRootFolder */
- private $rootFolder;
-
- /** @var IManager */
- private $shareManager;
-
- /** @var View */
- private $view;
-
- /**
- * FileSearchBackend constructor.
- *
- * @param CachingTree $tree
- * @param IUser $user
- * @param IRootFolder $rootFolder
- * @param IManager $shareManager
- * @param View $view
- * @internal param IRootFolder $rootFolder
- */
- public function __construct(CachingTree $tree, IUser $user, IRootFolder $rootFolder, IManager $shareManager, View $view) {
- $this->tree = $tree;
- $this->user = $user;
- $this->rootFolder = $rootFolder;
- $this->shareManager = $shareManager;
- $this->view = $view;
+ public const OPERATOR_LIMIT = 100;
+
+ public function __construct(
+ private Server $server,
+ private CachingTree $tree,
+ private IUser $user,
+ private IRootFolder $rootFolder,
+ private IManager $shareManager,
+ private View $view,
+ private IFilesMetadataManager $filesMetadataManager,
+ ) {
}
/**
* Search endpoint will be remote.php/dav
- *
- * @return string
*/
- public function getArbiterPath() {
+ public function getArbiterPath(): string {
return '';
}
- public function isValidScope($href, $depth, $path) {
+ public function isValidScope(string $href, $depth, ?string $path): bool {
// only allow scopes inside the dav server
if (is_null($path)) {
return false;
@@ -110,11 +77,11 @@ class FileSearchBackend implements ISearchBackend {
}
}
- public function getPropertyDefinitionsForScope($href, $path) {
+ public function getPropertyDefinitionsForScope(string $href, ?string $path): array {
// all valid scopes support the same schema
//todo dynamically load all propfind properties that are supported
- return [
+ $props = [
// queryable properties
new SearchPropertyDefinition('{DAV:}displayname', true, true, true),
new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true),
@@ -125,47 +92,128 @@ class FileSearchBackend implements ISearchBackend {
new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, false),
// select only properties
- new SearchPropertyDefinition('{DAV:}resourcetype', false, true, false),
- new SearchPropertyDefinition('{DAV:}getcontentlength', false, true, false),
- new SearchPropertyDefinition(FilesPlugin::CHECKSUMS_PROPERTYNAME, false, true, false),
- new SearchPropertyDefinition(FilesPlugin::PERMISSIONS_PROPERTYNAME, false, true, false),
- new SearchPropertyDefinition(FilesPlugin::GETETAG_PROPERTYNAME, false, true, false),
- new SearchPropertyDefinition(FilesPlugin::OWNER_DISPLAY_NAME_PROPERTYNAME, false, true, false),
- new SearchPropertyDefinition(FilesPlugin::DATA_FINGERPRINT_PROPERTYNAME, false, true, false),
- new SearchPropertyDefinition(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_BOOLEAN),
- new SearchPropertyDefinition(FilesPlugin::FILEID_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
+ new SearchPropertyDefinition('{DAV:}resourcetype', true, false, false),
+ new SearchPropertyDefinition('{DAV:}getcontentlength', true, false, false),
+ new SearchPropertyDefinition(FilesPlugin::CHECKSUMS_PROPERTYNAME, true, false, false),
+ new SearchPropertyDefinition(FilesPlugin::PERMISSIONS_PROPERTYNAME, true, false, false),
+ new SearchPropertyDefinition(FilesPlugin::GETETAG_PROPERTYNAME, true, false, false),
+ new SearchPropertyDefinition(FilesPlugin::OWNER_DISPLAY_NAME_PROPERTYNAME, true, false, false),
+ new SearchPropertyDefinition(FilesPlugin::DATA_FINGERPRINT_PROPERTYNAME, true, false, false),
+ new SearchPropertyDefinition(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, true, false, false, SearchPropertyDefinition::DATATYPE_BOOLEAN),
+ new SearchPropertyDefinition(FilesPlugin::FILEID_PROPERTYNAME, true, false, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
];
+
+ return array_merge($props, $this->getPropertyDefinitionsForMetadata());
+ }
+
+
+ private function getPropertyDefinitionsForMetadata(): array {
+ $metadataProps = [];
+ $metadata = $this->filesMetadataManager->getKnownMetadata();
+ $indexes = $metadata->getIndexes();
+ foreach ($metadata->getKeys() as $key) {
+ $isIndex = in_array($key, $indexes);
+ $type = match ($metadata->getType($key)) {
+ IMetadataValueWrapper::TYPE_INT => SearchPropertyDefinition::DATATYPE_INTEGER,
+ IMetadataValueWrapper::TYPE_FLOAT => SearchPropertyDefinition::DATATYPE_DECIMAL,
+ IMetadataValueWrapper::TYPE_BOOL => SearchPropertyDefinition::DATATYPE_BOOLEAN,
+ default => SearchPropertyDefinition::DATATYPE_STRING
+ };
+ $metadataProps[] = new SearchPropertyDefinition(
+ FilesPlugin::FILE_METADATA_PREFIX . $key,
+ true,
+ $isIndex,
+ $isIndex,
+ $type
+ );
+ }
+
+ return $metadataProps;
}
/**
- * @param Query $search
- * @return SearchResult[]
+ * @param INode[] $nodes
+ * @param string[] $requestProperties
*/
- public function search(Query $search) {
- if (count($search->from) !== 1) {
- throw new \InvalidArgumentException('Searching more than one folder is not supported');
- }
- $query = $this->transformQuery($search);
- $scope = $search->from[0];
- if ($scope->path === null) {
+ public function preloadPropertyFor(array $nodes, array $requestProperties): void {
+ $this->server->emit('preloadProperties', [$nodes, $requestProperties]);
+ }
+
+ private function getFolderForPath(?string $path = null): Folder {
+ if ($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);
+
+ $node = $this->tree->getNodeForPath($path);
+
if (!$node instanceof Directory) {
throw new \InvalidArgumentException('Search is only supported on directories');
}
$fileInfo = $node->getFileInfo();
- $folder = $this->rootFolder->get($fileInfo->getPath());
- /** @var Folder $folder $results */
- $results = $folder->search($query);
+
+ /** @var Folder */
+ return $this->rootFolder->get($fileInfo->getPath());
+ }
+
+ /**
+ * @param Query $search
+ * @return SearchResult[]
+ */
+ public function search(Query $search): array {
+ switch (count($search->from)) {
+ case 0:
+ throw new \InvalidArgumentException('You need to specify a scope for the search.');
+ break;
+ case 1:
+ $scope = $search->from[0];
+ $folder = $this->getFolderForPath($scope->path);
+ $query = $this->transformQuery($search);
+ $results = $folder->search($query);
+ break;
+ default:
+ $scopes = [];
+ foreach ($search->from as $scope) {
+ $folder = $this->getFolderForPath($scope->path);
+ $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 . '/%',
+ ''
+ ),
+ ]
+ );
+ }
+
+ $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) {
if ($node instanceof Folder) {
- $davNode = new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager);
+ $davNode = new Directory($this->view, $node, $this->tree, $this->shareManager);
} else {
- $davNode = new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager);
+ $davNode = new File($this->view, $node, $this->shareManager);
}
$path = $this->getHrefForNode($node);
$this->tree->cacheNode($davNode, $path);
@@ -270,13 +318,21 @@ class FileSearchBackend implements ISearchBackend {
/**
* @param Query $query
+ *
* @return ISearchQuery
*/
- private function transformQuery(Query $query): ISearchQuery {
- // TODO offset
+ private function transformQuery(Query $query, ?SearchBinaryOperator $scopeOperators = null): 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)) {
+ return new SearchOrder($direction, substr($order->property->name, strlen(FilesPlugin::FILE_METADATA_PREFIX)), IMetadataQuery::EXTRA);
+ } else {
+ return new SearchOrder($direction, $this->mapPropertyNameToColumn($order->property));
+ }
+ }, $query->orderBy);
+
$limit = $query->limit;
- $orders = array_map([$this, 'mapSearchOrder'], $query->orderBy);
- $offset = 0;
+ $offset = $limit->firstResult;
$limitHome = false;
$ownerProp = $this->extractWhereValue($query->where, FilesPlugin::OWNER_ID_PROPERTYNAME, Operator::OPERATION_EQUAL);
@@ -286,11 +342,23 @@ class FileSearchBackend implements ISearchBackend {
} else {
throw new \InvalidArgumentException("Invalid search value for '{http://owncloud.org/ns}owner-id', only the current user id is allowed");
}
- $offset = $limit->firstResult;
+ }
+
+ $operatorCount = $this->countSearchOperators($query->where);
+ if ($operatorCount > self::OPERATOR_LIMIT) {
+ throw new \InvalidArgumentException('Invalid search query, maximum operator limit of ' . self::OPERATOR_LIMIT . ' exceeded, got ' . $operatorCount . ' operators');
+ }
+
+ /** @var SearchBinaryOperator|SearchComparison */
+ $queryOperators = $this->transformSearchOperation($query->where);
+ if ($scopeOperators === null) {
+ $operators = $queryOperators;
+ } else {
+ $operators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [$queryOperators, $scopeOperators]);
}
return new SearchQuery(
- $this->transformSearchOperation($query->where),
+ $operators,
(int)$limit->maxResults,
$offset,
$orders,
@@ -299,12 +367,24 @@ class FileSearchBackend implements ISearchBackend {
);
}
- /**
- * @param Order $order
- * @return ISearchOrder
- */
- private function mapSearchOrder(Order $order) {
- return new SearchOrder($order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING, $this->mapPropertyNameToColumn($order->property));
+ private function countSearchOperators(Operator $operator): int {
+ switch ($operator->type) {
+ case Operator::OPERATION_AND:
+ case Operator::OPERATION_OR:
+ case Operator::OPERATION_NOT:
+ /** @var Operator[] $arguments */
+ $arguments = $operator->arguments;
+ return array_sum(array_map([$this, 'countSearchOperators'], $arguments));
+ case Operator::OPERATION_EQUAL:
+ case Operator::OPERATION_GREATER_OR_EQUAL_THAN:
+ case Operator::OPERATION_GREATER_THAN:
+ case Operator::OPERATION_LESS_OR_EQUAL_THAN:
+ case Operator::OPERATION_LESS_THAN:
+ case Operator::OPERATION_IS_LIKE:
+ case Operator::OPERATION_IS_COLLECTION:
+ default:
+ return 1;
+ }
}
/**
@@ -312,7 +392,7 @@ class FileSearchBackend implements ISearchBackend {
* @return ISearchOperator
*/
private function transformSearchOperation(Operator $operator) {
- list(, $trimmedType) = explode('}', $operator->type);
+ [, $trimmedType] = explode('}', $operator->type);
switch ($operator->type) {
case Operator::OPERATION_AND:
case Operator::OPERATION_OR:
@@ -328,13 +408,37 @@ class FileSearchBackend implements ISearchBackend {
if (count($operator->arguments) !== 2) {
throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation');
}
+ if (!($operator->arguments[1] instanceof Literal)) {
+ throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal');
+ }
+ $value = $operator->arguments[1]->value;
+ // no break
+ case Operator::OPERATION_IS_DEFINED:
if (!($operator->arguments[0] instanceof SearchPropertyDefinition)) {
throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property');
}
- if (!($operator->arguments[1] instanceof Literal)) {
- throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal');
+ $property = $operator->arguments[0];
+
+ if (str_starts_with($property->name, FilesPlugin::FILE_METADATA_PREFIX)) {
+ $field = substr($property->name, strlen(FilesPlugin::FILE_METADATA_PREFIX));
+ $extra = IMetadataQuery::EXTRA;
+ } else {
+ $field = $this->mapPropertyNameToColumn($property);
+ }
+
+ try {
+ $castedValue = $this->castValue($property, $value ?? '');
+ } catch (\Error $e) {
+ throw new \InvalidArgumentException('Invalid property value for ' . $property->name, previous: $e);
}
- return new SearchComparison($trimmedType, $this->mapPropertyNameToColumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value));
+
+ return new SearchComparison(
+ $trimmedType,
+ $field,
+ $castedValue,
+ $extra ?? ''
+ );
+
case Operator::OPERATION_IS_COLLECTION:
return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE);
default:
@@ -368,6 +472,10 @@ class FileSearchBackend implements ISearchBackend {
}
private function castValue(SearchPropertyDefinition $property, $value) {
+ if ($value === '') {
+ return '';
+ }
+
switch ($property->dataType) {
case SearchPropertyDefinition::DATATYPE_BOOLEAN:
return $value === 'yes';
@@ -379,7 +487,7 @@ class FileSearchBackend implements ISearchBackend {
if (is_numeric($value)) {
return max(0, 0 + $value);
}
- $date = \DateTime::createFromFormat(\DateTime::ATOM, $value);
+ $date = \DateTime::createFromFormat(\DateTimeInterface::ATOM, (string)$value);
return ($date instanceof \DateTime && $date->getTimestamp() !== false) ? $date->getTimestamp() : 0;
default:
return $value;