From 5101bc54faea28d1210eb24f72abd76944c83af6 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 4 Dec 2014 14:01:23 +0100 Subject: Add login/logout to TestCase base class --- tests/lib/testcase.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index 27c28329535..1ea3aa13547 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -148,4 +148,25 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { \OC_FileProxy::$enabled = true; \OC_FileProxy::clearProxies(); } + + /** + * Login and setup FS as a given user, + * sets the given user as the current user. + * + * @param string $user user id + */ + static protected function loginAsUser($user) { + self::logout(); + \OC\Files\Filesystem::tearDown(); + \OC_User::setUserId($user); + \OC_Util::setupFS($user); + } + + /** + * Logout the current user and tear down the filesystem. + */ + static protected function logout() { + \OC_Util::tearDownFS(); + \OC_User::setUserId(''); + } } -- cgit v1.2.3 From 745d8706b973ff0494af54f183acc0da361f0e83 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 10 Dec 2014 15:59:41 +0100 Subject: Add user parameter to tag manager --- lib/private/server.php | 3 +-- lib/private/tagmanager.php | 22 +++++++++------- lib/public/itagmanager.php | 7 +++--- tests/lib/tags.php | 62 ++++++++++++++++++++++++++++------------------ 4 files changed, 56 insertions(+), 38 deletions(-) diff --git a/lib/private/server.php b/lib/private/server.php index e0105506970..02cc4e2755a 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/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); } diff --git a/tests/lib/tags.php b/tests/lib/tags.php index 78f5085df39..71296d2e346 100644 --- a/tests/lib/tags.php +++ b/tests/lib/tags.php @@ -23,7 +23,10 @@ class Test_Tags extends \Test\TestCase { protected $objectType; + /** @var \OC\IUser */ protected $user; + /** @var \OC\IUserSession */ + protected $userSession; protected $backupGlobals = FALSE; /** @var \OC\Tagging\TagMapper */ protected $tagMapper; @@ -35,12 +38,19 @@ class Test_Tags extends \Test\TestCase { OC_User::clearBackends(); OC_User::useBackend('dummy'); - $this->user = $this->getUniqueID('user_'); + $userId = $this->getUniqueID('user_'); + OC_User::createUser($userId, 'pass'); + OC_User::setUserId($userId); + $this->user = new OC\User\User($userId, null); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->userSession + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $this->objectType = $this->getUniqueID('type_'); - OC_User::createUser($this->user, 'pass'); - OC_User::setUserId($this->user); $this->tagMapper = new OC\Tagging\TagMapper(\OC::$server->getDb()); - $this->tagMgr = new OC\TagManager($this->tagMapper, $this->user); + $this->tagMgr = new OC\TagManager($this->tagMapper, $this->userSession); } @@ -166,7 +176,7 @@ class Test_Tags extends \Test\TestCase { ); } - public function testdeleteTags() { + public function testDeleteTags() { $defaultTags = array('Friends', 'Family', 'Work', 'Other'); $tagger = $this->tagMgr->load($this->objectType, $defaultTags); @@ -177,7 +187,6 @@ class Test_Tags extends \Test\TestCase { $tagger->delete(array('Friends', 'Work', 'Other')); $this->assertEquals(0, count($tagger->getTags())); - } public function testRenameTag() { @@ -233,27 +242,32 @@ class Test_Tags extends \Test\TestCase { } public function testShareTags() { - $test_tag = 'TestTag'; + $testTag = 'TestTag'; OCP\Share::registerBackend('test', 'Test_Share_Backend'); $tagger = $this->tagMgr->load('test'); - $tagger->tagAs(1, $test_tag); - - $other_user = $this->getUniqueID('user2_'); - OC_User::createUser($other_user, 'pass'); - - OC_User::setUserId($other_user); - $other_tagMgr = new OC\TagManager($this->tagMapper, $other_user); - $other_tagger = $other_tagMgr->load('test'); - $this->assertFalse($other_tagger->hasTag($test_tag)); - - OC_User::setUserId($this->user); - OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $other_user, \OCP\Constants::PERMISSION_READ); - - OC_User::setUserId($other_user); - $other_tagger = $other_tagMgr->load('test', array(), true); // Update tags, load shared ones. - $this->assertTrue($other_tagger->hasTag($test_tag)); - $this->assertContains(1, $other_tagger->getIdsForTag($test_tag)); + $tagger->tagAs(1, $testTag); + + $otherUserId = $this->getUniqueID('user2_'); + OC_User::createUser($otherUserId, 'pass'); + OC_User::setUserId($otherUserId); + $otherUserSession = $this->getMock('\OCP\IUserSession'); + $otherUserSession + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue(new OC\User\User($otherUserId, null))); + + $otherTagMgr = new OC\TagManager($this->tagMapper, $otherUserSession); + $otherTagger = $otherTagMgr->load('test'); + $this->assertFalse($otherTagger->hasTag($testTag)); + + OC_User::setUserId($this->user->getUID()); + OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $otherUserId, \OCP\Constants::PERMISSION_READ); + + OC_User::setUserId($otherUserId); + $otherTagger = $otherTagMgr->load('test', array(), true); // Update tags, load shared ones. + $this->assertTrue($otherTagger->hasTag($testTag)); + $this->assertContains(1, $otherTagger->getIdsForTag($testTag)); } } -- cgit v1.2.3 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 --- apps/files_sharing/lib/cache.php | 40 +++++++++++++++++ apps/files_sharing/tests/cache.php | 33 ++++++++++++++ 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 +++++ lib/public/files/folder.php | 8 ++++ tests/lib/files/cache/cache.php | 57 ++++++++++++++++++++++++ 10 files changed, 230 insertions(+) diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index e09b64cb039..da8155ec6fa 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -344,6 +344,46 @@ class Shared_Cache extends Cache { return $result; } + /** + * search for files by tag + * + * @param string|int $tag tag to search for + * @param string $userId owner of the tags + * @return array file data + */ + public function searchByTag($tag, $userId = null) { + // TODO: inject this + $tagger = \OC::$server->getTagManager()->load('files', null, null, $userId); + $result = array(); + $exploreDirs = array(''); + // FIXME: this is so wrong and unefficient, need to replace with actual DB queries + while (count($exploreDirs) > 0) { + $dir = array_pop($exploreDirs); + $files = $this->getFolderContents($dir); + // no results? + if (!$files) { + // maybe it's a single shared file + $file = $this->get(''); + $tags = $tagger->getTagsForObjects(array((int)$file['fileid'])); + if (!empty($tags) && in_array($tag, current($tags))) { + $result[] = $file; + } + continue; + } + foreach ($files as $file) { + if ($file['mimetype'] === 'httpd/unix-directory') { + $exploreDirs[] = ltrim($dir . '/' . $file['name'], '/'); + } else { + $tags = $tagger->getTagsForObjects(array((int)$file['fileid'])); + if (!empty($tags) && in_array($tag, current($tags))) { + $result[] = $file; + } + } + } + } + return $result; + } + /** * get the size of a folder and set it in the cache * diff --git a/apps/files_sharing/tests/cache.php b/apps/files_sharing/tests/cache.php index aec1983bad3..c40a014d557 100644 --- a/apps/files_sharing/tests/cache.php +++ b/apps/files_sharing/tests/cache.php @@ -204,6 +204,39 @@ class Test_Files_Sharing_Cache extends TestCase { $this->verifyFiles($check, $results); } + /** + * Test searching by tag + */ + function testSearchByTag() { + $id1 = $this->sharedCache->get('bar.txt')['fileid']; + $id2 = $this->sharedCache->get('subdir/another too.txt')['fileid']; + $id3 = $this->sharedCache->get('subdir/not a text file.xml')['fileid']; + $id4 = $this->sharedCache->get('subdir/another.txt')['fileid']; + $tagManager = \OC::$server->getTagManager()->load('files'); + $tagManager->tagAs($id1, 'tag1'); + $tagManager->tagAs($id1, 'tag2'); + $tagManager->tagAs($id2, 'tag1'); + $tagManager->tagAs($id3, 'tag1'); + $tagManager->tagAs($id4, 'tag2'); + $results = $this->sharedStorage->getCache()->searchByTag('tag1'); + $check = array( + array( + 'name' => 'bar.txt', + 'path' => 'bar.txt' + ), + array( + 'name' => 'another too.txt', + 'path' => 'subdir/another too.txt' + ), + array( + 'name' => 'not a text file.xml', + 'path' => 'subdir/not a text file.xml' + ), + ); + $this->verifyFiles($check, $results); + $tagManager->delete(array('tag1', 'tag2')); + } + function testGetFolderContentsInRoot() { $results = $this->user2View->getDirectoryContent('/'); 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 diff --git a/lib/public/files/folder.php b/lib/public/files/folder.php index 7fec1c529a5..f54602d469d 100644 --- a/lib/public/files/folder.php +++ b/lib/public/files/folder.php @@ -116,6 +116,14 @@ interface Folder extends Node { */ public function searchByMime($mimetype); + /** + * search for files by tag + * + * @param string|int $tag tag name or tag id + * @return \OCP\Files\Node[] + */ + public function searchByTag($tag); + /** * get a file or folder inside the folder by it's internal id * diff --git a/tests/lib/files/cache/cache.php b/tests/lib/files/cache/cache.php index 7e44cb898ac..1af8e4da960 100644 --- a/tests/lib/files/cache/cache.php +++ b/tests/lib/files/cache/cache.php @@ -270,6 +270,63 @@ class Cache extends \Test\TestCase { $this->assertEquals(2, count($this->cache->searchByMime('foo/file'))); } + function testSearchByTag() { + $userId = $this->getUniqueId('user'); + \OC_User::createUser($userId, $userId); + $this->loginAsUser($userId); + $user = new \OC\User\User($userId, null); + + $file1 = 'folder'; + $file2 = 'folder/foobar'; + $file3 = 'folder/foo'; + $file4 = 'folder/foo2'; + $file5 = 'folder/foo3'; + $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'); + $fileData = array(); + $fileData['foobar'] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'); + $fileData['foo'] = array('size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'); + $fileData['foo2'] = array('size' => 25, 'mtime' => 28, 'mimetype' => 'foo/file'); + $fileData['foo3'] = array('size' => 88, 'mtime' => 34, 'mimetype' => 'foo/file'); + + $id1 = $this->cache->put($file1, $data1); + $id2 = $this->cache->put($file2, $fileData['foobar']); + $id3 = $this->cache->put($file3, $fileData['foo']); + $id4 = $this->cache->put($file4, $fileData['foo2']); + $id5 = $this->cache->put($file5, $fileData['foo3']); + + $tagManager = \OC::$server->getTagManager()->load('files', null, null, $userId); + $this->assertTrue($tagManager->tagAs($id1, 'tag1')); + $this->assertTrue($tagManager->tagAs($id1, 'tag2')); + $this->assertTrue($tagManager->tagAs($id2, 'tag2')); + $this->assertTrue($tagManager->tagAs($id3, 'tag1')); + $this->assertTrue($tagManager->tagAs($id4, 'tag2')); + + // use tag name + $results = $this->cache->searchByTag('tag1', $userId); + + $this->assertEquals(2, count($results)); + + $this->assertEquals('folder', $results[0]['name']); + $this->assertEquals('foo', $results[1]['name']); + + // use tag id + $tags = $tagManager->getTagsForUser($userId); + $this->assertNotEmpty($tags); + $tags = array_filter($tags, function($tag) { return $tag->getName() === 'tag2'; }); + $results = $this->cache->searchByTag(current($tags)->getId(), $userId); + $this->assertEquals(3, count($results)); + + $this->assertEquals('folder', $results[0]['name']); + $this->assertEquals('foobar', $results[1]['name']); + $this->assertEquals('foo2', $results[2]['name']); + + $tagManager->delete('tag1'); + $tagManager->delete('tag2'); + + $this->logout(); + \OC_User::deleteUser($userId); + } + function testMove() { $file1 = 'folder'; $file2 = 'folder/bar'; -- cgit v1.2.3 From 15ecb28d50e5b8ce4100075caa52d96d4f00ae13 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Fri, 12 Dec 2014 11:18:35 +0100 Subject: Make $userId mandatory for searchByTags $userId is now a mandatory parameter for searchByTags. Also fixed some places in the code where the argument was missing (Node API and View) --- apps/files_sharing/lib/cache.php | 2 +- apps/files_sharing/tests/cache.php | 5 +-- lib/private/files/cache/cache.php | 5 +-- lib/private/files/cache/wrapper/cachewrapper.php | 2 +- lib/private/files/filesystem.php | 5 +-- lib/private/files/node/folder.php | 21 +++++++------ lib/private/files/node/nonexistingfolder.php | 2 +- lib/private/files/view.php | 21 +++++++------ lib/public/files/folder.php | 3 +- tests/lib/files/node/folder.php | 39 ++++++++++++++++++++++++ 10 files changed, 73 insertions(+), 32 deletions(-) diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index da8155ec6fa..e3bee145876 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -351,7 +351,7 @@ class Shared_Cache extends Cache { * @param string $userId owner of the tags * @return array file data */ - public function searchByTag($tag, $userId = null) { + public function searchByTag($tag, $userId) { // TODO: inject this $tagger = \OC::$server->getTagManager()->load('files', null, null, $userId); $result = array(); diff --git a/apps/files_sharing/tests/cache.php b/apps/files_sharing/tests/cache.php index c40a014d557..b60bba73db8 100644 --- a/apps/files_sharing/tests/cache.php +++ b/apps/files_sharing/tests/cache.php @@ -208,17 +208,18 @@ class Test_Files_Sharing_Cache extends TestCase { * Test searching by tag */ function testSearchByTag() { + $userId = \OC::$server->getUserSession()->getUser()->getUId(); $id1 = $this->sharedCache->get('bar.txt')['fileid']; $id2 = $this->sharedCache->get('subdir/another too.txt')['fileid']; $id3 = $this->sharedCache->get('subdir/not a text file.xml')['fileid']; $id4 = $this->sharedCache->get('subdir/another.txt')['fileid']; - $tagManager = \OC::$server->getTagManager()->load('files'); + $tagManager = \OC::$server->getTagManager()->load('files', null, null, $userId); $tagManager->tagAs($id1, 'tag1'); $tagManager->tagAs($id1, 'tag2'); $tagManager->tagAs($id2, 'tag1'); $tagManager->tagAs($id3, 'tag1'); $tagManager->tagAs($id4, 'tag2'); - $results = $this->sharedStorage->getCache()->searchByTag('tag1'); + $results = $this->sharedStorage->getCache()->searchByTag('tag1', $userId); $check = array( array( 'name' => 'bar.txt', diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index a4ae3a069fe..9df64db7f07 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -512,10 +512,7 @@ class Cache { * @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(); - } + public function searchByTag($tag, $userId) { $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . '`mimetype`, `mimepart`, `size`, `mtime`, ' . '`encrypted`, `unencrypted_size`, `etag`, `permissions` ' . diff --git a/lib/private/files/cache/wrapper/cachewrapper.php b/lib/private/files/cache/wrapper/cachewrapper.php index 4da7c7ecf6f..83811520e4b 100644 --- a/lib/private/files/cache/wrapper/cachewrapper.php +++ b/lib/private/files/cache/wrapper/cachewrapper.php @@ -187,7 +187,7 @@ class CacheWrapper extends Cache { * @param string $userId owner of the tags * @return array file data */ - public function searchByTag($tag, $userId = null) { + public function searchByTag($tag, $userId) { $results = $this->cache->searchByTag($tag, $userId); return array_map(array($this, 'formatCacheEntry'), $results); } diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php index 3d55564f0c6..ed2be59c092 100644 --- a/lib/private/files/filesystem.php +++ b/lib/private/files/filesystem.php @@ -688,10 +688,11 @@ 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) { - return self::$defaultInstance->searchByTag($tag); + static public function searchByTag($tag, $userId) { + return self::$defaultInstance->searchByTag($tag, $userId); } /** diff --git a/lib/private/files/node/folder.php b/lib/private/files/node/folder.php index a65e641388d..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,25 +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)); } /** * search for files by tag * - * @param string $tag + * @param string|int $tag name or tag id + * @param string $userId owner of the tags * @return Node[] */ - public function searchByTag($tag) { - return $this->searchCommon($tag, 'searchByTag'); + public function searchByTag($tag, $userId) { + return $this->searchCommon('searchByTag', array($tag, $userId)); } /** - * @param string $query - * @param string $method + * @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); /** @@ -262,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']; @@ -279,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 9d452a94b9c..04f741e8a46 100644 --- a/lib/private/files/node/nonexistingfolder.php +++ b/lib/private/files/node/nonexistingfolder.php @@ -99,7 +99,7 @@ class NonExistingFolder extends Folder { throw new NotFoundException(); } - public function searchByTag($mime) { + public function searchByTag($tag, $userId) { throw new NotFoundException(); } diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 7090e03d40c..73faf261c14 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,25 +1131,26 @@ class View { * @return FileInfo[] */ public function searchByMime($mimetype) { - return $this->searchCommon($mimetype, 'searchByMime'); + return $this->searchCommon('searchByMime', array($mimetype)); } /** * 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) { - return $this->searchCommon($tag, 'searchByTag'); + public function searchByTag($tag, $userId) { + return $this->searchCommon('searchByTag', array($tag, $userId)); } /** - * @param string $query - * @param string $method + * @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); @@ -1158,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']; @@ -1175,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/public/files/folder.php b/lib/public/files/folder.php index f54602d469d..9797fbc46ed 100644 --- a/lib/public/files/folder.php +++ b/lib/public/files/folder.php @@ -120,9 +120,10 @@ interface Folder extends Node { * 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); + public function searchByTag($tag, $userId); /** * get a file or folder inside the folder by it's internal id diff --git a/tests/lib/files/node/folder.php b/tests/lib/files/node/folder.php index d8c047a2b75..e69a2776979 100644 --- a/tests/lib/files/node/folder.php +++ b/tests/lib/files/node/folder.php @@ -405,6 +405,45 @@ class Folder extends \Test\TestCase { $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); } + public function testSearchByTag() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + + $storage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $cache->expects($this->once()) + ->method('searchByTag') + ->with('tag1', 'user1') + ->will($this->returnValue(array( + array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain') + ))); + + $root->expects($this->once()) + ->method('getMountsIn') + ->with('/bar/foo') + ->will($this->returnValue(array())); + + $view->expects($this->once()) + ->method('resolvePath') + ->will($this->returnValue(array($storage, 'foo'))); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $result = $node->searchByTag('tag1', 'user1'); + $this->assertEquals(1, count($result)); + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); + } + public function testSearchSubStorages() { $manager = $this->getMock('\OC\Files\Mount\Manager'); /** -- cgit v1.2.3 From 3878c3782ff37dc56a4e463acea351a61ed7b4c2 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Fri, 12 Dec 2014 11:48:42 +0100 Subject: Added searchByTag in cache jail wrapper --- lib/private/files/cache/wrapper/cachejail.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 @@ -197,6 +197,18 @@ class CacheJail extends CacheWrapper { return $this->formatSearchResults($results); } + /** + * 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 * -- cgit v1.2.3