UnifiedSearchController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 InvalidArgumentException;
  29. use OC\Search\SearchComposer;
  30. use OC\Search\SearchQuery;
  31. use OC\Search\UnsupportedFilter;
  32. use OCA\Core\ResponseDefinitions;
  33. use OCP\AppFramework\Http;
  34. use OCP\AppFramework\Http\Attribute\ApiRoute;
  35. use OCP\AppFramework\Http\DataResponse;
  36. use OCP\AppFramework\OCSController;
  37. use OCP\IRequest;
  38. use OCP\IURLGenerator;
  39. use OCP\IUserSession;
  40. use OCP\Route\IRouter;
  41. use OCP\Search\ISearchQuery;
  42. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  43. /**
  44. * @psalm-import-type CoreUnifiedSearchProvider from ResponseDefinitions
  45. * @psalm-import-type CoreUnifiedSearchResult from ResponseDefinitions
  46. */
  47. class UnifiedSearchController extends OCSController {
  48. public function __construct(
  49. IRequest $request,
  50. private IUserSession $userSession,
  51. private SearchComposer $composer,
  52. private IRouter $router,
  53. private IURLGenerator $urlGenerator,
  54. ) {
  55. parent::__construct('core', $request);
  56. }
  57. /**
  58. * @NoAdminRequired
  59. * @NoCSRFRequired
  60. *
  61. * Get the providers for unified search
  62. *
  63. * @param string $from the url the user is currently at
  64. * @return DataResponse<Http::STATUS_OK, CoreUnifiedSearchProvider[], array{}>
  65. *
  66. * 200: Providers returned
  67. */
  68. #[ApiRoute(verb: 'GET', url: '/providers', root: '/search')]
  69. public function getProviders(string $from = ''): DataResponse {
  70. [$route, $parameters] = $this->getRouteInformation($from);
  71. $result = $this->composer->getProviders($route, $parameters);
  72. $response = new DataResponse($result);
  73. $response->setETag(md5(json_encode($result)));
  74. return $response;
  75. }
  76. /**
  77. * @NoAdminRequired
  78. * @NoCSRFRequired
  79. *
  80. * Launch a search for a specific search provider.
  81. *
  82. * Additional filters are available for each provider.
  83. * Send a request to /providers endpoint to list providers with their available filters.
  84. *
  85. * @param string $providerId ID of the provider
  86. * @param string $term Term to search
  87. * @param int|null $sortOrder Order of entries
  88. * @param int|null $limit Maximum amount of entries, limited to 25
  89. * @param int|string|null $cursor Offset for searching
  90. * @param string $from The current user URL
  91. *
  92. * @return DataResponse<Http::STATUS_OK, CoreUnifiedSearchResult, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, string, array{}>
  93. *
  94. * 200: Search entries returned
  95. * 400: Searching is not possible
  96. */
  97. #[ApiRoute(verb: 'GET', url: '/providers/{providerId}/search', root: '/search')]
  98. public function search(
  99. string $providerId,
  100. // Unused parameter for OpenAPI spec generator
  101. string $term = '',
  102. ?int $sortOrder = null,
  103. ?int $limit = null,
  104. $cursor = null,
  105. string $from = '',
  106. ): DataResponse {
  107. [$route, $routeParameters] = $this->getRouteInformation($from);
  108. $limit ??= SearchQuery::LIMIT_DEFAULT;
  109. $limit = max(1, min($limit, 25));
  110. try {
  111. $filters = $this->composer->buildFilterList($providerId, $this->request->getParams());
  112. } catch (UnsupportedFilter|InvalidArgumentException $e) {
  113. return new DataResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
  114. }
  115. return new DataResponse(
  116. $this->composer->search(
  117. $this->userSession->getUser(),
  118. $providerId,
  119. new SearchQuery(
  120. $filters,
  121. $sortOrder ?? ISearchQuery::SORT_DATE_DESC,
  122. $limit,
  123. $cursor,
  124. $route,
  125. $routeParameters
  126. )
  127. )->jsonSerialize()
  128. );
  129. }
  130. protected function getRouteInformation(string $url): array {
  131. $routeStr = '';
  132. $parameters = [];
  133. if ($url !== '') {
  134. $urlParts = parse_url($url);
  135. $urlPath = $urlParts['path'];
  136. // Optionally strip webroot from URL. Required for route matching on setups
  137. // with Nextcloud in a webserver subfolder (webroot).
  138. $webroot = $this->urlGenerator->getWebroot();
  139. if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) {
  140. $urlPath = substr($urlPath, strlen($webroot));
  141. }
  142. try {
  143. $parameters = $this->router->findMatchingRoute($urlPath);
  144. // contacts.PageController.index => contacts.Page.index
  145. $route = $parameters['caller'];
  146. if (substr($route[1], -10) === 'Controller') {
  147. $route[1] = substr($route[1], 0, -10);
  148. }
  149. $routeStr = implode('.', $route);
  150. // cleanup
  151. unset($parameters['_route'], $parameters['action'], $parameters['caller']);
  152. } catch (ResourceNotFoundException $exception) {
  153. }
  154. if (isset($urlParts['query'])) {
  155. parse_str($urlParts['query'], $queryParameters);
  156. $parameters = array_merge($parameters, $queryParameters);
  157. }
  158. }
  159. return [
  160. $routeStr,
  161. $parameters,
  162. ];
  163. }
  164. }