1
0

UnifiedSearchController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Core\Controller;
  27. use OC\Search\SearchComposer;
  28. use OC\Search\SearchQuery;
  29. use OCP\AppFramework\OCSController;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\IRequest;
  33. use OCP\IURLGenerator;
  34. use OCP\IUserSession;
  35. use OCP\Route\IRouter;
  36. use OCP\Search\ISearchQuery;
  37. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  38. class UnifiedSearchController extends OCSController {
  39. public function __construct(
  40. IRequest $request,
  41. private IUserSession $userSession,
  42. private SearchComposer $composer,
  43. private IRouter $router,
  44. private IURLGenerator $urlGenerator,
  45. ) {
  46. parent::__construct('core', $request);
  47. }
  48. /**
  49. * @NoAdminRequired
  50. * @NoCSRFRequired
  51. *
  52. * @param string $from the url the user is currently at
  53. */
  54. public function getProviders(string $from = ''): DataResponse {
  55. [$route, $parameters] = $this->getRouteInformation($from);
  56. $result = $this->composer->getProviders($route, $parameters);
  57. $response = new DataResponse($result);
  58. $response->setETag(md5(json_encode($result)));
  59. return $response;
  60. }
  61. /**
  62. * @NoAdminRequired
  63. * @NoCSRFRequired
  64. *
  65. * @param string $providerId
  66. * @param string $term
  67. * @param int|null $sortOrder
  68. * @param int|null $limit
  69. * @param int|string|null $cursor
  70. * @param string $from
  71. *
  72. * @return DataResponse
  73. */
  74. public function search(string $providerId,
  75. string $term = '',
  76. ?int $sortOrder = null,
  77. ?int $limit = null,
  78. $cursor = null,
  79. string $from = ''): DataResponse {
  80. if (empty(trim($term))) {
  81. return new DataResponse(null, Http::STATUS_BAD_REQUEST);
  82. }
  83. [$route, $routeParameters] = $this->getRouteInformation($from);
  84. return new DataResponse(
  85. $this->composer->search(
  86. $this->userSession->getUser(),
  87. $providerId,
  88. new SearchQuery(
  89. $term,
  90. $sortOrder ?? ISearchQuery::SORT_DATE_DESC,
  91. $limit ?? SearchQuery::LIMIT_DEFAULT,
  92. $cursor,
  93. $route,
  94. $routeParameters
  95. )
  96. )
  97. );
  98. }
  99. protected function getRouteInformation(string $url): array {
  100. $routeStr = '';
  101. $parameters = [];
  102. if ($url !== '') {
  103. $urlParts = parse_url($url);
  104. $urlPath = $urlParts['path'];
  105. // Optionally strip webroot from URL. Required for route matching on setups
  106. // with Nextcloud in a webserver subfolder (webroot).
  107. $webroot = $this->urlGenerator->getWebroot();
  108. if ($webroot !== '' && substr($urlPath, 0, strlen($webroot)) === $webroot) {
  109. $urlPath = substr($urlPath, strlen($webroot));
  110. }
  111. try {
  112. $parameters = $this->router->findMatchingRoute($urlPath);
  113. // contacts.PageController.index => contacts.Page.index
  114. $route = $parameters['caller'];
  115. if (substr($route[1], -10) === 'Controller') {
  116. $route[1] = substr($route[1], 0, -10);
  117. }
  118. $routeStr = implode('.', $route);
  119. // cleanup
  120. unset($parameters['_route'], $parameters['action'], $parameters['caller']);
  121. } catch (ResourceNotFoundException $exception) {
  122. }
  123. if (isset($urlParts['query'])) {
  124. parse_str($urlParts['query'], $queryParameters);
  125. $parameters = array_merge($parameters, $queryParameters);
  126. }
  127. }
  128. return [
  129. $routeStr,
  130. $parameters,
  131. ];
  132. }
  133. }