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.9KB

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