summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-12-04 14:01:15 +0100
committerVincent Petry <pvince81@owncloud.com>2014-12-11 17:38:50 +0100
commit25dde7e93bc648ec8cd14b8f2711d50f77d8d1bf (patch)
treef45b35dd647870b6c3aeded3e8e11faa41effcba /apps
parent745d8706b973ff0494af54f183acc0da361f0e83 (diff)
downloadnextcloud-server-25dde7e93bc648ec8cd14b8f2711d50f77d8d1bf.tar.gz
nextcloud-server-25dde7e93bc648ec8cd14b8f2711d50f77d8d1bf.zip
Added searchByTags to view, storage and cache
Diffstat (limited to 'apps')
-rw-r--r--apps/files_sharing/lib/cache.php40
-rw-r--r--apps/files_sharing/tests/cache.php33
2 files changed, 73 insertions, 0 deletions
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
@@ -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 = 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
*
* @param string $path
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('/');