DeleteOrphanedItems.php 4.4 KB

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