diff --git a/TODO b/TODO index 79a0ed88..0d86c664 100644 --- a/TODO +++ b/TODO @@ -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: diff --git a/src/Entities/Snapshot.php b/src/Entities/Snapshot.php index 0acafef3..8f0f961e 100644 --- a/src/Entities/Snapshot.php +++ b/src/Entities/Snapshot.php @@ -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; diff --git a/src/Services/TagHistoryService.php b/src/Services/TagHistoryService.php new file mode 100644 index 00000000..11db6881 --- /dev/null +++ b/src/Services/TagHistoryService.php @@ -0,0 +1,59 @@ +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)); + } +} + diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 0869fcc5..e2f44890 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -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); diff --git a/src/Services/TagSnapshotProvider.php b/src/Services/TagSnapshotProvider.php new file mode 100644 index 00000000..31f45077 --- /dev/null +++ b/src/Services/TagSnapshotProvider.php @@ -0,0 +1,51 @@ + $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; + } +} diff --git a/src/Upgrades/Upgrade29.php b/src/Upgrades/Upgrade29.php new file mode 100644 index 00000000..98d38b90 --- /dev/null +++ b/src/Upgrades/Upgrade29.php @@ -0,0 +1,26 @@ +tagDao = $tagDao; + $this->tagHistoryService = $tagHistoryService; + } + + public function run(DatabaseConnection $databaseConnection) + { + $pdo = $databaseConnection->getPDO(); + foreach ($this->tagDao->findAll() as $tag) + { + $this->tagHistoryService->saveTagChange($tag); + } + } +} diff --git a/src/di.php b/src/di.php index bd4145cd..80853591 100644 --- a/src/di.php +++ b/src/di.php @@ -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), ]; }),