From 1beda307921c7da0bfdffebc2ac8934c2a0e38ee Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 4 Mar 2013 11:47:58 -0800 Subject: [PATCH] Surface token counts for Pholio Summary: Provide this data so the list view can present it somehow. Test Plan: {F34520} Reviewers: chad Reviewed By: chad CC: aran Differential Revision: https://secure.phabricator.com/D5210 --- src/__phutil_library_map__.php | 2 + .../controller/PholioMockListController.php | 4 +- .../pholio/query/PholioMockQuery.php | 58 +++++++++++++------ .../pholio/storage/PholioMock.php | 13 +++++ .../query/PhabricatorTokenCountQuery.php | 40 +++++++++++++ 5 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 src/applications/tokens/query/PhabricatorTokenCountQuery.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 95790301d2..f9098d0322 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1343,6 +1343,7 @@ phutil_register_library_map(array( 'PhabricatorToken' => 'applications/tokens/storage/PhabricatorToken.php', 'PhabricatorTokenController' => 'applications/tokens/controller/PhabricatorTokenController.php', 'PhabricatorTokenCount' => 'applications/tokens/storage/PhabricatorTokenCount.php', + 'PhabricatorTokenCountQuery' => 'applications/tokens/query/PhabricatorTokenCountQuery.php', 'PhabricatorTokenDAO' => 'applications/tokens/storage/PhabricatorTokenDAO.php', 'PhabricatorTokenGiveController' => 'applications/tokens/controller/PhabricatorTokenGiveController.php', 'PhabricatorTokenGiven' => 'applications/tokens/storage/PhabricatorTokenGiven.php', @@ -2813,6 +2814,7 @@ phutil_register_library_map(array( ), 'PhabricatorTokenController' => 'PhabricatorController', 'PhabricatorTokenCount' => 'PhabricatorTokenDAO', + 'PhabricatorTokenCountQuery' => 'PhabricatorOffsetPagedQuery', 'PhabricatorTokenDAO' => 'PhabricatorLiskDAO', 'PhabricatorTokenGiveController' => 'PhabricatorTokenController', 'PhabricatorTokenGiven' => diff --git a/src/applications/pholio/controller/PholioMockListController.php b/src/applications/pholio/controller/PholioMockListController.php index dd6dba0474..62c67b8609 100644 --- a/src/applications/pholio/controller/PholioMockListController.php +++ b/src/applications/pholio/controller/PholioMockListController.php @@ -18,7 +18,8 @@ final class PholioMockListController extends PholioController { $query = id(new PholioMockQuery()) ->setViewer($user) - ->needCoverFiles(true); + ->needCoverFiles(true) + ->needTokenCounts(true); $nav = $this->buildSideNav(); $filter = $nav->selectFilter('view/'.$this->view, 'view/all'); @@ -65,6 +66,7 @@ final class PholioMockListController extends PholioController { 'div', array(), pht('Created on %s', $datetime))); + $board->addItem($item); } diff --git a/src/applications/pholio/query/PholioMockQuery.php b/src/applications/pholio/query/PholioMockQuery.php index a0f4c256ac..8b7572702e 100644 --- a/src/applications/pholio/query/PholioMockQuery.php +++ b/src/applications/pholio/query/PholioMockQuery.php @@ -13,6 +13,7 @@ final class PholioMockQuery private $needCoverFiles; private $needImages; private $needInlineComments; + private $needTokenCounts; public function withIDs(array $ids) { $this->ids = $ids; @@ -29,6 +30,26 @@ final class PholioMockQuery return $this; } + public function needCoverFiles($need_cover_files) { + $this->needCoverFiles = $need_cover_files; + return $this; + } + + public function needImages($need_images) { + $this->needImages = $need_images; + return $this; + } + + public function needInlineComments($need_inline_comments) { + $this->needInlineComments = $need_inline_comments; + return $this; + } + + public function needTokenCounts($need) { + $this->needTokenCounts = $need; + return $this; + } + public function loadPage() { $table = new PholioMock(); $conn_r = $table->establishConnection('r'); @@ -46,10 +67,14 @@ final class PholioMockQuery if ($mocks && $this->needImages) { $this->loadImages($mocks); } + if ($mocks && $this->needCoverFiles) { $this->loadCoverFiles($mocks); } + if ($mocks && $this->needTokenCounts) { + $this->loadTokenCounts($mocks); + } return $mocks; } @@ -83,22 +108,7 @@ final class PholioMockQuery return $this->formatWhereClause($where); } - public function needCoverFiles($need_cover_files) { - $this->needCoverFiles = $need_cover_files; - return $this; - } - - public function needImages($need_images) { - $this->needImages = $need_images; - return $this; - } - - public function needInlineComments($need_inline_comments) { - $this->needInlineComments = $need_inline_comments; - return $this; - } - - public function loadImages(array $mocks) { + private function loadImages(array $mocks) { assert_instances_of($mocks, 'PholioMock'); $mock_ids = mpull($mocks, 'getID'); @@ -133,7 +143,7 @@ final class PholioMockQuery } } - public function loadCoverFiles(array $mocks) { + private function loadCoverFiles(array $mocks) { assert_instances_of($mocks, 'PholioMock'); $cover_file_phids = mpull($mocks, 'getCoverPHID'); $cover_files = mpull(id(new PhabricatorFile())->loadAllWhere( @@ -144,4 +154,18 @@ final class PholioMockQuery $mock->attachCoverFile($cover_files[$mock->getCoverPHID()]); } } + + private function loadTokenCounts(array $mocks) { + assert_instances_of($mocks, 'PholioMock'); + + $phids = mpull($mocks, 'getPHID'); + $counts = id(new PhabricatorTokenCountQuery()) + ->withObjectPHIDs($phids) + ->execute(); + + foreach ($mocks as $mock) { + $mock->attachTokenCount(idx($counts, $mock->getPHID(), 0)); + } + } + } diff --git a/src/applications/pholio/storage/PholioMock.php b/src/applications/pholio/storage/PholioMock.php index 11bfa75031..5574c310a8 100644 --- a/src/applications/pholio/storage/PholioMock.php +++ b/src/applications/pholio/storage/PholioMock.php @@ -24,6 +24,7 @@ final class PholioMock extends PholioDAO private $images; private $coverFile; + private $tokenCount; public function getConfiguration() { return array( @@ -67,6 +68,18 @@ final class PholioMock extends PholioDAO return $this->coverFile; } + public function getTokenCount() { + if ($this->tokenCount === null) { + throw new Exception("Call attachTokenCount() before getTokenCount()!"); + } + return $this->tokenCount; + } + + public function attachTokenCount($count) { + $this->tokenCount = $count; + return $this; + } + /* -( PhabricatorSubscribableInterface Implementation )-------------------- */ diff --git a/src/applications/tokens/query/PhabricatorTokenCountQuery.php b/src/applications/tokens/query/PhabricatorTokenCountQuery.php new file mode 100644 index 0000000000..cc333109a2 --- /dev/null +++ b/src/applications/tokens/query/PhabricatorTokenCountQuery.php @@ -0,0 +1,40 @@ +objectPHIDs = $object_phids; + return $this; + } + + final public function execute() { + $table = new PhabricatorTokenCount(); + $conn_r = $table->establishConnection('r'); + + $rows = queryfx_all( + $conn_r, + 'SELECT objectPHID, tokenCount FROM %T %Q %Q', + $table->getTableName(), + $this->buildWhereClause($conn_r), + $this->buildLimitClause($conn_r)); + + return ipull($rows, 'tokenCount', 'objectPHID'); + } + + private function buildWhereClause(AphrontDatabaseConnection $conn_r) { + $where = array(); + + if ($this->objectPHIDs) { + $where[] = qsprintf( + $conn_r, + 'objectPHID IN (%Ls)', + $this->objectPHIDs); + } + + return $this->formatWhereClause($where); + } + +}