From 11b3aa2dbe792c306dab6f17b9633393ae739090 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 18 Dec 2014 17:27:56 +0100 Subject: Fix issues with searchByTag in shared storage --- apps/files_sharing/lib/cache.php | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'apps/files_sharing/lib') diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php index e3bee145876..21f807f3533 100644 --- a/apps/files_sharing/lib/cache.php +++ b/apps/files_sharing/lib/cache.php @@ -344,6 +344,20 @@ class Shared_Cache extends Cache { return $result; } + /** + * Checks whether the given file has the given tag. + * + * @param \OCP\ITags $tagger + * @param array $fileData file data + * @param string $tag tag to check for + * @return boolean true if the given file has the expected tag, + * false otherwise + */ + private function hasTag($tagger, $fileData, $tag) { + $tags = $tagger->getTagsForObjects(array((int)$fileData['fileid'])); + return (!empty($tags) && in_array($tag, current($tags))); + } + /** * search for files by tag * @@ -356,28 +370,24 @@ class Shared_Cache extends Cache { $tagger = \OC::$server->getTagManager()->load('files', null, null, $userId); $result = array(); $exploreDirs = array(''); + // check if root is tagged + $file = $this->get(''); + if ($this->hasTag($tagger, $file, $tag)) { + $result[] = $file; + } // 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 ($this->hasTag($tagger, $file, $tag)) { + $result[] = $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; - } } } } -- cgit v1.2.3