From 25dde7e93bc648ec8cd14b8f2711d50f77d8d1bf Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 4 Dec 2014 14:01:15 +0100 Subject: [PATCH] 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 ++++++++++++++++ .../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 e09b64cb03..da8155ec6f 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 aec1983bad..c40a014d55 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 4157da2281..a4ae3a069f 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 d3d64e3f0a..4da7c7ecf6 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 90643839e2..3d55564f0c 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 6fdcff13e1..a65e641388 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 0346cbf1e2..9d452a94b9 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 4b3d167f8e..7090e03d40 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 7fec1c529a..f54602d469 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 7e44cb898a..1af8e4da96 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';