SystemTagObjectMapper.php 7.5 KB

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