1
0

DeleteOrphanedItems.php 4.4 KB

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