Added removal of unused tags

This commit is contained in:
Marcin Kurczewski 2014-10-07 23:04:42 +02:00
parent 76d9e95e4b
commit 5bc73d220e
5 changed files with 46 additions and 4 deletions

1
TODO
View file

@ -26,7 +26,6 @@ everything related to users:
- removing
everything related to tags:
- automatic removal of unused tags in backend
- tags.json refresh when editing post
- basic tags
- mass tag

View file

@ -31,4 +31,9 @@ class TagDao extends AbstractDao implements ICrudDao
$arrayEntities = iterator_to_array($query);
return $this->arrayToEntities($arrayEntities);
}
public function deleteUnused()
{
$this->deleteBy('usages', 0);
}
}

View file

@ -132,7 +132,10 @@ class PostService
$this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($savedPost));
return $savedPost;
};
return $this->transactionManager->commit($transactionFunc);
$ret = $this->transactionManager->commit($transactionFunc);
$this->tagService->deleteUnusedTags();
$this->tagService->exportJson();
return $ret;
}
public function updatePost(\Szurubooru\Entities\Post $post, \Szurubooru\FormData\PostEditFormData $formData)
@ -167,7 +170,10 @@ class PostService
$this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($post));
return $this->postDao->save($post);
};
return $this->transactionManager->commit($transactionFunc);
$ret = $this->transactionManager->commit($transactionFunc);
$this->tagService->deleteUnusedTags();
$this->tagService->exportJson();
return $ret;
}
private function updatePostSafety(\Szurubooru\Entities\Post $post, $newSafety)
@ -271,7 +277,6 @@ class PostService
$tags[] = $tag;
}
$tags = $this->tagService->createTags($tags);
$this->tagService->exportJson();
$post->setTags($tags);
}

View file

@ -40,6 +40,15 @@ class TagService
$this->fileService->save('tags.json', $json);
}
public function deleteUnusedTags()
{
$transactionFunc = function()
{
$this->tagDao->deleteUnused();
};
$this->transactionManager->commit($transactionFunc);
}
public function createTags(array $tags)
{
$transactionFunc = function() use ($tags)

View file

@ -35,6 +35,30 @@ final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$this->assertEntitiesEqual($expected, $actual);
}
public function testRemovingUnused()
{
$tag1 = new \Szurubooru\Entities\Tag();
$tag1->setName('test1');
$tag1->setCreationTime(date('c'));
$tag2 = new \Szurubooru\Entities\Tag();
$tag2->setName('test2');
$tag2->setCreationTime(date('c'));
$tagDao = $this->getTagDao();
$tagDao->save($tag1);
$tagDao->save($tag2);
$pdo = $this->databaseConnection->getPDO();
$pdo->exec('INSERT INTO postTags(postId, tagId) VALUES (1, 2)');
$tag1 = $tagDao->findById($tag1->getId());
$tag2 = $tagDao->findById($tag2->getId());
$this->assertEquals(2, count($tagDao->findAll()));
$this->assertEquals(0, $tag1->getUsages());
$this->assertEquals(1, $tag2->getUsages());
$tagDao->deleteUnused();
$this->assertEquals(1, count($tagDao->findAll()));
$this->assertNull($tagDao->findById($tag1->getId()));
$this->assertEntitiesEqual($tag2, $tagDao->findById($tag2->getId()));
}
private function getTagDao()
{
return new \Szurubooru\Dao\TagDao($this->databaseConnection);