unify handling of Folder::search methods into "new" query objects
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
bc5e51af7e
commit
549db744dc
|
@ -31,6 +31,9 @@
|
|||
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;
|
||||
use OCP\Files\Config\ICachedMountInfo;
|
||||
|
@ -38,7 +41,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 {
|
||||
/**
|
||||
|
@ -94,8 +101,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);
|
||||
|
@ -198,6 +205,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
|
||||
*
|
||||
|
@ -206,40 +226,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');
|
||||
}
|
||||
|
@ -257,7 +247,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'];
|
||||
|
@ -275,7 +265,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'];
|
||||
|
@ -292,6 +282,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[]
|
||||
|
@ -318,7 +335,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 [];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue