From 5bc73d220e2c1864e005b1bc09d1fb9ca230b386 Mon Sep 17 00:00:00 2001
From: Marcin Kurczewski <mkurczew@gmail.com>
Date: Tue, 7 Oct 2014 23:04:42 +0200
Subject: [PATCH] Added removal of unused tags

---
 TODO                         |  1 -
 src/Dao/TagDao.php           |  5 +++++
 src/Services/PostService.php | 11 ++++++++---
 src/Services/TagService.php  |  9 +++++++++
 tests/Dao/TagDaoTest.php     | 24 ++++++++++++++++++++++++
 5 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/TODO b/TODO
index 416ba6bd..0a1a3eda 100644
--- a/TODO
+++ b/TODO
@@ -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
diff --git a/src/Dao/TagDao.php b/src/Dao/TagDao.php
index 47204286..29fa2f16 100644
--- a/src/Dao/TagDao.php
+++ b/src/Dao/TagDao.php
@@ -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);
+	}
 }
diff --git a/src/Services/PostService.php b/src/Services/PostService.php
index 047c0961..e3c99859 100644
--- a/src/Services/PostService.php
+++ b/src/Services/PostService.php
@@ -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);
 	}
 
diff --git a/src/Services/TagService.php b/src/Services/TagService.php
index 7b4a1a16..281aa4c1 100644
--- a/src/Services/TagService.php
+++ b/src/Services/TagService.php
@@ -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)
diff --git a/tests/Dao/TagDaoTest.php b/tests/Dao/TagDaoTest.php
index d7d3835f..bd507f5e 100644
--- a/tests/Dao/TagDaoTest.php
+++ b/tests/Dao/TagDaoTest.php
@@ -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);