SearchController.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OCP\AppFramework\Controller;
  9. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  10. use OCP\AppFramework\Http\JSONResponse;
  11. use OCP\IRequest;
  12. use OCP\ISearch;
  13. use OCP\Search\Result;
  14. use Psr\Log\LoggerInterface;
  15. class SearchController extends Controller {
  16. public function __construct(
  17. string $appName,
  18. IRequest $request,
  19. private ISearch $searcher,
  20. private LoggerInterface $logger,
  21. ) {
  22. parent::__construct($appName, $request);
  23. }
  24. /**
  25. * @NoAdminRequired
  26. */
  27. #[FrontpageRoute(verb: 'GET', url: '/core/search')]
  28. public function search(string $query, array $inApps = [], int $page = 1, int $size = 30): JSONResponse {
  29. $results = $this->searcher->searchPaged($query, $inApps, $page, $size);
  30. $results = array_filter($results, function (Result $result) {
  31. if (json_encode($result, JSON_HEX_TAG) === false) {
  32. $this->logger->warning("Skipping search result due to invalid encoding: {type: " . $result->type . ", id: " . $result->id . "}");
  33. return false;
  34. } else {
  35. return true;
  36. }
  37. });
  38. return new JSONResponse($results);
  39. }
  40. }