Added usage count tracking to tags and posts
This commit is contained in:
parent
af3908a23c
commit
39d854cb8e
10 changed files with 75 additions and 7 deletions
|
@ -9,6 +9,7 @@ class TagViewProxy extends AbstractViewProxy
|
|||
if ($tag)
|
||||
{
|
||||
$result->name = $tag->getName();
|
||||
$result->usages = $tag->getUsages();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 = [];
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
38
src/Upgrades/Upgrade06.php
Normal file
38
src/Upgrades/Upgrade06.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
namespace Szurubooru\Upgrades;
|
||||
|
||||
class Upgrade06 implements IUpgrade
|
||||
{
|
||||
public function run(\Szurubooru\DatabaseConnection $databaseConnection)
|
||||
{
|
||||
$pdo = $databaseConnection->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');
|
||||
}
|
||||
}
|
|
@ -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),
|
||||
];
|
||||
}),
|
||||
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue