DeleteOrphanedItems.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files\BackgroundJob;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\TimedJob;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IDBConnection;
  12. use Psr\Log\LoggerInterface;
  13. /**
  14. * Delete all share entries that have no matching entries in the file cache table.
  15. */
  16. class DeleteOrphanedItems extends TimedJob {
  17. public const CHUNK_SIZE = 200;
  18. protected $defaultIntervalMin = 60;
  19. /**
  20. * sets the correct interval for this timed job
  21. */
  22. public function __construct(
  23. ITimeFactory $time,
  24. protected IDBConnection $connection,
  25. protected LoggerInterface $logger,
  26. ) {
  27. parent::__construct($time);
  28. $this->interval = $this->defaultIntervalMin * 60;
  29. }
  30. /**
  31. * Makes the background job do its work
  32. *
  33. * @param array $argument unused argument
  34. */
  35. public function run($argument) {
  36. $this->cleanSystemTags();
  37. $this->cleanUserTags();
  38. $this->cleanComments();
  39. $this->cleanCommentMarkers();
  40. }
  41. /**
  42. * Deleting orphaned system tag mappings
  43. *
  44. * @param string $table
  45. * @param string $idCol
  46. * @param string $typeCol
  47. * @return int Number of deleted entries
  48. */
  49. protected function cleanUp($table, $idCol, $typeCol) {
  50. $deletedEntries = 0;
  51. $query = $this->connection->getQueryBuilder();
  52. $query->select('t1.' . $idCol)
  53. ->from($table, 't1')
  54. ->where($query->expr()->eq($typeCol, $query->expr()->literal('files')))
  55. ->andWhere($query->expr()->isNull('t2.fileid'))
  56. ->leftJoin('t1', 'filecache', 't2', $query->expr()->eq($query->expr()->castColumn('t1.' . $idCol, IQueryBuilder::PARAM_INT), 't2.fileid'))
  57. ->groupBy('t1.' . $idCol)
  58. ->setMaxResults(self::CHUNK_SIZE);
  59. $deleteQuery = $this->connection->getQueryBuilder();
  60. $deleteQuery->delete($table)
  61. ->where($deleteQuery->expr()->eq($idCol, $deleteQuery->createParameter('objectid')));
  62. $deletedInLastChunk = self::CHUNK_SIZE;
  63. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  64. $result = $query->execute();
  65. $deletedInLastChunk = 0;
  66. while ($row = $result->fetch()) {
  67. $deletedInLastChunk++;
  68. $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row[$idCol])
  69. ->execute();
  70. }
  71. $result->closeCursor();
  72. }
  73. return $deletedEntries;
  74. }
  75. /**
  76. * Deleting orphaned system tag mappings
  77. *
  78. * @return int Number of deleted entries
  79. */
  80. protected function cleanSystemTags() {
  81. $deletedEntries = $this->cleanUp('systemtag_object_mapping', 'objectid', 'objecttype');
  82. $this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedItems']);
  83. return $deletedEntries;
  84. }
  85. /**
  86. * Deleting orphaned user tag mappings
  87. *
  88. * @return int Number of deleted entries
  89. */
  90. protected function cleanUserTags() {
  91. $deletedEntries = $this->cleanUp('vcategory_to_object', 'objid', 'type');
  92. $this->logger->debug("$deletedEntries orphaned user tag relations deleted", ['app' => 'DeleteOrphanedItems']);
  93. return $deletedEntries;
  94. }
  95. /**
  96. * Deleting orphaned comments
  97. *
  98. * @return int Number of deleted entries
  99. */
  100. protected function cleanComments() {
  101. $deletedEntries = $this->cleanUp('comments', 'object_id', 'object_type');
  102. $this->logger->debug("$deletedEntries orphaned comments deleted", ['app' => 'DeleteOrphanedItems']);
  103. return $deletedEntries;
  104. }
  105. /**
  106. * Deleting orphaned comment read markers
  107. *
  108. * @return int Number of deleted entries
  109. */
  110. protected function cleanCommentMarkers() {
  111. $deletedEntries = $this->cleanUp('comments_read_markers', 'object_id', 'object_type');
  112. $this->logger->debug("$deletedEntries orphaned comment read marks deleted", ['app' => 'DeleteOrphanedItems']);
  113. return $deletedEntries;
  114. }
  115. }