Merge pull request #26257 from nextcloud/backport/26198/stable21

[stable21] Handle limit offset and sorting in files search
This commit is contained in:
Morris Jobke 2021-03-24 17:32:36 +01:00 committed by GitHub
commit ffb7c51ca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 469 additions and 246 deletions

View File

@ -29,10 +29,15 @@ declare(strict_types=1);
namespace OCA\Files\Search; namespace OCA\Files\Search;
use OC\Search\Provider\File; use OC\Files\Search\SearchComparison;
use OC\Search\Result\File as FileResult; use OC\Files\Search\SearchOrder;
use OC\Files\Search\SearchQuery;
use OCP\Files\FileInfo;
use OCP\Files\IMimeTypeDetector; use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Node;
use OCP\Files\Search\ISearchOrder;
use OCP\IL10N; use OCP\IL10N;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUser; use OCP\IUser;
@ -43,9 +48,6 @@ use OCP\Search\SearchResultEntry;
class FilesSearchProvider implements IProvider { class FilesSearchProvider implements IProvider {
/** @var File */
private $fileSearch;
/** @var IL10N */ /** @var IL10N */
private $l10n; private $l10n;
@ -58,13 +60,13 @@ class FilesSearchProvider implements IProvider {
/** @var IRootFolder */ /** @var IRootFolder */
private $rootFolder; private $rootFolder;
public function __construct(File $fileSearch, public function __construct(
IL10N $l10n, IL10N $l10n,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
IMimeTypeDetector $mimeTypeDetector, IMimeTypeDetector $mimeTypeDetector,
IRootFolder $rootFolder) { IRootFolder $rootFolder
) {
$this->l10n = $l10n; $this->l10n = $l10n;
$this->fileSearch = $fileSearch;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->mimeTypeDetector = $mimeTypeDetector; $this->mimeTypeDetector = $mimeTypeDetector;
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
@ -99,29 +101,42 @@ class FilesSearchProvider implements IProvider {
* @inheritDoc * @inheritDoc
*/ */
public function search(IUser $user, ISearchQuery $query): SearchResult { public function search(IUser $user, ISearchQuery $query): SearchResult {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
// Make sure we setup the users filesystem $fileQuery = new SearchQuery(
$this->rootFolder->getUserFolder($user->getUID()); new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query->getTerm() . '%'),
$query->getLimit(),
(int)$query->getCursor(),
$query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [
new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
] : [],
$user
);
return SearchResult::paginated( return SearchResult::paginated(
$this->l10n->t('Files'), $this->l10n->t('Files'),
array_map(function (FileResult $result) { array_map(function (Node $result) use ($userFolder) {
// Generate thumbnail url // Generate thumbnail url
$thumbnailUrl = $result->has_preview $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()]);
? $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->id]) $path = $userFolder->getRelativePath($result->getPath());
: ''; $link = $this->urlGenerator->linkToRoute(
'files.view.index',
[
'dir' => dirname($path),
'scrollto' => $result->getName(),
]
);
$searchResultEntry = new SearchResultEntry( $searchResultEntry = new SearchResultEntry(
$thumbnailUrl, $thumbnailUrl,
$result->name, $result->getName(),
$this->formatSubline($result), $this->formatSubline($path),
$this->urlGenerator->getAbsoluteURL($result->link), $this->urlGenerator->getAbsoluteURL($link),
$result->type === 'folder' ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->mime_type) $result->getMimetype() === FileInfo::MIMETYPE_FOLDER ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
); );
$searchResultEntry->addAttribute('fileId', (string)$result->id); $searchResultEntry->addAttribute('fileId', (string)$result->getId());
$searchResultEntry->addAttribute('path', $result->path); $searchResultEntry->addAttribute('path', $path);
return $searchResultEntry; return $searchResultEntry;
}, $this->fileSearch->search($query->getTerm(), $query->getLimit(), (int)$query->getCursor())), }, $userFolder->search($fileQuery)),
$query->getCursor() + $query->getLimit() $query->getCursor() + $query->getLimit()
); );
} }
@ -129,16 +144,16 @@ class FilesSearchProvider implements IProvider {
/** /**
* Format subline for files * Format subline for files
* *
* @param FileResult $result * @param string $path
* @return string * @return string
*/ */
private function formatSubline($result): string { private function formatSubline(string $path): string {
// Do not show the location if the file is in root // Do not show the location if the file is in root
if ($result->path === '/' . $result->name) { if (strrpos($path, '/') > 0) {
$path = ltrim(dirname($path), '/');
return $this->l10n->t('in %s', [$path]);
} else {
return ''; return '';
} }
$path = ltrim(dirname($result->path), '/');
return $this->l10n->t('in %s', [$path]);
} }
} }

View File

