diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-12-12 14:27:19 +0100 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-12-12 14:27:19 +0100 |
commit | 4b57892c4eeeb7bd3c8b1b0465acf032dc84fd19 (patch) | |
tree | fbad2095651334fc34b3729ae5102ad5e1cd866f /apps/files_sharing | |
parent | 6b4502adebf1d756707e8fb5b8ab697c1c746e6b (diff) | |
parent | 3878c3782ff37dc56a4e463acea351a61ed7b4c2 (diff) | |
download | nextcloud-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 'apps/files_sharing')
-rw-r--r-- | apps/files_sharing/lib/cache.php | 40 | ||||
-rw-r--r-- | apps/files_sharing/tests/cache.php | 34 |
2 files changed, 74 insertions, 0 deletions
diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index e09b64cb039..e3bee145876 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -345,6 +345,46 @@ class Shared_Cache extends Cache { } /** + * 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) { + // 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 * * @param string $path diff --git a/apps/files_sharing/tests/cache.php b/apps/files_sharing/tests/cache.php index aec1983bad3..b60bba73db8 100644 --- a/apps/files_sharing/tests/cache.php +++ b/apps/files_sharing/tests/cache.php @@ -204,6 +204,40 @@ class Test_Files_Sharing_Cache extends TestCase { $this->verifyFiles($check, $results); } + /** + * 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', 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', $userId); + $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('/'); |