File.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Search\Provider;
  8. use OC\Files\Search\SearchComparison;
  9. use OC\Files\Search\SearchOrder;
  10. use OC\Files\Search\SearchQuery;
  11. use OCP\Files\FileInfo;
  12. use OCP\Files\IRootFolder;
  13. use OCP\Files\Search\ISearchComparison;
  14. use OCP\Files\Search\ISearchOrder;
  15. use OCP\IUserSession;
  16. use OCP\Search\PagedProvider;
  17. /**
  18. * Provide search results from the 'files' app
  19. * @deprecated 20.0.0
  20. */
  21. class File extends PagedProvider {
  22. /**
  23. * Search for files and folders matching the given query
  24. *
  25. * @param string $query
  26. * @param int|null $limit
  27. * @param int|null $offset
  28. * @return \OCP\Search\Result[]
  29. * @deprecated 20.0.0
  30. */
  31. public function search($query, ?int $limit = null, ?int $offset = null) {
  32. /** @var IRootFolder $rootFolder */
  33. $rootFolder = \OCP\Server::get(IRootFolder::class);
  34. /** @var IUserSession $userSession */
  35. $userSession = \OCP\Server::get(IUserSession::class);
  36. $user = $userSession->getUser();
  37. if (!$user) {
  38. return [];
  39. }
  40. $userFolder = $rootFolder->getUserFolder($user->getUID());
  41. $fileQuery = new SearchQuery(
  42. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%'),
  43. (int)$limit,
  44. (int)$offset,
  45. [
  46. new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
  47. ],
  48. $user
  49. );
  50. $files = $userFolder->search($fileQuery);
  51. $results = [];
  52. // edit results
  53. foreach ($files as $fileData) {
  54. // create audio result
  55. if ($fileData->getMimePart() === 'audio') {
  56. $result = new \OC\Search\Result\Audio($fileData);
  57. }
  58. // create image result
  59. elseif ($fileData->getMimePart() === 'image') {
  60. $result = new \OC\Search\Result\Image($fileData);
  61. }
  62. // create folder result
  63. elseif ($fileData->getMimetype() === FileInfo::MIMETYPE_FOLDER) {
  64. $result = new \OC\Search\Result\Folder($fileData);
  65. }
  66. // or create file result
  67. else {
  68. $result = new \OC\Search\Result\File($fileData);
  69. }
  70. // add to results
  71. $results[] = $result;
  72. }
  73. // return
  74. return $results;
  75. }
  76. public function searchPaged($query, $page, $size) {
  77. if ($size === 0) {
  78. return $this->search($query);
  79. } else {
  80. return $this->search($query, $size, ($page - 1) * $size);
  81. }
  82. }
  83. }