]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add getTagsForObjects in ITags
authorVincent Petry <pvince81@owncloud.com>
Mon, 24 Nov 2014 20:51:45 +0000 (21:51 +0100)
committerVincent Petry <pvince81@owncloud.com>
Mon, 8 Dec 2014 21:02:00 +0000 (22:02 +0100)
Returns the list of tags that are set on the given object ids.

lib/private/tags.php
lib/public/itags.php
tests/lib/tags.php

index bab3d4952825b08c1c08c8d86ea3b5cc3125dd61..e00c1b90ca9ae5232259d03605bd495ded257415 100644 (file)
@@ -199,6 +199,48 @@ class Tags implements \OCP\ITags {
                );
        }
 
+       /**
+        * Get the list of tags for the given ids.
+        *
+        * @param array $objIds array of object ids
+        * @return array|boolean of tags id as key to array of tag names
+        * or false if an error occurred
+        */
+       public function getTagsForObjects(array $objIds) {
+               $entries = array();
+
+               try {
+                       $conn = \OC_DB::getConnection();
+                       $chunks = array_chunk($objIds, 1000, false);
+                       foreach ($chunks as $chunk) {
+                               $result = $conn->executeQuery(
+                                       'SELECT `category`, `categoryid`, `objid` ' .
+                                       'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' .
+                                       'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)',
+                                       array($this->user, $this->type, $chunk),
+                                       array(null, null, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY)
+                               );
+                               while ($row = $result->fetch()) {
+                                       $objId = (int)$row['objid'];
+                                       if (!isset($entries[$objId])) {
+                                               $entry = $entries[$objId] = array();
+                                       }
+                                       $entry = $entries[$objId][] = $row['category'];
+                               }
+                       }
+                       if (\OCP\DB::isError($result)) {
+                               \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR);
+                               return false;
+                       }
+               } catch(\Exception $e) {
+                       \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+                               \OCP\Util::ERROR);
+                       return false;
+               }
+
+               return $entries;
+       }
+
        /**
        * Get the a list if items tagged with $tag.
        *
index 4514746bbe88d56cc56b16280718118cb273265a..238b12c64247eaa21decf35a1f527012e771d595 100644 (file)
@@ -76,7 +76,23 @@ interface ITags {
        public function getTags();
 
        /**
-       * Get the a list if items tagged with $tag.
+        * Get a list of tags for the given item ids.
+        *
+        * This returns an array with object id / tag names:
+        * [
+        *   1 => array('First tag', 'Second tag'),
+        *   2 => array('Second tag'),
+        *   3 => array('Second tag', 'Third tag'),
+        * ]
+        *
+        * @param array $objIds item ids
+        * @return array|boolean with object id as key and an array
+        * of tag names as value or false if an error occurred
+        */
+       public function getTagsForObjects(array $objIds);
+
+       /**
+       * Get a list of items tagged with $tag.
        *
        * Throws an exception if the tag could not be found.
        *
index ab714bde3dfef23fff7483cc8fb0b35f9ac00e97..e7bb4db29e6965d952578f59ed04e636e5ccec87 100644 (file)
@@ -132,6 +132,36 @@ class Test_Tags extends \Test\TestCase {
                $this->assertFalse($tagger->isEmpty());
        }
 
+       public function testGetTagsForObjects() {
+               $defaultTags = array('Friends', 'Family', 'Work', 'Other');
+               $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
+
+               $tagger->tagAs(1, 'Friends');
+               $tagger->tagAs(1, 'Other');
+               $tagger->tagAs(2, 'Family');
+
+               $tags = $tagger->getTagsForObjects(array(1));
+               $this->assertEquals(1, count($tags));
+               $tags = current($tags);
+               sort($tags);
+               $this->assertSame(array('Friends', 'Other'), $tags);
+
+               $tags = $tagger->getTagsForObjects(array(1, 2));
+               $this->assertEquals(2, count($tags));
+               $tags1 = $tags[1];
+               sort($tags1);
+               $this->assertSame(array('Friends', 'Other'), $tags1);
+               $this->assertSame(array('Family'), $tags[2]);
+               $this->assertEquals(
+                       array(),
+                       $tagger->getTagsForObjects(array(4))
+               );
+               $this->assertEquals(
+                       array(),
+                       $tagger->getTagsForObjects(array(4, 5))
+               );
+       }
+
        public function testdeleteTags() {
                $defaultTags = array('Friends', 'Family', 'Work', 'Other');
                $tagger = $this->tagMgr->load($this->objectType, $defaultTags);