improve flexibility of search providers a bit

This commit is contained in:
Robin Appelman 2012-04-14 11:29:44 +02:00
parent 8ed4606685
commit 3babb8c22c
8 changed files with 43 additions and 17 deletions

View File

@ -20,8 +20,8 @@
*
*/
class OC_Search_Provider_Bookmarks implements OC_Search_Provider{
static function search($query){
class OC_Search_Provider_Bookmarks extends OC_Search_Provider{
function search($query){
$results=array();
$offset = 0;

View File

@ -1,6 +1,6 @@
<?php
class OC_Search_Provider_Calendar implements OC_Search_Provider{
static function search($query){
class OC_Search_Provider_Calendar extends OC_Search_Provider{
function search($query){
$calendars = OC_Calendar_Calendar::allCalendars(OC_User::getUser(), 1);
if(count($calendars)==0 || !OC_App::isEnabled('calendar')){
//return false;

View File

@ -1,6 +1,6 @@
<?php
class OC_Search_Provider_Contacts implements OC_Search_Provider{
static function search($query){
class OC_Search_Provider_Contacts extends OC_Search_Provider{
function search($query){
$addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser(), 1);
// if(count($calendars)==0 || !OC_App::isEnabled('contacts')){
// //return false;

View File

@ -41,8 +41,8 @@ OC_App::addNavigationEntry( array(
'icon' => OC_Helper::imagePath('core', 'places/picture.svg'),
'name' => $l->t('Pictures')));
class OC_GallerySearchProvider implements OC_Search_Provider{
static function search($query){
class OC_GallerySearchProvider extends OC_Search_Provider{
function search($query){
$stmt = OC_DB::prepare('SELECT * FROM *PREFIX*gallery_albums WHERE uid_owner = ? AND album_name LIKE ?');
$result = $stmt->execute(array(OC_User::getUser(),'%'.$query.'%'));
$results=array();

View File

@ -82,8 +82,8 @@ class OC_MEDIA{
}
}
class OC_MediaSearchProvider implements OC_Search_Provider{
static function search($query){
class OC_MediaSearchProvider extends OC_Search_Provider{
function search($query){
require_once('lib_collection.php');
$artists=OC_MEDIA_COLLECTION::getArtists($query);
$albums=OC_MEDIA_COLLECTION::getAlbums(0,$query);

View File

@ -26,13 +26,22 @@
*/
class OC_Search{
static private $providers=array();
static private $registeredProviders=array();
/**
* remove all registered search providers
*/
public static function clearProviders(){
self::$providers=array();
self::$registeredProviders=array();
}
/**
* register a new search provider to be used
* @param string $provider class name of a OC_Search_Provider
*/
public static function registerProvider($provider){
self::$providers[]=$provider;
public static function registerProvider($class,$options=array()){
self::$registeredProviders[]=array('class'=>$class,'options'=>$options);
}
/**
@ -41,10 +50,25 @@ class OC_Search{
* @return array An array of OC_Search_Result's
*/
public static function search($query){
self::initProviders();
$results=array();
foreach(self::$providers as $provider){
$results=array_merge($results, $provider::search($query));
$results=array_merge($results, $provider->search($query));
}
return $results;
}
/**
* create instances of all the registered search providers
*/
private static function initProviders(){
if(count(self::$providers)>0){
return;
}
foreach(self::$registeredProviders as $provider){
$class=$provider['class'];
$options=$provider['options'];
self::$providers[]=new $class($options);
}
}
}

View File

@ -2,11 +2,13 @@
/**
* provides search functionalty
*/
interface OC_Search_Provider {
class OC_Search_Provider {
public function __construct($options){}
/**
* search for $query
* @param string $query
* @return array An array of OC_Search_Result's
*/
static function search($query);
public function search($query){}
}

View File

@ -1,7 +1,7 @@
<?php
class OC_Search_Provider_File implements OC_Search_Provider{
static function search($query){
class OC_Search_Provider_File extends OC_Search_Provider{
function search($query){
$files=OC_FileCache::search($query,true);
$results=array();
foreach($files as $fileData){