summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-12-12 14:27:19 +0100
committerVincent Petry <pvince81@owncloud.com>2014-12-12 14:27:19 +0100
commit4b57892c4eeeb7bd3c8b1b0465acf032dc84fd19 (patch)
treefbad2095651334fc34b3729ae5102ad5e1cd866f /lib
parent6b4502adebf1d756707e8fb5b8ab697c1c746e6b (diff)
parent3878c3782ff37dc56a4e463acea351a61ed7b4c2 (diff)
downloadnextcloud-server-4b57892c4eeeb7bd3c8b1b0465acf032dc84fd19.tar.gz
nextcloud-server-4b57892c4eeeb7bd3c8b1b0465acf032dc84fd19.zip
Merge pull request #12778 from owncloud/searchbytags2
Added searchByTags to view, storage and cache
Diffstat (limited to 'lib')
-rw-r--r--lib/private/files/cache/cache.php45
-rw-r--r--lib/private/files/cache/wrapper/cachejail.php12
-rw-r--r--lib/private/files/cache/wrapper/cachewrapper.php12
-rw-r--r--lib/private/files/filesystem.php9
-rw-r--r--lib/private/files/node/folder.php25
-rw-r--r--lib/private/files/node/nonexistingfolder.php4
-rw-r--r--lib/private/files/view.php27
-rw-r--r--lib/private/server.php3
-rw-r--r--lib/private/tagmanager.php22
-rw-r--r--lib/public/files/folder.php9
-rw-r--r--lib/public/itagmanager.php7
11 files changed, 146 insertions, 29 deletions
diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php
index 4157da2281c..9df64db7f07 100644
--- a/lib/private/files/cache/cache.php
+++ b/lib/private/files/cache/cache.php
@@ -504,6 +504,51 @@ class Cache {
}
/**
+ * 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) {
+ $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
*
* @param string|boolean $path
diff --git a/lib/private/files/cache/wrapper/cachejail.php b/lib/private/files/cache/wrapper/cachejail.php
index 7982293f5ed..f4ffc67d76a 100644
--- a/lib/private/files/cache/wrapper/cachejail.php
+++ b/lib/private/files/cache/wrapper/cachejail.php
@@ -198,6 +198,18 @@ class CacheJail extends CacheWrapper {
}
/**
+ * search for files by mimetype
+ *
+ * @param string|int $tag name or tag id
+ * @param string $userId owner of the tags
+ * @return array
+ */
+ public function searchByTag($tag, $userId) {
+ $results = $this->cache->searchByTag($tag, $userId);
+ return $this->formatSearchResults($results);
+ }
+
+ /**
* update the folder size and the size of all parent folders
*
* @param string|boolean $path
diff --git a/lib/private/files/cache/wrapper/cachewrapper.php b/lib/private/files/cache/wrapper/cachewrapper.php
index d3d64e3f0a9..83811520e4b 100644
--- a/lib/private/files/cache/wrapper/cachewrapper.php
+++ b/lib/private/files/cache/wrapper/cachewrapper.php
@@ -181,6 +181,18 @@ class CacheWrapper extends Cache {
}
/**
+ * 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) {
+ $results = $this->cache->searchByTag($tag, $userId);
+ return array_map(array($this, 'formatCacheEntry'), $results);
+ }
+
+ /**
* update the folder size and the size of all parent folders
*
* @param string|boolean $path
diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php
index 90643839e22..ed2be59c092 100644
--- a/lib/private/files/filesystem.php
+++ b/lib/private/files/filesystem.php
@@ -687,6 +687,15 @@ class Filesystem {
}
/**
+ * @param string|int $tag name or tag id
+ * @param string $userId owner of the tags
+ * @return FileInfo[] array or file info
+ */
+ static public function searchByTag($tag, $userId) {
+ return self::$defaultInstance->searchByTag($tag, $userId);
+ }
+
+ /**
* check if a file or folder has been updated since $time
*
* @param string $path
diff --git a/lib/private/files/node/folder.php b/lib/private/files/node/folder.php
index 6fdcff13e1c..bdfb2346716 100644
--- a/lib/private/files/node/folder.php
+++ b/lib/private/files/node/folder.php
@@ -223,7 +223,7 @@ class Folder extends Node implements \OCP\Files\Folder {
* @return \OC\Files\Node\Node[]
*/
public function search($query) {
- return $this->searchCommon('%' . $query . '%', 'search');
+ return $this->searchCommon('search', array('%' . $query . '%'));
}
/**
@@ -233,15 +233,26 @@ class Folder extends Node implements \OCP\Files\Folder {
* @return Node[]
*/
public function searchByMime($mimetype) {
- return $this->searchCommon($mimetype, 'searchByMime');
+ return $this->searchCommon('searchByMime', array($mimetype));
}
/**
- * @param string $query
- * @param string $method
+ * search for files by tag
+ *
+ * @param string|int $tag name or tag id
+ * @param string $userId owner of the tags
+ * @return Node[]
+ */
+ public function searchByTag($tag, $userId) {
+ return $this->searchCommon('searchByTag', array($tag, $userId));
+ }
+
+ /**
+ * @param string $method cache method
+ * @param array $args call args
* @return \OC\Files\Node\Node[]
*/
- private function searchCommon($query, $method) {
+ private function searchCommon($method, $args) {
$files = array();
$rootLength = strlen($this->path);
/**
@@ -252,7 +263,7 @@ class Folder extends Node implements \OCP\Files\Folder {
$cache = $storage->getCache('');
- $results = $cache->$method($query);
+ $results = call_user_func_array(array($cache, $method), $args);
foreach ($results as $result) {
if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
$result['internalPath'] = $result['path'];
@@ -269,7 +280,7 @@ class Folder extends Node implements \OCP\Files\Folder {
$cache = $storage->getCache('');
$relativeMountPoint = substr($mount->getMountPoint(), $rootLength);
- $results = $cache->$method($query);
+ $results = call_user_func_array(array($cache, $method), $args);
foreach ($results as $result) {
$result['internalPath'] = $result['path'];
$result['path'] = $relativeMountPoint . $result['path'];
diff --git a/lib/private/files/node/nonexistingfolder.php b/lib/private/files/node/nonexistingfolder.php
index 0346cbf1e21..04f741e8a46 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($tag, $userId) {
+ 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 93edbede607..c01763cdad3 100644
--- a/lib/private/files/view.php
+++ b/lib/private/files/view.php
@@ -1111,7 +1111,7 @@ class View {
* @return FileInfo[]
*/
public function search($query) {
- return $this->searchCommon('%' . $query . '%', 'search');
+ return $this->searchCommon('search', array('%' . $query . '%'));
}
/**
@@ -1121,7 +1121,7 @@ class View {
* @return FileInfo[]
*/
public function searchRaw($query) {
- return $this->searchCommon($query, 'search');
+ return $this->searchCommon('search', array($query));
}
/**
@@ -1131,15 +1131,26 @@ class View {
* @return FileInfo[]
*/
public function searchByMime($mimetype) {
- return $this->searchCommon($mimetype, 'searchByMime');
+ return $this->searchCommon('searchByMime', array($mimetype));
}
/**
- * @param string $query
- * @param string $method
+ * search for files by tag
+ *
+ * @param string|int $tag name or tag id
+ * @param string $userId owner of the tags
+ * @return FileInfo[]
+ */
+ public function searchByTag($tag, $userId) {
+ return $this->searchCommon('searchByTag', array($tag, $userId));
+ }
+
+ /**
+ * @param string $method cache method
+ * @param array $args
* @return FileInfo[]
*/
- private function searchCommon($query, $method) {
+ private function searchCommon($method, $args) {
$files = array();
$rootLength = strlen($this->fakeRoot);
@@ -1148,7 +1159,7 @@ class View {
if ($storage) {
$cache = $storage->getCache('');
- $results = $cache->$method($query);
+ $results = call_user_func_array(array($cache, $method), $args);
foreach ($results as $result) {
if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') {
$internalPath = $result['path'];
@@ -1165,7 +1176,7 @@ class View {
$cache = $storage->getCache('');
$relativeMountPoint = substr($mountPoint, $rootLength);
- $results = $cache->$method($query);
+ $results = call_user_func_array(array($cache, $method), $args);
if ($results) {
foreach ($results as $result) {
$internalPath = $result['path'];
diff --git a/lib/private/server.php b/lib/private/server.php
index 94444623cfb..6066e2fa35e 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -87,8 +87,7 @@ class Server extends SimpleContainer implements IServerContainer {
});
$this->registerService('TagManager', function (Server $c) {
$tagMapper = $c->query('TagMapper');
- $user = \OC_User::getUser();
- return new TagManager($tagMapper, $user);
+ return new TagManager($tagMapper, $c->getUserSession());
});
$this->registerService('RootFolder', function (Server $c) {
// TODO: get user and user manager from container as well
diff --git a/lib/private/tagmanager.php b/lib/private/tagmanager.php
index d5bff04acff..6c7eeab87eb 100644
--- a/lib/private/tagmanager.php
+++ b/lib/private/tagmanager.php
@@ -38,11 +38,11 @@ use OC\Tagging\TagMapper;
class TagManager implements \OCP\ITagManager {
/**
- * User
+ * User session
*
- * @var string
+ * @var \OCP\IUserSession
*/
- private $user;
+ private $userSession;
/**
* TagMapper
@@ -55,12 +55,11 @@ class TagManager implements \OCP\ITagManager {
* Constructor.
*
* @param TagMapper $mapper Instance of the TagMapper abstraction layer.
- * @param string $user The user whose data the object will operate on.
+ * @param \OCP\IUserSession $userSession the user session
*/
- public function __construct(TagMapper $mapper, $user) {
-
+ public function __construct(TagMapper $mapper, \OCP\IUserSession $userSession) {
$this->mapper = $mapper;
- $this->user = $user;
+ $this->userSession = $userSession;
}
@@ -71,10 +70,15 @@ class TagManager implements \OCP\ITagManager {
* @param string $type The type identifier e.g. 'contact' or 'event'.
* @param array $defaultTags An array of default tags to be used if none are stored.
* @param boolean $includeShared Whether to include tags for items shared with this user by others.
+ * @param string $userId user for which to retrieve the tags, defaults to the currently
+ * logged in user
* @return \OCP\ITags
*/
- public function load($type, $defaultTags=array(), $includeShared=false) {
- return new Tags($this->mapper, $this->user, $type, $defaultTags, $includeShared);
+ public function load($type, $defaultTags = array(), $includeShared = false, $userId = null) {
+ if (is_null($userId)) {
+ $userId = $this->userSession->getUser()->getUId();
+ }
+ return new Tags($this->mapper, $userId, $type, $defaultTags, $includeShared);
}
}
diff --git a/lib/public/files/folder.php b/lib/public/files/folder.php
index 7fec1c529a5..9797fbc46ed 100644
--- a/lib/public/files/folder.php
+++ b/lib/public/files/folder.php
@@ -117,6 +117,15 @@ interface Folder extends Node {
public function searchByMime($mimetype);
/**
+ * search for files by tag
+ *
+ * @param string|int $tag tag name or tag id
+ * @param string $userId owner of the tags
+ * @return \OCP\Files\Node[]
+ */
+ public function searchByTag($tag, $userId);
+
+ /**
* get a file or folder inside the folder by it's internal id
*
* @param int $id
diff --git a/lib/public/itagmanager.php b/lib/public/itagmanager.php
index 54daa5cc1cb..ac80eebc72d 100644
--- a/lib/public/itagmanager.php
+++ b/lib/public/itagmanager.php
@@ -43,14 +43,15 @@ namespace OCP;
interface ITagManager {
/**
- * Create a new \OCP\ITags instance and load tags from db.
+ * Create a new \OCP\ITags instance and load tags from db for the current user.
*
* @see \OCP\ITags
* @param string $type The type identifier e.g. 'contact' or 'event'.
* @param array $defaultTags An array of default tags to be used if none are stored.
* @param boolean $includeShared Whether to include tags for items shared with this user by others.
+ * @param string $userId user for which to retrieve the tags, defaults to the currently
+ * logged in user
* @return \OCP\ITags
*/
- public function load($type, $defaultTags=array(), $includeShared=false);
-
+ public function load($type, $defaultTags = array(), $includeShared = false, $userId = null);
}