SystemTagObjectMapper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\SystemTag;
  24. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IDBConnection;
  27. use OCP\SystemTag\ISystemTag;
  28. use OCP\SystemTag\ISystemTagManager;
  29. use OCP\SystemTag\ISystemTagObjectMapper;
  30. use OCP\SystemTag\MapperEvent;
  31. use OCP\SystemTag\TagNotFoundException;
  32. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  33. class SystemTagObjectMapper implements ISystemTagObjectMapper {
  34. const RELATION_TABLE = 'systemtag_object_mapping';
  35. /** @var ISystemTagManager */
  36. protected $tagManager;
  37. /** @var IDBConnection */
  38. protected $connection;
  39. /** @var EventDispatcherInterface */
  40. protected $dispatcher;
  41. /**
  42. * Constructor.
  43. *
  44. * @param IDBConnection $connection database connection
  45. * @param ISystemTagManager $tagManager system tag manager
  46. * @param EventDispatcherInterface $dispatcher
  47. */
  48. public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) {
  49. $this->connection = $connection;
  50. $this->tagManager = $tagManager;
  51. $this->dispatcher = $dispatcher;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getTagIdsForObjects($objIds, $objectType) {
  57. if (!is_array($objIds)) {
  58. $objIds = [$objIds];
  59. } else if (empty($objIds)) {
  60. return [];
  61. }
  62. $query = $this->connection->getQueryBuilder();
  63. $query->select(['systemtagid', 'objectid'])
  64. ->from(self::RELATION_TABLE)
  65. ->where($query->expr()->in('objectid', $query->createParameter('objectids')))
  66. ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
  67. ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_INT_ARRAY)
  68. ->setParameter('objecttype', $objectType)
  69. ->addOrderBy('objectid', 'ASC')
  70. ->addOrderBy('systemtagid', 'ASC');
  71. $mapping = [];
  72. foreach ($objIds as $objId) {
  73. $mapping[$objId] = [];
  74. }
  75. $result = $query->execute();
  76. while ($row = $result->fetch()) {
  77. $objectId = $row['objectid'];
  78. $mapping[$objectId][] = $row['systemtagid'];
  79. }
  80. $result->closeCursor();
  81. return $mapping;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getObjectIdsForTags($tagIds, $objectType, $limit = 0, $offset = '') {
  87. if (!is_array($tagIds)) {
  88. $tagIds = [$tagIds];
  89. }
  90. $this->assertTagsExist($tagIds);
  91. $query = $this->connection->getQueryBuilder();
  92. $query->selectDistinct('objectid')
  93. ->from(self::RELATION_TABLE)
  94. ->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY)))
  95. ->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType)));
  96. if ($limit) {
  97. if (sizeof($tagIds) !== 1) {
  98. throw new \InvalidArgumentException('Limit is only allowed with a single tag');
  99. }
  100. $query->setMaxResults($limit)
  101. ->orderBy('objectid', 'ASC');
  102. if ($offset !== '') {
  103. $query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset)));
  104. }
  105. }
  106. $objectIds = [];
  107. $result = $query->execute();
  108. while ($row = $result->fetch()) {
  109. $objectIds[] = $row['objectid'];
  110. }
  111. return $objectIds;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function assignTags($objId, $objectType, $tagIds) {
  117. if (!is_array($tagIds)) {
  118. $tagIds = [$tagIds];
  119. }
  120. $this->assertTagsExist($tagIds);
  121. $query = $this->connection->getQueryBuilder();
  122. $query->insert(self::RELATION_TABLE)
  123. ->values([
  124. 'objectid' => $query->createNamedParameter($objId),
  125. 'objecttype' => $query->createNamedParameter($objectType),
  126. 'systemtagid' => $query->createParameter('tagid'),
  127. ]);
  128. foreach ($tagIds as $tagId) {
  129. try {
  130. $query->setParameter('tagid', $tagId);
  131. $query->execute();
  132. } catch (UniqueConstraintViolationException $e) {
  133. // ignore existing relations
  134. }
  135. }
  136. $this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent(
  137. MapperEvent::EVENT_ASSIGN,
  138. $objectType,
  139. $objId,
  140. $tagIds
  141. ));
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function unassignTags($objId, $objectType, $tagIds) {
  147. if (!is_array($tagIds)) {
  148. $tagIds = [$tagIds];
  149. }
  150. $this->assertTagsExist($tagIds);
  151. $query = $this->connection->getQueryBuilder();
  152. $query->delete(self::RELATION_TABLE)
  153. ->where($query->expr()->eq('objectid', $query->createParameter('objectid')))
  154. ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
  155. ->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids')))
  156. ->setParameter('objectid', $objId)
  157. ->setParameter('objecttype', $objectType)
  158. ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
  159. ->execute();
  160. $this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent(
  161. MapperEvent::EVENT_UNASSIGN,
  162. $objectType,
  163. $objId,
  164. $tagIds
  165. ));
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function haveTag($objIds, $objectType, $tagId, $all = true) {
  171. $this->assertTagsExist([$tagId]);
  172. if (!is_array($objIds)) {
  173. $objIds = [$objIds];
  174. }
  175. $query = $this->connection->getQueryBuilder();
  176. if (!$all) {
  177. // If we only need one entry, we make the query lighter, by not
  178. // counting the elements
  179. $query->select('*')
  180. ->setMaxResults(1);
  181. } else {
  182. $query->select($query->createFunction('COUNT(1)'));
  183. }
  184. $query->from(self::RELATION_TABLE)
  185. ->where($query->expr()->in('objectid', $query->createParameter('objectids')))
  186. ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
  187. ->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid')))
  188. ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY)
  189. ->setParameter('tagid', $tagId)
  190. ->setParameter('objecttype', $objectType);
  191. $result = $query->execute();
  192. $row = $result->fetch(\PDO::FETCH_NUM);
  193. $result->closeCursor();
  194. if ($all) {
  195. return ((int)$row[0] === count($objIds));
  196. } else {
  197. return (bool) $row;
  198. }
  199. }
  200. /**
  201. * Asserts that all the given tag ids exist.
  202. *
  203. * @param string[] $tagIds tag ids to check
  204. *
  205. * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist
  206. */
  207. private function assertTagsExist($tagIds) {
  208. $tags = $this->tagManager->getTagsByIds($tagIds);
  209. if (count($tags) !== count($tagIds)) {
  210. // at least one tag missing, bail out
  211. $foundTagIds = array_map(
  212. function(ISystemTag $tag) {
  213. return $tag->getId();
  214. },
  215. $tags
  216. );
  217. $missingTagIds = array_diff($tagIds, $foundTagIds);
  218. throw new TagNotFoundException(
  219. 'Tags not found', 0, null, $missingTagIds
  220. );
  221. }
  222. }
  223. }