1
0

Search.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /**
  33. * Provide an interface to all search providers
  34. */
  35. class Search implements ISearch {
  36. /** @var Provider[] */
  37. private $providers = [];
  38. private $registeredProviders = [];
  39. /**
  40. * Search all providers for $query
  41. * @param string $query
  42. * @param string[] $inApps optionally limit results to the given apps
  43. * @param int $page pages start at page 1
  44. * @param int $size, 0 = all
  45. * @return array An array of OC\Search\Result's
  46. */
  47. public function searchPaged($query, array $inApps = [], $page = 1, $size = 30) {
  48. $this->initProviders();
  49. $results = [];
  50. foreach ($this->providers as $provider) {
  51. if (! $provider->providesResultsFor($inApps)) {
  52. continue;
  53. }
  54. if ($provider instanceof PagedProvider) {
  55. $results = array_merge($results, $provider->searchPaged($query, $page, $size));
  56. } elseif ($provider instanceof Provider) {
  57. $providerResults = $provider->search($query);
  58. if ($size > 0) {
  59. $slicedResults = array_slice($providerResults, ($page - 1) * $size, $size);
  60. $results = array_merge($results, $slicedResults);
  61. } else {
  62. $results = array_merge($results, $providerResults);
  63. }
  64. } else {
  65. \OC::$server->getLogger()->warning('Ignoring Unknown search provider', ['provider' => $provider]);
  66. }
  67. }
  68. return $results;
  69. }
  70. /**
  71. * Remove all registered search providers
  72. */
  73. public function clearProviders() {
  74. $this->providers = [];
  75. $this->registeredProviders = [];
  76. }
  77. /**
  78. * Remove one existing search provider
  79. * @param string $provider class name of a OC\Search\Provider
  80. */
  81. public function removeProvider($provider) {
  82. $this->registeredProviders = array_filter(
  83. $this->registeredProviders,
  84. function ($element) use ($provider) {
  85. return ($element['class'] != $provider);
  86. }
  87. );
  88. // force regeneration of providers on next search
  89. $this->providers = [];
  90. }
  91. /**
  92. * Register a new search provider to search with
  93. * @param string $class class name of a OC\Search\Provider
  94. * @param array $options optional
  95. */
  96. public function registerProvider($class, array $options = []) {
  97. $this->registeredProviders[] = ['class' => $class, 'options' => $options];
  98. }
  99. /**
  100. * Create instances of all the registered search providers
  101. */
  102. private function initProviders() {
  103. if (! empty($this->providers)) {
  104. return;
  105. }
  106. foreach ($this->registeredProviders as $provider) {
  107. $class = $provider['class'];
  108. $options = $provider['options'];
  109. $this->providers[] = new $class($options);
  110. }
  111. }
  112. }