1
0

TagManager.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Reiter <ockham@raz.or.at>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Tanghus <thomas@tanghus.net>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC;
  28. use OC\Tagging\TagMapper;
  29. use OCP\Db\Exception as DBException;
  30. use OCP\DB\QueryBuilder\IQueryBuilder;
  31. use OCP\EventDispatcher\Event;
  32. use OCP\EventDispatcher\IEventListener;
  33. use OCP\IDBConnection;
  34. use OCP\ITagManager;
  35. use OCP\ITags;
  36. use OCP\IUserSession;
  37. use OCP\User\Events\UserDeletedEvent;
  38. use Psr\Log\LoggerInterface;
  39. /**
  40. * @template-implements IEventListener<UserDeletedEvent>
  41. */
  42. class TagManager implements ITagManager, IEventListener {
  43. private TagMapper $mapper;
  44. private IUserSession $userSession;
  45. private IDBConnection $connection;
  46. private LoggerInterface $logger;
  47. public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection, LoggerInterface $logger) {
  48. $this->mapper = $mapper;
  49. $this->userSession = $userSession;
  50. $this->connection = $connection;
  51. $this->logger = $logger;
  52. }
  53. /**
  54. * Create a new \OCP\ITags instance and load tags from db.
  55. *
  56. * @see \OCP\ITags
  57. * @param string $type The type identifier e.g. 'contact' or 'event'.
  58. * @param array $defaultTags An array of default tags to be used if none are stored.
  59. * @param boolean $includeShared Whether to include tags for items shared with this user by others.
  60. * @param string $userId user for which to retrieve the tags, defaults to the currently
  61. * logged in user
  62. * @return \OCP\ITags
  63. *
  64. * since 20.0.0 $includeShared isn't used anymore
  65. */
  66. public function load($type, $defaultTags = [], $includeShared = false, $userId = null) {
  67. if (is_null($userId)) {
  68. $user = $this->userSession->getUser();
  69. if ($user === null) {
  70. // nothing we can do without a user
  71. return null;
  72. }
  73. $userId = $this->userSession->getUser()->getUId();
  74. }
  75. return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $defaultTags);
  76. }
  77. /**
  78. * Get all users who favorited an object
  79. *
  80. * @param string $objectType
  81. * @param int $objectId
  82. * @return array
  83. */
  84. public function getUsersFavoritingObject(string $objectType, int $objectId): array {
  85. $query = $this->connection->getQueryBuilder();
  86. $query->select('uid')
  87. ->from('vcategory_to_object', 'o')
  88. ->innerJoin('o', 'vcategory', 'c', $query->expr()->eq('o.categoryid', 'c.id'))
  89. ->where($query->expr()->eq('objid', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT)))
  90. ->andWhere($query->expr()->eq('c.type', $query->createNamedParameter($objectType)))
  91. ->andWhere($query->expr()->eq('c.category', $query->createNamedParameter(ITags::TAG_FAVORITE)));
  92. $result = $query->execute();
  93. $users = $result->fetchAll(\PDO::FETCH_COLUMN);
  94. $result->closeCursor();
  95. return $users;
  96. }
  97. public function handle(Event $event): void {
  98. if (!($event instanceof UserDeletedEvent)) {
  99. return;
  100. }
  101. // Find all objectid/tagId pairs.
  102. $user = $event->getUser();
  103. $qb = $this->connection->getQueryBuilder();
  104. $qb->select('id')
  105. ->from('vcategory')
  106. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())));
  107. try {
  108. $result = $qb->executeQuery();
  109. } catch (DBException $e) {
  110. $this->logger->error($e->getMessage(), [
  111. 'app' => 'core',
  112. 'exception' => $e,
  113. ]);
  114. return;
  115. }
  116. $tagsIds = array_map(fn (array $row) => (int)$row['id'], $result->fetchAll());
  117. $result->closeCursor();
  118. if (count($tagsIds) === 0) {
  119. return;
  120. }
  121. // Clean vcategory_to_object table
  122. $qb = $this->connection->getQueryBuilder();
  123. $qb = $qb->delete('vcategory_to_object')
  124. ->where($qb->expr()->in('categoryid', $qb->createParameter('chunk')));
  125. // Clean vcategory
  126. $qb1 = $this->connection->getQueryBuilder();
  127. $qb1 = $qb1->delete('vcategory')
  128. ->where($qb1->expr()->in('uid', $qb1->createParameter('chunk')));
  129. foreach (array_chunk($tagsIds, 1000) as $tagChunk) {
  130. $qb->setParameter('chunk', $tagChunk, IQueryBuilder::PARAM_INT_ARRAY);
  131. $qb1->setParameter('chunk', $tagChunk, IQueryBuilder::PARAM_INT_ARRAY);
  132. try {
  133. $qb->executeStatement();
  134. $qb1->executeStatement();
  135. } catch (DBException $e) {
  136. $this->logger->error($e->getMessage(), [
  137. 'app' => 'core',
  138. 'exception' => $e,
  139. ]);
  140. }
  141. }
  142. }
  143. }