diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2016-02-06 13:26:00 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2016-02-06 13:26:00 +0100 |
commit | 5832178f591f1efca48e06c78dfb7a0405a7cce4 (patch) | |
tree | 5e162879b81a4a8d182090bcf3f9440305aeb249 /apps/files | |
parent | 72974081505dbf2eb6c5f00a03f8690235cb037b (diff) | |
parent | 065141f6f4212dbbca4633b0138f6e5ac3ec4f7a (diff) | |
download | nextcloud-server-5832178f591f1efca48e06c78dfb7a0405a7cce4.tar.gz nextcloud-server-5832178f591f1efca48e06c78dfb7a0405a7cce4.zip |
Merge pull request #22139 from owncloud/comments-files-cleanup
cleanup jobs for comments and comment read marks
Diffstat (limited to 'apps/files')
-rw-r--r-- | apps/files/appinfo/info.xml | 2 | ||||
-rw-r--r-- | apps/files/appinfo/install.php | 2 | ||||
-rw-r--r-- | apps/files/appinfo/update.php | 2 | ||||
-rw-r--r-- | apps/files/lib/backgroundjob/deleteorphaneditems.php (renamed from apps/files/lib/backgroundjob/deleteorphanedtagsjob.php) | 59 | ||||
-rw-r--r-- | apps/files/tests/backgroundjob/DeleteOrphanedItemsJobTest.php (renamed from apps/files/tests/backgroundjob/DeleteOrphanedTagsJobTest.php) | 110 |
5 files changed, 151 insertions, 24 deletions
diff --git a/apps/files/appinfo/info.xml b/apps/files/appinfo/info.xml index 136baa2ed07..b31232b799a 100644 --- a/apps/files/appinfo/info.xml +++ b/apps/files/appinfo/info.xml @@ -6,7 +6,7 @@ <licence>AGPL</licence> <author>Robin Appelman, Vincent Petry</author> <default_enable/> - <version>1.4.2</version> + <version>1.4.3</version> <types> <filesystem/> </types> diff --git a/apps/files/appinfo/install.php b/apps/files/appinfo/install.php index b9a893d1ee8..ae08e21a22e 100644 --- a/apps/files/appinfo/install.php +++ b/apps/files/appinfo/install.php @@ -21,4 +21,4 @@ // Cron job for scanning user storages \OC::$server->getJobList()->add('OCA\Files\BackgroundJob\ScanFiles'); -\OC::$server->getJobList()->add('OCA\Files\BackgroundJob\DeleteOrphanedTagsJob'); +\OC::$server->getJobList()->add('OCA\Files\BackgroundJob\DeleteOrphanedItems'); diff --git a/apps/files/appinfo/update.php b/apps/files/appinfo/update.php index 003f6916ac5..cb682cbc426 100644 --- a/apps/files/appinfo/update.php +++ b/apps/files/appinfo/update.php @@ -99,4 +99,4 @@ if ($installedVersion === '1.1.9' && ( // Add cron job for scanning user storages \OC::$server->getJobList()->add('OCA\Files\BackgroundJob\ScanFiles'); -\OC::$server->getJobList()->add('OCA\Files\BackgroundJob\DeleteOrphanedTagsJob'); +\OC::$server->getJobList()->add('OCA\Files\BackgroundJob\DeleteOrphanedItems'); diff --git a/apps/files/lib/backgroundjob/deleteorphanedtagsjob.php b/apps/files/lib/backgroundjob/deleteorphaneditems.php index 33f455b5b40..cefa1d655de 100644 --- a/apps/files/lib/backgroundjob/deleteorphanedtagsjob.php +++ b/apps/files/lib/backgroundjob/deleteorphaneditems.php @@ -22,11 +22,12 @@ namespace OCA\Files\BackgroundJob; use OC\BackgroundJob\TimedJob; +use OCP\DB\QueryBuilder\IQueryBuilder; /** * Delete all share entries that have no matching entries in the file cache table. */ -class DeleteOrphanedTagsJob extends TimedJob { +class DeleteOrphanedItems extends TimedJob { /** @var \OCP\IDBConnection */ protected $connection; @@ -58,6 +59,8 @@ class DeleteOrphanedTagsJob extends TimedJob { public function run($argument) { $this->cleanSystemTags(); $this->cleanUserTags(); + $this->cleanComments(); + $this->cleanCommentMarkers(); } /** @@ -65,19 +68,29 @@ class DeleteOrphanedTagsJob extends TimedJob { * * @return int Number of deleted entries */ - protected function cleanSystemTags() { + protected function cleanUp($table, $idCol, $typeCol) { $subQuery = $this->connection->getQueryBuilder(); $subQuery->select($subQuery->expr()->literal('1')) ->from('filecache', 'f') - ->where($subQuery->expr()->eq('objectid', 'f.fileid')); + ->where($subQuery->expr()->eq($idCol, 'f.fileid')); $query = $this->connection->getQueryBuilder(); - $deletedEntries = $query->delete('systemtag_object_mapping') - ->where($query->expr()->eq('objecttype', $query->expr()->literal('files'))) + $deletedEntries = $query->delete($table) + ->where($query->expr()->eq($typeCol, $query->expr()->literal('files'))) ->andWhere($query->expr()->isNull($query->createFunction('(' . $subQuery->getSql() . ')'))) ->execute(); - $this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedTagsJob']); + return $deletedEntries; + } + + /** + * Deleting orphaned system tag mappings + * + * @return int Number of deleted entries + */ + protected function cleanSystemTags() { + $deletedEntries = $this->cleanUp('systemtag_object_mapping', 'objectid', 'objecttype'); + $this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedItems']); return $deletedEntries; } @@ -87,18 +100,32 @@ class DeleteOrphanedTagsJob extends TimedJob { * @return int Number of deleted entries */ protected function cleanUserTags() { - $subQuery = $this->connection->getQueryBuilder(); - $subQuery->select($subQuery->expr()->literal('1')) - ->from('filecache', 'f') - ->where($subQuery->expr()->eq('objid', 'f.fileid')); + $deletedEntries = $this->cleanUp('vcategory_to_object', 'objid', 'type'); + $this->logger->debug("$deletedEntries orphaned user tag relations deleted", ['app' => 'DeleteOrphanedItems']); + return $deletedEntries; + } - $query = $this->connection->getQueryBuilder(); - $deletedEntries = $query->delete('vcategory_to_object') - ->where($query->expr()->eq('type', $query->expr()->literal('files'))) - ->andWhere($query->expr()->isNull($query->createFunction('(' . $subQuery->getSql() . ')'))) - ->execute(); + /** + * Deleting orphaned comments + * + * @return int Number of deleted entries + */ + protected function cleanComments() { + $qb = $this->connection->getQueryBuilder(); + $deletedEntries = $this->cleanUp('comments', $qb->expr()->castColumn('object_id', IQueryBuilder::PARAM_INT), 'object_type'); + $this->logger->debug("$deletedEntries orphaned comments deleted", ['app' => 'DeleteOrphanedItems']); + return $deletedEntries; + } - $this->logger->debug("$deletedEntries orphaned user tag relations deleted", ['app' => 'DeleteOrphanedTagsJob']); + /** + * Deleting orphaned comment read markers + * + * @return int Number of deleted entries + */ + protected function cleanCommentMarkers() { + $qb = $this->connection->getQueryBuilder(); + $deletedEntries = $this->cleanUp('comments_read_markers', $qb->expr()->castColumn('object_id', IQueryBuilder::PARAM_INT), 'object_type'); + $this->logger->debug("$deletedEntries orphaned comment read marks deleted", ['app' => 'DeleteOrphanedItems']); return $deletedEntries; } diff --git a/apps/files/tests/backgroundjob/DeleteOrphanedTagsJobTest.php b/apps/files/tests/backgroundjob/DeleteOrphanedItemsJobTest.php index d2e9d77cb20..8d8e5d3be52 100644 --- a/apps/files/tests/backgroundjob/DeleteOrphanedTagsJobTest.php +++ b/apps/files/tests/backgroundjob/DeleteOrphanedItemsJobTest.php @@ -21,17 +21,17 @@ namespace OCA\Files\Tests\BackgroundJob; -use OCA\Files\BackgroundJob\DeleteOrphanedTagsJob; +use OCA\Files\BackgroundJob\DeleteOrphanedItems; use OCP\DB\QueryBuilder\IQueryBuilder; /** - * Class DeleteOrphanedTagsJobTest + * Class DeleteOrphanedItemsJobTest * * @group DB * * @package Test\BackgroundJob */ -class DeleteOrphanedTagsJobTest extends \Test\TestCase { +class DeleteOrphanedItemsJobTest extends \Test\TestCase { /** @var \OCP\IDBConnection */ protected $connection; @@ -93,7 +93,7 @@ class DeleteOrphanedTagsJobTest extends \Test\TestCase { $mapping = $this->getMappings('systemtag_object_mapping'); $this->assertCount(2, $mapping); - $job = new DeleteOrphanedTagsJob(); + $job = new DeleteOrphanedItems(); $this->invokePrivate($job, 'cleanSystemTags'); $mapping = $this->getMappings('systemtag_object_mapping'); @@ -142,7 +142,7 @@ class DeleteOrphanedTagsJobTest extends \Test\TestCase { $mapping = $this->getMappings('vcategory_to_object'); $this->assertCount(2, $mapping); - $job = new DeleteOrphanedTagsJob(); + $job = new DeleteOrphanedItems(); $this->invokePrivate($job, 'cleanUserTags'); $mapping = $this->getMappings('vcategory_to_object'); @@ -155,4 +155,104 @@ class DeleteOrphanedTagsJobTest extends \Test\TestCase { $this->cleanMapping('vcategory_to_object'); } + /** + * Test clearing orphaned system tag mappings + */ + public function testClearComments() { + $this->cleanMapping('comments'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments') + ->values([ + 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + 'actor_type' => $query->createNamedParameter('users'), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments') + ->values([ + 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + 'actor_type' => $query->createNamedParameter('users'), + ])->execute(); + + $mapping = $this->getMappings('comments'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanComments'); + + $mapping = $this->getMappings('comments'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('comments'); + } + + /** + * Test clearing orphaned system tag mappings + */ + public function testClearCommentReadMarks() { + $this->cleanMapping('comments_read_markers'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments_read_markers') + ->values([ + 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments_read_markers') + ->values([ + 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + ])->execute(); + + $mapping = $this->getMappings('comments_read_markers'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanCommentMarkers'); + + $mapping = $this->getMappings('comments_read_markers'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('comments_read_markers'); + } + } |