From 25dde7e93bc648ec8cd14b8f2711d50f77d8d1bf Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 4 Dec 2014 14:01:15 +0100 Subject: Added searchByTags to view, storage and cache --- lib/private/files/cache/cache.php | 48 ++++++++++++++++++++++++ lib/private/files/cache/wrapper/cachewrapper.php | 12 ++++++ lib/private/files/filesystem.php | 8 ++++ lib/private/files/node/folder.php | 10 +++++ lib/private/files/node/nonexistingfolder.php | 4 ++ lib/private/files/view.php | 10 +++++ 6 files changed, 92 insertions(+) (limited to 'lib/private') diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index 4157da2281c..a4ae3a069fe 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -503,6 +503,54 @@ class Cache { return $files; } + /** + * Search for files by tag of a given users. + * + * Note that every user can tag files differently. + * + * @param string|int $tag name or tag id + * @param string $userId owner of the tags + * @return array file data + */ + public function searchByTag($tag, $userId = null) { + if (is_null($userId)) { + $userId = \OC::$server->getUserSession()->getUser()->getUID(); + } + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . + '`mimetype`, `mimepart`, `size`, `mtime`, ' . + '`encrypted`, `unencrypted_size`, `etag`, `permissions` ' . + 'FROM `*PREFIX*filecache` `file`, ' . + '`*PREFIX*vcategory_to_object` `tagmap`, ' . + '`*PREFIX*vcategory` `tag` ' . + // JOIN filecache to vcategory_to_object + 'WHERE `file`.`fileid` = `tagmap`.`objid` '. + // JOIN vcategory_to_object to vcategory + 'AND `tagmap`.`type` = `tag`.`type` ' . + 'AND `tagmap`.`categoryid` = `tag`.`id` ' . + // conditions + 'AND `file`.`storage` = ? '. + 'AND `tag`.`type` = \'files\' ' . + 'AND `tag`.`uid` = ? '; + if (is_int($tag)) { + $sql .= 'AND `tag`.`id` = ? '; + } else { + $sql .= 'AND `tag`.`category` = ? '; + } + $result = \OC_DB::executeAudited( + $sql, + array( + $this->getNumericStorageId(), + $userId, + $tag + ) + ); + $files = array(); + while ($row = $result->fetchRow()) { + $files[] = $row; + } + return $files; + } + /** * update the folder size and the size of all parent folders * diff --git a/lib/private/files/cache/wrapper/cachewrapper.php b/lib/private/files/cache/wrapper/cachewrapper.php index d3d64e3f0a9..4da7c7ecf6f 100644 --- a/lib/private/files/cache/wrapper/cachewrapper.php +++ b/lib/private/files/cache/wrapper/cachewrapper.php @@ -180,6 +180,18 @@ class CacheWrapper extends Cache { return array_map(array($this, 'formatCacheEntry'), $results); } + /** + * search for files by tag + * + * @param string|int $tag name or tag id + * @param string $userId owner of the tags + * @return array file data + */ + public function searchByTag($tag, $userId = null) { + $results = $this->cache->searchByTag($tag, $userId); + return array_map(array($this, 'formatCacheEntry'), $results); + } + /** * update the folder size and the size of all parent folders * diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php index 90643839e22..3d55564f0c6 100644 --- a/lib/private/files/filesystem.php +++ b/lib/private/files/filesystem.php @@ -686,6 +686,14 @@ class Filesystem { return self::$defaultInstance->searchByMime($query); } + /** + * @param string|int $tag name or tag id + * @return FileInfo[] array or file info + */ + static public function searchByTag($tag) { + return self::$defaultInstance->searchByTag($tag); + } + /** * check if a file or folder has been updated since $time * diff --git a/lib/private/files/node/folder.php b/lib/private/files/node/folder.php index 6fdcff13e1c..a65e641388d 100644 --- a/lib/private/files/node/folder.php +++ b/lib/private/files/node/folder.php @@ -236,6 +236,16 @@ class Folder extends Node implements \OCP\Files\Folder { return $this->searchCommon($mimetype, 'searchByMime'); } + /** + * search for files by tag + * + * @param string $tag + * @return Node[] + */ + public function searchByTag($tag) { + return $this->searchCommon($tag, 'searchByTag'); + } + /** * @param string $query * @param string $method diff --git a/lib/private/files/node/nonexistingfolder.php b/lib/private/files/node/nonexistingfolder.php index 0346cbf1e21..9d452a94b9c 100644 --- a/lib/private/files/node/nonexistingfolder.php +++ b/lib/private/files/node/nonexistingfolder.php @@ -99,6 +99,10 @@ class NonExistingFolder extends Folder { throw new NotFoundException(); } + public function searchByTag($mime) { + throw new NotFoundException(); + } + public function getById($id) { throw new NotFoundException(); } diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 4b3d167f8e9..7090e03d40c 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -1134,6 +1134,16 @@ class View { return $this->searchCommon($mimetype, 'searchByMime'); } + /** + * search for files by tag + * + * @param string|int $tag name or tag id + * @return FileInfo[] + */ + public function searchByTag($tag) { + return $this->searchCommon($tag, 'searchByTag'); + } + /** * @param string $query * @param string $method -- cgit v1.2.3