From ac72828eaea4fbc428082bcb6af8daa43867137b Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Thu, 22 Aug 2013 23:04:06 -0400 Subject: [PATCH 01/17] Move to new namespace convention and add new result sub-classing --- lib/private/search.php | 48 ++++++------ lib/private/search/provider.php | 55 ++++++++++---- lib/private/search/provider/file.php | 97 +++++++++++++----------- lib/private/search/result.php | 82 ++++++++++++++------ lib/search/result/file.php | 109 +++++++++++++++++++++++++++ lib/search/result/folder.php | 32 ++++++++ 6 files changed, 323 insertions(+), 100 deletions(-) create mode 100644 lib/search/result/file.php create mode 100644 lib/search/result/folder.php diff --git a/lib/private/search.php b/lib/private/search.php index 3f540090fdd..f3ade2683ce 100644 --- a/lib/private/search.php +++ b/lib/private/search.php @@ -20,33 +20,20 @@ * */ +namespace OC; /** - * provides an interface to all search providers + * Provide an interface to all search providers */ -class OC_Search{ +class 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 - */ - public static function registerProvider($class, $options=array()) { - self::$registeredProviders[]=array('class'=>$class, 'options'=>$options); - } - - /** - * search all provider for $query - * @param string $query - * @return array An array of OC_Search_Result's + * Search all providers for $query + * @param string query + * @return array An array of OC\Search\Result's */ public static function search($query) { self::initProviders(); @@ -58,8 +45,16 @@ class OC_Search{ } /** - * remove an existing search provider - * @param string $provider class name of a OC_Search_Provider + * Remove all registered search providers + */ + public static function clearProviders() { + self::$providers=array(); + self::$registeredProviders=array(); + } + + /** + * Remove one existing search provider + * @param string $provider class name of a OC\Search\Provider */ public static function removeProvider($provider) { self::$registeredProviders = array_filter( @@ -72,9 +67,16 @@ class OC_Search{ self::$providers=array(); } + /** + * Register a new search provider to search with + * @param string $provider class name of a OC\Search\Provider + */ + public static function registerProvider($class, $options=array()) { + self::$registeredProviders[]=array('class'=>$class, 'options'=>$options); + } /** - * create instances of all the registered search providers + * Create instances of all the registered search providers */ private static function initProviders() { if(count(self::$providers)>0) { diff --git a/lib/private/search/provider.php b/lib/private/search/provider.php index b617b9c5d94..e45d3790c3f 100644 --- a/lib/private/search/provider.php +++ b/lib/private/search/provider.php @@ -1,18 +1,47 @@ . + * */ -abstract class OC_Search_Provider { - private $options; - public function __construct($options) { - $this->options=$options; - } +namespace OC\Search; - /** - * search for $query - * @param string $query - * @return array An array of OC_Search_Result's - */ - abstract public function search($query); -} +/** + * Provides a template for search functionality throughout ownCloud; + */ +abstract class Provider { + + /** + * List of options (currently unused) + * @var array + */ + private $options; + + /** + * Constructor + * @param array $options + */ + public function __construct($options) { + $this->options = $options; + } + + /** + * Search for $query + * @param string $query + * @return array An array of OC\Search\Result's + */ + abstract public function search($query); +} \ No newline at end of file diff --git a/lib/private/search/provider/file.php b/lib/private/search/provider/file.php index 9bd50931517..5e3fb83721e 100644 --- a/lib/private/search/provider/file.php +++ b/lib/private/search/provider/file.php @@ -1,46 +1,59 @@ . + * + */ -class OC_Search_Provider_File extends OC_Search_Provider{ - function search($query) { - $files=\OC\Files\Filesystem::search($query, true); - $results=array(); - $l=OC_L10N::get('lib'); - foreach($files as $fileData) { - $path = $fileData['path']; - $mime = $fileData['mimetype']; +namespace OC\Search\Provider; - $name = basename($path); - $container = dirname($path); - $text = ''; - $skip = false; - if($mime=='httpd/unix-directory') { - $link = OC_Helper::linkTo( 'files', 'index.php', array('dir' => $path)); - $type = (string)$l->t('Files'); - }else{ - $link = OC_Helper::linkToRoute( 'download', array('file' => $path)); - $mimeBase = $fileData['mimepart']; - switch($mimeBase) { - case 'audio': - $skip = true; - break; - case 'text': - $type = (string)$l->t('Text'); - break; - case 'image': - $type = (string)$l->t('Images'); - break; - default: - if($mime=='application/xml') { - $type = (string)$l->t('Text'); - }else{ - $type = (string)$l->t('Files'); - } - } - } - if(!$skip) { - $results[] = new OC_Search_Result($name, $text, $link, $type, $container); - } - } - return $results; - } +/** + * Provide search results from the 'files' app + */ +class File extends \OC\Search\Provider{ + + /** + * Search for files and folders matching the given query + * @param string $query + * @return \OC\Search\Result + */ + function search($query) { + $files = \OC\Files\Filesystem::search($query); + $results = array(); + // edit results + foreach ($files as $fileData) { + // skip versions + if (strpos($fileData['path'], '_versions') === 0) { + continue; + } + // skip top-level folder + if ($fileData['name'] == 'files' && $fileData['parent'] == -1) { + continue; + } + // create folder result + if($fileData['mimetype'] == 'httpd/unix-directory'){ + $result = new \OC\Search\Result\Folder($fileData); + } + // or create file result + else{ + $result = new \OC\Search\Result\File($fileData); + } + // add to results + $results[] = $result; + } + // return + return $results; + } } diff --git a/lib/private/search/result.php b/lib/private/search/result.php index ceefeab2dae..e671aa1b609 100644 --- a/lib/private/search/result.php +++ b/lib/private/search/result.php @@ -1,27 +1,65 @@ . + * */ -class OC_Search_Result{ - public $name; - public $text; - public $link; - public $type; - public $container; - /** - * 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) - * @param string $container - */ - public function __construct($name, $text, $link, $type, $container) { - $this->name=$name; - $this->text=$text; - $this->link=$link; - $this->type=$type; - $this->container=$container; - } +namespace OC\Search; + +/** + * The generic result of a search + */ +abstract class Result { + + /** + * A unique identifier for the result, usually given as the item ID in its + * corresponding application. + * @var string + */ + public $id; + + /** + * The name of the item returned; this will be displayed in the search + * results. + * @var string + */ + public $name; + + /** + * URL to the application item. + * @var string + */ + public $link; + + /** + * The type of search result returned; for consistency, name this the same + * as the class name (e.g. \OC\Search\File -> 'file') in lowercase. + * @var string + */ + public $type = 'generic'; + + /** + * Create a new search result + * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' + * @param string $name displayed text of result + * @param string $link URL to the result within its app + */ + public function __construct($id = null, $name = null, $link = null) { + $this->id = $id; + $this->name = $name; + $this->link = $link; + } } diff --git a/lib/search/result/file.php b/lib/search/result/file.php new file mode 100644 index 00000000000..b97327e00f7 --- /dev/null +++ b/lib/search/result/file.php @@ -0,0 +1,109 @@ +. + * + */ + +namespace OC\Search\Result; + +/** + * A found file + */ +class File extends \OC\Search\Result { + + /** + * Type name; translated in templates + * @var string + */ + public $type = 'file'; + + /** + * Path to file + * @var string + */ + public $path; + + /** + * Size, in bytes + * @var int + */ + public $size; + + /** + * Date modified, in human readable form + * @var string + */ + public $modified; + + /** + * File mime type + * @var string + */ + public $mime_type; + + /** + * File permissions: + * + * @var string + */ + public $permissions; + + /** + * Create a new file search result + * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' + * @param string $name displayed text of result + * @param string $link URL to the result within its app + * @param array $data file data given by provider + */ + public function __construct(array $data = null) { + $info = pathinfo($data['path']); + $this->id = $data['fileid']; + $this->name = $info['basename']; + $this->link = \OCP\Util::linkTo('files', 'index.php', array('dir' => $info['dirname'], 'file' => $info['basename'])); + $this->permissions = self::get_permissions($data['path']); + $this->path = (strpos($data['path'], 'files') === 0) ? substr($data['path'], 5) : $data['path']; + $this->size = $data['size']; + $this->modified = $data['mtime']; + $this->mime_type = $data['mimetype']; + } + + /** + * Determine permissions for a given file path + * @param string $path + * @return int + */ + function get_permissions($path) { + // add read permissions + $permissions = \OCP\PERMISSION_READ; + // get directory + $fileinfo = pathinfo($path); + $dir = $fileinfo['dirname'] . '/'; + // add update permissions + if (\OC_Filesystem::isUpdatable($dir)) { + $permissions |= \OCP\PERMISSION_UPDATE; + } + // add delete permissions + if (\OC_Filesystem::isDeletable($dir)) { + $permissions |= \OCP\PERMISSION_DELETE; + } + // add share permissions + if (\OC_Filesystem::isSharable($dir)) { + $permissions |= \OCP\PERMISSION_SHARE; + } + // return + return $permissions; + } +} \ No newline at end of file diff --git a/lib/search/result/folder.php b/lib/search/result/folder.php new file mode 100644 index 00000000000..ba46b10bcc1 --- /dev/null +++ b/lib/search/result/folder.php @@ -0,0 +1,32 @@ +. + * + */ + +namespace OC\Search\Result; + +/** + * A found folder + */ +class Folder extends \OC\Search\Result\File { + + /** + * Type name; translated in templates + * @var string + */ + public $type = 'folder'; +} \ No newline at end of file From a6583d39769e48253835b28e0c2e717b9575bf5d Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Thu, 22 Aug 2013 23:05:07 -0400 Subject: [PATCH 02/17] Add legacy adapters for file search --- lib/legacy/search.php | 67 +++++++++++++++++++++++++++++ lib/legacy/search/provider.php | 22 ++++++++++ lib/legacy/search/provider/file.php | 22 ++++++++++ lib/legacy/search/result.php | 22 ++++++++++ 4 files changed, 133 insertions(+) create mode 100644 lib/legacy/search.php create mode 100644 lib/legacy/search/provider.php create mode 100644 lib/legacy/search/provider/file.php create mode 100644 lib/legacy/search/result.php diff --git a/lib/legacy/search.php b/lib/legacy/search.php new file mode 100644 index 00000000000..084f9dcd6ab --- /dev/null +++ b/lib/legacy/search.php @@ -0,0 +1,67 @@ +. + * + */ + + +/** + * provides an interface to all search providers + * @deprecated see lib/search.php + */ +class OC_Search{ + static private $providers=array(); + static private $registeredProviders=array(); + + /** + * remove all registered search providers + * @deprecated see lib/search.php + */ + public static function clearProviders() { + return \OC\Search::clearProviders(); + } + + /** + * register a new search provider to be used + * @param string $provider class name of a OC_Search_Provider + * @deprecated see lib/search.php + */ + public static function registerProvider($class, $options=array()) { + return \OC\Search::registerProvider($class, $options); + } + + /** + * search all provider for $query + * @param string query + * @return array An array of OC_Search_Result's + * @deprecated see lib/search.php + */ + public static function search($query) { + return \OC\Search::search($query); + } + + /** + * remove an existing search provider + * @param string $provider class name of a OC_Search_Provider + * @deprecated see lib/search.php + */ + public static function removeProvider($provider) { + return \OC\Search::removeProvider($provider); + } +} diff --git a/lib/legacy/search/provider.php b/lib/legacy/search/provider.php new file mode 100644 index 00000000000..6a5ee2dec6f --- /dev/null +++ b/lib/legacy/search/provider.php @@ -0,0 +1,22 @@ +. + * + */ + +abstract class OC_Search_Provider extends \OC\Search\Provider{ + +} \ No newline at end of file diff --git a/lib/legacy/search/provider/file.php b/lib/legacy/search/provider/file.php new file mode 100644 index 00000000000..c113cd79d27 --- /dev/null +++ b/lib/legacy/search/provider/file.php @@ -0,0 +1,22 @@ +. + * + */ + +class OC_Search_Provider_File extends \OC\Search\Provider\File{ + +} \ No newline at end of file diff --git a/lib/legacy/search/result.php b/lib/legacy/search/result.php new file mode 100644 index 00000000000..0c7326f209a --- /dev/null +++ b/lib/legacy/search/result.php @@ -0,0 +1,22 @@ +. + * + */ + +class OC_Search_Result extends \OC\Search\Result{ + +} \ No newline at end of file From afd24385a8b8a9ba75e6e0e7f3c56dfe1628991b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Mon, 26 Aug 2013 14:12:06 +0200 Subject: [PATCH 03/17] fix formatting --- lib/legacy/search.php | 11 +- lib/legacy/search/provider.php | 6 +- lib/legacy/search/provider/file.php | 6 +- lib/legacy/search/result.php | 6 +- lib/private/search.php | 11 +- lib/private/search/provider.php | 38 +++---- lib/private/search/provider/file.php | 69 ++++++------ lib/private/search/result.php | 70 ++++++------ lib/search/result/file.php | 159 ++++++++++++++------------- lib/search/result/folder.php | 15 +-- 10 files changed, 200 insertions(+), 191 deletions(-) diff --git a/lib/legacy/search.php b/lib/legacy/search.php index 084f9dcd6ab..61c7f167208 100644 --- a/lib/legacy/search.php +++ b/lib/legacy/search.php @@ -25,13 +25,13 @@ * provides an interface to all search providers * @deprecated see lib/search.php */ -class OC_Search{ +class OC_Search { static private $providers=array(); static private $registeredProviders=array(); /** * remove all registered search providers - * @deprecated see lib/search.php + * @deprecated see lib/search.php */ public static function clearProviders() { return \OC\Search::clearProviders(); @@ -40,7 +40,7 @@ class OC_Search{ /** * register a new search provider to be used * @param string $provider class name of a OC_Search_Provider - * @deprecated see lib/search.php + * @deprecated see lib/search.php */ public static function registerProvider($class, $options=array()) { return \OC\Search::registerProvider($class, $options); @@ -50,7 +50,7 @@ class OC_Search{ * search all provider for $query * @param string query * @return array An array of OC_Search_Result's - * @deprecated see lib/search.php + * @deprecated see lib/search.php */ public static function search($query) { return \OC\Search::search($query); @@ -59,9 +59,10 @@ class OC_Search{ /** * remove an existing search provider * @param string $provider class name of a OC_Search_Provider - * @deprecated see lib/search.php + * @deprecated see lib/search.php */ public static function removeProvider($provider) { return \OC\Search::removeProvider($provider); } + } diff --git a/lib/legacy/search/provider.php b/lib/legacy/search/provider.php index 6a5ee2dec6f..a14ae53c5d3 100644 --- a/lib/legacy/search/provider.php +++ b/lib/legacy/search/provider.php @@ -17,6 +17,6 @@ * */ -abstract class OC_Search_Provider extends \OC\Search\Provider{ - -} \ No newline at end of file +abstract class OC_Search_Provider extends \OC\Search\Provider { + +} diff --git a/lib/legacy/search/provider/file.php b/lib/legacy/search/provider/file.php index c113cd79d27..e610281131d 100644 --- a/lib/legacy/search/provider/file.php +++ b/lib/legacy/search/provider/file.php @@ -17,6 +17,6 @@ * */ -class OC_Search_Provider_File extends \OC\Search\Provider\File{ - -} \ No newline at end of file +class OC_Search_Provider_File extends \OC\Search\Provider\File { + +} diff --git a/lib/legacy/search/result.php b/lib/legacy/search/result.php index 0c7326f209a..dc8d36dbe4a 100644 --- a/lib/legacy/search/result.php +++ b/lib/legacy/search/result.php @@ -17,6 +17,6 @@ * */ -class OC_Search_Result extends \OC\Search\Result{ - -} \ No newline at end of file +class OC_Search_Result extends \OC\Search\Result { + +} diff --git a/lib/private/search.php b/lib/private/search.php index f3ade2683ce..966b0570135 100644 --- a/lib/private/search.php +++ b/lib/private/search.php @@ -25,7 +25,7 @@ namespace OC; /** * Provide an interface to all search providers */ -class Search{ +class Search { static private $providers=array(); static private $registeredProviders=array(); @@ -58,10 +58,10 @@ class Search{ */ public static function removeProvider($provider) { self::$registeredProviders = array_filter( - self::$registeredProviders, - function ($element) use ($provider) { - return ($element['class'] != $provider); - } + self::$registeredProviders, + function ($element) use ($provider) { + return ($element['class'] != $provider); + } ); // force regeneration of providers on next search self::$providers=array(); @@ -88,4 +88,5 @@ class Search{ self::$providers[]=new $class($options); } } + } diff --git a/lib/private/search/provider.php b/lib/private/search/provider.php index e45d3790c3f..45d9f823c55 100644 --- a/lib/private/search/provider.php +++ b/lib/private/search/provider.php @@ -24,24 +24,24 @@ namespace OC\Search; */ abstract class Provider { - /** - * List of options (currently unused) - * @var array - */ - private $options; + /** + * List of options (currently unused) + * @var array + */ + private $options; - /** - * Constructor - * @param array $options - */ - public function __construct($options) { - $this->options = $options; - } + /** + * Constructor + * @param array $options + */ + public function __construct($options) { + $this->options = $options; + } - /** - * Search for $query - * @param string $query - * @return array An array of OC\Search\Result's - */ - abstract public function search($query); -} \ No newline at end of file + /** + * Search for $query + * @param string $query + * @return array An array of OC\Search\Result's + */ + abstract public function search($query); +} diff --git a/lib/private/search/provider/file.php b/lib/private/search/provider/file.php index 5e3fb83721e..a9082763002 100644 --- a/lib/private/search/provider/file.php +++ b/lib/private/search/provider/file.php @@ -22,38 +22,39 @@ namespace OC\Search\Provider; /** * Provide search results from the 'files' app */ -class File extends \OC\Search\Provider{ - - /** - * Search for files and folders matching the given query - * @param string $query - * @return \OC\Search\Result - */ - function search($query) { - $files = \OC\Files\Filesystem::search($query); - $results = array(); - // edit results - foreach ($files as $fileData) { - // skip versions - if (strpos($fileData['path'], '_versions') === 0) { - continue; - } - // skip top-level folder - if ($fileData['name'] == 'files' && $fileData['parent'] == -1) { - continue; - } - // create folder result - if($fileData['mimetype'] == 'httpd/unix-directory'){ - $result = new \OC\Search\Result\Folder($fileData); - } - // or create file result - else{ - $result = new \OC\Search\Result\File($fileData); - } - // add to results - $results[] = $result; - } - // return - return $results; - } +class File extends \OC\Search\Provider { + + /** + * Search for files and folders matching the given query + * @param string $query + * @return \OC\Search\Result + */ + function search($query) { + $files = \OC\Files\Filesystem::search($query); + $results = array(); + // edit results + foreach ($files as $fileData) { + // skip versions + if (strpos($fileData['path'], '_versions') === 0) { + continue; + } + // skip top-level folder + if ($fileData['name'] === 'files' && $fileData['parent'] === -1) { + continue; + } + // create folder result + if($fileData['mimetype'] === 'httpd/unix-directory'){ + $result = new \OC\Search\Result\Folder($fileData); + } + // or create file result + else{ + $result = new \OC\Search\Result\File($fileData); + } + // add to results + $results[] = $result; + } + // return + return $results; + } + } diff --git a/lib/private/search/result.php b/lib/private/search/result.php index e671aa1b609..90e7081e753 100644 --- a/lib/private/search/result.php +++ b/lib/private/search/result.php @@ -24,42 +24,42 @@ namespace OC\Search; */ abstract class Result { - /** - * A unique identifier for the result, usually given as the item ID in its - * corresponding application. - * @var string - */ - public $id; + /** + * A unique identifier for the result, usually given as the item ID in its + * corresponding application. + * @var string + */ + public $id; - /** - * The name of the item returned; this will be displayed in the search - * results. - * @var string - */ - public $name; + /** + * The name of the item returned; this will be displayed in the search + * results. + * @var string + */ + public $name; - /** - * URL to the application item. - * @var string - */ - public $link; - - /** - * The type of search result returned; for consistency, name this the same - * as the class name (e.g. \OC\Search\File -> 'file') in lowercase. - * @var string - */ - public $type = 'generic'; + /** + * URL to the application item. + * @var string + */ + public $link; - /** - * Create a new search result - * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' - * @param string $name displayed text of result - * @param string $link URL to the result within its app - */ - public function __construct($id = null, $name = null, $link = null) { - $this->id = $id; - $this->name = $name; - $this->link = $link; - } + /** + * The type of search result returned; for consistency, name this the same + * as the class name (e.g. \OC\Search\File -> 'file') in lowercase. + * @var string + */ + public $type = 'generic'; + + /** + * Create a new search result + * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' + * @param string $name displayed text of result + * @param string $link URL to the result within its app + */ + public function __construct($id = null, $name = null, $link = null) { + $this->id = $id; + $this->name = $name; + $this->link = $link; + } } diff --git a/lib/search/result/file.php b/lib/search/result/file.php index b97327e00f7..0ecbc0ba1fa 100644 --- a/lib/search/result/file.php +++ b/lib/search/result/file.php @@ -23,87 +23,92 @@ namespace OC\Search\Result; * A found file */ class File extends \OC\Search\Result { - - /** - * Type name; translated in templates - * @var string - */ - public $type = 'file'; - /** - * Path to file - * @var string - */ - public $path; + /** + * Type name; translated in templates + * @var string + */ + public $type = 'file'; - /** - * Size, in bytes - * @var int - */ - public $size; + /** + * Path to file + * @var string + */ + public $path; - /** - * Date modified, in human readable form - * @var string - */ - public $modified; + /** + * Size, in bytes + * @var int + */ + public $size; - /** - * File mime type - * @var string - */ - public $mime_type; + /** + * Date modified, in human readable form + * @var string + */ + public $modified; - /** - * File permissions: - * - * @var string - */ - public $permissions; + /** + * File mime type + * @var string + */ + public $mime_type; - /** - * Create a new file search result - * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' - * @param string $name displayed text of result - * @param string $link URL to the result within its app - * @param array $data file data given by provider - */ - public function __construct(array $data = null) { - $info = pathinfo($data['path']); - $this->id = $data['fileid']; - $this->name = $info['basename']; - $this->link = \OCP\Util::linkTo('files', 'index.php', array('dir' => $info['dirname'], 'file' => $info['basename'])); - $this->permissions = self::get_permissions($data['path']); - $this->path = (strpos($data['path'], 'files') === 0) ? substr($data['path'], 5) : $data['path']; - $this->size = $data['size']; - $this->modified = $data['mtime']; - $this->mime_type = $data['mimetype']; - } + /** + * File permissions: + * + * @var string + */ + public $permissions; - /** - * Determine permissions for a given file path - * @param string $path - * @return int - */ - function get_permissions($path) { - // add read permissions - $permissions = \OCP\PERMISSION_READ; - // get directory - $fileinfo = pathinfo($path); - $dir = $fileinfo['dirname'] . '/'; - // add update permissions - if (\OC_Filesystem::isUpdatable($dir)) { - $permissions |= \OCP\PERMISSION_UPDATE; - } - // add delete permissions - if (\OC_Filesystem::isDeletable($dir)) { - $permissions |= \OCP\PERMISSION_DELETE; - } - // add share permissions - if (\OC_Filesystem::isSharable($dir)) { - $permissions |= \OCP\PERMISSION_SHARE; - } - // return - return $permissions; - } -} \ No newline at end of file + /** + * Create a new file search result + * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' + * @param string $name displayed text of result + * @param string $link URL to the result within its app + * @param array $data file data given by provider + */ + public function __construct(array $data = null) { + $info = pathinfo($data['path']); + $this->id = $data['fileid']; + $this->name = $info['basename']; + $this->link = \OCP\Util::linkTo( + 'files', + 'index.php', + array('dir' => $info['dirname'], 'file' => $info['basename']) + ); + $this->permissions = self::get_permissions($data['path']); + $this->path = (strpos($data['path'], 'files') === 0) ? substr($data['path'], 5) : $data['path']; + $this->size = $data['size']; + $this->modified = $data['mtime']; + $this->mime_type = $data['mimetype']; + } + + /** + * Determine permissions for a given file path + * @param string $path + * @return int + */ + function get_permissions($path) { + // add read permissions + $permissions = \OCP\PERMISSION_READ; + // get directory + $fileinfo = pathinfo($path); + $dir = $fileinfo['dirname'] . '/'; + // add update permissions + if (\OC_Filesystem::isUpdatable($dir)) { + $permissions |= \OCP\PERMISSION_UPDATE; + } + // add delete permissions + if (\OC_Filesystem::isDeletable($dir)) { + $permissions |= \OCP\PERMISSION_DELETE; + } + // add share permissions + if (\OC_Filesystem::isSharable($dir)) { + $permissions |= \OCP\PERMISSION_SHARE; + } + // return + return $permissions; + } + +} diff --git a/lib/search/result/folder.php b/lib/search/result/folder.php index ba46b10bcc1..8346f933b4d 100644 --- a/lib/search/result/folder.php +++ b/lib/search/result/folder.php @@ -23,10 +23,11 @@ namespace OC\Search\Result; * A found folder */ class Folder extends \OC\Search\Result\File { - - /** - * Type name; translated in templates - * @var string - */ - public $type = 'folder'; -} \ No newline at end of file + + /** + * Type name; translated in templates + * @var string + */ + public $type = 'folder'; + +} From c46d51473340e1a6fa1b6d1ee7a24422595a0c32 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Fri, 6 Sep 2013 17:42:21 -0400 Subject: [PATCH 04/17] Add audio and image result types --- lib/private/search/provider/file.php | 10 +++++++- lib/search/result/audio.php | 36 ++++++++++++++++++++++++++++ lib/search/result/image.php | 36 ++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 lib/search/result/audio.php create mode 100644 lib/search/result/image.php diff --git a/lib/private/search/provider/file.php b/lib/private/search/provider/file.php index a9082763002..d5f167b730c 100644 --- a/lib/private/search/provider/file.php +++ b/lib/private/search/provider/file.php @@ -43,7 +43,15 @@ class File extends \OC\Search\Provider { continue; } // create folder result - if($fileData['mimetype'] === 'httpd/unix-directory'){ + if($fileData['mimepart'] === 'audio'){ + $result = new \OC\Search\Result\Audio($fileData); + } + // create image result + elseif($fileData['mimepart'] === 'image'){ + $result = new \OC\Search\Result\Image($fileData); + } + // create audio result + elseif($fileData['mimetype'] === 'httpd/unix-directory'){ $result = new \OC\Search\Result\Folder($fileData); } // or create file result diff --git a/lib/search/result/audio.php b/lib/search/result/audio.php new file mode 100644 index 00000000000..46f7396ec9f --- /dev/null +++ b/lib/search/result/audio.php @@ -0,0 +1,36 @@ +. + * + */ + +namespace OC\Search\Result; + +/** + * A found audio file + */ +class Audio extends \OC\Search\Result\File { + + /** + * Type name; translated in templates + * @var string + */ + public $type = 'audio'; + + /** + * @TODO add ID3 information + */ +} diff --git a/lib/search/result/image.php b/lib/search/result/image.php new file mode 100644 index 00000000000..ecc706fffe6 --- /dev/null +++ b/lib/search/result/image.php @@ -0,0 +1,36 @@ +. + * + */ + +namespace OC\Search\Result; + +/** + * A found image file + */ +class Image extends \OC\Search\Result\File { + + /** + * Type name; translated in templates + * @var string + */ + public $type = 'image'; + + /** + * @TODO add EXIF information + */ +} From 70be98f6d0bee928a25bd7a38b543dac02084db5 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Fri, 6 Sep 2013 17:42:21 -0400 Subject: [PATCH 05/17] Add audio and image result types --- lib/private/search/provider/file.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/private/search/provider/file.php b/lib/private/search/provider/file.php index d5f167b730c..26ebe309951 100644 --- a/lib/private/search/provider/file.php +++ b/lib/private/search/provider/file.php @@ -42,7 +42,7 @@ class File extends \OC\Search\Provider { if ($fileData['name'] === 'files' && $fileData['parent'] === -1) { continue; } - // create folder result + // create audio result if($fileData['mimepart'] === 'audio'){ $result = new \OC\Search\Result\Audio($fileData); } @@ -50,7 +50,7 @@ class File extends \OC\Search\Provider { elseif($fileData['mimepart'] === 'image'){ $result = new \OC\Search\Result\Image($fileData); } - // create audio result + // create folder result elseif($fileData['mimetype'] === 'httpd/unix-directory'){ $result = new \OC\Search\Result\Folder($fileData); } From b03755c0ec3dedc15a5dfbeceb045a28a04f8006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Tue, 17 Sep 2013 17:29:22 +0200 Subject: [PATCH 06/17] fix minor typos --- search/js/result.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/search/js/result.js b/search/js/result.js index 780f513edcf..856330414a0 100644 --- a/search/js/result.js +++ b/search/js/result.js @@ -105,3 +105,28 @@ OC.search.renderCurrent=function(){ $(result).addClass('current'); } }; + +// +// customize search results, currently replaces a technical type with a more human friendly version +// TODO implement search result renderers instead of changing results after adding them to the DOM +// +OC.search.customResults.file = function (row, item) { + if(row.children('td.type').text() === 'file') { + row.children('td.type').text(t('lib','Files')); + }; +} +OC.search.customResults.folder = function (row, item) { + if(row.children('td.type').text() === 'folder') { + row.children('td.type').text(t('lib','Folders')); + }; +} +OC.search.customResults.image = function (row, item) { + if(row.children('td.type').text() === 'image') { + row.children('td.type').text(t('lib','Images')); + }; +} +OC.search.customResults.audio = function (row, item) { + if(row.children('td.type').text() === 'audio') { + row.children('td.type').text(t('lib','Audio')); + }; +} From 9b63f945e3ecbbb372ac7ac4d9017f933fcee02b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 19 Sep 2013 16:14:37 +0200 Subject: [PATCH 07/17] decode filename for scrollto --- apps/files/js/files.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files/js/files.js b/apps/files/js/files.js index 8d871d6dac6..df0c40a4405 100644 --- a/apps/files/js/files.js +++ b/apps/files/js/files.js @@ -284,7 +284,7 @@ $('#webdavurl').select(); }); - //scroll to and highlight preselected file + //FIXME scroll to and highlight preselected file /* if (getURLParameter('scrollto')) { FileList.scrollTo(getURLParameter('scrollto')); From 3510674d2445dab7fe5d58187d2275ab11a8de4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 19 Sep 2013 16:15:17 +0200 Subject: [PATCH 08/17] use path param to create scrollto url --- search/js/result.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/search/js/result.js b/search/js/result.js index 856330414a0..d2d68145304 100644 --- a/search/js/result.js +++ b/search/js/result.js @@ -57,22 +57,26 @@ OC.search.showResults=function(results){ var row=$('#searchresults tr.template').clone(); row.removeClass('template'); row.addClass('result'); + row.data('type', typeid); row.data('name', type[i].name); row.data('text', type[i].text); - row.data('container', type[i].container); + row.data('index',index); + if (i === 0){ row.children('td.type').text(typeid); } row.find('td.result div.name').text(type[i].name); row.find('td.result div.text').text(type[i].text); - if (type[i].container) { - var containerName = OC.basename(type[i].container); + + if (type[i].path) { + var parent = OC.dirname(type[i].path); + var containerName = OC.basename(parent); if (containerName === '') { containerName = '/'; } var containerLink = OC.linkTo('files', 'index.php') - +'?dir='+encodeURIComponent(type[i].container) + +'?dir='+encodeURIComponent(parent) +'&scrollto='+encodeURIComponent(type[i].name); row.find('td.result a') .attr('href', containerLink) @@ -80,10 +84,11 @@ OC.search.showResults=function(results){ } else { row.find('td.result a').attr('href', type[i].link); } - row.data('index',index); + index++; - if(OC.search.customResults[typeid]){//give plugins the ability to customize the entries in here - OC.search.customResults[typeid](row,type[i]); + //give plugins the ability to customize the entries in here + if(OC.search.customResults[typeid]){ + OC.search.customResults[typeid](row, type[i]); } $('#searchresults tbody').append(row); } @@ -114,19 +119,19 @@ OC.search.customResults.file = function (row, item) { if(row.children('td.type').text() === 'file') { row.children('td.type').text(t('lib','Files')); }; -} +}; OC.search.customResults.folder = function (row, item) { if(row.children('td.type').text() === 'folder') { row.children('td.type').text(t('lib','Folders')); }; -} +}; OC.search.customResults.image = function (row, item) { if(row.children('td.type').text() === 'image') { row.children('td.type').text(t('lib','Images')); }; -} +}; OC.search.customResults.audio = function (row, item) { if(row.children('td.type').text() === 'audio') { row.children('td.type').text(t('lib','Audio')); }; -} +}; From 794a133f9ada6d26638f7aba56a2260cb6655df4 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 30 Sep 2013 18:35:02 -0400 Subject: [PATCH 09/17] Re-add type to legacy OC_Search_Result class --- lib/legacy/search/result.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/legacy/search/result.php b/lib/legacy/search/result.php index dc8d36dbe4a..7cacb3579e5 100644 --- a/lib/legacy/search/result.php +++ b/lib/legacy/search/result.php @@ -18,5 +18,18 @@ */ class OC_Search_Result extends \OC\Search\Result { - + + /** + * Create a new search result + * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' + * @param string $name displayed text of result + * @param string $link URL to the result within its app + * @param string $type @deprecated because it is now set in \OC\Search\Result descendants + */ + public function __construct($id = null, $name = null, $link = null, $type = null) { + $this->id = $id; + $this->name = $name; + $this->link = $link; + $this->type = $type; + } } From 8a223eb62d9d0a1f92ce869ba2639816843cd1a6 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 17 Feb 2014 17:37:06 -0800 Subject: [PATCH 10/17] Move legacy search to 'lib/private/legacy' This fixes errors where apps would try to start providers using OC_Search and would fail. Also, made OC_Search simply inherit from \OC\Search. --- lib/legacy/search.php | 68 ------------------- lib/private/legacy/search.php | 29 ++++++++ lib/{ => private}/legacy/search/provider.php | 0 .../legacy/search/provider/file.php | 0 lib/{ => private}/legacy/search/result.php | 0 5 files changed, 29 insertions(+), 68 deletions(-) delete mode 100644 lib/legacy/search.php create mode 100644 lib/private/legacy/search.php rename lib/{ => private}/legacy/search/provider.php (100%) rename lib/{ => private}/legacy/search/provider/file.php (100%) rename lib/{ => private}/legacy/search/result.php (100%) diff --git a/lib/legacy/search.php b/lib/legacy/search.php deleted file mode 100644 index 61c7f167208..00000000000 --- a/lib/legacy/search.php +++ /dev/null @@ -1,68 +0,0 @@ -. - * - */ - - -/** - * provides an interface to all search providers - * @deprecated see lib/search.php - */ -class OC_Search { - static private $providers=array(); - static private $registeredProviders=array(); - - /** - * remove all registered search providers - * @deprecated see lib/search.php - */ - public static function clearProviders() { - return \OC\Search::clearProviders(); - } - - /** - * register a new search provider to be used - * @param string $provider class name of a OC_Search_Provider - * @deprecated see lib/search.php - */ - public static function registerProvider($class, $options=array()) { - return \OC\Search::registerProvider($class, $options); - } - - /** - * search all provider for $query - * @param string query - * @return array An array of OC_Search_Result's - * @deprecated see lib/search.php - */ - public static function search($query) { - return \OC\Search::search($query); - } - - /** - * remove an existing search provider - * @param string $provider class name of a OC_Search_Provider - * @deprecated see lib/search.php - */ - public static function removeProvider($provider) { - return \OC\Search::removeProvider($provider); - } - -} diff --git a/lib/private/legacy/search.php b/lib/private/legacy/search.php new file mode 100644 index 00000000000..e6396020225 --- /dev/null +++ b/lib/private/legacy/search.php @@ -0,0 +1,29 @@ +. + * + */ + +/** + * provides an interface to all search providers + * @deprecated see lib/search.php + */ +class OC_Search extends \OC\Search{ + +} diff --git a/lib/legacy/search/provider.php b/lib/private/legacy/search/provider.php similarity index 100% rename from lib/legacy/search/provider.php rename to lib/private/legacy/search/provider.php diff --git a/lib/legacy/search/provider/file.php b/lib/private/legacy/search/provider/file.php similarity index 100% rename from lib/legacy/search/provider/file.php rename to lib/private/legacy/search/provider/file.php diff --git a/lib/legacy/search/result.php b/lib/private/legacy/search/result.php similarity index 100% rename from lib/legacy/search/result.php rename to lib/private/legacy/search/result.php From 7a224f57626f684415b5f45660bd21ec7ebafe72 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 17 Feb 2014 17:57:27 -0800 Subject: [PATCH 11/17] Move new search results to 'lib/private/search' --- lib/private/search/result.php | 2 +- lib/{ => private}/search/result/audio.php | 0 lib/{ => private}/search/result/file.php | 0 lib/{ => private}/search/result/folder.php | 0 lib/{ => private}/search/result/image.php | 0 5 files changed, 1 insertion(+), 1 deletion(-) rename lib/{ => private}/search/result/audio.php (100%) rename lib/{ => private}/search/result/file.php (100%) rename lib/{ => private}/search/result/folder.php (100%) rename lib/{ => private}/search/result/image.php (100%) diff --git a/lib/private/search/result.php b/lib/private/search/result.php index 90e7081e753..d228f833810 100644 --- a/lib/private/search/result.php +++ b/lib/private/search/result.php @@ -22,7 +22,7 @@ namespace OC\Search; /** * The generic result of a search */ -abstract class Result { +class Result { /** * A unique identifier for the result, usually given as the item ID in its diff --git a/lib/search/result/audio.php b/lib/private/search/result/audio.php similarity index 100% rename from lib/search/result/audio.php rename to lib/private/search/result/audio.php diff --git a/lib/search/result/file.php b/lib/private/search/result/file.php similarity index 100% rename from lib/search/result/file.php rename to lib/private/search/result/file.php diff --git a/lib/search/result/folder.php b/lib/private/search/result/folder.php similarity index 100% rename from lib/search/result/folder.php rename to lib/private/search/result/folder.php diff --git a/lib/search/result/image.php b/lib/private/search/result/image.php similarity index 100% rename from lib/search/result/image.php rename to lib/private/search/result/image.php From 5dc2e73fe08189833c0398117916b752b0f730d6 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Tue, 18 Feb 2014 17:22:04 -0800 Subject: [PATCH 12/17] Simplify client-side result customization The issue was that search results from other providers (contacts, calendar, etc.) were unformatted, like 'event' or 'contact', while the built-in event types (folder, file, etc.) were being modified by custom result functions to something like 'Files' or 'Folders'. The fix is to capitalize and translate all result types by default. Custom formatting is still allowed (and example documentation has been added) but the built-in result formatters where now unnecessary and were removed. --- search/js/result.js | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/search/js/result.js b/search/js/result.js index d2d68145304..b300f90f4c2 100644 --- a/search/js/result.js +++ b/search/js/result.js @@ -64,7 +64,8 @@ OC.search.showResults=function(results){ row.data('index',index); if (i === 0){ - row.children('td.type').text(typeid); + var typeName = typeid.charAt(0).toUpperCase() + typeid.slice(1); + row.children('td.type').text(t('lib', typeName)); } row.find('td.result div.name').text(type[i].name); row.find('td.result div.text').text(type[i].text); @@ -86,7 +87,12 @@ OC.search.showResults=function(results){ } index++; - //give plugins the ability to customize the entries in here + /** + * Give plugins the ability to customize the search results. For example: + * OC.search.customResults.file = function (row, item){ + * if(item.name.search('.json') >= 0) ... + * }; + */ if(OC.search.customResults[typeid]){ OC.search.customResults[typeid](row, type[i]); } @@ -109,29 +115,4 @@ OC.search.renderCurrent=function(){ $('#searchresults tr.result').removeClass('current'); $(result).addClass('current'); } -}; - -// -// customize search results, currently replaces a technical type with a more human friendly version -// TODO implement search result renderers instead of changing results after adding them to the DOM -// -OC.search.customResults.file = function (row, item) { - if(row.children('td.type').text() === 'file') { - row.children('td.type').text(t('lib','Files')); - }; -}; -OC.search.customResults.folder = function (row, item) { - if(row.children('td.type').text() === 'folder') { - row.children('td.type').text(t('lib','Folders')); - }; -}; -OC.search.customResults.image = function (row, item) { - if(row.children('td.type').text() === 'image') { - row.children('td.type').text(t('lib','Images')); - }; -}; -OC.search.customResults.audio = function (row, item) { - if(row.children('td.type').text() === 'audio') { - row.children('td.type').text(t('lib','Audio')); - }; -}; +}; \ No newline at end of file From 5034bd1b129b75e7b571cb0ed239d02010c806f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 5 Jun 2014 19:35:24 +0200 Subject: [PATCH 13/17] minor phpdoc cleanup and imports --- lib/private/search.php | 7 +++++-- lib/private/search/provider/file.php | 3 ++- lib/private/search/result/audio.php | 2 +- lib/private/search/result/file.php | 11 ++++------- lib/private/search/result/folder.php | 2 +- lib/private/search/result/image.php | 2 +- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/private/search.php b/lib/private/search.php index 966b0570135..8ac9e9fdf33 100644 --- a/lib/private/search.php +++ b/lib/private/search.php @@ -21,6 +21,7 @@ */ namespace OC; +use OC\Search\Provider; /** * Provide an interface to all search providers @@ -32,13 +33,14 @@ class Search { /** * Search all providers for $query - * @param string query + * @param string $query * @return array An array of OC\Search\Result's */ public static function search($query) { self::initProviders(); $results=array(); foreach(self::$providers as $provider) { + /** @var $provider Provider */ $results=array_merge($results, $provider->search($query)); } return $results; @@ -69,7 +71,8 @@ class Search { /** * Register a new search provider to search with - * @param string $provider class name of a OC\Search\Provider + * @param string $class class name of a OC\Search\Provider + * @param array $options optional */ public static function registerProvider($class, $options=array()) { self::$registeredProviders[]=array('class'=>$class, 'options'=>$options); diff --git a/lib/private/search/provider/file.php b/lib/private/search/provider/file.php index 26ebe309951..daf73cf79c2 100644 --- a/lib/private/search/provider/file.php +++ b/lib/private/search/provider/file.php @@ -18,6 +18,7 @@ */ namespace OC\Search\Provider; +use OC\Files\Filesystem; /** * Provide search results from the 'files' app @@ -30,7 +31,7 @@ class File extends \OC\Search\Provider { * @return \OC\Search\Result */ function search($query) { - $files = \OC\Files\Filesystem::search($query); + $files = Filesystem::search($query); $results = array(); // edit results foreach ($files as $fileData) { diff --git a/lib/private/search/result/audio.php b/lib/private/search/result/audio.php index 46f7396ec9f..3adbb400560 100644 --- a/lib/private/search/result/audio.php +++ b/lib/private/search/result/audio.php @@ -22,7 +22,7 @@ namespace OC\Search\Result; /** * A found audio file */ -class Audio extends \OC\Search\Result\File { +class Audio extends File { /** * Type name; translated in templates diff --git a/lib/private/search/result/file.php b/lib/private/search/result/file.php index 0ecbc0ba1fa..32a4aebc700 100644 --- a/lib/private/search/result/file.php +++ b/lib/private/search/result/file.php @@ -18,7 +18,7 @@ */ namespace OC\Search\Result; - +use \OC\Files\Filesystem; /** * A found file */ @@ -63,9 +63,6 @@ class File extends \OC\Search\Result { /** * Create a new file search result - * @param string $id unique identifier from application: '[app_name]/[item_identifier_in_app]' - * @param string $name displayed text of result - * @param string $link URL to the result within its app * @param array $data file data given by provider */ public function __construct(array $data = null) { @@ -96,15 +93,15 @@ class File extends \OC\Search\Result { $fileinfo = pathinfo($path); $dir = $fileinfo['dirname'] . '/'; // add update permissions - if (\OC_Filesystem::isUpdatable($dir)) { + if (Filesystem::isUpdatable($dir)) { $permissions |= \OCP\PERMISSION_UPDATE; } // add delete permissions - if (\OC_Filesystem::isDeletable($dir)) { + if (Filesystem::isDeletable($dir)) { $permissions |= \OCP\PERMISSION_DELETE; } // add share permissions - if (\OC_Filesystem::isSharable($dir)) { + if (Filesystem::isSharable($dir)) { $permissions |= \OCP\PERMISSION_SHARE; } // return diff --git a/lib/private/search/result/folder.php b/lib/private/search/result/folder.php index 8346f933b4d..29469a1d112 100644 --- a/lib/private/search/result/folder.php +++ b/lib/private/search/result/folder.php @@ -22,7 +22,7 @@ namespace OC\Search\Result; /** * A found folder */ -class Folder extends \OC\Search\Result\File { +class Folder extends File { /** * Type name; translated in templates diff --git a/lib/private/search/result/image.php b/lib/private/search/result/image.php index ecc706fffe6..f4e6be56c8d 100644 --- a/lib/private/search/result/image.php +++ b/lib/private/search/result/image.php @@ -22,7 +22,7 @@ namespace OC\Search\Result; /** * A found image file */ -class Image extends \OC\Search\Result\File { +class Image extends File { /** * Type name; translated in templates From aaf0d131718dc9079a5b3717276455f8701feeea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 6 Jun 2014 01:17:02 +0200 Subject: [PATCH 14/17] make search non-static, add ISearch to server container, make legacy a static wrapper for it, move provider and result to public api --- lib/private/legacy/search.php | 41 ++++++++++++++- lib/private/search.php | 49 +++++++++--------- lib/private/search/provider/file.php | 4 +- lib/private/search/result/file.php | 2 +- lib/private/server.php | 10 ++++ lib/public/isearch.php | 57 +++++++++++++++++++++ lib/public/iservercontainer.php | 8 +++ lib/{private => public}/search/provider.php | 4 +- lib/{private => public}/search/result.php | 2 +- 9 files changed, 146 insertions(+), 31 deletions(-) create mode 100644 lib/public/isearch.php rename lib/{private => public}/search/provider.php (93%) rename lib/{private => public}/search/result.php (98%) diff --git a/lib/private/legacy/search.php b/lib/private/legacy/search.php index e6396020225..f77b43be2e0 100644 --- a/lib/private/legacy/search.php +++ b/lib/private/legacy/search.php @@ -24,6 +24,45 @@ * provides an interface to all search providers * @deprecated see lib/search.php */ -class OC_Search extends \OC\Search{ +class OC_Search { + /** + * @return \OCP\ISearch + */ + private static function getSearch() { + return \OC::$server->getSearch(); + } + + /** + * Search all providers for $query + * @param string $query + * @return array An array of OCP\Search\Result's + */ + public static function search($query) { + return self::getSearch()->search($query); + } + + /** + * Register a new search provider to search with + * @param string $class class name of a OCP\Search\Provider + * @param array $options optional + */ + public static function registerProvider($class, $options = array()) { + return self::getSearch()->registerProvider($class, $options); + } + + /** + * Remove one existing search provider + * @param string $provider class name of a OCP\Search\Provider + */ + public static function removeProvider($provider) { + return self::getSearch()->removeProvider($provider); + } + + /** + * Remove all registered search providers + */ + public static function clearProviders() { + return self::getSearch()->clearProviders(); + } } diff --git a/lib/private/search.php b/lib/private/search.php index 8ac9e9fdf33..bcaebdddd9c 100644 --- a/lib/private/search.php +++ b/lib/private/search.php @@ -21,27 +21,28 @@ */ namespace OC; -use OC\Search\Provider; +use OCP\Search\Provider; +use OCP\ISearch; /** * Provide an interface to all search providers */ -class Search { +class Search implements ISearch { - static private $providers=array(); - static private $registeredProviders=array(); + private $providers = array(); + private $registeredProviders = array(); /** * Search all providers for $query * @param string $query * @return array An array of OC\Search\Result's */ - public static function search($query) { - self::initProviders(); - $results=array(); - foreach(self::$providers as $provider) { + public function search($query) { + $this->initProviders(); + $results = array(); + foreach($this->providers as $provider) { /** @var $provider Provider */ - $results=array_merge($results, $provider->search($query)); + $results = array_merge($results, $provider->search($query)); } return $results; } @@ -49,24 +50,24 @@ class Search { /** * Remove all registered search providers */ - public static function clearProviders() { - self::$providers=array(); - self::$registeredProviders=array(); + public function clearProviders() { + $this->providers=array(); + $this->registeredProviders=array(); } /** * Remove one existing search provider * @param string $provider class name of a OC\Search\Provider */ - public static function removeProvider($provider) { - self::$registeredProviders = array_filter( - self::$registeredProviders, + public function removeProvider($provider) { + $this->registeredProviders = array_filter( + $this->registeredProviders, function ($element) use ($provider) { return ($element['class'] != $provider); } ); // force regeneration of providers on next search - self::$providers=array(); + $this->providers=array(); } /** @@ -74,21 +75,21 @@ class Search { * @param string $class class name of a OC\Search\Provider * @param array $options optional */ - public static function registerProvider($class, $options=array()) { - self::$registeredProviders[]=array('class'=>$class, 'options'=>$options); + public function registerProvider($class, $options=array()) { + $this->registeredProviders[]=array('class'=>$class, 'options'=>$options); } /** * Create instances of all the registered search providers */ - private static function initProviders() { - if(count(self::$providers)>0) { + private function initProviders() { + if(count($this->providers)>0) { return; } - foreach(self::$registeredProviders as $provider) { - $class=$provider['class']; - $options=$provider['options']; - self::$providers[]=new $class($options); + foreach($this->registeredProviders as $provider) { + $class = $provider['class']; + $options = $provider['options']; + $this->providers[]=new $class($options); } } diff --git a/lib/private/search/provider/file.php b/lib/private/search/provider/file.php index daf73cf79c2..cbd3a253732 100644 --- a/lib/private/search/provider/file.php +++ b/lib/private/search/provider/file.php @@ -23,12 +23,12 @@ use OC\Files\Filesystem; /** * Provide search results from the 'files' app */ -class File extends \OC\Search\Provider { +class File extends \OCP\Search\Provider { /** * Search for files and folders matching the given query * @param string $query - * @return \OC\Search\Result + * @return \OCP\Search\Result */ function search($query) { $files = Filesystem::search($query); diff --git a/lib/private/search/result/file.php b/lib/private/search/result/file.php index 32a4aebc700..da5fa64ef45 100644 --- a/lib/private/search/result/file.php +++ b/lib/private/search/result/file.php @@ -22,7 +22,7 @@ use \OC\Files\Filesystem; /** * A found file */ -class File extends \OC\Search\Result { +class File extends \OCP\Search\Result { /** * Type name; translated in templates diff --git a/lib/private/server.php b/lib/private/server.php index 47bdee4b0f8..da705863078 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -186,6 +186,9 @@ class Server extends SimpleContainer implements IServerContainer { } return $router; }); + $this->registerService('Search', function($c){ + return new Search(); + }); $this->registerService('Db', function($c){ return new Db(); }); @@ -422,6 +425,13 @@ class Server extends SimpleContainer implements IServerContainer { return $this->query('Router'); } + /** + * Returns a search instance + * @return \OCP\ISearch + */ + function getSearch() { + return $this->query('Search'); + } /** * Returns an instance of the db facade diff --git a/lib/public/isearch.php b/lib/public/isearch.php new file mode 100644 index 00000000000..3b83dbf35e8 --- /dev/null +++ b/lib/public/isearch.php @@ -0,0 +1,57 @@ +. + * + */ + +namespace OCP; + + +/** + * Small Interface for Search + */ +interface ISearch { + + /** + * Search all providers for $query + * @param string $query + * @return array An array of OCP\Search\Result's + */ + public function search($query); + + /** + * Register a new search provider to search with + * @param string $class class name of a OCP\Search\Provider + * @param array $options optional + */ + public function registerProvider($class, $options = array()); + + /** + * Remove one existing search provider + * @param string $provider class name of a OCP\Search\Provider + */ + public function removeProvider($provider); + + /** + * Remove all registered search providers + */ + public function clearProviders(); + +} diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index 22176c36b8a..8bf97828581 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -204,4 +204,12 @@ interface IServerContainer { * @return \OCP\Route\IRouter */ function getRouter(); + + /** + * Returns a search instance + * + * @return \OCP\ISearch + */ + function getSearch(); + } diff --git a/lib/private/search/provider.php b/lib/public/search/provider.php similarity index 93% rename from lib/private/search/provider.php rename to lib/public/search/provider.php index 45d9f823c55..0506f091dd9 100644 --- a/lib/private/search/provider.php +++ b/lib/public/search/provider.php @@ -17,7 +17,7 @@ * */ -namespace OC\Search; +namespace OCP\Search; /** * Provides a template for search functionality throughout ownCloud; @@ -41,7 +41,7 @@ abstract class Provider { /** * Search for $query * @param string $query - * @return array An array of OC\Search\Result's + * @return array An array of OCP\Search\Result's */ abstract public function search($query); } diff --git a/lib/private/search/result.php b/lib/public/search/result.php similarity index 98% rename from lib/private/search/result.php rename to lib/public/search/result.php index d228f833810..c70f1bde880 100644 --- a/lib/private/search/result.php +++ b/lib/public/search/result.php @@ -17,7 +17,7 @@ * */ -namespace OC\Search; +namespace OCP\Search; /** * The generic result of a search From 56470fa9b3a03c2737aeb2c29c2cdebfcaa8a99b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 6 Jun 2014 11:01:35 +0200 Subject: [PATCH 15/17] fix namespace for legacy search classes --- lib/private/legacy/search/provider.php | 2 +- lib/private/legacy/search/result.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/private/legacy/search/provider.php b/lib/private/legacy/search/provider.php index a14ae53c5d3..d200c988c45 100644 --- a/lib/private/legacy/search/provider.php +++ b/lib/private/legacy/search/provider.php @@ -17,6 +17,6 @@ * */ -abstract class OC_Search_Provider extends \OC\Search\Provider { +abstract class OC_Search_Provider extends \OCP\Search\Provider { } diff --git a/lib/private/legacy/search/result.php b/lib/private/legacy/search/result.php index 7cacb3579e5..f3e6ecbb990 100644 --- a/lib/private/legacy/search/result.php +++ b/lib/private/legacy/search/result.php @@ -17,7 +17,7 @@ * */ -class OC_Search_Result extends \OC\Search\Result { +class OC_Search_Result extends \OCP\Search\Result { /** * Create a new search result From 2801f0f2228c571a0bfd9de3684f87e0fe408c57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 6 Jun 2014 11:02:05 +0200 Subject: [PATCH 16/17] use non static access to search --- apps/files/appinfo/app.php | 2 +- search/ajax/search.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/files/appinfo/app.php b/apps/files/appinfo/app.php index 43e6cffa335..9af36c682fd 100644 --- a/apps/files/appinfo/app.php +++ b/apps/files/appinfo/app.php @@ -10,7 +10,7 @@ OCP\App::addNavigationEntry(array("id" => "files_index", "icon" => OCP\Util::imagePath("core", "places/files.svg"), "name" => $l->t("Files"))); -OC_Search::registerProvider('OC_Search_Provider_File'); +\OC::$server->getSearch()->registerProvider('OC\Search\Provider\File'); $templateManager = OC_Helper::getFileTemplateManager(); $templateManager->registerTemplate('text/html', 'core/templates/filetemplates/template.html'); diff --git a/search/ajax/search.php b/search/ajax/search.php index 0cc1f9d30cd..546fccc644f 100644 --- a/search/ajax/search.php +++ b/search/ajax/search.php @@ -26,7 +26,7 @@ OC_JSON::checkLoggedIn(); $query=(isset($_GET['query']))?$_GET['query']:''; if($query) { - $result=OC_Search::search($query); + $result = \OC::$server->getSearch()->search($query); OC_JSON::encodedPrint($result); } else { From 7c29645f22eeb686ce7a25c9f8ad97b464d7febb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Fri, 6 Jun 2014 11:28:43 +0200 Subject: [PATCH 17/17] allow apps to add translation for search result typeid --- core/js/js.js | 7 +++++++ search/js/result.js | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/js/js.js b/core/js/js.js index b3cefa83bee..03b496b2635 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -585,6 +585,13 @@ OC.search.customResults={}; OC.search.currentResult=-1; OC.search.lastQuery=''; OC.search.lastResults={}; +//translations for result type ids, can be extended by apps +OC.search.resultTypes={ + file: t('core','File'), + folder: t('core','Folder'), + image: t('core','Image'), + audio: t('core','Audio') +}; OC.addStyle.loaded=[]; OC.addScript.loaded=[]; diff --git a/search/js/result.js b/search/js/result.js index b300f90f4c2..04e999c62ee 100644 --- a/search/js/result.js +++ b/search/js/result.js @@ -64,7 +64,7 @@ OC.search.showResults=function(results){ row.data('index',index); if (i === 0){ - var typeName = typeid.charAt(0).toUpperCase() + typeid.slice(1); + var typeName = OC.search.resultTypes[typeid]; row.children('td.type').text(t('lib', typeName)); } row.find('td.result div.name').text(type[i].name);