diff --git a/resources/sql/autopatches/20170820.phame.01.post.views.sql b/resources/sql/autopatches/20170820.phame.01.post.views.sql new file mode 100644 --- /dev/null +++ b/resources/sql/autopatches/20170820.phame.01.post.views.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_phame.phame_post + ADD views INTEGER NOT NULL; diff --git a/resources/sql/autopatches/20170820.phame.02.post.views.sql b/resources/sql/autopatches/20170820.phame.02.post.views.sql new file mode 100644 --- /dev/null +++ b/resources/sql/autopatches/20170820.phame.02.post.views.sql @@ -0,0 +1,2 @@ +UPDATE {$NAMESPACE}_phame.phame_post + SET views = 0; diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -4417,6 +4417,7 @@ 'PhamePostTransactionQuery' => 'applications/phame/query/PhamePostTransactionQuery.php', 'PhamePostTransactionType' => 'applications/phame/xaction/PhamePostTransactionType.php', 'PhamePostViewController' => 'applications/phame/controller/post/PhamePostViewController.php', + 'PhamePostViewsTransaction' => 'applications/phame/xaction/PhamePostViewsTransaction.php', 'PhamePostVisibilityTransaction' => 'applications/phame/xaction/PhamePostVisibilityTransaction.php', 'PhameSchemaSpec' => 'applications/phame/storage/PhameSchemaSpec.php', 'PhameSite' => 'applications/phame/site/PhameSite.php', @@ -10041,6 +10042,7 @@ 'PhamePostTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 'PhamePostTransactionType' => 'PhabricatorModularTransactionType', 'PhamePostViewController' => 'PhameLiveController', + 'PhamePostViewsTransaction' => 'PhamePostTransactionType', 'PhamePostVisibilityTransaction' => 'PhamePostTransactionType', 'PhameSchemaSpec' => 'PhabricatorConfigSchemaSpec', 'PhameSite' => 'PhabricatorSite', diff --git a/src/applications/phame/controller/post/PhamePostViewController.php b/src/applications/phame/controller/post/PhamePostViewController.php --- a/src/applications/phame/controller/post/PhamePostViewController.php +++ b/src/applications/phame/controller/post/PhamePostViewController.php @@ -18,6 +18,25 @@ $is_live = $this->getIsLive(); $is_external = $this->getIsExternal(); + // Register a blog "view" count + // + if (!$post->isDraft() && !$post->isArchived()) { + $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); + $xactions = array(); + $xactions[] = id(new PhamePostTransaction()) + ->setTransactionType(PhamePostViewsTransaction::TRANSACTIONTYPE) + ->setNewValue(null); + + $editor = id(new PhamePostEditor()) + ->setActor($viewer) + ->setContentSourceFromRequest($request) + ->setContinueOnMissingFields(true) + ->setContinueOnNoEffect(true); + + $editor->applyTransactions($post, $xactions); + unset($unguarded); + } + $header = id(new PHUIHeaderView()) ->addClass('phame-header-bar') ->setUser($viewer); @@ -151,6 +170,11 @@ ->setUser($viewer) ->setObject($post); + $views = id(new PhutilNumber($post->getViews())); + $properties->addProperty( + pht('Views'), + pht('%s', $views)); + $is_live = $this->getIsLive(); $is_external = $this->getIsExternal(); $next_view = new PhameNextPostView(); diff --git a/src/applications/phame/storage/PhamePost.php b/src/applications/phame/storage/PhamePost.php --- a/src/applications/phame/storage/PhamePost.php +++ b/src/applications/phame/storage/PhamePost.php @@ -22,6 +22,7 @@ protected $phameTitle; protected $body; protected $visibility; + protected $views; protected $configData; protected $datePublished; protected $blogPHID; @@ -40,7 +41,8 @@ ->setBlogPHID($blog->getPHID()) ->attachBlog($blog) ->setDatePublished(PhabricatorTime::getNow()) - ->setVisibility(PhameConstants::VISIBILITY_PUBLISHED); + ->setVisibility(PhameConstants::VISIBILITY_PUBLISHED) + ->setViews(0); return $post; } @@ -128,6 +130,7 @@ 'subtitle' => 'text64', 'phameTitle' => 'sort64?', 'visibility' => 'uint32', + 'views' => 'uint32', 'mailKey' => 'bytes20', 'headerImagePHID' => 'phid?', diff --git a/src/applications/phame/xaction/PhamePostViewsTransaction.php b/src/applications/phame/xaction/PhamePostViewsTransaction.php new file mode 100644 --- /dev/null +++ b/src/applications/phame/xaction/PhamePostViewsTransaction.php @@ -0,0 +1,18 @@ +<?php + +final class PhamePostViewsTransaction + extends PhamePostTransactionType { + + const TRANSACTIONTYPE = 'phame.post.views'; + + public function generateOldValue($object) { + return $object->getViews(); + } + + public function applyInternalEffects($object, $value) { + $views = $object->getViews(); + $views++; + $object->setViews($views); + } + +}