DeleteOrphanedItems.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Vincent Petry <vincent@nextcloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files\BackgroundJob;
  26. use OC\BackgroundJob\TimedJob;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. /**
  29. * Delete all share entries that have no matching entries in the file cache table.
  30. */
  31. class DeleteOrphanedItems extends TimedJob {
  32. public const CHUNK_SIZE = 200;
  33. /** @var \OCP\IDBConnection */
  34. protected $connection;
  35. /** @var \OCP\ILogger */
  36. protected $logger;
  37. /**
  38. * Default interval in minutes
  39. *
  40. * @var int $defaultIntervalMin
  41. **/
  42. protected $defaultIntervalMin = 60;
  43. /**
  44. * sets the correct interval for this timed job
  45. */
  46. public function __construct() {
  47. $this->interval = $this->defaultIntervalMin * 60;
  48. $this->connection = \OC::$server->getDatabaseConnection();
  49. $this->logger = \OC::$server->getLogger();
  50. }
  51. /**
  52. * Makes the background job do its work
  53. *
  54. * @param array $argument unused argument
  55. */
  56. public function run($argument) {
  57. $this->cleanSystemTags();
  58. $this->cleanUserTags();
  59. $this->cleanComments();
  60. $this->cleanCommentMarkers();
  61. }
  62. /**
  63. * Deleting orphaned system tag mappings
  64. *
  65. * @param string $table
  66. * @param string $idCol
  67. * @param string $typeCol
  68. * @return int Number of deleted entries
  69. */
  70. protected function cleanUp($table, $idCol, $typeCol) {
  71. $deletedEntries = 0;
  72. $query = $this->connection->getQueryBuilder();
  73. $query->select('t1.' . $idCol)
  74. ->from($table, 't1')
  75. ->where($query->expr()->eq($typeCol, $query->expr()->literal('files')))
  76. ->andWhere($query->expr()->isNull('t2.fileid'))
  77. ->leftJoin('t1', 'filecache', 't2', $query->expr()->eq($query->expr()->castColumn('t1.' . $idCol, IQueryBuilder::PARAM_INT), 't2.fileid'))
  78. ->groupBy('t1.' . $idCol)
  79. ->setMaxResults(self::CHUNK_SIZE);
  80. $deleteQuery = $this->connection->getQueryBuilder();
  81. $deleteQuery->delete($table)
  82. ->where($deleteQuery->expr()->eq($idCol, $deleteQuery->createParameter('objectid')));
  83. $deletedInLastChunk = self::CHUNK_SIZE;
  84. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  85. $result = $query->execute();
  86. $deletedInLastChunk = 0;
  87. while ($row = $result->fetch()) {
  88. $deletedInLastChunk++;
  89. $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row[$idCol])
  90. ->execute();
  91. }
  92. $result->closeCursor();
  93. }
  94. return $deletedEntries;
  95. }
  96. /**
  97. * Deleting orphaned system tag mappings
  98. *
  99. * @return int Number of deleted entries
  100. */
  101. protected function cleanSystemTags() {
  102. $deletedEntries = $this->cleanUp('systemtag_object_mapping', 'objectid', 'objecttype');
  103. $this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedItems']);
  104. return $deletedEntries;
  105. }
  106. /**
  107. * Deleting orphaned user tag mappings
  108. *
  109. * @return int Number of deleted entries
  110. */
  111. protected function cleanUserTags() {
  112. $deletedEntries = $this->cleanUp('vcategory_to_object', 'objid', 'type');
  113. $this->logger->debug("$deletedEntries orphaned user tag relations deleted", ['app' => 'DeleteOrphanedItems']);
  114. return $deletedEntries;
  115. }
  116. /**
  117. * Deleting orphaned comments
  118. *
  119. * @return int Number of deleted entries
  120. */
  121. protected function cleanComments() {
  122. $deletedEntries = $this->cleanUp('comments', 'object_id', 'object_type');
  123. $this->logger->debug("$deletedEntries orphaned comments deleted", ['app' => 'DeleteOrphanedItems']);
  124. return $deletedEntries;
  125. }
  126. /**
  127. * Deleting orphaned comment read markers
  128. *
  129. * @return int Number of deleted entries
  130. */
  131. protected function cleanCommentMarkers() {
  132. $deletedEntries = $this->cleanUp('comments_read_markers', 'object_id', 'object_type');
  133. $this->logger->debug("$deletedEntries orphaned comment read marks deleted", ['app' => 'DeleteOrphanedItems']);
  134. return $deletedEntries;
  135. }
  136. }