search.php 3.9 KB

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