diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Files/Cache/Cache.php | 5 | ||||
-rw-r--r-- | lib/private/Files/Cache/QuerySearchHelper.php | 8 | ||||
-rw-r--r-- | lib/private/Files/Node/Folder.php | 38 | ||||
-rw-r--r-- | lib/private/Files/Search/SearchQuery.php | 16 | ||||
-rw-r--r-- | lib/public/Files/Search/ISearchQuery.php | 8 |
5 files changed, 56 insertions, 19 deletions
diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index a90617f5c53..842704d1471 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -793,7 +793,10 @@ class Cache implements ICache { ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID()))); } - $query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation())); + $searchExpr = $this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation()); + if ($searchExpr) { + $query->andWhere($searchExpr); + } $this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder()); diff --git a/lib/private/Files/Cache/QuerySearchHelper.php b/lib/private/Files/Cache/QuerySearchHelper.php index 2d9d8f374f7..47380b72ffa 100644 --- a/lib/private/Files/Cache/QuerySearchHelper.php +++ b/lib/private/Files/Cache/QuerySearchHelper.php @@ -88,14 +88,18 @@ class QuerySearchHelper { * @param ISearchOperator $operator */ public function searchOperatorArrayToDBExprArray(IQueryBuilder $builder, array $operators) { - return array_map(function ($operator) use ($builder) { + return array_filter(array_map(function ($operator) use ($builder) { return $this->searchOperatorToDBExpr($builder, $operator); - }, $operators); + }, $operators)); } public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) { $expr = $builder->expr(); if ($operator instanceof ISearchBinaryOperator) { + if (count($operator->getArguments()) === 0) { + return null; + } + switch ($operator->getType()) { case ISearchBinaryOperator::OPERATOR_NOT: $negativeOperator = $operator->getArguments()[0]; diff --git a/lib/private/Files/Node/Folder.php b/lib/private/Files/Node/Folder.php index 4a134cdcdbf..56ec39d86a8 100644 --- a/lib/private/Files/Node/Folder.php +++ b/lib/private/Files/Node/Folder.php @@ -34,7 +34,7 @@ use OCP\Files\FileInfo; use OCP\Files\Mount\IMountPoint; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; -use OCP\Files\Search\ISearchOperator; +use OCP\Files\Search\ISearchQuery; class Folder extends Node implements \OCP\Files\Folder { /** @@ -191,7 +191,7 @@ class Folder extends Node implements \OCP\Files\Folder { /** * search for files with the name matching $query * - * @param string|ISearchOperator $query + * @param string|ISearchQuery $query * @return \OC\Files\Node\Node[] */ public function search($query) { @@ -229,6 +229,11 @@ class Folder extends Node implements \OCP\Files\Folder { * @return \OC\Files\Node\Node[] */ private function searchCommon($method, $args) { + $limitToHome = ($method === 'searchQuery')? $args[0]->limitToHome(): false; + if ($limitToHome && count(explode('/', $this->path)) !== 3) { + throw new \InvalidArgumentException('searching by owner is only allows on the users home folder'); + } + $files = array(); $rootLength = strlen($this->path); $mount = $this->root->getMount($this->path); @@ -252,19 +257,22 @@ class Folder extends Node implements \OCP\Files\Folder { } } - $mounts = $this->root->getMountsIn($this->path); - foreach ($mounts as $mount) { - $storage = $mount->getStorage(); - if ($storage) { - $cache = $storage->getCache(''); - - $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); - $results = call_user_func_array(array($cache, $method), $args); - 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); + if (!$limitToHome) { + $mounts = $this->root->getMountsIn($this->path); + foreach ($mounts as $mount) { + $storage = $mount->getStorage(); + if ($storage) { + $cache = $storage->getCache(''); + + $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); + $results = call_user_func_array([$cache, $method], $args); + 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); + } } } } diff --git a/lib/private/Files/Search/SearchQuery.php b/lib/private/Files/Search/SearchQuery.php index a22db2f60b3..ddad1fb49de 100644 --- a/lib/private/Files/Search/SearchQuery.php +++ b/lib/private/Files/Search/SearchQuery.php @@ -39,6 +39,7 @@ class SearchQuery implements ISearchQuery { private $order; /** @var IUser */ private $user; + private $limitToHome; /** * SearchQuery constructor. @@ -48,13 +49,22 @@ class SearchQuery implements ISearchQuery { * @param int $offset * @param array $order * @param IUser $user + * @param bool $limitToHome */ - public function __construct(ISearchOperator $searchOperation, $limit, $offset, array $order, IUser $user) { + public function __construct( + ISearchOperator $searchOperation, + int $limit, + int $offset, + array $order, + IUser $user, + bool $limitToHome = false + ) { $this->searchOperation = $searchOperation; $this->limit = $limit; $this->offset = $offset; $this->order = $order; $this->user = $user; + $this->limitToHome = $limitToHome; } /** @@ -91,4 +101,8 @@ class SearchQuery implements ISearchQuery { public function getUser() { return $this->user; } + + public function limitToHome(): bool { + return $this->limitToHome; + } } diff --git a/lib/public/Files/Search/ISearchQuery.php b/lib/public/Files/Search/ISearchQuery.php index 0929a190489..35093e340a2 100644 --- a/lib/public/Files/Search/ISearchQuery.php +++ b/lib/public/Files/Search/ISearchQuery.php @@ -66,4 +66,12 @@ interface ISearchQuery { * @since 12.0.0 */ public function getUser(); + + /** + * Whether or not the search should be limited to the users home storage + * + * @return bool + * @since 18.0.0 + */ + public function limitToHome(): bool; } |