search.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Provider;
  24. use OCP\ISearch;
  25. /**
  26. * Provide an interface to all search providers
  27. */
  28. class Search implements ISearch {
  29. private $providers = array();
  30. private $registeredProviders = array();
  31. /**
  32. * Search all providers for $query
  33. * @param string $query
  34. * @return array An array of OC\Search\Result's
  35. */
  36. public function search($query) {
  37. $this->initProviders();
  38. $results = array();
  39. foreach($this->providers as $provider) {
  40. /** @var $provider Provider */
  41. $results = array_merge($results, $provider->search($query));
  42. }
  43. return $results;
  44. }
  45. /**
  46. * Remove all registered search providers
  47. */
  48. public function clearProviders() {
  49. $this->providers=array();
  50. $this->registeredProviders=array();
  51. }
  52. /**
  53. * Remove one existing search provider
  54. * @param string $provider class name of a OC\Search\Provider
  55. */
  56. public function removeProvider($provider) {
  57. $this->registeredProviders = array_filter(
  58. $this->registeredProviders,
  59. function ($element) use ($provider) {
  60. return ($element['class'] != $provider);
  61. }
  62. );
  63. // force regeneration of providers on next search
  64. $this->providers=array();
  65. }
  66. /**
  67. * Register a new search provider to search with
  68. * @param string $class class name of a OC\Search\Provider
  69. * @param array $options optional
  70. */
  71. public function registerProvider($class, $options=array()) {
  72. $this->registeredProviders[]=array('class'=>$class, 'options'=>$options);
  73. }
  74. /**
  75. * Create instances of all the registered search providers
  76. */
  77. private function initProviders() {
  78. if(count($this->providers)>0) {
  79. return;
  80. }
  81. foreach($this->registeredProviders as $provider) {
  82. $class = $provider['class'];
  83. $options = $provider['options'];
  84. $this->providers[]=new $class($options);
  85. }
  86. }
  87. }