TagManager.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use OC\Tagging\TagMapper;
  9. use OCP\Db\Exception as DBException;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\EventDispatcher\Event;
  12. use OCP\EventDispatcher\IEventListener;
  13. use OCP\IDBConnection;
  14. use OCP\ITagManager;
  15. use OCP\ITags;
  16. use OCP\IUserSession;
  17. use OCP\User\Events\UserDeletedEvent;
  18. use Psr\Log\LoggerInterface;
  19. /**
  20. * @template-implements IEventListener<UserDeletedEvent>
  21. */
  22. class TagManager implements ITagManager, IEventListener {
  23. private TagMapper $mapper;
  24. private IUserSession $userSession;
  25. private IDBConnection $connection;
  26. private LoggerInterface $logger;
  27. public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection, LoggerInterface $logger) {
  28. $this->mapper = $mapper;
  29. $this->userSession = $userSession;
  30. $this->connection = $connection;
  31. $this->logger = $logger;
  32. }
  33. /**
  34. * Create a new \OCP\ITags instance and load tags from db.
  35. *
  36. * @see \OCP\ITags
  37. * @param string $type The type identifier e.g. 'contact' or 'event'.
  38. * @param array $defaultTags An array of default tags to be used if none are stored.
  39. * @param boolean $includeShared Whether to include tags for items shared with this user by others.
  40. * @param string $userId user for which to retrieve the tags, defaults to the currently
  41. * logged in user
  42. * @return \OCP\ITags
  43. *
  44. * since 20.0.0 $includeShared isn't used anymore
  45. */
  46. public function load($type, $defaultTags = [], $includeShared = false, $userId = null) {
  47. if (is_null($userId)) {
  48. $user = $this->userSession->getUser();
  49. if ($user === null) {
  50. // nothing we can do without a user
  51. return null;
  52. }
  53. $userId = $this->userSession->getUser()->getUId();
  54. }
  55. return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $defaultTags);
  56. }
  57. /**
  58. * Get all users who favorited an object
  59. *
  60. * @param string $objectType
  61. * @param int $objectId
  62. * @return array
  63. */
  64. public function getUsersFavoritingObject(string $objectType, int $objectId): array {
  65. $query = $this->connection->getQueryBuilder();
  66. $query->select('uid')
  67. ->from('vcategory_to_object', 'o')
  68. ->innerJoin('o', 'vcategory', 'c', $query->expr()->eq('o.categoryid', 'c.id'))
  69. ->where($query->expr()->eq('objid', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT)))
  70. ->andWhere($query->expr()->eq('c.type', $query->createNamedParameter($objectType)))
  71. ->andWhere($query->expr()->eq('c.category', $query->createNamedParameter(ITags::TAG_FAVORITE)));
  72. $result = $query->execute();
  73. $users = $result->fetchAll(\PDO::FETCH_COLUMN);
  74. $result->closeCursor();
  75. return $users;
  76. }
  77. public function handle(Event $event): void {
  78. if (!($event instanceof UserDeletedEvent)) {
  79. return;
  80. }
  81. // Find all objectid/tagId pairs.
  82. $user = $event->getUser();
  83. $qb = $this->connection->getQueryBuilder();
  84. $qb->select('id')
  85. ->from('vcategory')
  86. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())));
  87. try {
  88. $result = $qb->executeQuery();
  89. } catch (DBException $e) {
  90. $this->logger->error($e->getMessage(), [
  91. 'app' => 'core',
  92. 'exception' => $e,
  93. ]);
  94. return;
  95. }
  96. $tagsIds = array_map(fn (array $row) => (int)$row['id'], $result->fetchAll());
  97. $result->closeCursor();
  98. if (count($tagsIds) === 0) {
  99. return;
  100. }
  101. // Clean vcategory_to_object table
  102. $qb = $this->connection->getQueryBuilder();
  103. $qb = $qb->delete('vcategory_to_object')
  104. ->where($qb->expr()->in('categoryid', $qb->createParameter('chunk')));
  105. // Clean vcategory
  106. $qb1 = $this->connection->getQueryBuilder();
  107. $qb1 = $qb1->delete('vcategory')
  108. ->where($qb1->expr()->in('uid', $qb1->createParameter('chunk')));
  109. foreach (array_chunk($tagsIds, 1000) as $tagChunk) {
  110. $qb->setParameter('chunk', $tagChunk, IQueryBuilder::PARAM_INT_ARRAY);
  111. $qb1->setParameter('chunk', $tagChunk, IQueryBuilder::PARAM_INT_ARRAY);
  112. try {
  113. $qb->executeStatement();
  114. $qb1->executeStatement();
  115. } catch (DBException $e) {
  116. $this->logger->error($e->getMessage(), [
  117. 'app' => 'core',
  118. 'exception' => $e,
  119. ]);
  120. }
  121. }
  122. }
  123. }