File.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andrew Brown <andrew@casabrown.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Jakob Sack <mail@jakobsack.de>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Search\Provider;
  29. use OC\Files\Search\SearchComparison;
  30. use OC\Files\Search\SearchOrder;
  31. use OC\Files\Search\SearchQuery;
  32. use OCP\Files\FileInfo;
  33. use OCP\Files\IRootFolder;
  34. use OCP\Files\Search\ISearchComparison;
  35. use OCP\Files\Search\ISearchOrder;
  36. use OCP\IUserSession;
  37. use OCP\Search\PagedProvider;
  38. /**
  39. * Provide search results from the 'files' app
  40. * @deprecated 20.0.0
  41. */
  42. class File extends PagedProvider {
  43. /**
  44. * Search for files and folders matching the given query
  45. *
  46. * @param string $query
  47. * @param int|null $limit
  48. * @param int|null $offset
  49. * @return \OCP\Search\Result[]
  50. * @deprecated 20.0.0
  51. */
  52. public function search($query, int $limit = null, int $offset = null) {
  53. /** @var IRootFolder $rootFolder */
  54. $rootFolder = \OC::$server->query(IRootFolder::class);
  55. /** @var IUserSession $userSession */
  56. $userSession = \OC::$server->query(IUserSession::class);
  57. $user = $userSession->getUser();
  58. if (!$user) {
  59. return [];
  60. }
  61. $userFolder = $rootFolder->getUserFolder($user->getUID());
  62. $fileQuery = new SearchQuery(
  63. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%'),
  64. (int)$limit,
  65. (int)$offset,
  66. [
  67. new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
  68. ],
  69. $user
  70. );
  71. $files = $userFolder->search($fileQuery);
  72. $results = [];
  73. // edit results
  74. foreach ($files as $fileData) {
  75. // create audio result
  76. if ($fileData->getMimePart() === 'audio') {
  77. $result = new \OC\Search\Result\Audio($fileData);
  78. }
  79. // create image result
  80. elseif ($fileData->getMimePart() === 'image') {
  81. $result = new \OC\Search\Result\Image($fileData);
  82. }
  83. // create folder result
  84. elseif ($fileData->getMimetype() === FileInfo::MIMETYPE_FOLDER) {
  85. $result = new \OC\Search\Result\Folder($fileData);
  86. }
  87. // or create file result
  88. else {
  89. $result = new \OC\Search\Result\File($fileData);
  90. }
  91. // add to results
  92. $results[] = $result;
  93. }
  94. // return
  95. return $results;
  96. }
  97. public function searchPaged($query, $page, $size) {
  98. if ($size === 0) {
  99. return $this->search($query);
  100. } else {
  101. return $this->search($query, $size, ($page - 1) * $size);
  102. }
  103. }
  104. }