UnifiedSearchController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * 200: Providers returned
  64. */
  65. public function getProviders(string $from = ''): DataResponse {
  66. [$route, $parameters] = $this->getRouteInformation($from);
  67. $result = $this->composer->getProviders($route, $parameters);
  68. $response = new DataResponse($result);
  69. $response->setETag(md5(json_encode($result)));
  70. return $response;
  71. }
  72. /**
  73. * @NoAdminRequired
  74. * @NoCSRFRequired
  75. *
  76. * Search
  77. *
  78. * @param string $providerId ID of the provider
  79. * @param string $term Term to search
  80. * @param int|null $sortOrder Order of entries
  81. * @param int|null $limit Maximum amount of entries
  82. * @param int|string|null $cursor Offset for searching
  83. * @param string $from The current user URL
  84. *
  85. * @return DataResponse<Http::STATUS_OK, CoreUnifiedSearchResult, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, null, array{}>
  86. *
  87. * 200: Search entries returned
  88. * 400: Searching is not possible
  89. */
  90. public function search(string $providerId,
  91. string $term = '',
  92. ?int $sortOrder = null,
  93. ?int $limit = null,
  94. $cursor = null,
  95. string $from = ''): DataResponse {
  96. if (trim($term) === "") {
  97. return new DataResponse(null, Http::STATUS_BAD_REQUEST);
  98. }
  99. [$route, $routeParameters] = $this->getRouteInformation($from);
  100. return new DataResponse(
  101. $this->composer->search(
  102. $this->userSession->getUser(),
  103. $providerId,
  104. new SearchQuery(
  105. $term,
  106. $sortOrder ?? ISearchQuery::SORT_DATE_DESC,
  107. $limit ?? SearchQuery::LIMIT_DEFAULT,
  108. $cursor,
  109. $route,
  110. $routeParameters
  111. )
  112. )->jsonSerialize()
  113. );
  114. }
  115. protected function getRouteInformation(string $url): array {
  116. $routeStr = '';
  117. $parameters = [];
  118. if ($url !== '') {
  119. $urlParts = parse_url($url);
  120. $urlPath = $urlParts['path'];
  121. // Optionally strip webroot from URL. Required for route matching on setups
  122. // with Nextcloud in a webserver subfolder (webroot).
  123. $webroot = $this->urlGenerator->getWebroot();
  124. if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) {
  125. $urlPath = substr($urlPath, strlen($webroot));
  126. }
  127. try {
  128. $parameters = $this->router->findMatchingRoute($urlPath);
  129. // contacts.PageController.index => contacts.Page.index
  130. $route = $parameters['caller'];
  131. if (substr($route[1], -10) === 'Controller') {
  132. $route[1] = substr($route[1], 0, -10);
  133. }
  134. $routeStr = implode('.', $route);
  135. // cleanup
  136. unset($parameters['_route'], $parameters['action'], $parameters['caller']);
  137. } catch (ResourceNotFoundException $exception) {
  138. }
  139. if (isset($urlParts['query'])) {
  140. parse_str($urlParts['query'], $queryParameters);
  141. $parameters = array_merge($parameters, $queryParameters);
  142. }
  143. }
  144. return [
  145. $routeStr,
  146. $parameters,
  147. ];
  148. }
  149. }