]> source.dussan.org Git - nextcloud-server.git/commitdiff
add search functionality, for now only searches files but plugins/apps can extend...
authorRobin Appelman <icewind1991@gmail.com>
Sun, 24 Apr 2011 14:09:07 +0000 (16:09 +0200)
committerRobin Appelman <icewind1991@gmail.com>
Sun, 24 Apr 2011 14:09:27 +0000 (16:09 +0200)
13 files changed:
css/styles.css
lib/base.php
lib/filestorage.php
lib/filesystem.php
lib/search.php [new file with mode: 0644]
lib/template.php
search/appinfo/app.php [new file with mode: 0644]
search/css/search.css [new file with mode: 0644]
search/index.php [new file with mode: 0644]
search/templates/index.php [new file with mode: 0644]
templates/layout.admin.php
templates/layout.user.php
templates/part.searchbox.php [new file with mode: 0644]

index 6b86684354103b0d395bfc1896c789d5d26c0652..b737181d01bc695cce88332782984c88d7f84f2c 100644 (file)
@@ -130,3 +130,7 @@ p.actions a.delete, div.actions a.delete { background-image:url('../img/delete.p
 #logs_options input[type="submit"] { float:right; margin:0 2em 0 0; }
 #logs_options input[type="text"] { margin:0; padding:0; border:1px solid #ccc; text-align:right; }
 li.error{ list-style:none; width:640px; margin:4em auto; padding:1em 1em 1em 4em; background-color:#fee; background-image:url('../img/task-attention.png'); background-position:0.8em 0.8em; background-repeat:no-repeat; border:1px solid #ccc; -moz-border-radius:10px; -webkit-border-radius:10px; border-radius:10px; }
+
+/* SEARCH --------------------------------------------------------------------- */
+form.searchbox{display:inline; position:fixed; top:1.5em; right:9em; margin:0; padding:0;};
+form.searchbox .prettybutton{font-size:1.5em !important};
\ No newline at end of file
index 3f1bb6f31498a6e50cd6a239e9c34be11d0e55f2..10d59a9d0d624bbdc0dc8fe92fd2714fa126684d 100644 (file)
@@ -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);
 
index 3d0bdf4cc0ed0653d30164dadc21c96fb2d2cedc..429961b3d693400741eb7214514e5b9d2865bf00 100644 (file)
@@ -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
index 03fbecdc4c04a578f0bbbd83d5b7653a7ad056a1..2b5c3a56b6e3e90574649e8286fef6e484fad4f5 100644 (file)
@@ -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 (file)
index 0000000..ef82e22
--- /dev/null
@@ -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
index 75e4303ff6ae4851b28630af4031a56e10bff65a..0d6776aa26d22230fd6038daa6b760d226c5fe46 100644 (file)
@@ -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());
diff --git a/search/appinfo/app.php b/search/appinfo/app.php
new file mode 100644 (file)
index 0000000..4483449
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+
+OC_APP::register( array( 'order' => 2, "id" => 'search', 'name' => 'Search' ));
+
+?>
diff --git a/search/css/search.css b/search/css/search.css
new file mode 100644 (file)
index 0000000..df0712b
--- /dev/null
@@ -0,0 +1,17 @@
+#searchresults{
+       margin: 2em;
+       list-style:none;
+       border: solid 1px #CCC;
+}
+
+#searchresults li.resultHeader{
+       font-size:1.2em;
+       font-weight:bold;
+       border-bottom: solid 1px #CCC;
+       padding:0.2em;
+       background-color:#eee;
+}
+
+#searchresults li.result{
+       margin-left:2em;
+}
\ No newline at end of file
diff --git a/search/index.php b/search/index.php
new file mode 100644 (file)
index 0000000..e6f4152
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+/**
+* ownCloud - ajax frontend
+*
+* @author Robin Appelman
+* @copyright 2010 Robin Appelman icewind1991@gmail.com
+*
+* 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/>.
+*
+*/
+
+
+// Init owncloud
+require_once('../lib/base.php');
+require( 'template.php' );
+
+// Check if we are a user
+if( !OC_USER::isLoggedIn()){
+       header( "Location: ".OC_HELPER::linkTo( '', 'index.php' ));
+       exit();
+}
+
+// Load the files we need
+OC_UTIL::addStyle( 'search', 'search' );
+
+$query=(isset($_POST['query']))?$_POST['query']:'';
+if($query){
+       $results=OC_SEARCH::search($query);
+}
+
+$resultTypes=array();
+foreach($results as $result){
+       if(!isset($resultTypes[$result->type])){
+               $resultTypes[$result->type]=array();
+       }
+       $resultTypes[$result->type][]=$result;
+}
+
+$tmpl = new OC_TEMPLATE( 'search', 'index', 'user' );
+$tmpl->assign('resultTypes',$resultTypes);
+$tmpl->printPage();
+
+?>
diff --git a/search/templates/index.php b/search/templates/index.php
new file mode 100644 (file)
index 0000000..7241553
--- /dev/null
@@ -0,0 +1,17 @@
+<ul id='searchresults'>
+       <?php foreach($_['resultTypes'] as $resultType):?>
+               <li class='resultHeader'>
+                       <p><?php echo $resultType[0]->type?></p>
+               </li>
+               <?php foreach($resultType as $result):?>
+                       <li class='result'>
+                               <p>
+                                       <a href='<?php echo $result->link?>' title='<?php echo $result->name?>'><?php echo $result->name?></a>
+                               </p>
+                               <p>
+                                       <?php echo $result->text?>
+                               </p>
+                       </li>
+               <?php endforeach;?>
+       <?php endforeach;?>
+</ul>
index 0212419a9522e7ee1dc5e72fcbb7746c2c16ac01..efd9a8b6fdb5d2ef71ae7899b158ae0bd405a073 100644 (file)
@@ -15,7 +15,7 @@
        <body id="body-settings">
                <div id="header">
                        <a href="<?php echo link_to('', 'index.php'); ?>" title="" id="owncloud"><img src="<?php echo image_path('', 'owncloud-logo-small-white.png'); ?>" alt="ownCloud" /></a>
