Added automatic tag JSON export

I plan to use it in autocomplete inputs.
This commit is contained in:
Marcin Kurczewski 2014-09-29 19:16:33 +02:00
parent 5dfa011d5a
commit 060ddf46ad
10 changed files with 119 additions and 16 deletions

View file

@ -1 +1,2 @@
posts posts
tags.json

View file

@ -34,18 +34,24 @@ abstract class AbstractDao implements ICrudDao
public function save(&$entity) public function save(&$entity)
{ {
if ($entity->getId()) $entity = $this->upsert($entity);
{
$entity = $this->update($entity);
}
else
{
$entity = $this->create($entity);
}
$this->afterSave($entity); $this->afterSave($entity);
$this->afterBatchSave([$entity]);
return $entity; return $entity;
} }
public function batchSave(array $entities)
{
foreach ($entities as $key => $entity)
{
$entities[$key] = $this->upsert($entity);
$this->afterSave($entity);
}
if (count($entities) > 0)
$this->afterBatchSave([$entity]);
return $entities;
}
public function findAll() public function findAll()
{ {
$query = $this->fpdo->from($this->tableName); $query = $this->fpdo->from($this->tableName);
@ -165,6 +171,10 @@ abstract class AbstractDao implements ICrudDao
{ {
} }
protected function afterBatchSave(array $entities)
{
}
protected function beforeDelete(\Szurubooru\Entities\Entity $entity) protected function beforeDelete(\Szurubooru\Entities\Entity $entity)
{ {
} }
@ -247,4 +257,16 @@ abstract class AbstractDao implements ICrudDao
$orderByString .= $orderColumn . ' ' . ($orderDir === \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC ? 'DESC' : 'ASC') . ', '; $orderByString .= $orderColumn . ' ' . ($orderDir === \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC ? 'DESC' : 'ASC') . ', ';
return substr($orderByString, 0, -2); return substr($orderByString, 0, -2);
} }
private function upsert(\Szurubooru\Entities\Entity $entity)
{
if ($entity->getId())
{
return $this->update($entity);
}
else
{
return $this->create($entity);
}
}
} }

View file

@ -9,6 +9,8 @@ interface ICrudDao
public function save(&$object); public function save(&$object);
public function batchSave(array $object);
public function deleteById($objectId); public function deleteById($objectId);
public function deleteAll(); public function deleteAll();

View file

@ -203,6 +203,8 @@ class PostDao extends AbstractDao implements ICrudDao
{ {
$this->fpdo->deleteFrom('postTags')->where('postId', $post->getId())->where('tagId', $tagId)->execute(); $this->fpdo->deleteFrom('postTags')->where('postId', $post->getId())->where('tagId', $tagId)->execute();
} }
$this->tagDao->exportJson();
} }
private function syncPostRelations(\Szurubooru\Entities\Post $post) private function syncPostRelations(\Szurubooru\Entities\Post $post)

View file

@ -3,12 +3,18 @@ namespace Szurubooru\Dao;
class TagDao extends AbstractDao implements ICrudDao class TagDao extends AbstractDao implements ICrudDao
{ {
public function __construct(\Szurubooru\DatabaseConnection $databaseConnection) private $fileService;
public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection,
\Szurubooru\Services\FileService $fileService)
{ {
parent::__construct( parent::__construct(
$databaseConnection, $databaseConnection,
'tags', 'tags',
new \Szurubooru\Dao\EntityConverters\TagEntityConverter()); new \Szurubooru\Dao\EntityConverters\TagEntityConverter());
$this->fileService = $fileService;
} }
public function findByNames($tagNames) public function findByNames($tagNames)
@ -27,6 +33,17 @@ class TagDao extends AbstractDao implements ICrudDao
return $this->findByIds($tagIds); return $this->findByIds($tagIds);
} }
public function exportJson()
{
$tags = [];
foreach ($this->findAll() as $tag)
{
$tags[$tag->getName()] = $tag->getUsages();
}
$json = json_encode($tags);
$this->fileService->save('tags.json', $json);
}
public function createMissingTags(array $tagNames) public function createMissingTags(array $tagNames)
{ {
$tagNames = array_filter(array_unique($tagNames)); $tagNames = array_filter(array_unique($tagNames));
@ -42,11 +59,19 @@ class TagDao extends AbstractDao implements ICrudDao
$tagNamesToCreate = array_udiff($tagNames, $tagNamesNotToCreate, 'strcasecmp'); $tagNamesToCreate = array_udiff($tagNames, $tagNamesNotToCreate, 'strcasecmp');
$tags = [];
foreach ($tagNamesToCreate as $tagName) foreach ($tagNamesToCreate as $tagName)
{ {
$tag = new \Szurubooru\Entities\Tag; $tag = new \Szurubooru\Entities\Tag;
$tag->setName($tagName); $tag->setName($tagName);
$this->save($tag); $tags[] = $tag;
} }
$this->batchSave($tags);
}
protected function afterBatchSave(array $entities)
{
if (count($entities) > 0)
$this->exportJson();
} }
} }

View file

@ -0,0 +1,17 @@
<?php
namespace Szurubooru\Upgrades;
class Upgrade12 implements IUpgrade
{
private $tagDao;
public function __construct(\Szurubooru\Dao\TagDao $tagDao)
{
$this->tagDao = $tagDao;
}
public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{
$this->tagDao->exportJson();
}
}

