@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator

Add a basic view count to Phame

Summary: This adds a very very basic view count to Phame, so bloggers can get some idea which posts are more popular than others. Anything more than this I think should be Facts or Google Analytics.

Test Plan: Write a new post, see post count. Reload page, post count goes up. Archive post, post count stays the same.

Reviewers: epriestley

Reviewed By: epriestley

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D18446

+58 -1
+2
resources/sql/autopatches/20170820.phame.01.post.views.sql
··· 1 + ALTER TABLE {$NAMESPACE}_phame.phame_post 2 + ADD views INTEGER NOT NULL;
+2
resources/sql/autopatches/20170820.phame.02.post.views.sql
··· 1 + UPDATE {$NAMESPACE}_phame.phame_post 2 + SET views = 0;
+2
src/__phutil_library_map__.php
··· 4417 4417 'PhamePostTransactionQuery' => 'applications/phame/query/PhamePostTransactionQuery.php', 4418 4418 'PhamePostTransactionType' => 'applications/phame/xaction/PhamePostTransactionType.php', 4419 4419 'PhamePostViewController' => 'applications/phame/controller/post/PhamePostViewController.php', 4420 + 'PhamePostViewsTransaction' => 'applications/phame/xaction/PhamePostViewsTransaction.php', 4420 4421 'PhamePostVisibilityTransaction' => 'applications/phame/xaction/PhamePostVisibilityTransaction.php', 4421 4422 'PhameSchemaSpec' => 'applications/phame/storage/PhameSchemaSpec.php', 4422 4423 'PhameSite' => 'applications/phame/site/PhameSite.php', ··· 10041 10042 'PhamePostTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 10042 10043 'PhamePostTransactionType' => 'PhabricatorModularTransactionType', 10043 10044 'PhamePostViewController' => 'PhameLiveController', 10045 + 'PhamePostViewsTransaction' => 'PhamePostTransactionType', 10044 10046 'PhamePostVisibilityTransaction' => 'PhamePostTransactionType', 10045 10047 'PhameSchemaSpec' => 'PhabricatorConfigSchemaSpec', 10046 10048 'PhameSite' => 'PhabricatorSite',
+24
src/applications/phame/controller/post/PhamePostViewController.php
··· 18 18 $is_live = $this->getIsLive(); 19 19 $is_external = $this->getIsExternal(); 20 20 21 + // Register a blog "view" count 22 + // 23 + if (!$post->isDraft() && !$post->isArchived()) { 24 + $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); 25 + $xactions = array(); 26 + $xactions[] = id(new PhamePostTransaction()) 27 + ->setTransactionType(PhamePostViewsTransaction::TRANSACTIONTYPE) 28 + ->setNewValue(null); 29 + 30 + $editor = id(new PhamePostEditor()) 31 + ->setActor($viewer) 32 + ->setContentSourceFromRequest($request) 33 + ->setContinueOnMissingFields(true) 34 + ->setContinueOnNoEffect(true); 35 + 36 + $editor->applyTransactions($post, $xactions); 37 + unset($unguarded); 38 + } 39 + 21 40 $header = id(new PHUIHeaderView()) 22 41 ->addClass('phame-header-bar') 23 42 ->setUser($viewer); ··· 150 169 $properties = id(new PHUIPropertyListView()) 151 170 ->setUser($viewer) 152 171 ->setObject($post); 172 + 173 + $views = id(new PhutilNumber($post->getViews())); 174 + $properties->addProperty( 175 + pht('Views'), 176 + pht('%s', $views)); 153 177 154 178 $is_live = $this->getIsLive(); 155 179 $is_external = $this->getIsExternal();
+6
src/applications/phame/editor/PhamePostEditor.php
··· 41 41 if ($object->isDraft() || $object->isArchived()) { 42 42 return false; 43 43 } 44 + foreach ($xactions as $xaction) { 45 + switch ($xaction->getTransactionType()) { 46 + case PhamePostViewsTransaction::TRANSACTIONTYPE: 47 + return false; 48 + } 49 + } 44 50 return true; 45 51 } 46 52
+4 -1
src/applications/phame/storage/PhamePost.php
··· 22 22 protected $phameTitle; 23 23 protected $body; 24 24 protected $visibility; 25 + protected $views; 25 26 protected $configData; 26 27 protected $datePublished; 27 28 protected $blogPHID; ··· 40 41 ->setBlogPHID($blog->getPHID()) 41 42 ->attachBlog($blog) 42 43 ->setDatePublished(PhabricatorTime::getNow()) 43 - ->setVisibility(PhameConstants::VISIBILITY_PUBLISHED); 44 + ->setVisibility(PhameConstants::VISIBILITY_PUBLISHED) 45 + ->setViews(0); 44 46 45 47 return $post; 46 48 } ··· 128 130 'subtitle' => 'text64', 129 131 'phameTitle' => 'sort64?', 130 132 'visibility' => 'uint32', 133 + 'views' => 'uint32', 131 134 'mailKey' => 'bytes20', 132 135 'headerImagePHID' => 'phid?', 133 136
+18
src/applications/phame/xaction/PhamePostViewsTransaction.php
··· 1 + <?php 2 + 3 + final class PhamePostViewsTransaction 4 + extends PhamePostTransactionType { 5 + 6 + const TRANSACTIONTYPE = 'phame.post.views'; 7 + 8 + public function generateOldValue($object) { 9 + return $object->getViews(); 10 + } 11 + 12 + public function applyInternalEffects($object, $value) { 13 + $views = $object->getViews(); 14 + $views++; 15 + $object->setViews($views); 16 + } 17 + 18 + }