Added tag snapshots to backend

This commit is contained in:
Marcin Kurczewski 2014-11-07 13:43:07 +01:00
parent 7a5fb406b0
commit e64fb9bfad
7 changed files with 146 additions and 1 deletions

1
TODO
View file

@ -6,7 +6,6 @@ first major release.
- users: add user-configurable "about me" (should support Markdown)
- tags: add tag merging
- tags: add tag descriptions
- tags: add tag edit snapshots (backed-only)
- add /history
refactors:

View file

@ -5,6 +5,7 @@ use Szurubooru\Entities\User;
final class Snapshot extends Entity
{
const TYPE_POST = 0;
const TYPE_TAG = 1;
const OPERATION_CHANGE = 0;
const OPERATION_DELETE = 1;

View file

@ -0,0 +1,59 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Dao\TransactionManager;
use Szurubooru\Entities\Tag;
use Szurubooru\Entities\Snapshot;
use Szurubooru\SearchServices\Filters\SnapshotFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\Services\HistoryService;
use Szurubooru\Services\TagSnapshotProvider;
class TagHistoryService
{
private $transactionManager;
private $historyService;
private $tagSnapshotProvider;
public function __construct(
TransactionManager $transactionManager,
HistoryService $historyService,
TagSnapshotProvider $tagSnapshotProvider)
{
$this->transactionManager = $transactionManager;
$this->historyService = $historyService;
$this->tagSnapshotProvider = $tagSnapshotProvider;
}
public function getTagHistory(Tag $tag)
{
$transactionFunc = function() use ($tag)
{
$filter = new SnapshotFilter();
$requirement = new Requirement();
$requirement->setType(SnapshotFilter::REQUIREMENT_PRIMARY_KEY);
$requirement->setValue(new RequirementSingleValue($tag->getId()));
$filter->addRequirement($requirement);
$requirement = new Requirement();
$requirement->setType(SnapshotFilter::REQUIREMENT_TYPE);
$requirement->setValue(new RequirementSingleValue(Snapshot::TYPE_TAG));
$filter->addRequirement($requirement);
return $this->historyService->getFiltered($filter)->getEntities();
};
return $this->transactionManager->rollback($transactionFunc);
}
public function saveTagChange(Tag $tag)
{
$this->historyService->saveSnapshot($this->tagSnapshotProvider->getTagChangeSnapshot($tag));
}
public function saveTagDeletion(Tag $tag)
{
$this->historyService->saveSnapshot($this->tagSnapshotProvider->getTagDeleteSnapshot($tag));
}
}

View file

@ -19,6 +19,7 @@ class TagService
private $fileDao;
private $timeService;
private $postHistoryService;
private $tagHistoryService;
public function __construct(
Validator $validator,
@ -27,6 +28,7 @@ class TagService
TagDao $tagDao,
PublicFileDao $fileDao,
PostHistoryService $postHistoryService,
TagHistoryService $tagHistoryService,
TimeService $timeService)
{
$this->validator = $validator;
@ -35,6 +37,7 @@ class TagService
$this->tagDao = $tagDao;
$this->fileDao = $fileDao;
$this->postHistoryService = $postHistoryService;
$this->tagHistoryService = $tagHistoryService;
$this->timeService = $timeService;
}
@ -108,7 +111,10 @@ class TagService
$createdTags = [];
foreach ($this->tagDao->batchSave($tagsToCreate) as $tag)
{
$createdTags[$tagNameGetter($tag)] = $tag;
$this->tagHistoryService->saveTagChange($tag);
}
$result = [];
foreach ($tags as $key => $tag)
@ -146,6 +152,7 @@ class TagService
if ($formData->suggestions !== null)
$this->updateSuggestions($tag, $formData->suggestions);
$this->tagHistoryService->saveTagChange($tag);
return $this->tagDao->save($tag);
};
$ret = $this->transactionManager->commit($transactionFunc);
@ -173,6 +180,7 @@ class TagService
$transactionFunc = function() use ($tag)
{
$this->tagDao->deleteById($tag->getId());
$this->tagHistoryService->saveTagDeletion($tag);
};
$this->transactionManager->commit($transactionFunc);

View file

@ -0,0 +1,51 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Entities\Snapshot;
use Szurubooru\Entities\Tag;
class TagSnapshotProvider
{
public function getTagChangeSnapshot(Tag $tag)
{
$data =
[
'name' => $tag->getName(),
'banned' => $tag->isBanned(),
'category' => $tag->getCategory(),
'implications' => array_values(array_map(function (Tag $impliedTag)
{
return $impliedTag->getName();
}, $tag->getImpliedTags())),
'suggestions' => array_values(array_map(function (Tag $suggestedTag)
{
return $suggestedTag->getName();
}, $tag->getSuggestedTags())),
];
sort($data['implications']);
sort($data['suggestions']);
$snapshot = $this->getTagSnapshot($tag);
$snapshot->setOperation(Snapshot::OPERATION_CHANGE);
$snapshot->setData($data);
return $snapshot;
}
public function getTagDeleteSnapshot(Tag $tag)
{
$snapshot = $this->getTagSnapshot($tag);
$snapshot->setData([]);
$snapshot->setOperation(Snapshot::OPERATION_DELETE);
return $snapshot;
}
private function getTagSnapshot(Tag $tag)
{
$snapshot = new Snapshot();
$snapshot->setType(Snapshot::TYPE_TAG);
$snapshot->setPrimaryKey($tag->getId());
return $snapshot;
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Szurubooru\Upgrades;
use Szurubooru\Dao\TagDao;
use Szurubooru\DatabaseConnection;
use Szurubooru\Services\TagHistoryService;
class Upgrade29 implements IUpgrade
{
private $tagDao;
private $tagHistoryService;
public function __construct(TagDao $tagDao, TagHistoryService $tagHistoryService)
{
$this->tagDao = $tagDao;
$this->tagHistoryService = $tagHistoryService;
}
public function run(DatabaseConnection $databaseConnection)
{
$pdo = $databaseConnection->getPDO();
foreach ($this->tagDao->findAll() as $tag)
{
$this->tagHistoryService->saveTagChange($tag);
}
}
}

View file

@ -44,6 +44,7 @@ return [
$container->get(\Szurubooru\Upgrades\Upgrade26::class),
$container->get(\Szurubooru\Upgrades\Upgrade27::class),
$container->get(\Szurubooru\Upgrades\Upgrade28::class),
$container->get(\Szurubooru\Upgrades\Upgrade29::class),
];
}),