View file

@ -27,6 +27,7 @@ return [
$container->get(\Szurubooru\Upgrades\Upgrade09::class), $container->get(\Szurubooru\Upgrades\Upgrade09::class),
$container->get(\Szurubooru\Upgrades\Upgrade10::class), $container->get(\Szurubooru\Upgrades\Upgrade10::class),
$container->get(\Szurubooru\Upgrades\Upgrade11::class), $container->get(\Szurubooru\Upgrades\Upgrade11::class),
$container->get(\Szurubooru\Upgrades\Upgrade12::class),
]; ];
}), }),

View file

@ -13,8 +13,10 @@ abstract class AbstractDatabaseTestCase extends \Szurubooru\Tests\AbstractTestCa
$config->set('database/user', ''); $config->set('database/user', '');
$config->set('database/password', ''); $config->set('database/password', '');
$fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
$this->databaseConnection = new \Szurubooru\DatabaseConnection($config); $this->databaseConnection = new \Szurubooru\DatabaseConnection($config);
\Szurubooru\Injector::set(\Szurubooru\DatabaseConnection::class, $this->databaseConnection); \Szurubooru\Injector::set(\Szurubooru\DatabaseConnection::class, $this->databaseConnection);
\Szurubooru\Injector::set(\Szurubooru\Services\FileService::class, $fileServiceMock);
$upgradeRepository = \Szurubooru\Injector::get(\Szurubooru\Upgrades\UpgradeRepository::class); $upgradeRepository = \Szurubooru\Injector::get(\Szurubooru\Upgrades\UpgradeRepository::class);
$upgradeService = new \Szurubooru\Services\UpgradeService($config, $this->databaseConnection, $upgradeRepository); $upgradeService = new \Szurubooru\Services\UpgradeService($config, $this->databaseConnection, $upgradeRepository);

View file

@ -13,7 +13,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
parent::setUp(); parent::setUp();
$this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class); $this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
$this->thumbnailServiceMock = $this->mock(\Szurubooru\Services\ThumbnailService::class); $this->thumbnailServiceMock = $this->mock(\Szurubooru\Services\ThumbnailService::class);
$this->tagDao = new \Szurubooru\Dao\TagDao($this->databaseConnection); $this->tagDao = new \Szurubooru\Dao\TagDao($this->databaseConnection, $this->fileServiceMock);
$this->userDao = new \Szurubooru\Dao\UserDao( $this->userDao = new \Szurubooru\Dao\UserDao(
$this->databaseConnection, $this->databaseConnection,
$this->fileServiceMock, $this->fileServiceMock,
@ -243,9 +243,11 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
[$post->getThumbnailSourceContentPath()]); [$post->getThumbnailSourceContentPath()]);
$this->fileServiceMock $this->fileServiceMock
->expects($this->once()) ->expects($this->exactly(2))
->method('save') ->method('save')
->with($post->getContentPath(), 'whatever'); ->withConsecutive(
[$post->getContentPath(), 'whatever'],
['tags.json', '[]']);
$postDao->save($post); $postDao->save($post);
} }
@ -264,11 +266,12 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
[$post->getThumbnailSourceContentPath()]); [$post->getThumbnailSourceContentPath()]);
$this->fileServiceMock $this->fileServiceMock
->expects($this->exactly(2)) ->expects($this->exactly(3))
->method('save') ->method('save')
->withConsecutive( ->withConsecutive(
[$post->getContentPath(), 'whatever'], [$post->getContentPath(), 'whatever'],
[$post->getThumbnailSourceContentPath(), 'an image of sharks']); [$post->getThumbnailSourceContentPath(), 'an image of sharks'],
['tags.json', '[]']);
$postDao->save($post); $postDao->save($post);
} }

View file

@ -3,6 +3,14 @@ namespace Szurubooru\Tests\Dao;
final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
private $fileServiceMock;
public function setUp()
{
parent::setUp();
$this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
}
public function testFindByPostIds() public function testFindByPostIds()
{ {
$pdo = $this->databaseConnection->getPDO(); $pdo = $this->databaseConnection->getPDO();
@ -29,8 +37,28 @@ final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$this->assertEntitiesEqual($expected, $actual); $this->assertEntitiesEqual($expected, $actual);
} }
public function testExportSingle()
{
$tag1 = new \Szurubooru\Entities\Tag();
$tag1->setName('test');
$this->fileServiceMock->expects($this->once())->method('save')->with('tags.json', '{"test":0}');
$tagDao = $this->getTagDao();
$tagDao->save($tag1);
}
public function testExportMultiple()
{
$tag1 = new \Szurubooru\Entities\Tag();
$tag1->setName('test1');
$tag2 = new \Szurubooru\Entities\Tag();
$tag2->setName('test2');
$this->fileServiceMock->expects($this->once())->method('save')->with('tags.json', '{"test1":0,"test2":0}');
$tagDao = $this->getTagDao();
$tagDao->batchSave([$tag1, $tag2]);
}
private function getTagDao() private function getTagDao()
{ {
return new \Szurubooru\Dao\TagDao($this->databaseConnection); return new \Szurubooru\Dao\TagDao($this->databaseConnection, $this->fileServiceMock);
} }
} }