-
+                       <?php echo $_['searchbox']?>
                        <ul id="metanav">
                                <li><a href="<?php echo link_to('', 'index.php'); ?>" title="Back to files"><img src="<?php echo image_path('', 'layout/back.png'); ?>"></a></li>
                                <li><a href="<?php echo link_to('', 'index.php?logout=true'); ?>" title="Log out"><img src="<?php echo image_path('', 'layout/logout.png'); ?>"></a></li>
index 4e9190d91ff44a0a0a14c1cd1f8505db59147fe5..23210b6880875153a23bf4428f482def5fc6ba99 100644 (file)
@@ -15,7 +15,7 @@
        <body id="body-user">
                <div id="header">
                        <a href="<?php echo link_to('', 'index.php'); ?>" title="" id="owncloud"><img src="<?php echo image_path('', 'owncloud-logo-small-white.png'); ?>" alt="ownCloud" /></a>
-
+                       <?php echo $_['searchbox']?>
                        <ul id="metanav">
                                <li><a href="<?php echo link_to('settings', 'index.php'); ?>" title="Settings"><img src="<?php echo image_path('', 'layout/settings.png'); ?>"></a></li>
                                <li><a href="<?php echo link_to('', 'index.php'); ?>?logout=true" title="Log out"><img src="<?php echo image_path('', 'layout/logout.png'); ?>"></a></li>
diff --git a/templates/part.searchbox.php b/templates/part.searchbox.php
new file mode 100644 (file)
index 0000000..910af81
--- /dev/null
@@ -0,0 +1,4 @@
+<form class='searchbox' action='<?php echo $_['searchurl']?>' method='post'>
+       <input name='query' value='<?php if(isset($_POST['query'])){echo $_POST['query'];};?>'/>
+       <input type='submit' value='Search' class='prettybutton'/>
+</form>
\ No newline at end of file