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 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'apps/files_sharing/lib/cache.php') 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 * -- cgit v1.2.3