diff options
author | Robin Appelman <icewind1991@gmail.com> | 2011-04-24 16:09:07 +0200 |
---|---|---|
committer | Robin Appelman <icewind1991@gmail.com> | 2011-04-24 16:09:27 +0200 |
commit | b7aba15f179125455298aaee411864ebf4ed86f9 (patch) | |
tree | 781cd833dfb6fa18eeb89967f3094dcf0fa5a33e /lib | |
parent | 8d52fdb7faf4be4ed4282ecdd3e3a08af1fbf150 (diff) | |
download | nextcloud-server-b7aba15f179125455298aaee411864ebf4ed86f9.tar.gz nextcloud-server-b7aba15f179125455298aaee411864ebf4ed86f9.zip |
add search functionality, for now only searches files but plugins/apps can extend that
Diffstat (limited to 'lib')
-rw-r--r-- | lib/base.php | 1 | ||||
-rw-r--r-- | lib/filestorage.php | 21 | ||||
-rw-r--r-- | lib/filesystem.php | 15 | ||||
-rw-r--r-- | lib/search.php | 121 | ||||
-rw-r--r-- | lib/template.php | 6 |
5 files changed, 163 insertions, 1 deletions
diff --git a/lib/base.php b/lib/base.php index 3f1bb6f3149..10d59a9d0d6 100644 --- a/lib/base.php +++ b/lib/base.php @@ -88,6 +88,7 @@ require_once('ocsclient.php'); require_once('connect.php'); require_once('remotestorage.php'); require_once('plugin.php'); +require_once('search.php'); $error=(count(OC_UTIL::checkServer())>0); diff --git a/lib/filestorage.php b/lib/filestorage.php index 3d0bdf4cc0e..429961b3d69 100644 --- a/lib/filestorage.php +++ b/lib/filestorage.php @@ -62,6 +62,7 @@ class OC_FILESTORAGE{ public function getTree($path){} public function hash($type,$path,$raw){} public function free_space($path){} + public function search($query){} } @@ -468,7 +469,25 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{ public function free_space($path){ return disk_free_space($this->datadir.$path); } - + + public function search($query){ + return $this->searchInDir($query); + } + + private function searchInDir($query,$dir=''){ + $files=array(); + foreach (scandir($this->datadir.$dir) as $item) { + if ($item == '.' || $item == '..') continue; + if(strstr(strtolower($item),strtolower($query))!==false){ + $files[]=$dir.'/'.$item; + } + if(is_dir($this->datadir.$dir.'/'.$item)){ + $files=array_merge($files,$this->searchInDir($query,$dir.'/'.$item)); + } + } + return $files; + } + /** * @brief get the size of folder and it's content * @param string $path file path diff --git a/lib/filesystem.php b/lib/filesystem.php index 03fbecdc4c0..2b5c3a56b6e 100644 --- a/lib/filesystem.php +++ b/lib/filesystem.php @@ -457,5 +457,20 @@ class OC_FILESYSTEM{ return $storage->free_space($path); } } + + static public function search($query){ + $files=array(); + $fakeRootLength=strlen(self::$fakeRoot); + foreach(self::$storages as $mountpoint=>$storage){ + $results=$storage->search($query); + foreach($results as $result){ + $file=str_replace('//','/',$mountpoint.$result); + $file=substr($file,$fakeRootLength); + $files[]=$file; + } + } + return $files; + + } } ?> diff --git a/lib/search.php b/lib/search.php new file mode 100644 index 00000000000..ef82e225f3d --- /dev/null +++ b/lib/search.php @@ -0,0 +1,121 @@ +<?php +/** + * ownCloud + * + * @author Frank Karlitschek + * @copyright 2010 Frank Karlitschek karlitschek@kde.org + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ + + +/** + * provides an interface to all search providers + */ +class OC_SEARCH{ + static private $providers=array(); + + /** + * register a new search provider to be used + * @param OC_SearchProvider $provider + */ + public static function registerProvider($provider){ + self::$providers[]=$provider; + } + + /** + * search all provider for $query + * @param string query + * @return array An array of OC_SearchResult's + */ + public static function search($query){ + $results=array(); + foreach(self::$providers as $provider){ + $results=array_merge($results,$provider->search($query)); + } + return $results; + } +} + +/** + * provides search functionalty + */ +abstract class OC_SearchProvider{ + public function __construct(){ + OC_SEARCH::registerProvider($this); + } + + /** + * search for $query + * @param string $query + * @return array An array of OC_SearchResult's + */ + abstract function search($query); +} + +/** + * a result of a search + */ +class OC_SearchResult{ + private $name; + private $text; + private $link; + private $type; + + /** + * create a new search result + * @param string $name short name for the result + * @param string $text some more information about the result + * @param string $link link for the result + * @param string $type the type of result as human readable string ('File', 'Music', etc) + */ + public function __construct($name,$text,$link,$type){ + $this->name=$name; + $this->text=$text; + $this->link=$link; + $this->type=$type; + } + + public function __get($name){ + switch($name){ + case 'name': + return $this->name; + case 'text': + return $this->text; + case 'link': + return $this->link; + case 'type': + return $this->type; + } + } +} + +class OC_FileSearchProvider extends OC_SearchProvider{ + function search($query){ + $files=OC_FILESYSTEM::search($query); + $results=array(); + foreach($files as $file){ + if(OC_FILESYSTEM::is_dir($file)){ + $results[]=new OC_SearchResult(basename($file),$file,OC_HELPER::linkTo( 'files', 'index.php?dir='.$file ),'Files'); + }else{ + $results[]=new OC_SearchResult(basename($file),$file,OC_HELPER::linkTo( 'files', 'download.php?file='.$file ),'Files'); + } + } + return $results; + } +} + +new OC_FileSearchProvider(); +?>
\ No newline at end of file diff --git a/lib/template.php b/lib/template.php index 75e4303ff6a..0d6776aa26d 100644 --- a/lib/template.php +++ b/lib/template.php @@ -190,6 +190,9 @@ class OC_TEMPLATE{ if( $this->renderas == "user" ) { $page = new OC_TEMPLATE( "core", "layout.user" ); + $search=new OC_TEMPLATE( 'core', 'part.searchbox'); + $search->assign('searchurl',OC_HELPER::linkTo( 'search', 'index.php' )); + $page->assign('searchbox', $search->fetchPage()); // Add menu data // Add navigation entry @@ -198,6 +201,9 @@ class OC_TEMPLATE{ elseif( $this->renderas == "admin" ) { $page = new OC_TEMPLATE( "core", "layout.admin" ); + $search=new OC_TEMPLATE( 'core', 'part.searchbox'); + $search->assign('searchurl',OC_HELPER::linkTo( 'search', 'index.php' )); + $page->assign('searchbox', $search->fetchPage()); // Add menu data if( OC_GROUP::inGroup( $_SESSION["user_id"], "admin" )){ $page->assign( "settingsnavigation", OC_APP::getSettingsNavigation()); |