1
0

Search.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use OCP\ISearch;
  9. use OCP\Search\PagedProvider;
  10. use OCP\Search\Provider;
  11. use Psr\Log\LoggerInterface;
  12. /**
  13. * Provide an interface to all search providers
  14. */
  15. class Search implements ISearch {
  16. /** @var Provider[] */
  17. private $providers = [];
  18. private $registeredProviders = [];
  19. /**
  20. * Search all providers for $query
  21. * @param string $query
  22. * @param string[] $inApps optionally limit results to the given apps
  23. * @param int $page pages start at page 1
  24. * @param int $size, 0 = all
  25. * @return array An array of OC\Search\Result's
  26. */
  27. public function searchPaged($query, array $inApps = [], $page = 1, $size = 30) {
  28. $this->initProviders();
  29. $results = [];
  30. foreach ($this->providers as $provider) {
  31. if (! $provider->providesResultsFor($inApps)) {
  32. continue;
  33. }
  34. if ($provider instanceof PagedProvider) {
  35. $results = array_merge($results, $provider->searchPaged($query, $page, $size));
  36. } elseif ($provider instanceof Provider) {
  37. $providerResults = $provider->search($query);
  38. if ($size > 0) {
  39. $slicedResults = array_slice($providerResults, ($page - 1) * $size, $size);
  40. $results = array_merge($results, $slicedResults);
  41. } else {
  42. $results = array_merge($results, $providerResults);
  43. }
  44. } else {
  45. \OCP\Server::get(LoggerInterface::class)->warning('Ignoring Unknown search provider', ['provider' => $provider]);
  46. }
  47. }
  48. return $results;
  49. }
  50. /**
  51. * Remove all registered search providers
  52. */
  53. public function clearProviders() {
  54. $this->providers = [];
  55. $this->registeredProviders = [];
  56. }
  57. /**
  58. * Remove one existing search provider
  59. * @param string $provider class name of a OC\Search\Provider
  60. */
  61. public function removeProvider($provider) {
  62. $this->registeredProviders = array_filter(
  63. $this->registeredProviders,
  64. function ($element) use ($provider) {
  65. return ($element['class'] != $provider);
  66. }
  67. );
  68. // force regeneration of providers on next search
  69. $this->providers = [];
  70. }
  71. /**
  72. * Register a new search provider to search with
  73. * @param string $class class name of a OC\Search\Provider
  74. * @param array $options optional
  75. */
  76. public function registerProvider($class, array $options = []) {
  77. $this->registeredProviders[] = ['class' => $class, 'options' => $options];
  78. }
  79. /**
  80. * Create instances of all the registered search providers
  81. */
  82. private function initProviders() {
  83. if (! empty($this->providers)) {
  84. return;
  85. }
  86. foreach ($this->registeredProviders as $provider) {
  87. $class = $provider['class'];
  88. $options = $provider['options'];
  89. $this->providers[] = new $class($options);
  90. }
  91. }
  92. }