unify handling of Folder::search methods into "new" query objects

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2021-03-18 16:35:41 +01:00 committed by backportbot[bot]
parent 680e21b7c5
commit d2245734d4
1 changed files with 61 additions and 45 deletions

View File

@ -32,6 +32,8 @@
namespace OC\Files\Node; namespace OC\Files\Node;
use OC\DB\QueryBuilder\Literal; use OC\DB\QueryBuilder\Literal;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
use OC\Files\Storage\Wrapper\Jail; use OC\Files\Storage\Wrapper\Jail;
use OCA\Files_Sharing\SharedStorage; use OCA\Files_Sharing\SharedStorage;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
@ -40,7 +42,11 @@ use OCP\Files\FileInfo;
use OCP\Files\Mount\IMountPoint; use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchOperator;
use OCP\Files\Search\ISearchQuery; use OCP\Files\Search\ISearchQuery;
use OCP\IUserManager;
use OCP\IUserSession;
class Folder extends Node implements \OCP\Files\Folder { class Folder extends Node implements \OCP\Files\Folder {
/** /**
@ -96,8 +102,8 @@ class Folder extends Node implements \OCP\Files\Folder {
/** /**
* get the content of this directory * get the content of this directory
* *
* @throws \OCP\Files\NotFoundException
* @return Node[] * @return Node[]
* @throws \OCP\Files\NotFoundException
*/ */
public function getDirectoryListing() { public function getDirectoryListing() {
$folderContent = $this->view->getDirectoryContent($this->path); $folderContent = $this->view->getDirectoryContent($this->path);
@ -200,6 +206,19 @@ class Folder extends Node implements \OCP\Files\Folder {
throw new NotPermittedException('No create permission for path'); throw new NotPermittedException('No create permission for path');
} }
private function queryFromOperator(ISearchOperator $operator, string $uid = null): ISearchQuery {
if (!$uid) {
/** @var IUserSession $session */
$session = \OC::$server->query(IUserSession::class);
$user = $session->getUser();
} else {
/** @var IUserManager $userManager */
$userManager = \OC::$server->query(IUserManager::class);
$user = $userManager->get($uid);
}
return new SearchQuery($operator, 0, 0, [], $user);
}
/** /**
* search for files with the name matching $query * search for files with the name matching $query
* *
@ -208,40 +227,10 @@ class Folder extends Node implements \OCP\Files\Folder {
*/ */
public function search($query) { public function search($query) {
if (is_string($query)) { if (is_string($query)) {
return $this->searchCommon('search', ['%' . $query . '%']); $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%'));
} else {
return $this->searchCommon('searchQuery', [$query]);
} }
}
/** $limitToHome = $query->limitToHome();
* search for files by mimetype
*
* @param string $mimetype
* @return Node[]
*/
public function searchByMime($mimetype) {
return $this->searchCommon('searchByMime', [$mimetype]);
}
/**
* search for files by tag
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return Node[]
*/
public function searchByTag($tag, $userId) {
return $this->searchCommon('searchByTag', [$tag, $userId]);
}
/**
* @param string $method cache method
* @param array $args call args
* @return \OC\Files\Node\Node[]
*/
private function searchCommon($method, $args) {
$limitToHome = ($method === 'searchQuery')? $args[0]->limitToHome(): false;
if ($limitToHome && count(explode('/', $this->path)) !== 3) { if ($limitToHome && count(explode('/', $this->path)) !== 3) {
throw new \InvalidArgumentException('searching by owner is only allows on the users home folder'); throw new \InvalidArgumentException('searching by owner is only allows on the users home folder');
} }
@ -259,7 +248,7 @@ class Folder extends Node implements \OCP\Files\Folder {
$cache = $storage->getCache(''); $cache = $storage->getCache('');
$results = call_user_func_array([$cache, $method], $args); $results = $cache->searchQuery($query);
foreach ($results as $result) { foreach ($results as $result) {
if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) { if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
$result['internalPath'] = $result['path']; $result['internalPath'] = $result['path'];
@ -277,7 +266,7 @@ class Folder extends Node implements \OCP\Files\Folder {
$cache = $storage->getCache(''); $cache = $storage->getCache('');
$relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
$results = call_user_func_array([$cache, $method], $args); $results = $cache->searchQuery($query);
foreach ($results as $result) { foreach ($results as $result) {
$result['internalPath'] = $result['path']; $result['internalPath'] = $result['path'];
$result['path'] = $relativeMountPoint . $result['path']; $result['path'] = $relativeMountPoint . $result['path'];
@ -294,6 +283,33 @@ class Folder extends Node implements \OCP\Files\Folder {
}, $files); }, $files);
} }
/**
* search for files by mimetype
*
* @param string $mimetype
* @return Node[]
*/
public function searchByMime($mimetype) {
if (strpos($mimetype, '/') === false) {
$query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%'));
} else {
$query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype));
}
return $this->search($query);
}
/**
* search for files by tag
*
* @param string|int $tag name or tag id
* @param string $userId owner of the tags
* @return Node[]
*/
public function searchByTag($tag, $userId) {
$query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', $tag), $userId);
return $this->search($query);
}
/** /**
* @param int $id * @param int $id
* @return \OC\Files\Node\Node[] * @return \OC\Files\Node\Node[]
@ -320,7 +336,7 @@ class Folder extends Node implements \OCP\Files\Folder {
if (count($mountsContainingFile) === 0) { if (count($mountsContainingFile) === 0) {
if ($user === $this->getAppDataDirectoryName()) { if ($user === $this->getAppDataDirectoryName()) {
return $this->getByIdInRootMount((int) $id); return $this->getByIdInRootMount((int)$id);
} }
return []; return [];
} }
@ -383,11 +399,11 @@ class Folder extends Node implements \OCP\Files\Folder {
return [$this->root->createNode( return [$this->root->createNode(
$absolutePath, new \OC\Files\FileInfo( $absolutePath, new \OC\Files\FileInfo(
$absolutePath, $absolutePath,
$mount->getStorage(), $mount->getStorage(),
$cacheEntry->getPath(), $cacheEntry->getPath(),
$cacheEntry, $cacheEntry,
$mount $mount
))]; ))];
} }
@ -518,10 +534,10 @@ class Folder extends Node implements \OCP\Files\Folder {
$query->andWhere($storageFilters); $query->andWhere($storageFilters);
$query->andWhere($builder->expr()->orX( $query->andWhere($builder->expr()->orX(
// handle non empty folders separate // handle non empty folders separate
$builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
$builder->expr()->eq('f.size', new Literal(0)) $builder->expr()->eq('f.size', new Literal(0))
)) ))
->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_versions/%'))) ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_versions/%')))
->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_trashbin/%'))) ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_trashbin/%')))
->orderBy('f.mtime', 'DESC') ->orderBy('f.mtime', 'DESC')