summaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2017-03-13 00:55:12 -0600
committerGitHub <noreply@github.com>2017-03-13 00:55:12 -0600
commitf7cef9f7024a0f94a36746daee2fe4ef6335bac6 (patch)
tree31d483fc81b5bc47fa70164a6395d8e6e6843ce5 /lib/private
parent0dd0242d2f82410da66b6d4c6d455a1702779df1 (diff)
parente61606a767cd6e2c28e3897e7e913e39371078e5 (diff)
downloadnextcloud-server-f7cef9f7024a0f94a36746daee2fe4ef6335bac6.tar.gz
nextcloud-server-f7cef9f7024a0f94a36746daee2fe4ef6335bac6.zip
Merge pull request #3772 from nextcloud/favorite-search
Allow searching for favorites
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/Files/Cache/Cache.php21
-rw-r--r--lib/private/Files/Cache/QuerySearchHelper.php32
-rw-r--r--lib/private/Files/Search/SearchQuery.php14
3 files changed, 60 insertions, 7 deletions
diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php
index b0527d801d6..c3ac0f8444f 100644
--- a/lib/private/Files/Cache/Cache.php
+++ b/lib/private/Files/Cache/Cache.php
@@ -645,9 +645,22 @@ class Cache implements ICache {
$builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum'])
- ->from('filecache')
- ->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId())))
- ->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation()));
+ ->from('filecache', 'file');
+
+ $query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId())));
+
+ if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) {
+ $query
+ ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid'))
+ ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX(
+ $builder->expr()->eq('tagmap.type', 'tag.type'),
+ $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())));
+ }
+
+ $query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation()));
if ($searchQuery->getLimit()) {
$query->setMaxResults($searchQuery->getLimit());
@@ -660,7 +673,7 @@ class Cache implements ICache {
return $this->searchResultToCacheEntries($result);
}
- /**
+ /**
* Search for files by tag of a given users.
*
* Note that every user can tag files differently.
diff --git a/lib/private/Files/Cache/QuerySearchHelper.php b/lib/private/Files/Cache/QuerySearchHelper.php
index 931f258ec5b..7d8098f0efa 100644
--- a/lib/private/Files/Cache/QuerySearchHelper.php
+++ b/lib/private/Files/Cache/QuerySearchHelper.php
@@ -49,6 +49,8 @@ class QuerySearchHelper {
ISearchComparison::COMPARE_LESS_THAN_EQUAL => 'lt'
];
+ const TAG_FAVORITE = '_$!<Favorite>!$_';
+
/** @var IMimeTypeLoader */
private $mimetypeLoader;
@@ -61,6 +63,23 @@ class QuerySearchHelper {
$this->mimetypeLoader = $mimetypeLoader;
}
+ /**
+ * Whether or not the tag tables should be joined to complete the search
+ *
+ * @param ISearchOperator $operator
+ * @return boolean
+ */
+ public function shouldJoinTags(ISearchOperator $operator) {
+ if ($operator instanceof ISearchBinaryOperator) {
+ return array_reduce($operator->getArguments(), function ($shouldJoin, ISearchOperator $operator) {
+ return $shouldJoin || $this->shouldJoinTags($operator);
+ }, false);
+ } else if ($operator instanceof ISearchComparison) {
+ return $operator->getField() === 'tagname' || $operator->getField() === 'favorite';
+ }
+ return false;
+ }
+
public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) {
$expr = $builder->expr();
if ($operator instanceof ISearchBinaryOperator) {
@@ -116,6 +135,11 @@ class QuerySearchHelper {
throw new \InvalidArgumentException('Unsupported query value for mimetype: ' . $value . ', only values in the format "mime/type" or "mime/%" are supported');
}
}
+ } else if ($field === 'favorite') {
+ $field = 'tag.category';
+ $value = self::TAG_FAVORITE;
+ } else if ($field === 'tagname') {
+ $field = 'tag.category';
}
return [$field, $value, $type];
}
@@ -125,13 +149,17 @@ class QuerySearchHelper {
'mimetype' => 'string',
'mtime' => 'integer',
'name' => 'string',
- 'size' => 'integer'
+ 'size' => 'integer',
+ 'tagname' => 'string',
+ 'favorite' => 'boolean'
];
$comparisons = [
'mimetype' => ['eq', 'like'],
'mtime' => ['eq', 'gt', 'lt', 'gte', 'lte'],
'name' => ['eq', 'like'],
- 'size' => ['eq', 'gt', 'lt', 'gte', 'lte']
+ 'size' => ['eq', 'gt', 'lt', 'gte', 'lte'],
+ 'tagname' => ['eq', 'like'],
+ 'favorite' => ['eq'],
];
if (!isset($types[$operator->getField()])) {
diff --git a/lib/private/Files/Search/SearchQuery.php b/lib/private/Files/Search/SearchQuery.php
index 8a0478ae98e..c1da5220516 100644
--- a/lib/private/Files/Search/SearchQuery.php
+++ b/lib/private/Files/Search/SearchQuery.php
@@ -24,6 +24,7 @@ namespace OC\Files\Search;
use OCP\Files\Search\ISearchOperator;
use OCP\Files\Search\ISearchOrder;
use OCP\Files\Search\ISearchQuery;
+use OCP\IUser;
class SearchQuery implements ISearchQuery {
/** @var ISearchOperator */
@@ -34,6 +35,8 @@ class SearchQuery implements ISearchQuery {
private $offset;
/** @var ISearchOrder[] */
private $order;
+ /** @var IUser */
+ private $user;
/**
* SearchQuery constructor.
@@ -42,12 +45,14 @@ class SearchQuery implements ISearchQuery {
* @param int $limit
* @param int $offset
* @param array $order
+ * @param IUser $user
*/
- public function __construct(ISearchOperator $searchOperation, $limit, $offset, array $order) {
+ public function __construct(ISearchOperator $searchOperation, $limit, $offset, array $order, IUser $user) {
$this->searchOperation = $searchOperation;
$this->limit = $limit;
$this->offset = $offset;
$this->order = $order;
+ $this->user = $user;
}
/**
@@ -77,4 +82,11 @@ class SearchQuery implements ISearchQuery {
public function getOrder() {
return $this->order;
}
+
+ /**
+ * @return IUser
+ */
+ public function getUser() {
+ return $this->user;
+ }
}