cleanup jobs for comments and comment read marks

This commit is contained in:
Arthur Schiwon 2016-02-04 18:34:01 +01:00 committed by Joas Schilling
parent e15a120f83
commit d537cae063
5 changed files with 155 additions and 31 deletions

View File

@ -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>

View File

@ -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');

View File

@ -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');

View File

@ -26,7 +26,7 @@ use OC\BackgroundJob\TimedJob;
/**
* 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 +58,28 @@ class DeleteOrphanedTagsJob extends TimedJob {
public function run($argument) {
$this->cleanSystemTags();
$this->cleanUserTags();
$this->cleanComments();
$this->cleanCommentMarkers();
}
/**
* Deleting orphaned system tag mappings
*
* @return int Number of deleted entries
*/
protected function cleanUp($table, $idCol, $typeCol) {
$subQuery = $this->connection->getQueryBuilder();
$subQuery->select($subQuery->expr()->literal('1'))
->from('filecache', 'f')
->where($subQuery->expr()->eq($idCol, 'f.fileid'));
$query = $this->connection->getQueryBuilder();
$deletedEntries = $query->delete($table)
->where($query->expr()->eq($typeCol, $query->expr()->literal('files')))
->andWhere($query->expr()->isNull($query->createFunction('(' . $subQuery->getSql() . ')')))
->execute();
return $deletedEntries;
}
/**
@ -66,18 +88,8 @@ class DeleteOrphanedTagsJob extends TimedJob {
* @return int Number of deleted entries
*/
protected function cleanSystemTags() {
$subQuery = $this->connection->getQueryBuilder();
$subQuery->select($subQuery->expr()->literal('1'))
->from('filecache', 'f')
->where($subQuery->expr()->eq('objectid', 'f.fileid'));
$query = $this->connection->getQueryBuilder();
$deletedEntries = $query->delete('systemtag_object_mapping')
->where($query->expr()->eq('objecttype', $query->expr()->literal('files')))
->andWhere($query->expr()->isNull($query->createFunction('(' . $subQuery->getSql() . ')')))
->execute();
$this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedTagsJob']);
$deletedEntries = $this->cleanUp('systemtag_object_mapping', 'objectid', 'objecttype');
$this->logger->debug("$deletedEntries orphaned system tag relations deleted", ['app' => 'DeleteOrphanedItems']);
return $deletedEntries;
}
@ -87,18 +99,30 @@ 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() {
$deletedEntries = $this->cleanUp('comments', 'object_id', '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() {
$deletedEntries = $this->cleanUp('comments_read_markers', 'object_id', 'object_type');
$this->logger->debug("$deletedEntries orphaned comment read marks deleted", ['app' => 'DeleteOrphanedItems']);
return $deletedEntries;
}

View File

@ -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');
}
}