123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace OC\Files\Cache;
- use OC\Files\Cache\Wrapper\CacheJail;
- use OC\Files\Search\QueryOptimizer\QueryOptimizer;
- use OC\Files\Search\SearchBinaryOperator;
- use OC\SystemConfig;
- use OCP\DB\QueryBuilder\IQueryBuilder;
- use OCP\Files\Cache\ICache;
- use OCP\Files\Cache\ICacheEntry;
- use OCP\Files\IMimeTypeLoader;
- use OCP\Files\IRootFolder;
- use OCP\Files\Mount\IMountPoint;
- use OCP\Files\Search\ISearchBinaryOperator;
- use OCP\Files\Search\ISearchQuery;
- use OCP\FilesMetadata\IFilesMetadataManager;
- use OCP\FilesMetadata\IMetadataQuery;
- use OCP\IDBConnection;
- use OCP\IGroupManager;
- use OCP\IUser;
- use Psr\Log\LoggerInterface;
- class QuerySearchHelper {
- public function __construct(
- private IMimeTypeLoader $mimetypeLoader,
- private IDBConnection $connection,
- private SystemConfig $systemConfig,
- private LoggerInterface $logger,
- private SearchBuilder $searchBuilder,
- private QueryOptimizer $queryOptimizer,
- private IGroupManager $groupManager,
- private IFilesMetadataManager $filesMetadataManager,
- ) {
- }
- protected function getQueryBuilder() {
- return new CacheQueryBuilder(
- $this->connection->getQueryBuilder(),
- $this->filesMetadataManager,
- );
- }
-
- protected function applySearchConstraints(
- CacheQueryBuilder $query,
- ISearchQuery $searchQuery,
- array $caches,
- ?IMetadataQuery $metadataQuery = null
- ): void {
- $storageFilters = array_values(array_map(function (ICache $cache) {
- return $cache->getQueryFilterForStorage();
- }, $caches));
- $storageFilter = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $storageFilters);
- $filter = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [$searchQuery->getSearchOperation(), $storageFilter]);
- $this->queryOptimizer->processOperator($filter);
- $searchExpr = $this->searchBuilder->searchOperatorToDBExpr($query, $filter, $metadataQuery);
- if ($searchExpr) {
- $query->andWhere($searchExpr);
- }
- $this->searchBuilder->addSearchOrdersToQuery($query, $searchQuery->getOrder(), $metadataQuery);
- if ($searchQuery->getLimit()) {
- $query->setMaxResults($searchQuery->getLimit());
- }
- if ($searchQuery->getOffset()) {
- $query->setFirstResult($searchQuery->getOffset());
- }
- }
-
- public function findUsedTagsInCaches(ISearchQuery $searchQuery, array $caches): array {
- $query = $this->getQueryBuilder();
- $query->selectTagUsage();
- $this->applySearchConstraints($query, $searchQuery, $caches);
- $result = $query->execute();
- $tags = $result->fetchAll();
- $result->closeCursor();
- return $tags;
- }
- protected function equipQueryForSystemTags(CacheQueryBuilder $query, IUser $user): void {
- $query->leftJoin('file', 'systemtag_object_mapping', 'systemtagmap', $query->expr()->andX(
- $query->expr()->eq('file.fileid', $query->expr()->castColumn('systemtagmap.objectid', IQueryBuilder::PARAM_INT)),
- $query->expr()->eq('systemtagmap.objecttype', $query->createNamedParameter('files'))
- ));
- $on = $query->expr()->andX($query->expr()->eq('systemtag.id', 'systemtagmap.systemtagid'));
- if (!$this->groupManager->isAdmin($user->getUID())) {
- $on->add($query->expr()->eq('systemtag.visibility', $query->createNamedParameter(true)));
- }
- $query->leftJoin('systemtagmap', 'systemtag', 'systemtag', $on);
- }
- protected function equipQueryForDavTags(CacheQueryBuilder $query, IUser $user): void {
- $query
- ->leftJoin('file', 'vcategory_to_object', 'tagmap', $query->expr()->eq('file.fileid', 'tagmap.objid'))
- ->leftJoin('tagmap', 'vcategory', 'tag', $query->expr()->andX(
- $query->expr()->eq('tagmap.categoryid', 'tag.id'),
- $query->expr()->eq('tag.type', $query->createNamedParameter('files')),
- $query->expr()->eq('tag.uid', $query->createNamedParameter($user->getUID()))
- ));
- }
- protected function equipQueryForShares(CacheQueryBuilder $query): void {
- $query->join('file', 'share', 's', $query->expr()->eq('file.fileid', 's.file_source'));
- }
-
- public function searchInCaches(ISearchQuery $searchQuery, array $caches): array {
-
-
-
-
-
-
-
-
-
-
-
-
-
- $builder = $this->getQueryBuilder();
- $query = $builder->selectFileCache('file', false);
- $requestedFields = $this->searchBuilder->extractRequestedFields($searchQuery->getSearchOperation());
- if (in_array('systemtag', $requestedFields)) {
- $this->equipQueryForSystemTags($query, $this->requireUser($searchQuery));
- }
- if (in_array('tagname', $requestedFields) || in_array('favorite', $requestedFields)) {
- $this->equipQueryForDavTags($query, $this->requireUser($searchQuery));
- }
- if (in_array('owner', $requestedFields) || in_array('share_with', $requestedFields) || in_array('share_type', $requestedFields)) {
- $this->equipQueryForShares($query);
- }
- $metadataQuery = $query->selectMetadata();
- $this->applySearchConstraints($query, $searchQuery, $caches, $metadataQuery);
- $result = $query->execute();
- $files = $result->fetchAll();
- $rawEntries = array_map(function (array $data) use ($metadataQuery) {
- $data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
- return Cache::cacheEntryFromData($data, $this->mimetypeLoader);
- }, $files);
- $result->closeCursor();
-
-
- $results = array_fill_keys(array_keys($caches), []);
- foreach ($rawEntries as $rawEntry) {
- foreach ($caches as $cacheKey => $cache) {
- $entry = $cache->getCacheEntryFromSearchResult($rawEntry);
- if ($entry) {
- $results[$cacheKey][] = $entry;
- }
- }
- }
- return $results;
- }
- protected function requireUser(ISearchQuery $searchQuery): IUser {
- $user = $searchQuery->getUser();
- if ($user === null) {
- throw new \InvalidArgumentException("This search operation requires the user to be set in the query");
- }
- return $user;
- }
-
- public function getCachesAndMountPointsForSearch(IRootFolder $root, string $path, bool $limitToHome = false): array {
- $rootLength = strlen($path);
- $mount = $root->getMount($path);
- $storage = $mount->getStorage();
- if ($storage === null) {
- return [];
- }
- $internalPath = $mount->getInternalPath($path);
- if ($internalPath !== '') {
-
-
- $caches = ['' => new CacheJail($storage->getCache(''), $internalPath)];
- } else {
-
- $caches = ['' => $storage->getCache('')];
- }
-
- $mountByMountPoint = ['' => $mount];
- if (!$limitToHome) {
- $mounts = $root->getMountsIn($path);
- foreach ($mounts as $mount) {
- $storage = $mount->getStorage();
- if ($storage) {
- $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
- $caches[$relativeMountPoint] = $storage->getCache('');
- $mountByMountPoint[$relativeMountPoint] = $mount;
- }
- }
- }
- return [$caches, $mountByMountPoint];
- }
- }
|