From 39d854cb8e59ce03a42628955d86be0109c61c93 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 21 Sep 2014 09:58:04 +0200 Subject: [PATCH] Added usage count tracking to tags and posts --- src/Controllers/ViewProxies/TagViewProxy.php | 1 + .../EntityConverters/PostEntityConverter.php | 1 + .../EntityConverters/TagEntityConverter.php | 1 + src/Entities/Entity.php | 18 +++++++++ src/Entities/Post.php | 8 ++++ src/Entities/Tag.php | 9 +---- src/Upgrades/Upgrade06.php | 38 +++++++++++++++++++ src/di.php | 1 + tests/AbstractDatabaseTestCase.php | 2 + tests/Dao/PostDaoTest.php | 3 ++ 10 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 src/Upgrades/Upgrade06.php diff --git a/src/Controllers/ViewProxies/TagViewProxy.php b/src/Controllers/ViewProxies/TagViewProxy.php index 2a74068a..dfcb417b 100644 --- a/src/Controllers/ViewProxies/TagViewProxy.php +++ b/src/Controllers/ViewProxies/TagViewProxy.php @@ -9,6 +9,7 @@ class TagViewProxy extends AbstractViewProxy if ($tag) { $result->name = $tag->getName(); + $result->usages = $tag->getUsages(); } return $result; } diff --git a/src/Dao/EntityConverters/PostEntityConverter.php b/src/Dao/EntityConverters/PostEntityConverter.php index 07133907..6b54800c 100644 --- a/src/Dao/EntityConverters/PostEntityConverter.php +++ b/src/Dao/EntityConverters/PostEntityConverter.php @@ -40,6 +40,7 @@ class PostEntityConverter extends AbstractEntityConverter implements IEntityConv $entity->setImageHeight($array['imageHeight']); $entity->setOriginalFileSize($array['originalFileSize']); $entity->setOriginalFileName($array['originalFileName']); + $entity->setMeta(\Szurubooru\Entities\Post::META_TAG_COUNT, intval($array['tagCount'])); return $entity; } } diff --git a/src/Dao/EntityConverters/TagEntityConverter.php b/src/Dao/EntityConverters/TagEntityConverter.php index 7db1d964..276b9b9d 100644 --- a/src/Dao/EntityConverters/TagEntityConverter.php +++ b/src/Dao/EntityConverters/TagEntityConverter.php @@ -16,6 +16,7 @@ class TagEntityConverter extends AbstractEntityConverter implements IEntityConve { $entity = new \Szurubooru\Entities\Tag($array['id']); $entity->setName($array['name']); + $entity->setMeta(\Szurubooru\Entities\Tag::META_USAGES, intval($array['usages'])); return $entity; } } diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index c52a24df..e31cab4c 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -6,6 +6,7 @@ abstract class Entity protected $id = null; private $lazyLoaders = []; private $lazyContainers = []; + private $meta; public function __construct($id = null) { @@ -22,6 +23,23 @@ abstract class Entity $this->id = $id; } + public function getMeta($metaName, $default = null) + { + if (!isset($this->meta[$metaName])) + return $default; + return $this->meta[$metaName]; + } + + public function setMeta($metaName, $value) + { + $this->meta[$metaName] = $value; + } + + public function resetMeta() + { + $this->meta = []; + } + public function resetLazyLoaders() { $this->lazyLoaders = []; diff --git a/src/Entities/Post.php b/src/Entities/Post.php index 8704ad1d..8b32379f 100644 --- a/src/Entities/Post.php +++ b/src/Entities/Post.php @@ -16,6 +16,8 @@ final class Post extends Entity const LAZY_LOADER_CONTENT = 'content'; const LAZY_LOADER_THUMBNAIL_SOURCE_CONTENT = 'thumbnailSourceContent'; + const META_TAG_COUNT = 'tagCount'; + protected $name; protected $userId; protected $uploadTime; @@ -185,6 +187,7 @@ final class Post extends Entity public function setTags(array $tags) { $this->lazySave(self::LAZY_LOADER_TAGS, $tags); + $this->setMeta(self::META_TAG_COUNT, count($tags)); } public function getContent() @@ -216,4 +219,9 @@ final class Post extends Entity { return 'posts' . DIRECTORY_SEPARATOR . $this->getName() . '-custom-thumb'; } + + public function getTagCount() + { + return $this->getMeta(self::META_TAG_COUNT, 0); + } } diff --git a/src/Entities/Tag.php b/src/Entities/Tag.php index b499966a..9d6e5d48 100644 --- a/src/Entities/Tag.php +++ b/src/Entities/Tag.php @@ -4,8 +4,8 @@ namespace Szurubooru\Entities; final class Tag extends Entity { protected $name; - protected $usages; + const META_USAGES = 'usages'; public function getName() { @@ -19,11 +19,6 @@ final class Tag extends Entity public function getUsages() { - return $this->usages; - } - - public function setUsages($usages) - { - $this->usages = $usages; + return $this->getMeta(self::META_USAGES); } } diff --git a/src/Upgrades/Upgrade06.php b/src/Upgrades/Upgrade06.php new file mode 100644 index 00000000..b603de05 --- /dev/null +++ b/src/Upgrades/Upgrade06.php @@ -0,0 +1,38 @@ +getPDO(); + + $pdo->exec('ALTER TABLE posts ADD COLUMN tagCount INTEGER NOT NULL DEFAULT 0'); + + $pdo->exec(' + CREATE TRIGGER postTagsDelete BEFORE DELETE ON postTags + FOR EACH ROW + BEGIN + UPDATE posts SET tagCount = tagCount - 1 WHERE posts.id = OLD.postId; + UPDATE tags SET usages = usages - 1 WHERE tags.id = OLD.tagId; + END'); + + $pdo->exec(' + CREATE TRIGGER postTagsInsert AFTER INSERT ON postTags + FOR EACH ROW + BEGIN + UPDATE posts SET tagCount = tagCount + 1 WHERE posts.id = NEW.postId; + UPDATE tags SET usages = usages + 1 WHERE tags.id = NEW.tagId; + END'); + + $pdo->exec(' + CREATE TRIGGER postTagsUpdate AFTER UPDATE ON postTags + FOR EACH ROW + BEGIN + UPDATE posts SET tagCount = tagCount + 1 WHERE posts.id = NEW.postId; + UPDATE posts SET tagCount = tagCount - 1 WHERE posts.id = OLD.postId; + UPDATE tags SET usages = usages + 1 WHERE tags.id = NEW.tagId; + UPDATE tags SET usages = usages - 1 WHERE tags.id = OLD.tagId; + END'); + } +} diff --git a/src/di.php b/src/di.php index 2a3de839..eaf96d5d 100644 --- a/src/di.php +++ b/src/di.php @@ -21,6 +21,7 @@ return [ $container->get(\Szurubooru\Upgrades\Upgrade03::class), $container->get(\Szurubooru\Upgrades\Upgrade04::class), $container->get(\Szurubooru\Upgrades\Upgrade05::class), + $container->get(\Szurubooru\Upgrades\Upgrade06::class), ]; }), diff --git a/tests/AbstractDatabaseTestCase.php b/tests/AbstractDatabaseTestCase.php index 879b2776..655c3818 100644 --- a/tests/AbstractDatabaseTestCase.php +++ b/tests/AbstractDatabaseTestCase.php @@ -43,7 +43,9 @@ abstract class AbstractDatabaseTestCase extends \Szurubooru\Tests\AbstractTestCa else { $expected[$key]->resetLazyLoaders(); + $expected[$key]->resetMeta(); $actual[$key]->resetLazyLoaders(); + $actual[$key]->resetMeta(); $this->assertEquals($expected[$key], $actual[$key]); } } diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php index a70039a5..31b2a7f7 100644 --- a/tests/Dao/PostDaoTest.php +++ b/tests/Dao/PostDaoTest.php @@ -125,6 +125,9 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $this->assertEntitiesEqual($testTags, $post->getTags()); $this->assertEquals(2, count($savedPost->getTags())); + $this->assertEquals(2, $post->getTagCount()); + $this->assertEquals(2, $savedPost->getTagCount()); + $tagDao = $this->getTagDao(); $this->assertEquals(2, count($tagDao->findAll())); }