search.php 3.7 KB

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