UnifiedSearchController.php 3.7 KB

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