You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.9 KiB

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