From 320cd2e194aa60d0fb4ff544d11657102082e732 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 1 Jun 2014 14:07:53 +0200 Subject: [PATCH] Simplified post revision management --- src/Controllers/PostController.php | 4 ++-- src/Models/Entities/PostEntity.php | 27 ++++++------------------ src/Models/PostModel.php | 1 + src/Upgrades/mysql/Upgrade16.sql | 1 + src/Upgrades/sqlite/Upgrade16.sql | 1 + src/Views/post/post-edit.phtml | 6 +++--- tests/Tests/JobTests/EditPostJobTest.php | 7 ++++-- 7 files changed, 20 insertions(+), 27 deletions(-) create mode 100644 src/Upgrades/mysql/Upgrade16.sql create mode 100644 src/Upgrades/sqlite/Upgrade16.sql diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 61445dcd..fc29bc7b 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -157,8 +157,8 @@ class PostController extends AbstractController { $post = PostModel::getByIdOrName($identifier); - $editToken = InputHelper::get('edit-token'); - if ($editToken != $post->getEditToken()) + $revision = InputHelper::get('revision'); + if ($revision != $post->getRevision()) throw new SimpleException('This post was already edited by someone else in the meantime'); $jobArgs = diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 1ca0d1c1..680da76f 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -47,6 +47,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ $this->setCache('comment_count', $row['comment_count']); $this->setCache('fav_count', $row['fav_count']); $this->setCache('score', $row['score']); + $this->setCache('revision', $row['revision']); $this->setType(new PostType($row['type'])); $this->setSafety(new PostSafety($row['safety'])); } @@ -70,6 +71,7 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ 'tags' => array_map(function($tag) { return $tag->getName(); }, $this->getTags()), 'type' => $this->getType()->toInteger(), 'safety' => $this->getSafety()->toInteger(), + 'revision' => $this->getRevision(), ]; } @@ -145,6 +147,11 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ return $favorites; } + public function getRevision() + { + return (int) $this->getColumnWithCache('revision'); + } + public function getScore() { return (int) $this->getColumnWithCache('score'); @@ -521,24 +528,4 @@ final class PostEntity extends AbstractEntity implements IValidatable, ISerializ unlink($tmpPath); } } - - public function getEditToken() - { - $x = []; - - foreach ($this->getTags() as $tag) - $x []= TextHelper::reprTag($tag->getName()); - - foreach ($this->getRelations() as $relatedPost) - $x []= TextHelper::reprPost($relatedPost); - - $x []= $this->getSafety()->toInteger(); - $x []= $this->getSource(); - $x []= $this->getFileHash(); - - natcasesort($x); - - $x = join(' ', $x); - return md5($x); - } } diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 4727caae..16137ed0 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -30,6 +30,7 @@ final class PostModel extends AbstractCrudModel 'image_height' => $post->getImageHeight(), 'uploader_id' => $post->getUploaderId(), 'source' => $post->getSource(), + 'revision' => $post->getRevision() + 1, ]; $stmt = Sql\Statements::update(); diff --git a/src/Upgrades/mysql/Upgrade16.sql b/src/Upgrades/mysql/Upgrade16.sql new file mode 100644 index 00000000..87677f77 --- /dev/null +++ b/src/Upgrades/mysql/Upgrade16.sql @@ -0,0 +1 @@ +ALTER TABLE post ADD COLUMN revision INTEGER DEFAULT 1; diff --git a/src/Upgrades/sqlite/Upgrade16.sql b/src/Upgrades/sqlite/Upgrade16.sql new file mode 100644 index 00000000..87677f77 --- /dev/null +++ b/src/Upgrades/sqlite/Upgrade16.sql @@ -0,0 +1 @@ +ALTER TABLE post ADD COLUMN revision INTEGER DEFAULT 1; diff --git a/src/Views/post/post-edit.phtml b/src/Views/post/post-edit.phtml index 52c60d01..9cd1a426 100644 --- a/src/Views/post/post-edit.phtml +++ b/src/Views/post/post-edit.phtml @@ -8,9 +8,9 @@

edit post

+ name="revision" + id="revision" + value="context->transport->post->getRevision()) ?>"/> grantAccess('editPostContent'); $post = $this->postMocker->mockSingle(); + $this->assert->areEqual(1, $post->getRevision()); $args = [ @@ -20,10 +21,12 @@ class EditPostJobTest extends AbstractTest new ApiFileInput($this->testSupport->getPath('image.jpg'), 'test.jpg'), ]; - $this->assert->doesNotThrow(function() use ($args) + $post = $this->assert->doesNotThrow(function() use ($args) { - Api::run(new EditPostJob(), $args); + return Api::run(new EditPostJob(), $args); }); + + $this->assert->areEqual(2, $post->getRevision()); } public function testPartialPrivilegeFail()