Added tag snapshots to backend
This commit is contained in:
parent
7a5fb406b0
commit
e64fb9bfad
7 changed files with 146 additions and 1 deletions
1
TODO
1
TODO
|
@ -6,7 +6,6 @@ first major release.
|
||||||
- users: add user-configurable "about me" (should support Markdown)
|
- users: add user-configurable "about me" (should support Markdown)
|
||||||
- tags: add tag merging
|
- tags: add tag merging
|
||||||
- tags: add tag descriptions
|
- tags: add tag descriptions
|
||||||
- tags: add tag edit snapshots (backed-only)
|
|
||||||
- add /history
|
- add /history
|
||||||
|
|
||||||
refactors:
|
refactors:
|
||||||
|
|
|
@ -5,6 +5,7 @@ use Szurubooru\Entities\User;
|
||||||
final class Snapshot extends Entity
|
final class Snapshot extends Entity
|
||||||
{
|
{
|
||||||
const TYPE_POST = 0;
|
const TYPE_POST = 0;
|
||||||
|
const TYPE_TAG = 1;
|
||||||
|
|
||||||
const OPERATION_CHANGE = 0;
|
const OPERATION_CHANGE = 0;
|
||||||
const OPERATION_DELETE = 1;
|
const OPERATION_DELETE = 1;
|
||||||
|
|
59
src/Services/TagHistoryService.php
Normal file
59
src/Services/TagHistoryService.php
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ class TagService
|
||||||
private $fileDao;
|
private $fileDao;
|
||||||
private $timeService;
|
private $timeService;
|
||||||
private $postHistoryService;
|
private $postHistoryService;
|
||||||
|
private $tagHistoryService;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Validator $validator,
|
Validator $validator,
|
||||||
|
@ -27,6 +28,7 @@ class TagService
|
||||||
TagDao $tagDao,
|
TagDao $tagDao,
|
||||||
PublicFileDao $fileDao,
|
PublicFileDao $fileDao,
|
||||||
PostHistoryService $postHistoryService,
|
PostHistoryService $postHistoryService,
|
||||||
|
TagHistoryService $tagHistoryService,
|
||||||
TimeService $timeService)
|
TimeService $timeService)
|
||||||
{
|
{
|
||||||
$this->validator = $validator;
|
$this->validator = $validator;
|
||||||
|
@ -35,6 +37,7 @@ class TagService
|
||||||
$this->tagDao = $tagDao;
|
$this->tagDao = $tagDao;
|
||||||
$this->fileDao = $fileDao;
|
$this->fileDao = $fileDao;
|
||||||
$this->postHistoryService = $postHistoryService;
|
$this->postHistoryService = $postHistoryService;
|
||||||
|
$this->tagHistoryService = $tagHistoryService;
|
||||||
$this->timeService = $timeService;
|
$this->timeService = $timeService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +111,10 @@ class TagService
|
||||||
|
|
||||||
$createdTags = [];
|
$createdTags = [];
|
||||||
foreach ($this->tagDao->batchSave($tagsToCreate) as $tag)
|
foreach ($this->tagDao->batchSave($tagsToCreate) as $tag)
|
||||||
|
{
|
||||||
$createdTags[$tagNameGetter($tag)] = $tag;
|
$createdTags[$tagNameGetter($tag)] = $tag;
|
||||||
|
$this->tagHistoryService->saveTagChange($tag);
|
||||||
|
}
|
||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
foreach ($tags as $key => $tag)
|
foreach ($tags as $key => $tag)
|
||||||
|
@ -146,6 +152,7 @@ class TagService
|
||||||
if ($formData->suggestions !== null)
|
if ($formData->suggestions !== null)
|
||||||
$this->updateSuggestions($tag, $formData->suggestions);
|
$this->updateSuggestions($tag, $formData->suggestions);
|
||||||
|
|
||||||
|
$this->tagHistoryService->saveTagChange($tag);
|
||||||
return $this->tagDao->save($tag);
|
return $this->tagDao->save($tag);
|
||||||
};
|
};
|
||||||
$ret = $this->transactionManager->commit($transactionFunc);
|
$ret = $this->transactionManager->commit($transactionFunc);
|
||||||
|
@ -173,6 +180,7 @@ class TagService
|
||||||
$transactionFunc = function() use ($tag)
|
$transactionFunc = function() use ($tag)
|
||||||
{
|
{
|
||||||
$this->tagDao->deleteById($tag->getId());
|
$this->tagDao->deleteById($tag->getId());
|
||||||
|
$this->tagHistoryService->saveTagDeletion($tag);
|
||||||
};
|
};
|
||||||
|
|
||||||
$this->transactionManager->commit($transactionFunc);
|
$this->transactionManager->commit($transactionFunc);
|
||||||
|
|
51
src/Services/TagSnapshotProvider.php
Normal file
51
src/Services/TagSnapshotProvider.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
26
src/Upgrades/Upgrade29.php
Normal file
26
src/Upgrades/Upgrade29.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -44,6 +44,7 @@ return [
|
||||||
$container->get(\Szurubooru\Upgrades\Upgrade26::class),
|
$container->get(\Szurubooru\Upgrades\Upgrade26::class),
|
||||||
$container->get(\Szurubooru\Upgrades\Upgrade27::class),
|
$container->get(\Szurubooru\Upgrades\Upgrade27::class),
|
||||||
$container->get(\Szurubooru\Upgrades\Upgrade28::class),
|
$container->get(\Szurubooru\Upgrades\Upgrade28::class),
|
||||||
|
$container->get(\Szurubooru\Upgrades\Upgrade29::class),
|
||||||
];
|
];
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue