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