1
0

DeleteOrphanedItems.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\TimedJob;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. /**
  30. * Delete all share entries that have no matching entries in the file cache table.
  31. */
  32. class DeleteOrphanedItems extends TimedJob {
  33. public const CHUNK_SIZE = 200;
  34. /** @var \OCP\IDBConnection */
  35. protected $connection;
  36. /** @var \OCP\ILogger */
  37. protected $logger;
  38. /**
  39. * Default interval in minutes
  40. *
  41. * @var int $defaultIntervalMin
  42. **/
  43. protected $defaultIntervalMin = 60;
  44. /**
  45. * sets the correct interval for this timed job
  46. */
  47. public function __construct(ITimeFactory $time) {
  48. parent::__construct($time);
  49. $this->interval = $this->defaultIntervalMin * 60;
  50. $this->connection = \OC::$server->getDatabaseConnection();
  51. $this->logger = \OC::$server->getLogger();
  52. }
  53. /**
  54. * Makes the background job do its work
  55. *
  56. * @param array $argument unused argument
  57. */
  58. public function run($argument) {
  59. $this->cleanSystemTags();
  60. $this->cleanUserTags();
  61. $this->cleanComments();
  62. $this->cleanCommentMarkers();
  63. }
  64. /**
  65. * Deleting orphaned system tag mappings
  66. *
  67. * @param string $table
  68. * @param string $idCol
  69. * @param string $typeCol
  70. * @return int Number of deleted entries
  71. */
  72. protected function cleanUp($table, $idCol, $typeCol) {
  73. $deletedEntries = 0;
  74. $query = $this->connection->getQueryBuilder();
  75. $query->select('t1.' . $idCol)
  76. ->from($table, 't1')
  77. ->where($query->expr()->eq($typeCol, $query->expr()->literal('files')))
  78. ->andWhere($query->expr()->isNull('t2.fileid'))
  79. ->leftJoin('t1', 'filecache', 't2', $query->expr()->eq($query->expr()->castColumn('t1.' . $idCol, IQueryBuilder::PARAM_INT), 't2.fileid'))
  80. ->groupBy('t1.' . $idCol)
  81. ->setMaxResults(self::CHUNK_SIZE);
  82. $deleteQuery = $this->connection->getQueryBuilder();
  83. $deleteQuery->delete($table)
  84. ->where($deleteQuery->expr()->eq($idCol, $deleteQuery->createParameter('objectid')));
  85. $deletedInLastChunk = self::CHUNK_SIZE;
  86. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  87. $result = $query->execute();
  88. $deletedInLastChunk = 0;
  89. while ($row = $result->fetch()) {
  90. $deletedInLastChunk++;
  91. $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row[$idCol])
  92. ->execute();
  93. }
  94. $result->closeCursor();
  95. }
  96. return $deletedEntries;
  97. }
  98. /**
  99. * Deleting orphaned system tag mappings
  100. *
  101. * @return int Number of deleted entries
  102. */
  103. protected function cleanSystemTags() {
  104. $deletedEntries = $this->cleanUp('systemtag_object_mapping', 'objectid', 'objecttype');
  105. $this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedItems']);
  106. return $deletedEntries;
  107. }
  108. /**
  109. * Deleting orphaned user tag mappings
  110. *
  111. * @return int Number of deleted entries
  112. */
  113. protected function cleanUserTags() {
  114. $deletedEntries = $this->cleanUp('vcategory_to_object', 'objid', 'type');
  115. $this->logger->debug("$deletedEntries orphaned user tag relations deleted", ['app' => 'DeleteOrphanedItems']);
  116. return $deletedEntries;
  117. }
  118. /**
  119. * Deleting orphaned comments
  120. *
  121. * @return int Number of deleted entries
  122. */
  123. protected function cleanComments() {
  124. $deletedEntries = $this->cleanUp('comments', 'object_id', 'object_type');
  125. $this->logger->debug("$deletedEntries orphaned comments deleted", ['app' => 'DeleteOrphanedItems']);
  126. return $deletedEntries;
  127. }
  128. /**
  129. * Deleting orphaned comment read markers
  130. *
  131. * @return int Number of deleted entries
  132. */
  133. protected function cleanCommentMarkers() {
  134. $deletedEntries = $this->cleanUp('comments_read_markers', 'object_id', 'object_type');
  135. $this->logger->debug("$deletedEntries orphaned comment read marks deleted", ['app' => 'DeleteOrphanedItems']);
  136. return $deletedEntries;
  137. }
  138. }