unify handling of Folder::search methods into "new" query objects
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
680e21b7c5
commit
d2245734d4
|
@ -32,6 +32,8 @@
|
|||
namespace OC\Files\Node;
|
||||
|
||||
use OC\DB\QueryBuilder\Literal;
|
||||
use OC\Files\Search\SearchComparison;
|
||||
use OC\Files\Search\SearchQuery;
|
||||
use OC\Files\Storage\Wrapper\Jail;
|
||||
use OCA\Files_Sharing\SharedStorage;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
|
@ -40,7 +42,11 @@ use OCP\Files\FileInfo;
|
|||
use OCP\Files\Mount\IMountPoint;
|
||||
use OCP\Files\NotFoundException;
|
||||
use OCP\Files\NotPermittedException;
|
||||
use OCP\Files\Search\ISearchComparison;
|
||||
use OCP\Files\Search\ISearchOperator;
|
||||
use OCP\Files\Search\ISearchQuery;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IUserSession;
|
||||
|
||||
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
|
||||
*
|
||||
* @throws \OCP\Files\NotFoundException
|
||||
* @return Node[]
|
||||
* @throws \OCP\Files\NotFoundException
|
||||
*/
|
||||
public function getDirectoryListing() {
|
||||
$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');
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
|
@ -208,40 +227,10 @@ class Folder extends Node implements \OCP\Files\Folder {
|
|||
*/
|
||||
public function search($query) {
|
||||
if (is_string($query)) {
|
||||
return $this->searchCommon('search', ['%' . $query . '%']);
|
||||
} else {
|
||||
return $this->searchCommon('searchQuery', [$query]);
|
||||
$query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
$limitToHome = $query->limitToHome();
|
||||
if ($limitToHome && count(explode('/', $this->path)) !== 3) {
|
||||
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('');
|
||||
|
||||
$results = call_user_func_array([$cache, $method], $args);
|
||||
$results = $cache->searchQuery($query);
|
||||
foreach ($results as $result) {
|
||||
if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
|
||||
$result['internalPath'] = $result['path'];
|
||||
|
@ -277,7 +266,7 @@ class Folder extends Node implements \OCP\Files\Folder {
|
|||
$cache = $storage->getCache('');
|
||||
|
||||
$relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
|
||||
$results = call_user_func_array([$cache, $method], $args);
|
||||
$results = $cache->searchQuery($query);
|
||||
foreach ($results as $result) {
|
||||
$result['internalPath'] = $result['path'];
|
||||
$result['path'] = $relativeMountPoint . $result['path'];
|
||||
|
@ -294,6 +283,33 @@ class Folder extends Node implements \OCP\Files\Folder {
|
|||
}, $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
|
||||
* @return \OC\Files\Node\Node[]
|
||||
|
@ -320,7 +336,7 @@ class Folder extends Node implements \OCP\Files\Folder {
|
|||
|
||||
if (count($mountsContainingFile) === 0) {
|
||||
if ($user === $this->getAppDataDirectoryName()) {
|
||||
return $this->getByIdInRootMount((int) $id);
|
||||
return $this->getByIdInRootMount((int)$id);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
@ -383,11 +399,11 @@ class Folder extends Node implements \OCP\Files\Folder {
|
|||
|
||||
return [$this->root->createNode(
|
||||
$absolutePath, new \OC\Files\FileInfo(
|
||||
$absolutePath,
|
||||
$mount->getStorage(),
|
||||
$cacheEntry->getPath(),
|
||||
$cacheEntry,
|
||||
$mount
|
||||
$absolutePath,
|
||||
$mount->getStorage(),
|
||||
$cacheEntry->getPath(),
|
||||
$cacheEntry,
|
||||
$mount
|
||||
))];
|
||||
}
|
||||
|
||||
|
@ -518,10 +534,10 @@ class Folder extends Node implements \OCP\Files\Folder {
|
|||
$query->andWhere($storageFilters);
|
||||
|
||||
$query->andWhere($builder->expr()->orX(
|
||||
// handle non empty folders separate
|
||||
$builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
|
||||
$builder->expr()->eq('f.size', new Literal(0))
|
||||
))
|
||||
// handle non empty folders separate
|
||||
$builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
|
||||
$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_trashbin/%')))
|
||||
->orderBy('f.mtime', 'DESC')
|
||||
|
|
Loading…
Reference in New Issue