You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileSearchBackend.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Christian <16852529+cviereck@users.noreply.github.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Files;
  27. use OC\Files\Search\SearchBinaryOperator;
  28. use OC\Files\Search\SearchComparison;
  29. use OC\Files\Search\SearchOrder;
  30. use OC\Files\Search\SearchQuery;
  31. use OC\Files\View;
  32. use OC\Metadata\IMetadataManager;
  33. use OCA\DAV\Connector\Sabre\CachingTree;
  34. use OCA\DAV\Connector\Sabre\Directory;
  35. use OCA\DAV\Connector\Sabre\FilesPlugin;
  36. use OCA\DAV\Connector\Sabre\TagsPlugin;
  37. use OCP\Files\Cache\ICacheEntry;
  38. use OCP\Files\Folder;
  39. use OCP\Files\IRootFolder;
  40. use OCP\Files\Node;
  41. use OCP\Files\Search\ISearchOperator;
  42. use OCP\Files\Search\ISearchOrder;
  43. use OCP\Files\Search\ISearchQuery;
  44. use OCP\IUser;
  45. use OCP\Share\IManager;
  46. use Sabre\DAV\Exception\NotFound;
  47. use Sabre\DAV\INode;
  48. use SearchDAV\Backend\ISearchBackend;
  49. use SearchDAV\Backend\SearchPropertyDefinition;
  50. use SearchDAV\Backend\SearchResult;
  51. use SearchDAV\Query\Literal;
  52. use SearchDAV\Query\Operator;
  53. use SearchDAV\Query\Order;
  54. use SearchDAV\Query\Query;
  55. class FileSearchBackend implements ISearchBackend {
  56. /** @var CachingTree */
  57. private $tree;
  58. /** @var IUser */
  59. private $user;
  60. /** @var IRootFolder */
  61. private $rootFolder;
  62. /** @var IManager */
  63. private $shareManager;
  64. /** @var View */
  65. private $view;
  66. /**
  67. * FileSearchBackend constructor.
  68. *
  69. * @param CachingTree $tree
  70. * @param IUser $user
  71. * @param IRootFolder $rootFolder
  72. * @param IManager $shareManager
  73. * @param View $view
  74. * @internal param IRootFolder $rootFolder
  75. */
  76. public function __construct(CachingTree $tree, IUser $user, IRootFolder $rootFolder, IManager $shareManager, View $view) {
  77. $this->tree = $tree;
  78. $this->user = $user;
  79. $this->rootFolder = $rootFolder;
  80. $this->shareManager = $shareManager;
  81. $this->view = $view;
  82. }
  83. /**
  84. * Search endpoint will be remote.php/dav
  85. */
  86. public function getArbiterPath(): string {
  87. return '';
  88. }
  89. public function isValidScope(string $href, $depth, ?string $path): bool {
  90. // only allow scopes inside the dav server
  91. if (is_null($path)) {
  92. return false;
  93. }
  94. try {
  95. $node = $this->tree->getNodeForPath($path);
  96. return $node instanceof Directory;
  97. } catch (NotFound $e) {
  98. return false;
  99. }
  100. }
  101. public function getPropertyDefinitionsForScope(string $href, ?string $path): array {
  102. // all valid scopes support the same schema
  103. //todo dynamically load all propfind properties that are supported
  104. return [
  105. // queryable properties
  106. new SearchPropertyDefinition('{DAV:}displayname', true, true, true),
  107. new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true),
  108. new SearchPropertyDefinition('{DAV:}getlastmodified', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
  109. new SearchPropertyDefinition(FilesPlugin::SIZE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
  110. new SearchPropertyDefinition(TagsPlugin::FAVORITE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_BOOLEAN),
  111. new SearchPropertyDefinition(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, true, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
  112. new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, false),
  113. // select only properties
  114. new SearchPropertyDefinition('{DAV:}resourcetype', false, true, false),
  115. new SearchPropertyDefinition('{DAV:}getcontentlength', false, true, false),
  116. new SearchPropertyDefinition(FilesPlugin::CHECKSUMS_PROPERTYNAME, false, true, false),
  117. new SearchPropertyDefinition(FilesPlugin::PERMISSIONS_PROPERTYNAME, false, true, false),
  118. new SearchPropertyDefinition(FilesPlugin::GETETAG_PROPERTYNAME, false, true, false),
  119. new SearchPropertyDefinition(FilesPlugin::OWNER_DISPLAY_NAME_PROPERTYNAME, false, true, false),
  120. new SearchPropertyDefinition(FilesPlugin::DATA_FINGERPRINT_PROPERTYNAME, false, true, false),
  121. new SearchPropertyDefinition(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_BOOLEAN),
  122. new SearchPropertyDefinition(FilesPlugin::FILE_METADATA_SIZE, false, true, false, SearchPropertyDefinition::DATATYPE_STRING),
  123. new SearchPropertyDefinition(FilesPlugin::FILEID_PROPERTYNAME, false, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
  124. ];
  125. }
  126. /**
  127. * @param INode[] $nodes
  128. * @param string[] $requestProperties
  129. */
  130. public function preloadPropertyFor(array $nodes, array $requestProperties): void {
  131. if (in_array(FilesPlugin::FILE_METADATA_SIZE, $requestProperties, true)) {
  132. // Preloading of the metadata
  133. $fileIds = [];
  134. foreach ($nodes as $node) {
  135. /** @var \OCP\Files\Node|\OCA\DAV\Connector\Sabre\Node $node */
  136. if (str_starts_with($node->getFileInfo()->getMimeType(), 'image/')) {
  137. /** @var \OCA\DAV\Connector\Sabre\File $node */
  138. $fileIds[] = $node->getFileInfo()->getId();
  139. }
  140. }
  141. /** @var IMetaDataManager $metadataManager */
  142. $metadataManager = \OC::$server->get(IMetadataManager::class);
  143. $preloadedMetadata = $metadataManager->fetchMetadataFor('size', $fileIds);
  144. foreach ($nodes as $node) {
  145. /** @var \OCP\Files\Node|\OCA\DAV\Connector\Sabre\Node $node */
  146. if (str_starts_with($node->getFileInfo()->getMimeType(), 'image/')) {
  147. /** @var \OCA\DAV\Connector\Sabre\File $node */
  148. $node->setMetadata('size', $preloadedMetadata[$node->getFileInfo()->getId()]);
  149. }
  150. }
  151. }
  152. }
  153. /**
  154. * @param Query $search
  155. * @return SearchResult[]
  156. */
  157. public function search(Query $search): array {
  158. if (count($search->from) !== 1) {
  159. throw new \InvalidArgumentException('Searching more than one folder is not supported');
  160. }
  161. $query = $this->transformQuery($search);
  162. $scope = $search->from[0];
  163. if ($scope->path === null) {
  164. throw new \InvalidArgumentException('Using uri\'s as scope is not supported, please use a path relative to the search arbiter instead');
  165. }
  166. $node = $this->tree->getNodeForPath($scope->path);
  167. if (!$node instanceof Directory) {
  168. throw new \InvalidArgumentException('Search is only supported on directories');
  169. }
  170. $fileInfo = $node->getFileInfo();
  171. $folder = $this->rootFolder->get($fileInfo->getPath());
  172. /** @var Folder $folder $results */
  173. $results = $folder->search($query);
  174. /** @var SearchResult[] $nodes */
  175. $nodes = array_map(function (Node $node) {
  176. if ($node instanceof Folder) {
  177. $davNode = new \OCA\DAV\Connector\Sabre\Directory($this->view, $node, $this->tree, $this->shareManager);
  178. } else {
  179. $davNode = new \OCA\DAV\Connector\Sabre\File($this->view, $node, $this->shareManager);
  180. }
  181. $path = $this->getHrefForNode($node);
  182. $this->tree->cacheNode($davNode, $path);
  183. return new SearchResult($davNode, $path);
  184. }, $results);
  185. if (!$query->limitToHome()) {
  186. // Sort again, since the result from multiple storages is appended and not sorted
  187. usort($nodes, function (SearchResult $a, SearchResult $b) use ($search) {
  188. return $this->sort($a, $b, $search->orderBy);
  189. });
  190. }
  191. // If a limit is provided use only return that number of files
  192. if ($search->limit->maxResults !== 0) {
  193. $nodes = \array_slice($nodes, 0, $search->limit->maxResults);
  194. }
  195. return $nodes;
  196. }
  197. private function sort(SearchResult $a, SearchResult $b, array $orders) {
  198. /** @var Order $order */
  199. foreach ($orders as $order) {
  200. $v1 = $this->getSearchResultProperty($a, $order->property);
  201. $v2 = $this->getSearchResultProperty($b, $order->property);
  202. if ($v1 === null && $v2 === null) {
  203. continue;
  204. }
  205. if ($v1 === null) {
  206. return $order->order === Order::ASC ? 1 : -1;
  207. }
  208. if ($v2 === null) {
  209. return $order->order === Order::ASC ? -1 : 1;
  210. }
  211. $s = $this->compareProperties($v1, $v2, $order);
  212. if ($s === 0) {
  213. continue;
  214. }
  215. if ($order->order === Order::DESC) {
  216. $s = -$s;
  217. }
  218. return $s;
  219. }
  220. return 0;
  221. }
  222. private function compareProperties($a, $b, Order $order) {
  223. switch ($order->property->dataType) {
  224. case SearchPropertyDefinition::DATATYPE_STRING:
  225. return strcmp($a, $b);
  226. case SearchPropertyDefinition::DATATYPE_BOOLEAN:
  227. if ($a === $b) {
  228. return 0;
  229. }
  230. if ($a === false) {
  231. return -1;
  232. }
  233. return 1;
  234. default:
  235. if ($a === $b) {
  236. return 0;
  237. }
  238. if ($a < $b) {
  239. return -1;
  240. }
  241. return 1;
  242. }
  243. }
  244. private function getSearchResultProperty(SearchResult $result, SearchPropertyDefinition $property) {
  245. /** @var \OCA\DAV\Connector\Sabre\Node $node */
  246. $node = $result->node;
  247. switch ($property->name) {
  248. case '{DAV:}displayname':
  249. return $node->getName();
  250. case '{DAV:}getlastmodified':
  251. return $node->getLastModified();
  252. case FilesPlugin::SIZE_PROPERTYNAME:
  253. return $node->getSize();
  254. case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
  255. return $node->getInternalFileId();
  256. default:
  257. return null;
  258. }
  259. }
  260. /**
  261. * @param Node $node
  262. * @return string
  263. */
  264. private function getHrefForNode(Node $node) {
  265. $base = '/files/' . $this->user->getUID();
  266. return $base . $this->view->getRelativePath($node->getPath());
  267. }
  268. /**
  269. * @param Query $query
  270. * @return ISearchQuery
  271. */
  272. private function transformQuery(Query $query): ISearchQuery {
  273. $limit = $query->limit;
  274. $orders = array_map([$this, 'mapSearchOrder'], $query->orderBy);
  275. $offset = $limit->firstResult;
  276. $limitHome = false;
  277. $ownerProp = $this->extractWhereValue($query->where, FilesPlugin::OWNER_ID_PROPERTYNAME, Operator::OPERATION_EQUAL);
  278. if ($ownerProp !== null) {
  279. if ($ownerProp === $this->user->getUID()) {
  280. $limitHome = true;
  281. } else {
  282. throw new \InvalidArgumentException("Invalid search value for '{http://owncloud.org/ns}owner-id', only the current user id is allowed");
  283. }
  284. }
  285. return new SearchQuery(
  286. $this->transformSearchOperation($query->where),
  287. (int)$limit->maxResults,
  288. $offset,
  289. $orders,
  290. $this->user,
  291. $limitHome
  292. );
  293. }
  294. /**
  295. * @param Order $order
  296. * @return ISearchOrder
  297. */
  298. private function mapSearchOrder(Order $order) {
  299. return new SearchOrder($order->order === Order::ASC ? ISearchOrder::DIRECTION_ASCENDING : ISearchOrder::DIRECTION_DESCENDING, $this->mapPropertyNameToColumn($order->property));
  300. }
  301. /**
  302. * @param Operator $operator
  303. * @return ISearchOperator
  304. */
  305. private function transformSearchOperation(Operator $operator) {
  306. [, $trimmedType] = explode('}', $operator->type);
  307. switch ($operator->type) {
  308. case Operator::OPERATION_AND:
  309. case Operator::OPERATION_OR:
  310. case Operator::OPERATION_NOT:
  311. $arguments = array_map([$this, 'transformSearchOperation'], $operator->arguments);
  312. return new SearchBinaryOperator($trimmedType, $arguments);
  313. case Operator::OPERATION_EQUAL:
  314. case Operator::OPERATION_GREATER_OR_EQUAL_THAN:
  315. case Operator::OPERATION_GREATER_THAN:
  316. case Operator::OPERATION_LESS_OR_EQUAL_THAN:
  317. case Operator::OPERATION_LESS_THAN:
  318. case Operator::OPERATION_IS_LIKE:
  319. if (count($operator->arguments) !== 2) {
  320. throw new \InvalidArgumentException('Invalid number of arguments for ' . $trimmedType . ' operation');
  321. }
  322. if (!($operator->arguments[0] instanceof SearchPropertyDefinition)) {
  323. throw new \InvalidArgumentException('Invalid argument 1 for ' . $trimmedType . ' operation, expected property');
  324. }
  325. if (!($operator->arguments[1] instanceof Literal)) {
  326. throw new \InvalidArgumentException('Invalid argument 2 for ' . $trimmedType . ' operation, expected literal');
  327. }
  328. return new SearchComparison($trimmedType, $this->mapPropertyNameToColumn($operator->arguments[0]), $this->castValue($operator->arguments[0], $operator->arguments[1]->value));
  329. case Operator::OPERATION_IS_COLLECTION:
  330. return new SearchComparison('eq', 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE);
  331. default:
  332. throw new \InvalidArgumentException('Unsupported operation ' . $trimmedType . ' (' . $operator->type . ')');
  333. }
  334. }
  335. /**
  336. * @param SearchPropertyDefinition $property
  337. * @return string
  338. */
  339. private function mapPropertyNameToColumn(SearchPropertyDefinition $property) {
  340. switch ($property->name) {
  341. case '{DAV:}displayname':
  342. return 'name';
  343. case '{DAV:}getcontenttype':
  344. return 'mimetype';
  345. case '{DAV:}getlastmodified':
  346. return 'mtime';
  347. case FilesPlugin::SIZE_PROPERTYNAME:
  348. return 'size';
  349. case TagsPlugin::FAVORITE_PROPERTYNAME:
  350. return 'favorite';
  351. case TagsPlugin::TAGS_PROPERTYNAME:
  352. return 'tagname';
  353. case FilesPlugin::INTERNAL_FILEID_PROPERTYNAME:
  354. return 'fileid';
  355. default:
  356. throw new \InvalidArgumentException('Unsupported property for search or order: ' . $property->name);
  357. }
  358. }
  359. private function castValue(SearchPropertyDefinition $property, $value) {
  360. switch ($property->dataType) {
  361. case SearchPropertyDefinition::DATATYPE_BOOLEAN:
  362. return $value === 'yes';
  363. case SearchPropertyDefinition::DATATYPE_DECIMAL:
  364. case SearchPropertyDefinition::DATATYPE_INTEGER:
  365. case SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER:
  366. return 0 + $value;
  367. case SearchPropertyDefinition::DATATYPE_DATETIME:
  368. if (is_numeric($value)) {
  369. return max(0, 0 + $value);
  370. }
  371. $date = \DateTime::createFromFormat(\DateTimeInterface::ATOM, $value);
  372. return ($date instanceof \DateTime && $date->getTimestamp() !== false) ? $date->getTimestamp() : 0;
  373. default:
  374. return $value;
  375. }
  376. }
  377. /**
  378. * Get a specific property from the were clause
  379. */
  380. private function extractWhereValue(Operator &$operator, string $propertyName, string $comparison, bool $acceptableLocation = true): ?string {
  381. switch ($operator->type) {
  382. case Operator::OPERATION_AND:
  383. case Operator::OPERATION_OR:
  384. case Operator::OPERATION_NOT:
  385. foreach ($operator->arguments as &$argument) {
  386. $value = $this->extractWhereValue($argument, $propertyName, $comparison, $acceptableLocation && $operator->type === Operator::OPERATION_AND);
  387. if ($value !== null) {
  388. return $value;
  389. }
  390. }
  391. return null;
  392. case Operator::OPERATION_EQUAL:
  393. case Operator::OPERATION_GREATER_OR_EQUAL_THAN:
  394. case Operator::OPERATION_GREATER_THAN:
  395. case Operator::OPERATION_LESS_OR_EQUAL_THAN:
  396. case Operator::OPERATION_LESS_THAN:
  397. case Operator::OPERATION_IS_LIKE:
  398. if ($operator->arguments[0]->name === $propertyName) {
  399. if ($operator->type === $comparison) {
  400. if ($acceptableLocation) {
  401. if ($operator->arguments[1] instanceof Literal) {
  402. $value = $operator->arguments[1]->value;
  403. // to remove the comparison from the query, we replace it with an empty AND
  404. $operator = new Operator(Operator::OPERATION_AND);
  405. return $value;
  406. } else {
  407. throw new \InvalidArgumentException("searching by '$propertyName' is only allowed with a literal value");
  408. }
  409. } else {
  410. throw new \InvalidArgumentException("searching by '$propertyName' is not allowed inside a '{DAV:}or' or '{DAV:}not'");
  411. }
  412. } else {
  413. throw new \InvalidArgumentException("searching by '$propertyName' is only allowed inside a '$comparison'");
  414. }
  415. } else {
  416. return null;
  417. }
  418. // no break
  419. default:
  420. return null;
  421. }
  422. }
  423. }