@ -55,6 +55,7 @@ class SearchController extends Controller {
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired
*/ */
public function search(string $query, array $inApps = [], int $page = 1, int $size = 30): JSONResponse { public function search(string $query, array $inApps = [], int $page = 1, int $size = 30): JSONResponse {
$results = $this->searcher->searchPaged($query, $inApps, $page, $size); $results = $this->searcher->searchPaged($query, $inApps, $page, $size);

View File

@ -198,10 +198,10 @@ class Cache implements ICache {
} }
$data['permissions'] = (int)$data['permissions']; $data['permissions'] = (int)$data['permissions'];
if (isset($data['creation_time'])) { if (isset($data['creation_time'])) {
$data['creation_time'] = (int) $data['creation_time']; $data['creation_time'] = (int)$data['creation_time'];
} }
if (isset($data['upload_time'])) { if (isset($data['upload_time'])) {
$data['upload_time'] = (int) $data['upload_time']; $data['upload_time'] = (int)$data['upload_time'];
} }
return new CacheEntry($data); return new CacheEntry($data);
} }
@ -845,6 +845,10 @@ class Cache implements ICache {
$query->whereStorageId(); $query->whereStorageId();
if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) { if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) {
$user = $searchQuery->getUser();
if ($user === null) {
throw new \InvalidArgumentException("Searching by tag requires the user to be set in the query");
}
$query $query
->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid'))
->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX(
@ -852,7 +856,7 @@ class Cache implements ICache {
$builder->expr()->eq('tagmap.categoryid', 'tag.id') $builder->expr()->eq('tagmap.categoryid', 'tag.id')
)) ))
->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files'))) ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files')))
->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID()))); ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($user->getUID())));
} }
$searchExpr = $this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation()); $searchExpr = $this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation());
@ -1035,7 +1039,7 @@ class Cache implements ICache {
return null; return null;
} }
return (string) $path; return (string)$path;
} }
/** /**

View File

@ -32,15 +32,24 @@
namespace OC\Files\Node; namespace OC\Files\Node;
use OC\DB\QueryBuilder\Literal; use OC\DB\QueryBuilder\Literal;
use OC\Files\Search\SearchBinaryOperator;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
use OC\Files\Storage\Wrapper\Jail; use OC\Files\Storage\Wrapper\Jail;
use OC\Files\Storage\Storage;
use OCA\Files_Sharing\SharedStorage; use OCA\Files_Sharing\SharedStorage;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Config\ICachedMountInfo; use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\FileInfo; 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\ISearchBinaryOperator;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchOperator;
use OCP\Files\Search\ISearchQuery; use OCP\Files\Search\ISearchQuery;
use OCP\IUserManager;
class Folder extends Node implements \OCP\Files\Folder { class Folder extends Node implements \OCP\Files\Folder {
/** /**
@ -96,8 +105,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 +209,17 @@ 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 === null) {
$user = null;
} 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,10 +228,106 @@ 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]);
} }
// Limit+offset for queries with ordering
//
// Because we currently can't do ordering between the results from different storages in sql
// The only way to do ordering is requesting the $limit number of entries from all storages
// sorting them and returning the first $limit entries.
//
// For offset we have the same problem, we don't know how many entries from each storage should be skipped
// by a given $offset, so instead we query $offset + $limit from each storage and return entries $offset..($offset+$limit)
// after merging and sorting them.
//
// This is suboptimal but because limit and offset tend to be fairly small in real world use cases it should
// still be significantly better than disabling paging altogether
$limitToHome = $query->limitToHome();
if ($limitToHome && count(explode('/', $this->path)) !== 3) {
throw new \InvalidArgumentException('searching by owner is only allows on the users home folder');
}
$rootLength = strlen($this->path);
$mount = $this->root->getMount($this->path);
$storage = $mount->getStorage();
$internalPath = $mount->getInternalPath($this->path);
$internalPath = rtrim($internalPath, '/');
if ($internalPath !== '') {
$internalPath = $internalPath . '/';
}
$subQueryLimit = $query->getLimit() > 0 ? $query->getLimit() + $query->getOffset() : 0;
$rootQuery = new SearchQuery(
new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [
new SearchComparison(ISearchComparison::COMPARE_LIKE, 'path', $internalPath . '%'),
$query->getSearchOperation(),
]
),
$subQueryLimit,
0,
$query->getOrder(),
$query->getUser()
);
$files = [];
$cache = $storage->getCache('');
$results = $cache->searchQuery($rootQuery);
foreach ($results as $result) {
$files[] = $this->cacheEntryToFileInfo($mount, '', $internalPath, $result);
}
if (!$limitToHome) {
$mounts = $this->root->getMountsIn($this->path);
foreach ($mounts as $mount) {
$subQuery = new SearchQuery(
$query->getSearchOperation(),
$subQueryLimit,
0,
$query->getOrder(),
$query->getUser()
);
$storage = $mount->getStorage();
if ($storage) {
$cache = $storage->getCache('');
$relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
$results = $cache->searchQuery($subQuery);
foreach ($results as $result) {
$files[] = $this->cacheEntryToFileInfo($mount, $relativeMountPoint, '', $result);
}
}
}
}
$order = $query->getOrder();
if ($order) {
usort($files, function (FileInfo $a,FileInfo $b) use ($order) {
foreach ($order as $orderField) {
$cmp = $orderField->sortFileInfo($a, $b);
if ($cmp !== 0) {
return $cmp;
}
}
return 0;
});
}
$files = array_values(array_slice($files, $query->getOffset(), $query->getLimit() > 0 ? $query->getLimit() : null));
return array_map(function (FileInfo $file) {
return $this->createNode($file->getPath(), $file);
}, $files);
}
private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, string $trimRoot, ICacheEntry $cacheEntry): FileInfo {
$trimLength = strlen($trimRoot);
$cacheEntry['internalPath'] = $cacheEntry['path'];
$cacheEntry['path'] = $appendRoot . substr($cacheEntry['path'], $trimLength);
return new \OC\Files\FileInfo($this->path . '/' . $cacheEntry['path'], $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount);
} }
/** /**
@ -221,7 +337,12 @@ class Folder extends Node implements \OCP\Files\Folder {
* @return Node[] * @return Node[]
*/ */
public function searchByMime($mimetype) { public function searchByMime($mimetype) {
return $this->searchCommon('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);
} }
/** /**
@ -232,66 +353,8 @@ class Folder extends Node implements \OCP\Files\Folder {
* @return Node[] * @return Node[]
*/ */
public function searchByTag($tag, $userId) { public function searchByTag($tag, $userId) {
return $this->searchCommon('searchByTag', [$tag, $userId]); $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', $tag), $userId);
} return $this->search($query);
/**
* @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) {
throw new \InvalidArgumentException('searching by owner is only allows on the users home folder');
}
$files = [];
$rootLength = strlen($this->path);
$mount = $this->root->getMount($this->path);
$storage = $mount->getStorage();
$internalPath = $mount->getInternalPath($this->path);
$internalPath = rtrim($internalPath, '/');
if ($internalPath !== '') {
$internalPath = $internalPath . '/';
}
$internalRootLength = strlen($internalPath);
$cache = $storage->getCache('');
$results = call_user_func_array([$cache, $method], $args);
foreach ($results as $result) {
if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
$result['internalPath'] = $result['path'];
$result['path'] = substr($result['path'], $internalRootLength);
$result['storage'] = $storage;
$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
}
}
if (!$limitToHome) {
$mounts = $this->root->getMountsIn($this->path);
foreach ($mounts as $mount) {
$storage = $mount->getStorage();
if ($storage) {
$cache = $storage->getCache('');
$relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
$results = call_user_func_array([$cache, $method], $args);
foreach ($results as $result) {
$result['internalPath'] = $result['path'];
$result['path'] = $relativeMountPoint . $result['path'];
$result['storage'] = $storage;
$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage,
$result['internalPath'], $result, $mount);
}
}
}
}
return array_map(function (FileInfo $file) {
return $this->createNode($file->getPath(), $file);
}, $files);
} }
/** /**
@ -320,7 +383,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 +446,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 +581,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')

View File

@ -23,6 +23,7 @@
namespace OC\Files\Search; namespace OC\Files\Search;
use OCP\Files\FileInfo;
use OCP\Files\Search\ISearchOrder; use OCP\Files\Search\ISearchOrder;
class SearchOrder implements ISearchOrder { class SearchOrder implements ISearchOrder {
@ -55,4 +56,28 @@ class SearchOrder implements ISearchOrder {
public function getField() { public function getField() {
return $this->field; return $this->field;
} }
public function sortFileInfo(FileInfo $a, FileInfo $b): int {
$cmp = $this->sortFileInfoNoDirection($a, $b);
return $cmp * ($this->direction === ISearchOrder::DIRECTION_ASCENDING ? 1 : -1);
}
private function sortFileInfoNoDirection(FileInfo $a, FileInfo $b): int {
switch ($this->field) {
case 'name':
return $a->getName() <=> $b->getName();
case 'mimetype':
return $a->getMimetype() <=> $b->getMimetype();
case 'mtime':
return $a->getMtime() <=> $b->getMtime();
case 'size':
return $a->getSize() <=> $b->getSize();
case 'fileid':
return $a->getId() <=> $b->getId();
case 'permissions':
return $a->getPermissions() <=> $b->getPermissions();
default:
return 0;
}
}
} }

View File

@ -37,7 +37,7 @@ class SearchQuery implements ISearchQuery {
private $offset; private $offset;
/** @var ISearchOrder[] */ /** @var ISearchOrder[] */
private $order; private $order;
/** @var IUser */ /** @var ?IUser */
private $user; private $user;
private $limitToHome; private $limitToHome;
@ -48,7 +48,7 @@ class SearchQuery implements ISearchQuery {
* @param int $limit * @param int $limit
* @param int $offset * @param int $offset
* @param array $order * @param array $order
* @param IUser $user * @param ?IUser $user
* @param bool $limitToHome * @param bool $limitToHome
*/ */
public function __construct( public function __construct(
@ -56,7 +56,7 @@ class SearchQuery implements ISearchQuery {
int $limit, int $limit,
int $offset, int $offset,
array $order, array $order,
IUser $user, ?IUser $user = null,
bool $limitToHome = false bool $limitToHome = false
) { ) {
$this->searchOperation = $searchOperation; $this->searchOperation = $searchOperation;
@ -96,7 +96,7 @@ class SearchQuery implements ISearchQuery {
} }
/** /**
* @return IUser * @return ?IUser
*/ */
public function getUser() { public function getUser() {
return $this->user; return $this->user;

View File

@ -29,7 +29,14 @@
namespace OC\Search\Provider; namespace OC\Search\Provider;
use OC\Files\Filesystem; use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchOrder;
use OC\Files\Search\SearchQuery;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchOrder;
use OCP\IUserSession;
use OCP\Search\PagedProvider; use OCP\Search\PagedProvider;
/** /**
@ -48,35 +55,38 @@ class File extends PagedProvider {
* @deprecated 20.0.0 * @deprecated 20.0.0
*/ */
public function search($query, int $limit = null, int $offset = null) { public function search($query, int $limit = null, int $offset = null) {
if ($offset === null) { /** @var IRootFolder $rootFolder */
$offset = 0; $rootFolder = \OC::$server->query(IRootFolder::class);
/** @var IUserSession $userSession */
$userSession = \OC::$server->query(IUserSession::class);
$user = $userSession->getUser();
if (!$user) {
return [];
} }
\OC_Util::setupFS(); $userFolder = $rootFolder->getUserFolder($user->getUID());
$files = Filesystem::search($query); $fileQuery = new SearchQuery(
new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%'),
(int)$limit,
(int)$offset,
[
new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
],
$user
);
$files = $userFolder->search($fileQuery);
$results = []; $results = [];
if ($limit !== null) {
$files = array_slice($files, $offset, $offset + $limit);
}
// edit results // edit results
foreach ($files as $fileData) { foreach ($files as $fileData) {
// skip versions
if (strpos($fileData['path'], '_versions') === 0) {
continue;
}
// skip top-level folder
if ($fileData['name'] === 'files' && $fileData['parent'] === -1) {
continue;
}
// create audio result // create audio result
if ($fileData['mimepart'] === 'audio') { if ($fileData->getMimePart() === 'audio') {
$result = new \OC\Search\Result\Audio($fileData); $result = new \OC\Search\Result\Audio($fileData);
} }
// create image result // create image result
elseif ($fileData['mimepart'] === 'image') { elseif ($fileData->getMimePart() === 'image') {
$result = new \OC\Search\Result\Image($fileData); $result = new \OC\Search\Result\Image($fileData);
} }
// create folder result // create folder result
elseif ($fileData['mimetype'] === 'httpd/unix-directory') { elseif ($fileData->getMimetype() === FileInfo::MIMETYPE_FOLDER) {
$result = new \OC\Search\Result\Folder($fileData); $result = new \OC\Search\Result\Folder($fileData);
} }
// or create file result // or create file result

View File

@ -97,14 +97,13 @@ class File extends \OCP\Search\Result {
public function __construct(FileInfo $data) { public function __construct(FileInfo $data) {
$path = $this->getRelativePath($data->getPath()); $path = $this->getRelativePath($data->getPath());
$info = pathinfo($path);
$this->id = $data->getId(); $this->id = $data->getId();
$this->name = $info['basename']; $this->name = $data->getName();
$this->link = \OC::$server->getURLGenerator()->linkToRoute( $this->link = \OC::$server->getURLGenerator()->linkToRoute(
'files.view.index', 'files.view.index',
[ [
'dir' => $info['dirname'], 'dir' => dirname($path),
'scrollto' => $info['basename'], 'scrollto' => $data->getName(),
] ]
); );
$this->permissions = $data->getPermissions(); $this->permissions = $data->getPermissions();

View File

@ -24,6 +24,8 @@
namespace OCP\Files\Search; namespace OCP\Files\Search;
use OCP\Files\FileInfo;
/** /**
* @since 12.0.0 * @since 12.0.0
*/ */
@ -46,4 +48,14 @@ interface ISearchOrder {
* @since 12.0.0 * @since 12.0.0
*/ */
public function getField(); public function getField();
/**
* Apply the sorting on 2 FileInfo objects
*
* @param FileInfo $a
* @param FileInfo $b
* @return int -1 if $a < $b, 0 if $a = $b, 1 if $a > $b (for ascending, reverse for descending)
* @since 22.0.0
*/
public function sortFileInfo(FileInfo $a, FileInfo $b): int;
} }

View File

@ -62,7 +62,7 @@ interface ISearchQuery {
/** /**
* The user that issued the search * The user that issued the search
* *
* @return IUser * @return ?IUser
* @since 12.0.0 * @since 12.0.0
*/ */
public function getUser(); public function getUser();

View File

@ -14,13 +14,20 @@ use OC\Files\Config\CachedMountInfo;
use OC\Files\FileInfo; use OC\Files\FileInfo;
use OC\Files\Mount\Manager; use OC\Files\Mount\Manager;
use OC\Files\Mount\MountPoint; use OC\Files\Mount\MountPoint;
use OC\Files\Node\Folder;
use OC\Files\Node\Node; use OC\Files\Node\Node;
use OC\Files\Node\Root; use OC\Files\Node\Root;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchOrder;
use OC\Files\Search\SearchQuery;
use OC\Files\Storage\Temporary; use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\Jail; use OC\Files\Storage\Wrapper\Jail;
use OC\Files\View; use OC\Files\View;
use OCP\Files\Mount\IMountPoint; use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\Search\ISearchComparison;
use OCP\Files\Search\ISearchOrder;
use OCP\Files\Search\ISearchQuery;
use OCP\Files\Storage; use OCP\Files\Storage;
/** /**
@ -32,7 +39,7 @@ use OCP\Files\Storage;
*/ */
class FolderTest extends NodeTest { class FolderTest extends NodeTest {
protected function createTestNode($root, $view, $path) { protected function createTestNode($root, $view, $path) {
return new \OC\Files\Node\Folder($root, $view, $path); return new Folder($root, $view, $path);
} }
protected function getNodeClass() { protected function getNodeClass() {
@ -65,10 +72,10 @@ class FolderTest extends NodeTest {
->with('/bar/foo') ->with('/bar/foo')
->willReturn([ ->willReturn([
new FileInfo('/bar/foo/asd', null, 'foo/asd', ['fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'], null), new FileInfo('/bar/foo/asd', null, 'foo/asd', ['fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'], null),
new FileInfo('/bar/foo/qwerty', null, 'foo/qwerty', ['fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory'], null) new FileInfo('/bar/foo/qwerty', null, 'foo/qwerty', ['fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory'], null),
]); ]);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$children = $node->getDirectoryListing(); $children = $node->getDirectoryListing();
$this->assertEquals(2, count($children)); $this->assertEquals(2, count($children));
$this->assertInstanceOf('\OC\Files\Node\File', $children[0]); $this->assertInstanceOf('\OC\Files\Node\File', $children[0]);
@ -96,7 +103,7 @@ class FolderTest extends NodeTest {
->method('get') ->method('get')
->with('/bar/foo/asd'); ->with('/bar/foo/asd');
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$node->get('asd'); $node->get('asd');
} }
@ -113,14 +120,14 @@ class FolderTest extends NodeTest {
->method('getUser') ->method('getUser')
->willReturn($this->user); ->willReturn($this->user);
$child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd'); $child = new Folder($root, $view, '/bar/foo/asd');
$root->expects($this->once()) $root->expects($this->once())
->method('get') ->method('get')
->with('/bar/foo/asd') ->with('/bar/foo/asd')
->willReturn($child); ->willReturn($child);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$this->assertTrue($node->nodeExists('asd')); $this->assertTrue($node->nodeExists('asd'));
} }
@ -142,7 +149,7 @@ class FolderTest extends NodeTest {
->with('/bar/foo/asd') ->with('/bar/foo/asd')
->will($this->throwException(new NotFoundException())); ->will($this->throwException(new NotFoundException()));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$this->assertFalse($node->nodeExists('asd')); $this->assertFalse($node->nodeExists('asd'));
} }
@ -169,8 +176,8 @@ class FolderTest extends NodeTest {
->with('/bar/foo/asd') ->with('/bar/foo/asd')
->willReturn(true); ->willReturn(true);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd'); $child = new Folder($root, $view, '/bar/foo/asd');
$result = $node->newFolder('asd'); $result = $node->newFolder('asd');
$this->assertEquals($child, $result); $this->assertEquals($child, $result);
} }
@ -196,7 +203,7 @@ class FolderTest extends NodeTest {
->with('/bar/foo') ->with('/bar/foo')
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$node->newFolder('asd'); $node->newFolder('asd');
} }
@ -223,7 +230,7 @@ class FolderTest extends NodeTest {
->with('/bar/foo/asd') ->with('/bar/foo/asd')
->willReturn(true); ->willReturn(true);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd'); $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd');
$result = $node->newFile('asd'); $result = $node->newFile('asd');
$this->assertEquals($child, $result); $this->assertEquals($child, $result);
@ -250,7 +257,7 @@ class FolderTest extends NodeTest {
->with('/bar/foo') ->with('/bar/foo')
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$node->newFile('asd'); $node->newFile('asd');
} }
@ -272,7 +279,7 @@ class FolderTest extends NodeTest {
->with('/bar/foo') ->with('/bar/foo')
->willReturn(100); ->willReturn(100);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$this->assertEquals(100, $node->getFreeSpace()); $this->assertEquals(100, $node->getFreeSpace());
} }
@ -285,43 +292,35 @@ class FolderTest extends NodeTest {
$root = $this->getMockBuilder(Root::class) $root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager]) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock(); ->getMock();
$root->expects($this->any()) $root->method('getUser')
->method('getUser')
->willReturn($this->user); ->willReturn($this->user);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('getId')->willReturn(''); $storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$storage->expects($this->once()) $storage->method('getCache')
->method('getCache')
->willReturn($cache); ->willReturn($cache);
$mount = $this->createMock(IMountPoint::class); $mount = $this->createMock(IMountPoint::class);
$mount->expects($this->once()) $mount->method('getStorage')
->method('getStorage')
->willReturn($storage); ->willReturn($storage);
$mount->expects($this->once()) $mount->method('getInternalPath')
->method('getInternalPath')
->willReturn('foo'); ->willReturn('foo');
$cache->expects($this->once()) $cache->method('searchQuery')
->method('search')
->with('%qw%')
->willReturn([ ->willReturn([
['fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'] new CacheEntry(['fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
]); ]);
$root->expects($this->once()) $root->method('getMountsIn')
->method('getMountsIn')
->with('/bar/foo') ->with('/bar/foo')
->willReturn([]); ->willReturn([]);
$root->expects($this->once()) $root->method('getMount')
->method('getMount')
->with('/bar/foo') ->with('/bar/foo')
->willReturn($mount); ->willReturn($mount);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$result = $node->search('qw'); $result = $node->search('qw');
$this->assertEquals(1, count($result)); $this->assertEquals(1, count($result));
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
@ -346,32 +345,24 @@ class FolderTest extends NodeTest {
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$mount = $this->createMock(IMountPoint::class); $mount = $this->createMock(IMountPoint::class);
$mount->expects($this->once()) $mount->method('getStorage')
->method('getStorage')
->willReturn($storage); ->willReturn($storage);
$mount->expects($this->once()) $mount->method('getInternalPath')
->method('getInternalPath')
->willReturn('files'); ->willReturn('files');
$storage->expects($this->once()) $storage->method('getCache')
->method('getCache')
->willReturn($cache); ->willReturn($cache);
$cache->expects($this->once()) $cache->method('searchQuery')
->method('search')
->with('%qw%')
->willReturn([ ->willReturn([
['fileid' => 3, 'path' => 'files/foo', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'], new CacheEntry(['fileid' => 3, 'path' => 'files/foo', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
['fileid' => 3, 'path' => 'files_trashbin/foo2.d12345', 'name' => 'foo2.d12345', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'],
]); ]);
$root->expects($this->once()) $root->method('getMountsIn')
->method('getMountsIn')
->with('') ->with('')
->willReturn([]); ->willReturn([]);
$root->expects($this->once()) $root->method('getMount')
->method('getMount')
->with('') ->with('')
->willReturn($mount); ->willReturn($mount);
@ -389,43 +380,35 @@ class FolderTest extends NodeTest {
$root = $this->getMockBuilder(Root::class) $root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager]) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock(); ->getMock();
$root->expects($this->any()) $root->method('getUser')
->method('getUser')
->willReturn($this->user); ->willReturn($this->user);
$storage = $this->createMock(Storage::class); $storage = $this->createMock(Storage::class);
$storage->method('getId')->willReturn(''); $storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$mount = $this->createMock(IMountPoint::class); $mount = $this->createMock(IMountPoint::class);
$mount->expects($this->once()) $mount->method('getStorage')
->method('getStorage')
->willReturn($storage); ->willReturn($storage);
$mount->expects($this->once()) $mount->method('getInternalPath')
->method('getInternalPath')
->willReturn(''); ->willReturn('');
$storage->expects($this->once()) $storage->method('getCache')
->method('getCache')
->willReturn($cache); ->willReturn($cache);
$cache->expects($this->once()) $cache->method('searchQuery')
->method('search')
->with('%qw%')
->willReturn([ ->willReturn([
['fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'] new CacheEntry(['fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
]); ]);
$root->expects($this->once()) $root->method('getMountsIn')
->method('getMountsIn')
->with('/bar') ->with('/bar')
->willReturn([]); ->willReturn([]);
$root->expects($this->once()) $root->method('getMount')
->method('getMount')
->with('/bar') ->with('/bar')
->willReturn($mount); ->willReturn($mount);
$node = new \OC\Files\Node\Folder($root, $view, '/bar'); $node = new Folder($root, $view, '/bar');
$result = $node->search('qw'); $result = $node->search('qw');
$this->assertEquals(1, count($result)); $this->assertEquals(1, count($result));
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
@ -451,62 +434,50 @@ class FolderTest extends NodeTest {
$subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock(); $subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
$mount = $this->createMock(IMountPoint::class); $mount = $this->createMock(IMountPoint::class);
$mount->expects($this->once()) $mount->method('getStorage')
->method('getStorage')
->willReturn($storage); ->willReturn($storage);
$mount->expects($this->once()) $mount->method('getInternalPath')
->method('getInternalPath')
->willReturn('foo'); ->willReturn('foo');
$subMount->expects($this->once()) $subMount->method('getStorage')
->method('getStorage')
->willReturn($subStorage); ->willReturn($subStorage);
$subMount->expects($this->once()) $subMount->method('getMountPoint')
->method('getMountPoint')
->willReturn('/bar/foo/bar/'); ->willReturn('/bar/foo/bar/');
$storage->expects($this->once()) $storage->method('getCache')
->method('getCache')
->willReturn($cache); ->willReturn($cache);
$subStorage->expects($this->once()) $subStorage->method('getCache')
->method('getCache')
->willReturn($subCache); ->willReturn($subCache);
$cache->expects($this->once()) $cache->method('searchQuery')
->method('search')
->with('%qw%')
->willReturn([ ->willReturn([
['fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'] new CacheEntry(['fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
]); ]);
$subCache->expects($this->once()) $subCache->method('searchQuery')
->method('search')
->with('%qw%')
->willReturn([ ->willReturn([
['fileid' => 4, 'path' => 'asd/qweasd', 'name' => 'qweasd', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'] new CacheEntry(['fileid' => 4, 'path' => 'asd/qweasd', 'name' => 'qweasd', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']),
]); ]);
$root->expects($this->once()) $root->method('getMountsIn')
->method('getMountsIn')
->with('/bar/foo') ->with('/bar/foo')
->willReturn([$subMount]); ->willReturn([$subMount]);
$root->expects($this->once()) $root->method('getMount')
->method('getMount')
->with('/bar/foo') ->with('/bar/foo')
->willReturn($mount); ->willReturn($mount);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$result = $node->search('qw'); $result = $node->search('qw');
$this->assertEquals(2, count($result)); $this->assertEquals(2, count($result));
} }
public function testIsSubNode() { public function testIsSubNode() {
$file = new Node(null, null, '/foo/bar'); $file = new Node(null, null, '/foo/bar');
$folder = new \OC\Files\Node\Folder(null, null, '/foo'); $folder = new Folder(null, null, '/foo');
$this->assertTrue($folder->isSubNode($file)); $this->assertTrue($folder->isSubNode($file));
$this->assertFalse($folder->isSubNode($folder)); $this->assertFalse($folder->isSubNode($folder));
@ -562,7 +533,7 @@ class FolderTest extends NodeTest {
->with('/bar/foo') ->with('/bar/foo')
->willReturn($mount); ->willReturn($mount);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$result = $node->getById(1); $result = $node->getById(1);
$this->assertEquals(1, count($result)); $this->assertEquals(1, count($result));
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
@ -611,7 +582,7 @@ class FolderTest extends NodeTest {
->with('/bar') ->with('/bar')
->willReturn($mount); ->willReturn($mount);
$node = new \OC\Files\Node\Folder($root, $view, '/bar'); $node = new Folder($root, $view, '/bar');
$result = $node->getById(1); $result = $node->getById(1);
$this->assertEquals(1, count($result)); $this->assertEquals(1, count($result));
$this->assertEquals('/bar', $result[0]->getPath()); $this->assertEquals('/bar', $result[0]->getPath());
@ -665,7 +636,7 @@ class FolderTest extends NodeTest {
->with('/bar/foo') ->with('/bar/foo')
->willReturn($mount); ->willReturn($mount);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$result = $node->getById(1); $result = $node->getById(1);
$this->assertEquals(0, count($result)); $this->assertEquals(0, count($result));
} }
@ -711,7 +682,7 @@ class FolderTest extends NodeTest {
'/bar/foo/asd/', '/bar/foo/asd/',
1, 1,
'' ''
) ),
]); ]);
$storage->expects($this->any()) $storage->expects($this->any())
@ -733,7 +704,7 @@ class FolderTest extends NodeTest {
->with('/bar/foo') ->with('/bar/foo')
->willReturn($mount1); ->willReturn($mount1);
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); $node = new Folder($root, $view, '/bar/foo');
$result = $node->getById(1); $result = $node->getById(1);
$this->assertEquals(2, count($result)); $this->assertEquals(2, count($result));
$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
@ -745,7 +716,7 @@ class FolderTest extends NodeTest {
// input, existing, expected // input, existing, expected
['foo', [], 'foo'], ['foo', [], 'foo'],
['foo', ['foo'], 'foo (2)'], ['foo', ['foo'], 'foo (2)'],
['foo', ['foo', 'foo (2)'], 'foo (3)'] ['foo', ['foo', 'foo (2)'], 'foo (3)'],
]; ];
} }
@ -775,7 +746,7 @@ class FolderTest extends NodeTest {
return false; return false;
}); });
$node = new \OC\Files\Node\Folder($root, $view, $folderPath); $node = new Folder($root, $view, $folderPath);
$this->assertEquals($expected, $node->getNonExistingName($name)); $this->assertEquals($expected, $node->getNonExistingName($name));
} }
@ -810,30 +781,30 @@ class FolderTest extends NodeTest {
'mtime' => $baseTime, 'mtime' => $baseTime,
'mimetype' => 'text/plain', 'mimetype' => 'text/plain',
'size' => 3, 'size' => 3,
'permissions' => \OCP\Constants::PERMISSION_ALL 'permissions' => \OCP\Constants::PERMISSION_ALL,
]); ]);
$id2 = $cache->put('bar/foo/old.txt', [ $id2 = $cache->put('bar/foo/old.txt', [
'storage_mtime' => $baseTime - 100, 'storage_mtime' => $baseTime - 100,
'mtime' => $baseTime - 100, 'mtime' => $baseTime - 100,
'mimetype' => 'text/plain', 'mimetype' => 'text/plain',
'size' => 3, 'size' => 3,
'permissions' => \OCP\Constants::PERMISSION_READ 'permissions' => \OCP\Constants::PERMISSION_READ,
]); ]);
$cache->put('bar/asd/outside.txt', [ $cache->put('bar/asd/outside.txt', [
'storage_mtime' => $baseTime, 'storage_mtime' => $baseTime,
'mtime' => $baseTime, 'mtime' => $baseTime,
'mimetype' => 'text/plain', 'mimetype' => 'text/plain',
'size' => 3 'size' => 3,
]); ]);
$id3 = $cache->put('bar/foo/older.txt', [ $id3 = $cache->put('bar/foo/older.txt', [
'storage_mtime' => $baseTime - 600, 'storage_mtime' => $baseTime - 600,
'mtime' => $baseTime - 600, 'mtime' => $baseTime - 600,
'mimetype' => 'text/plain', 'mimetype' => 'text/plain',
'size' => 3, 'size' => 3,
'permissions' => \OCP\Constants::PERMISSION_ALL 'permissions' => \OCP\Constants::PERMISSION_ALL,
]); ]);
$node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo); $node = new Folder($root, $view, $folderPath, $folderInfo);
$nodes = $node->getRecent(5); $nodes = $node->getRecent(5);
@ -874,7 +845,7 @@ class FolderTest extends NodeTest {
'mtime' => $baseTime, 'mtime' => $baseTime,
'mimetype' => \OCP\Files\FileInfo::MIMETYPE_FOLDER, 'mimetype' => \OCP\Files\FileInfo::MIMETYPE_FOLDER,
'size' => 3, 'size' => 3,
'permissions' => 0 'permissions' => 0,
]); ]);
$id2 = $cache->put('bar/foo/folder/bar.txt', [ $id2 = $cache->put('bar/foo/folder/bar.txt', [
'storage_mtime' => $baseTime, 'storage_mtime' => $baseTime,
@ -882,7 +853,7 @@ class FolderTest extends NodeTest {
'mimetype' => 'text/plain', 'mimetype' => 'text/plain',
'size' => 3, 'size' => 3,
'parent' => $id1, 'parent' => $id1,
'permissions' => \OCP\Constants::PERMISSION_ALL 'permissions' => \OCP\Constants::PERMISSION_ALL,
]); ]);
$id3 = $cache->put('bar/foo/folder/asd.txt', [ $id3 = $cache->put('bar/foo/folder/asd.txt', [
'storage_mtime' => $baseTime - 100, 'storage_mtime' => $baseTime - 100,
@ -890,10 +861,10 @@ class FolderTest extends NodeTest {
'mimetype' => 'text/plain', 'mimetype' => 'text/plain',
'size' => 3, 'size' => 3,
'parent' => $id1, 'parent' => $id1,
'permissions' => \OCP\Constants::PERMISSION_ALL 'permissions' => \OCP\Constants::PERMISSION_ALL,
]); ]);
$node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo); $node = new Folder($root, $view, $folderPath, $folderInfo);
$nodes = $node->getRecent(5); $nodes = $node->getRecent(5);
@ -925,7 +896,7 @@ class FolderTest extends NodeTest {
$storage = new Temporary(); $storage = new Temporary();
$jail = new Jail([ $jail = new Jail([
'storage' => $storage, 'storage' => $storage,
'root' => 'folder' 'root' => 'folder',
]); ]);
$mount = new MountPoint($jail, '/bar/foo'); $mount = new MountPoint($jail, '/bar/foo');
@ -940,16 +911,16 @@ class FolderTest extends NodeTest {
'mtime' => $baseTime, 'mtime' => $baseTime,
'mimetype' => 'text/plain', 'mimetype' => 'text/plain',
'size' => 3, 'size' => 3,
'permissions' => \OCP\Constants::PERMISSION_ALL 'permissions' => \OCP\Constants::PERMISSION_ALL,
]); ]);
$cache->put('outside.txt', [ $cache->put('outside.txt', [
'storage_mtime' => $baseTime - 100, 'storage_mtime' => $baseTime - 100,
'mtime' => $baseTime - 100, 'mtime' => $baseTime - 100,
'mimetype' => 'text/plain', 'mimetype' => 'text/plain',
'size' => 3 'size' => 3,
]); ]);
$node = new \OC\Files\Node\Folder($root, $view, $folderPath, $folderInfo); $node = new Folder($root, $view, $folderPath, $folderInfo);
$nodes = $node->getRecent(5); $nodes = $node->getRecent(5);
$ids = array_map(function (Node $node) { $ids = array_map(function (Node $node) {
@ -957,4 +928,127 @@ class FolderTest extends NodeTest {
}, $nodes); }, $nodes);
$this->assertEquals([$id1], $ids); $this->assertEquals([$id1], $ids);
} }
public function offsetLimitProvider() {
return [
[0, 10, [10, 11, 12, 13, 14, 15, 16, 17], []],
[0, 5, [10, 11, 12, 13, 14], []],
[0, 2, [10, 11], []],
[3, 2, [13, 14], []],
[3, 5, [13, 14, 15, 16, 17], []],
[5, 2, [15, 16], []],
[6, 2, [16, 17], []],
[7, 2, [17], []],
[10, 2, [], []],
[0, 5, [16, 10, 14, 11, 12], [new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'mtime')]],
[3, 2, [11, 12], [new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'mtime')]],
[0, 5, [14, 15, 16, 10, 11], [
new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'size'),
new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'mtime')
]],
];
}
/**
* @dataProvider offsetLimitProvider
* @param int $offset
* @param int $limit
* @param int[] $expectedIds
* @param ISearchOrder[] $ordering
* @throws NotFoundException
* @throws \OCP\Files\InvalidPathException
*/
public function testSearchSubStoragesLimitOffset(int $offset, int $limit, array $expectedIds, array $ordering) {
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
$view = $this->createMock(View::class);
$root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$root->expects($this->any())
->method('getUser')
->willReturn($this->user);
$storage = $this->createMock(Storage::class);
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$subCache1 = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$subStorage1 = $this->createMock(Storage::class);
$subMount1 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
$subCache2 = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$subStorage2 = $this->createMock(Storage::class);
$subMount2 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
$mount = $this->createMock(IMountPoint::class);
$mount->method('getStorage')
->willReturn($storage);
$mount->method('getInternalPath')
->willReturn('foo');
$subMount1->method('getStorage')
->willReturn($subStorage1);
$subMount1->method('getMountPoint')
->willReturn('/bar/foo/bar/');
$storage->method('getCache')
->willReturn($cache);
$subStorage1->method('getCache')
->willReturn($subCache1);
$subMount2->method('getStorage')
->willReturn($subStorage2);
$subMount2->method('getMountPoint')
->willReturn('/bar/foo/bar2/');
$subStorage2->method('getCache')
->willReturn($subCache2);
$cache->method('searchQuery')
->willReturnCallback(function (ISearchQuery $query) {
return array_slice([
new CacheEntry(['fileid' => 10, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 10, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 11, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 20, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 12, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 30, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 13, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 40, 'mimetype' => 'text/plain']),
], $query->getOffset(), $query->getOffset() + $query->getLimit());
});
$subCache1->method('searchQuery')
->willReturnCallback(function (ISearchQuery $query) {
return array_slice([
new CacheEntry(['fileid' => 14, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 300, 'mtime' => 15, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 15, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 300, 'mtime' => 50, 'mimetype' => 'text/plain']),
], $query->getOffset(), $query->getOffset() + $query->getLimit());
});
$subCache2->method('searchQuery')
->willReturnCallback(function (ISearchQuery $query) {
return array_slice([
new CacheEntry(['fileid' => 16, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 5, 'mimetype' => 'text/plain']),
new CacheEntry(['fileid' => 17, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 60, 'mimetype' => 'text/plain']),
], $query->getOffset(), $query->getOffset() + $query->getLimit());
});
$root->method('getMountsIn')
->with('/bar/foo')
->willReturn([$subMount1, $subMount2]);
$root->method('getMount')
->with('/bar/foo')
->willReturn($mount);
$node = new Folder($root, $view, '/bar/foo');
$comparison = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%foo%');
$query = new SearchQuery($comparison, $limit, $offset, $ordering);
$result = $node->search($query);
$ids = array_map(function (Node $info) {
return $info->getId();
}, $result);
$this->assertEquals($expectedIds, $ids);
}
} }