UnifiedSearchController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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) <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\IUserSession;
  34. use OCP\Route\IRouter;
  35. use OCP\Search\ISearchQuery;
  36. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  37. class UnifiedSearchController extends OCSController {
  38. /** @var SearchComposer */
  39. private $composer;
  40. /** @var IUserSession */
  41. private $userSession;
  42. /** @var IRouter */
  43. private $router;
  44. public function __construct(IRequest $request,
  45. IUserSession $userSession,
  46. SearchComposer $composer,
  47. IRouter $router) {
  48. parent::__construct('core', $request);
  49. $this->composer = $composer;
  50. $this->userSession = $userSession;
  51. $this->router = $router;
  52. }
  53. /**
  54. * @NoAdminRequired
  55. * @NoCSRFRequired
  56. *
  57. * @param string $from the url the user is currently at
  58. *
  59. * @return DataResponse
  60. */
  61. public function getProviders(string $from = ''): DataResponse {
  62. [$route, $parameters] = $this->getRouteInformation($from);
  63. $result = $this->composer->getProviders($route, $parameters);
  64. $response = new DataResponse($result);
  65. $response->setETag(md5(json_encode($result)));
  66. return $response;
  67. }
  68. /**
  69. * @NoAdminRequired
  70. * @NoCSRFRequired
  71. *
  72. * @param string $providerId
  73. * @param string $term
  74. * @param int|null $sortOrder
  75. * @param int|null $limit
  76. * @param int|string|null $cursor
  77. * @param string $from
  78. *
  79. * @return DataResponse
  80. */
  81. public function search(string $providerId,
  82. string $term = '',
  83. ?int $sortOrder = null,
  84. ?int $limit = null,
  85. $cursor = null,
  86. string $from = ''): DataResponse {
  87. if (empty(trim($term))) {
  88. return new DataResponse(null, Http::STATUS_BAD_REQUEST);
  89. }
  90. [$route, $routeParameters] = $this->getRouteInformation($from);
  91. return new DataResponse(
  92. $this->composer->search(
  93. $this->userSession->getUser(),
  94. $providerId,
  95. new SearchQuery(
  96. $term,
  97. $sortOrder ?? ISearchQuery::SORT_DATE_DESC,
  98. $limit ?? SearchQuery::LIMIT_DEFAULT,
  99. $cursor,
  100. $route,
  101. $routeParameters
  102. )
  103. )
  104. );
  105. }
  106. protected function getRouteInformation(string $url): array {
  107. $routeStr = '';
  108. $parameters = [];
  109. if ($url !== '') {
  110. $urlParts = parse_url($url);
  111. try {
  112. $parameters = $this->router->findMatchingRoute($urlParts['path']);
  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. }