UnifiedSearchController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Kate Döen <kate.doeen@nextcloud.com>
  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\Core\Controller;
  28. use OC\Search\SearchComposer;
  29. use OC\Search\SearchQuery;
  30. use OCA\Core\ResponseDefinitions;
  31. use OCP\AppFramework\OCSController;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\IRequest;
  35. use OCP\IURLGenerator;
  36. use OCP\IUserSession;
  37. use OCP\Route\IRouter;
  38. use OCP\Search\ISearchQuery;
  39. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  40. /**
  41. * @psalm-import-type CoreUnifiedSearchProvider from ResponseDefinitions
  42. * @psalm-import-type CoreUnifiedSearchResult from ResponseDefinitions
  43. */
  44. class UnifiedSearchController extends OCSController {
  45. public function __construct(
  46. IRequest $request,
  47. private IUserSession $userSession,
  48. private SearchComposer $composer,
  49. private IRouter $router,
  50. private IURLGenerator $urlGenerator,
  51. ) {
  52. parent::__construct('core', $request);
  53. }
  54. /**
  55. * @NoAdminRequired
  56. * @NoCSRFRequired
  57. *
  58. * Get the providers for unified search
  59. *
  60. * @param string $from the url the user is currently at
  61. * @return DataResponse<Http::STATUS_OK, CoreUnifiedSearchProvider[], array{}>
  62. */
  63. public function getProviders(string $from = ''): DataResponse {
  64. [$route, $parameters] = $this->getRouteInformation($from);
  65. $result = $this->composer->getProviders($route, $parameters);
  66. $response = new DataResponse($result);
  67. $response->setETag(md5(json_encode($result)));
  68. return $response;
  69. }
  70. /**
  71. * @NoAdminRequired
  72. * @NoCSRFRequired
  73. *
  74. * Search
  75. *
  76. * @param string $providerId ID of the provider
  77. * @param string $term Term to search
  78. * @param int|null $sortOrder Order of entries
  79. * @param int|null $limit Maximum amount of entries
  80. * @param int|string|null $cursor Offset for searching
  81. * @param string $from The current user URL
  82. *
  83. * @return DataResponse<Http::STATUS_OK, CoreUnifiedSearchResult, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, null, array{}>
  84. *
  85. * 200: Search entries returned
  86. * 400: Searching is not possible
  87. */
  88. public function search(string $providerId,
  89. string $term = '',
  90. ?int $sortOrder = null,
  91. ?int $limit = null,
  92. $cursor = null,
  93. string $from = ''): DataResponse {
  94. if (empty(trim($term))) {
  95. return new DataResponse(null, Http::STATUS_BAD_REQUEST);
  96. }
  97. [$route, $routeParameters] = $this->getRouteInformation($from);
  98. return new DataResponse(
  99. $this->composer->search(
  100. $this->userSession->getUser(),
  101. $providerId,
  102. new SearchQuery(
  103. $term,
  104. $sortOrder ?? ISearchQuery::SORT_DATE_DESC,
  105. $limit ?? SearchQuery::LIMIT_DEFAULT,
  106. $cursor,
  107. $route,
  108. $routeParameters
  109. )
  110. )->jsonSerialize()
  111. );
  112. }
  113. protected function getRouteInformation(string $url): array {
  114. $routeStr = '';
  115. $parameters = [];
  116. if ($url !== '') {
  117. $urlParts = parse_url($url);
  118. $urlPath = $urlParts['path'];
  119. // Optionally strip webroot from URL. Required for route matching on setups
  120. // with Nextcloud in a webserver subfolder (webroot).
  121. $webroot = $this->urlGenerator->getWebroot();
  122. if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) {
  123. $urlPath = substr($urlPath, strlen($webroot));
  124. }
  125. try {
  126. $parameters = $this->router->findMatchingRoute($urlPath);
  127. // contacts.PageController.index => contacts.Page.index
  128. $route = $parameters['caller'];
  129. if (substr($route[1], -10) === 'Controller') {
  130. $route[1] = substr($route[1], 0, -10);
  131. }
  132. $routeStr = implode('.', $route);
  133. // cleanup
  134. unset($parameters['_route'], $parameters['action'], $parameters['caller']);
  135. } catch (ResourceNotFoundException $exception) {
  136. }
  137. if (isset($urlParts['query'])) {
  138. parse_str($urlParts['query'], $queryParameters);
  139. $parameters = array_merge($parameters, $queryParameters);
  140. }
  141. }
  142. return [
  143. $routeStr,
  144. $parameters,
  145. ];
  146. }
  147. }