UnifiedSearchController.php 5.2 KB

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