1
0

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