diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2016-06-14 12:30:22 +0200 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2016-06-14 12:38:09 +0200 |
commit | 123bf78ca8e25cbc91d937f5090423594bf7ae35 (patch) | |
tree | 132f210679db133745d05df968dda2e8d68b1dc1 /lib/private/Repair | |
parent | ce676c4eb6172763a86c1fa1501cd6120d8c119a (diff) | |
download | nextcloud-server-123bf78ca8e25cbc91d937f5090423594bf7ae35.tar.gz nextcloud-server-123bf78ca8e25cbc91d937f5090423594bf7ae35.zip |
Clean up tags of deleted users
Diffstat (limited to 'lib/private/Repair')
-rw-r--r-- | lib/private/Repair/CleanTags.php | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/lib/private/Repair/CleanTags.php b/lib/private/Repair/CleanTags.php index 60ddeff08f3..4241fa6da3a 100644 --- a/lib/private/Repair/CleanTags.php +++ b/lib/private/Repair/CleanTags.php @@ -25,6 +25,7 @@ namespace OC\Repair; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\IUserManager; use OCP\Migration\IOutput; use OCP\Migration\IRepairStep; @@ -38,11 +39,18 @@ class CleanTags implements IRepairStep { /** @var IDBConnection */ protected $connection; + /** @var IUserManager */ + protected $userManager; + + protected $deletedTags = 0; + /** * @param IDBConnection $connection + * @param IUserManager $userManager */ - public function __construct(IDBConnection $connection) { + public function __construct(IDBConnection $connection, IUserManager $userManager) { $this->connection = $connection; + $this->userManager = $userManager; } /** @@ -56,12 +64,59 @@ class CleanTags implements IRepairStep { * Updates the configuration after running an update */ public function run(IOutput $output) { + $this->deleteOrphanTags($output); $this->deleteOrphanFileEntries($output); $this->deleteOrphanTagEntries($output); $this->deleteOrphanCategoryEntries($output); } /** + * Delete tags for deleted users + */ + protected function deleteOrphanTags(IOutput $output) { + $offset = 0; + while ($this->checkTags($offset)) { + $offset += 50; + } + + $output->info(sprintf('%d tags of deleted users have been removed.', $this->deletedTags)); + } + + protected function checkTags($offset) { + $query = $this->connection->getQueryBuilder(); + $query->select('uid') + ->from('vcategory') + ->groupBy('uid') + ->orderBy('uid') + ->setMaxResults(50) + ->setFirstResult($offset); + $result = $query->execute(); + + $users = []; + $hadResults = false; + while ($row = $result->fetch()) { + $hadResults = true; + if (!$this->userManager->userExists($row['uid'])) { + $users[] = $row['uid']; + } + } + $result->closeCursor(); + + if (!$hadResults) { + // No more tags, stop looping + return false; + } + + if (!empty($users)) { + $query = $this->connection->getQueryBuilder(); + $query->delete('vcategory') + ->where($query->expr()->in('uid', $query->createNamedParameter($users, IQueryBuilder::PARAM_STR_ARRAY))); + $this->deletedTags += $query->execute(); + } + return true; + } + + /** * Delete tag entries for deleted files */ protected function deleteOrphanFileEntries(IOutput $output) { |