You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Search.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andrew Brown <andrew@casabrown.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC;
  29. use OCP\ISearch;
  30. use OCP\Search\PagedProvider;
  31. use OCP\Search\Provider;
  32. use Psr\Log\LoggerInterface;
  33. /**
  34. * Provide an interface to all search providers
  35. */
  36. class Search implements ISearch {
  37. /** @var Provider[] */
  38. private $providers = [];
  39. private $registeredProviders = [];
  40. /**
  41. * Search all providers for $query
  42. * @param string $query
  43. * @param string[] $inApps optionally limit results to the given apps
  44. * @param int $page pages start at page 1
  45. * @param int $size, 0 = all
  46. * @return array An array of OC\Search\Result's
  47. */
  48. public function searchPaged($query, array $inApps = [], $page = 1, $size = 30) {
  49. $this->initProviders();
  50. $results = [];
  51. foreach ($this->providers as $provider) {
  52. if (! $provider->providesResultsFor($inApps)) {
  53. continue;
  54. }
  55. if ($provider instanceof PagedProvider) {
  56. $results = array_merge($results, $provider->searchPaged($query, $page, $size));
  57. } elseif ($provider instanceof Provider) {
  58. $providerResults = $provider->search($query);
  59. if ($size > 0) {
  60. $slicedResults = array_slice($providerResults, ($page - 1) * $size, $size);
  61. $results = array_merge($results, $slicedResults);
  62. } else {
  63. $results = array_merge($results, $providerResults);
  64. }
  65. } else {
  66. \OCP\Server::get(LoggerInterface::class)->warning('Ignoring Unknown search provider', ['provider' => $provider]);
  67. }
  68. }
  69. return $results;
  70. }
  71. /**
  72. * Remove all registered search providers
  73. */
  74. public function clearProviders() {
  75. $this->providers = [];
  76. $this->registeredProviders = [];
  77. }
  78. /**
  79. * Remove one existing search provider
  80. * @param string $provider class name of a OC\Search\Provider
  81. */
  82. public function removeProvider($provider) {
  83. $this->registeredProviders = array_filter(
  84. $this->registeredProviders,
  85. function ($element) use ($provider) {
  86. return ($element['class'] != $provider);
  87. }
  88. );
  89. // force regeneration of providers on next search
  90. $this->providers = [];
  91. }
  92. /**
  93. * Register a new search provider to search with
  94. * @param string $class class name of a OC\Search\Provider
  95. * @param array $options optional
  96. */
  97. public function registerProvider($class, array $options = []) {
  98. $this->registeredProviders[] = ['class' => $class, 'options' => $options];
  99. }
  100. /**
  101. * Create instances of all the registered search providers
  102. */
  103. private function initProviders() {
  104. if (! empty($this->providers)) {
  105. return;
  106. }
  107. foreach ($this->registeredProviders as $provider) {
  108. $class = $provider['class'];
  109. $options = $provider['options'];
  110. $this->providers[] = new $class($options);
  111. }
  112. }
  113. }