Skip to content
Prev Previous commit
Next Next commit
unify handling of Folder::search methods into "new" query objects
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and backportbot[bot] committed Mar 23, 2021
commit d2245734d4a9427c4f03f3568b2361728e3c42fd
106 changes: 61 additions & 45 deletions lib/private/Files/Node/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,7 +42,11 @@
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 {
/**
Expand Down Expand Up @@ -96,8 +102,8 @@ public function isSubNode($node) {
/**
* 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);
Expand Down Expand Up @@ -200,6 +206,19 @@ public function newFile($path, $content = null) {
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
*
Expand All @@ -208,40 +227,10 @@ public function newFile($path, $content = null) {
*/
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');
}
Expand All @@ -259,7 +248,7 @@ private function searchCommon($method, $args) {

$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'];
Expand All @@ -277,7 +266,7 @@ private function searchCommon($method, $args) {
$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'];
Expand All @@ -294,6 +283,33 @@ private function searchCommon($method, $args) {
}, $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[]
Expand All @@ -320,7 +336,7 @@ public function getById($id) {

if (count($mountsContainingFile) === 0) {
if ($user === $this->getAppDataDirectoryName()) {
return $this->getByIdInRootMount((int) $id);
return $this->getByIdInRootMount((int)$id);
}
return [];
}
Expand Down Expand Up @@ -383,11 +399,11 @@ protected function getByIdInRootMount(int $id): array {

return [$this->root->createNode(
$absolutePath, new \OC\Files\FileInfo(
$absolutePath,
$mount->getStorage(),
$cacheEntry->getPath(),
$cacheEntry,
$mount
$absolutePath,
$mount->getStorage(),
$cacheEntry->getPath(),
$cacheEntry,
$mount
))];
}

Expand Down Expand Up @@ -518,10 +534,10 @@ private function recentSearch($limit, $offset, $folderMimetype, $filters) {
$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')
Expand Down