From f98ca03d7df5ed5baa4fc7ad1db4267122b4d539 Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 26 Jul 2013 13:13:38 -0700 Subject: [PATCH] Clean up more PonderAnswer cruft Summary: Ref T2715. Ref T3578. Moves PonderAnswerQuery toward being policy-aware so we can use application PHIDs. Test Plan: Viewed various Ponder things, voted on answers. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T2715, T3578 Differential Revision: https://secure.phabricator.com/D6581 --- .../controller/PonderVoteSaveController.php | 2 +- .../ponder/query/PonderAnswerQuery.php | 93 ++++++++++--------- .../ponder/storage/PonderAnswer.php | 9 +- 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/src/applications/ponder/controller/PonderVoteSaveController.php b/src/applications/ponder/controller/PonderVoteSaveController.php index 24ef3c4c55..d4169f9f4b 100644 --- a/src/applications/ponder/controller/PonderVoteSaveController.php +++ b/src/applications/ponder/controller/PonderVoteSaveController.php @@ -25,7 +25,7 @@ final class PonderVoteSaveController extends PonderController { } else if ($this->kind == "answer") { $target = id(new PonderAnswerQuery()) ->setViewer($user) - ->withPHID($phid) + ->withPHIDs(array($phid)) ->executeOne(); } diff --git a/src/applications/ponder/query/PonderAnswerQuery.php b/src/applications/ponder/query/PonderAnswerQuery.php index 608c3c62e5..ed0c6f926f 100644 --- a/src/applications/ponder/query/PonderAnswerQuery.php +++ b/src/applications/ponder/query/PonderAnswerQuery.php @@ -2,10 +2,10 @@ final class PonderAnswerQuery extends PhabricatorOffsetPagedQuery { - private $id; - private $phid; - private $authorPHID; - private $orderNewest; + private $ids; + private $phids; + private $authorPHIDs; + private $questionIDs; private $viewer; @@ -22,74 +22,83 @@ final class PonderAnswerQuery extends PhabricatorOffsetPagedQuery { return head($this->execute()); } - public function withID($qid) { - $this->id = $qid; + public function withIDs(array $ids) { + $this->ids = $ids; return $this; } - public function withPHID($phid) { - $this->phid = $phid; + public function withPHIDs(array $phids) { + $this->phids = $phids; return $this; } - public function withAuthorPHID($phid) { - $this->authorPHID = $phid; + public function withAuthorPHIDs(array $phids) { + $this->authorPHIDs = $phids; return $this; } - public function orderByNewest($usethis) { - $this->orderNewest = $usethis; + public function withQuestionIDs(array $ids) { + $this->questionIDs = $ids; return $this; } private function buildWhereClause($conn_r) { $where = array(); - if ($this->id) { - $where[] = qsprintf($conn_r, '(id = %d)', $this->id); + + if ($this->ids) { + $where[] = qsprintf( + $conn_r, + 'id IN (%Ld)', + $this->ids); } - if ($this->phid) { - $where[] = qsprintf($conn_r, '(phid = %s)', $this->phid); + + if ($this->phids) { + $where[] = qsprintf( + $conn_r, + 'phid IN (%Ls)', + $this->phids); } - if ($this->authorPHID) { - $where[] = qsprintf($conn_r, '(authorPHID = %s)', $this->authorPHID); + + if ($this->authorPHIDs) { + $where[] = qsprintf( + $conn_r, + 'authorPHID IN (%Ls)', + $this->authorPHIDs); } return $this->formatWhereClause($where); } private function buildOrderByClause($conn_r) { - $order = array(); - if ($this->orderNewest) { - $order[] = qsprintf($conn_r, 'id DESC'); - } - - if (count($order) == 0) { - $order[] = qsprintf($conn_r, 'id ASC'); - } - - return ($order ? 'ORDER BY ' . implode(', ', $order) : ''); + return 'ORDER BY id ASC'; } public function execute() { $answer = new PonderAnswer(); $conn_r = $answer->establishConnection('r'); - $select = qsprintf( + $data = queryfx_all( $conn_r, - 'SELECT r.* FROM %T r', - $answer->getTableName()); + 'SELECT a.* FROM %T a %Q %Q %Q', + $answer->getTableName(), + $this->buildWhereClause($conn_r), + $this->buildOrderByClause($conn_r), + $this->buildLimitClause($conn_r)); - $where = $this->buildWhereClause($conn_r); - $order_by = $this->buildOrderByClause($conn_r); - $limit = $this->buildLimitClause($conn_r); + $answers = $answer->loadAllFromArray($data); - return $answer->loadAllFromArray( - queryfx_all( - $conn_r, - '%Q %Q %Q %Q', - $select, - $where, - $order_by, - $limit)); + if ($answers) { + $questions = id(new PonderQuestionQuery()) + ->setViewer($this->getViewer()) + ->withIDs(mpull($answers, 'getQuestionID')) + ->execute(); + + foreach ($answers as $answer) { + $question = idx($questions, $answer->getQuestionID()); + $answer->attachQuestion($question); + } + } + + return $answers; } } diff --git a/src/applications/ponder/storage/PonderAnswer.php b/src/applications/ponder/storage/PonderAnswer.php index fd4b0ad988..ba6b1f24da 100644 --- a/src/applications/ponder/storage/PonderAnswer.php +++ b/src/applications/ponder/storage/PonderAnswer.php @@ -14,16 +14,21 @@ final class PonderAnswer extends PonderDAO protected $voteCount; private $vote; - private $question = null; + private $question = self::ATTACHABLE; private $comments; + // TODO: Get rid of this method. public function setQuestion($question) { + return $this->attachQuestion($question); + } + + public function attachQuestion(PonderQuestion $question = null) { $this->question = $question; return $this; } public function getQuestion() { - return $this->question; + return $this->assertAttached($this->question); } public function setUserVote($vote) {