1
0

UnifiedSearchController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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
  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. try {
  109. $filters = $this->composer->buildFilterList($providerId, $this->request->getParams());
  110. } catch (UnsupportedFilter|InvalidArgumentException $e) {
  111. return new DataResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
  112. }
  113. return new DataResponse(
  114. $this->composer->search(
  115. $this->userSession->getUser(),
  116. $providerId,
  117. new SearchQuery(
  118. $filters,
  119. $sortOrder ?? ISearchQuery::SORT_DATE_DESC,
  120. $limit ?? SearchQuery::LIMIT_DEFAULT,
  121. $cursor,
  122. $route,
  123. $routeParameters
  124. )
  125. )->jsonSerialize()
  126. );
  127. }
  128. protected function getRouteInformation(string $url): array {
  129. $routeStr = '';
  130. $parameters = [];
  131. if ($url !== '') {
  132. $urlParts = parse_url($url);
  133. $urlPath = $urlParts['path'];
  134. // Optionally strip webroot from URL. Required for route matching on setups
  135. // with Nextcloud in a webserver subfolder (webroot).
  136. $webroot = $this->urlGenerator->getWebroot();
  137. if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) {
  138. $urlPath = substr($urlPath, strlen($webroot));
  139. }
  140. try {
  141. $parameters = $this->router->findMatchingRoute($urlPath);
  142. // contacts.PageController.index => contacts.Page.index
  143. $route = $parameters['caller'];
  144. if (substr($route[1], -10) === 'Controller') {
  145. $route[1] = substr($route[1], 0, -10);
  146. }
  147. $routeStr = implode('.', $route);
  148. // cleanup
  149. unset($parameters['_route'], $parameters['action'], $parameters['caller']);
  150. } catch (ResourceNotFoundException $exception) {
  151. }
  152. if (isset($urlParts['query'])) {
  153. parse_str($urlParts['query'], $queryParameters);
  154. $parameters = array_merge($parameters, $queryParameters);
  155. }
  156. }
  157. return [
  158. $routeStr,
  159. $parameters,
  160. ];
  161. }
  162. }