1
0

QuerySearchHelper.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Tobias Kaminsky <tobias@kaminsky.me>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Files\Cache;
  28. use OC\Files\Cache\Wrapper\CacheJail;
  29. use OC\Files\Search\QueryOptimizer\QueryOptimizer;
  30. use OC\Files\Search\SearchBinaryOperator;
  31. use OC\SystemConfig;
  32. use OCP\DB\QueryBuilder\IQueryBuilder;
  33. use OCP\Files\Cache\ICache;
  34. use OCP\Files\Cache\ICacheEntry;
  35. use OCP\Files\IMimeTypeLoader;
  36. use OCP\Files\IRootFolder;
  37. use OCP\Files\Mount\IMountPoint;
  38. use OCP\Files\Search\ISearchBinaryOperator;
  39. use OCP\Files\Search\ISearchQuery;
  40. use OCP\FilesMetadata\IFilesMetadataManager;
  41. use OCP\FilesMetadata\IMetadataQuery;
  42. use OCP\IDBConnection;
  43. use OCP\IGroupManager;
  44. use OCP\IUser;
  45. use Psr\Log\LoggerInterface;
  46. class QuerySearchHelper {
  47. public function __construct(
  48. private IMimeTypeLoader $mimetypeLoader,
  49. private IDBConnection $connection,
  50. private SystemConfig $systemConfig,
  51. private LoggerInterface $logger,
  52. private SearchBuilder $searchBuilder,
  53. private QueryOptimizer $queryOptimizer,
  54. private IGroupManager $groupManager,
  55. private IFilesMetadataManager $filesMetadataManager,
  56. ) {
  57. }
  58. protected function getQueryBuilder() {
  59. return new CacheQueryBuilder(
  60. $this->connection,
  61. $this->systemConfig,
  62. $this->logger,
  63. $this->filesMetadataManager,
  64. );
  65. }
  66. /**
  67. * @param CacheQueryBuilder $query
  68. * @param ISearchQuery $searchQuery
  69. * @param array $caches
  70. * @param IMetadataQuery|null $metadataQuery
  71. */
  72. protected function applySearchConstraints(
  73. CacheQueryBuilder $query,
  74. ISearchQuery $searchQuery,
  75. array $caches,
  76. ?IMetadataQuery $metadataQuery = null
  77. ): void {
  78. $storageFilters = array_values(array_map(function (ICache $cache) {
  79. return $cache->getQueryFilterForStorage();
  80. }, $caches));
  81. $storageFilter = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $storageFilters);
  82. $filter = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [$searchQuery->getSearchOperation(), $storageFilter]);
  83. $this->queryOptimizer->processOperator($filter);
  84. $searchExpr = $this->searchBuilder->searchOperatorToDBExpr($query, $filter, $metadataQuery);
  85. if ($searchExpr) {
  86. $query->andWhere($searchExpr);
  87. }
  88. $this->searchBuilder->addSearchOrdersToQuery($query, $searchQuery->getOrder(), $metadataQuery);
  89. if ($searchQuery->getLimit()) {
  90. $query->setMaxResults($searchQuery->getLimit());
  91. }
  92. if ($searchQuery->getOffset()) {
  93. $query->setFirstResult($searchQuery->getOffset());
  94. }
  95. }
  96. /**
  97. * @return array<array-key, array{id: int, name: string, visibility: int, editable: int, ref_file_id: int, number_files: int}>
  98. */
  99. public function findUsedTagsInCaches(ISearchQuery $searchQuery, array $caches): array {
  100. $query = $this->getQueryBuilder();
  101. $query->selectTagUsage();
  102. $this->applySearchConstraints($query, $searchQuery, $caches);
  103. $result = $query->execute();
  104. $tags = $result->fetchAll();
  105. $result->closeCursor();
  106. return $tags;
  107. }
  108. protected function equipQueryForSystemTags(CacheQueryBuilder $query, IUser $user): void {
  109. $query->leftJoin('file', 'systemtag_object_mapping', 'systemtagmap', $query->expr()->andX(
  110. $query->expr()->eq('file.fileid', $query->expr()->castColumn('systemtagmap.objectid', IQueryBuilder::PARAM_INT)),
  111. $query->expr()->eq('systemtagmap.objecttype', $query->createNamedParameter('files'))
  112. ));
  113. $on = $query->expr()->andX($query->expr()->eq('systemtag.id', 'systemtagmap.systemtagid'));
  114. if (!$this->groupManager->isAdmin($user->getUID())) {
  115. $on->add($query->expr()->eq('systemtag.visibility', $query->createNamedParameter(true)));
  116. }
  117. $query->leftJoin('systemtagmap', 'systemtag', 'systemtag', $on);
  118. }
  119. protected function equipQueryForDavTags(CacheQueryBuilder $query, IUser $user): void {
  120. $query
  121. ->leftJoin('file', 'vcategory_to_object', 'tagmap', $query->expr()->eq('file.fileid', 'tagmap.objid'))
  122. ->leftJoin('tagmap', 'vcategory', 'tag', $query->expr()->andX(
  123. $query->expr()->eq('tagmap.type', 'tag.type'),
  124. $query->expr()->eq('tagmap.categoryid', 'tag.id'),
  125. $query->expr()->eq('tag.type', $query->createNamedParameter('files')),
  126. $query->expr()->eq('tag.uid', $query->createNamedParameter($user->getUID()))
  127. ));
  128. }
  129. protected function equipQueryForShares(CacheQueryBuilder $query): void {
  130. $query->join('file', 'share', 's', $query->expr()->eq('file.fileid', 's.file_source'));
  131. }
  132. /**
  133. * Perform a file system search in multiple caches
  134. *
  135. * the results will be grouped by the same array keys as the $caches argument to allow
  136. * post-processing based on which cache the result came from
  137. *
  138. * @template T of array-key
  139. * @param ISearchQuery $searchQuery
  140. * @param array<T, ICache> $caches
  141. * @return array<T, ICacheEntry[]>
  142. */
  143. public function searchInCaches(ISearchQuery $searchQuery, array $caches): array {
  144. // search in multiple caches at once by creating one query in the following format
  145. // SELECT ... FROM oc_filecache WHERE
  146. // <filter expressions from the search query>
  147. // AND (
  148. // <filter expression for storage1> OR
  149. // <filter expression for storage2> OR
  150. // ...
  151. // );
  152. //
  153. // This gives us all the files matching the search query from all caches
  154. //
  155. // while the resulting rows don't have a way to tell what storage they came from (multiple storages/caches can share storage_id)
  156. // we can just ask every cache if the row belongs to them and give them the cache to do any post processing on the result.
  157. $builder = $this->getQueryBuilder();
  158. $query = $builder->selectFileCache('file', false);
  159. $requestedFields = $this->searchBuilder->extractRequestedFields($searchQuery->getSearchOperation());
  160. if (in_array('systemtag', $requestedFields)) {
  161. $this->equipQueryForSystemTags($query, $this->requireUser($searchQuery));
  162. }
  163. if (in_array('tagname', $requestedFields) || in_array('favorite', $requestedFields)) {
  164. $this->equipQueryForDavTags($query, $this->requireUser($searchQuery));
  165. }
  166. if (in_array('owner', $requestedFields) || in_array('share_with', $requestedFields) || in_array('share_type', $requestedFields)) {
  167. $this->equipQueryForShares($query);
  168. }
  169. $metadataQuery = $query->selectMetadata();
  170. $this->applySearchConstraints($query, $searchQuery, $caches, $metadataQuery);
  171. $result = $query->execute();
  172. $files = $result->fetchAll();
  173. $rawEntries = array_map(function (array $data) use ($metadataQuery) {
  174. // migrate to null safe ...
  175. if ($metadataQuery === null) {
  176. $data['metadata'] = [];
  177. } else {
  178. $data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
  179. }
  180. return Cache::cacheEntryFromData($data, $this->mimetypeLoader);
  181. }, $files);
  182. $result->closeCursor();
  183. // loop through all caches for each result to see if the result matches that storage
  184. // results are grouped by the same array keys as the caches argument to allow the caller to distinguish the source of the results
  185. $results = array_fill_keys(array_keys($caches), []);
  186. foreach ($rawEntries as $rawEntry) {
  187. foreach ($caches as $cacheKey => $cache) {
  188. $entry = $cache->getCacheEntryFromSearchResult($rawEntry);
  189. if ($entry) {
  190. $results[$cacheKey][] = $entry;
  191. }
  192. }
  193. }
  194. return $results;
  195. }
  196. protected function requireUser(ISearchQuery $searchQuery): IUser {
  197. $user = $searchQuery->getUser();
  198. if ($user === null) {
  199. throw new \InvalidArgumentException("This search operation requires the user to be set in the query");
  200. }
  201. return $user;
  202. }
  203. /**
  204. * @return list{0?: array<array-key, ICache>, 1?: array<array-key, IMountPoint>}
  205. */
  206. public function getCachesAndMountPointsForSearch(IRootFolder $root, string $path, bool $limitToHome = false): array {
  207. $rootLength = strlen($path);
  208. $mount = $root->getMount($path);
  209. $storage = $mount->getStorage();
  210. if ($storage === null) {
  211. return [];
  212. }
  213. $internalPath = $mount->getInternalPath($path);
  214. if ($internalPath !== '') {
  215. // a temporary CacheJail is used to handle filtering down the results to within this folder
  216. /** @var ICache[] $caches */
  217. $caches = ['' => new CacheJail($storage->getCache(''), $internalPath)];
  218. } else {
  219. /** @var ICache[] $caches */
  220. $caches = ['' => $storage->getCache('')];
  221. }
  222. /** @var IMountPoint[] $mountByMountPoint */
  223. $mountByMountPoint = ['' => $mount];
  224. if (!$limitToHome) {
  225. $mounts = $root->getMountsIn($path);
  226. foreach ($mounts as $mount) {
  227. $storage = $mount->getStorage();
  228. if ($storage) {
  229. $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
  230. $caches[$relativeMountPoint] = $storage->getCache('');
  231. $mountByMountPoint[$relativeMountPoint] = $mount;
  232. }
  233. }
  234. }
  235. return [$caches, $mountByMountPoint];
  236. }
  237. }