From ac72828eaea4fbc428082bcb6af8daa43867137b Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Thu, 22 Aug 2013 23:04:06 -0400 Subject: [PATCH] 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 -- 2.39.5