search.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /**
  23. * provides an interface to all search providers
  24. */
  25. class OC_Search{
  26. static private $providers=array();
  27. static private $registeredProviders=array();
  28. /**
  29. * remove all registered search providers
  30. */
  31. public static function clearProviders() {
  32. self::$providers=array();
  33. self::$registeredProviders=array();
  34. }
  35. /**
  36. * register a new search provider to be used
  37. * @param string $provider class name of a OC_Search_Provider
  38. */
  39. public static function registerProvider($class, $options=array()) {
  40. self::$registeredProviders[]=array('class'=>$class, 'options'=>$options);
  41. }
  42. /**
  43. * search all provider for $query
  44. * @param string query
  45. * @return array An array of OC_Search_Result's
  46. */
  47. public static function search($query) {
  48. self::initProviders();
  49. $results=array();
  50. foreach(self::$providers as $provider) {
  51. $results=array_merge($results, $provider->search($query));
  52. }
  53. return $results;
  54. }
  55. /**
  56. * remove an existing search provider
  57. * @param string $provider class name of a OC_Search_Provider
  58. */
  59. public static function removeProvider($provider) {
  60. self::$registeredProviders = array_filter(
  61. self::$registeredProviders,
  62. function ($element) use ($provider) {
  63. return ($element['class'] != $provider);
  64. }
  65. );
  66. // force regeneration of providers on next search
  67. self::$providers=array();
  68. }
  69. /**
  70. * create instances of all the registered search providers
  71. */
  72. private static function initProviders() {
  73. if(count(self::$providers)>0) {
  74. return;
  75. }
  76. foreach(self::$registeredProviders as $provider) {
  77. $class=$provider['class'];
  78. $options=$provider['options'];
  79. self::$providers[]=new $class($options);
  80. }
  81. }
  82. }