1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-23 15:22:41 +01:00

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
This commit is contained in:
epriestley 2013-07-26 13:13:38 -07:00
parent 7522b45ef2
commit f98ca03d7d
3 changed files with 59 additions and 45 deletions

View file

@ -25,7 +25,7 @@ final class PonderVoteSaveController extends PonderController {
} else if ($this->kind == "answer") { } else if ($this->kind == "answer") {
$target = id(new PonderAnswerQuery()) $target = id(new PonderAnswerQuery())
->setViewer($user) ->setViewer($user)
->withPHID($phid) ->withPHIDs(array($phid))
->executeOne(); ->executeOne();
} }

View file

@ -2,10 +2,10 @@
final class PonderAnswerQuery extends PhabricatorOffsetPagedQuery { final class PonderAnswerQuery extends PhabricatorOffsetPagedQuery {
private $id; private $ids;
private $phid; private $phids;
private $authorPHID; private $authorPHIDs;
private $orderNewest; private $questionIDs;
private $viewer; private $viewer;
@ -22,74 +22,83 @@ final class PonderAnswerQuery extends PhabricatorOffsetPagedQuery {
return head($this->execute()); return head($this->execute());
} }
public function withID($qid) { public function withIDs(array $ids) {
$this->id = $qid; $this->ids = $ids;
return $this; return $this;
} }
public function withPHID($phid) { public function withPHIDs(array $phids) {
$this->phid = $phid; $this->phids = $phids;
return $this; return $this;
} }
public function withAuthorPHID($phid) { public function withAuthorPHIDs(array $phids) {
$this->authorPHID = $phid; $this->authorPHIDs = $phids;
return $this; return $this;
} }
public function orderByNewest($usethis) { public function withQuestionIDs(array $ids) {
$this->orderNewest = $usethis; $this->questionIDs = $ids;
return $this; return $this;
} }
private function buildWhereClause($conn_r) { private function buildWhereClause($conn_r) {
$where = array(); $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); return $this->formatWhereClause($where);
} }
private function buildOrderByClause($conn_r) { private function buildOrderByClause($conn_r) {
$order = array(); return 'ORDER BY id ASC';
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) : '');
} }
public function execute() { public function execute() {
$answer = new PonderAnswer(); $answer = new PonderAnswer();
$conn_r = $answer->establishConnection('r'); $conn_r = $answer->establishConnection('r');
$select = qsprintf( $data = queryfx_all(
$conn_r, $conn_r,
'SELECT r.* FROM %T r', 'SELECT a.* FROM %T a %Q %Q %Q',
$answer->getTableName()); $answer->getTableName(),
$this->buildWhereClause($conn_r),
$this->buildOrderByClause($conn_r),
$this->buildLimitClause($conn_r));
$where = $this->buildWhereClause($conn_r); $answers = $answer->loadAllFromArray($data);
$order_by = $this->buildOrderByClause($conn_r);
$limit = $this->buildLimitClause($conn_r);
return $answer->loadAllFromArray( if ($answers) {
queryfx_all( $questions = id(new PonderQuestionQuery())
$conn_r, ->setViewer($this->getViewer())
'%Q %Q %Q %Q', ->withIDs(mpull($answers, 'getQuestionID'))
$select, ->execute();
$where,
$order_by, foreach ($answers as $answer) {
$limit)); $question = idx($questions, $answer->getQuestionID());
$answer->attachQuestion($question);
}
}
return $answers;
} }
} }

View file

@ -14,16 +14,21 @@ final class PonderAnswer extends PonderDAO
protected $voteCount; protected $voteCount;
private $vote; private $vote;
private $question = null; private $question = self::ATTACHABLE;
private $comments; private $comments;
// TODO: Get rid of this method.
public function setQuestion($question) { public function setQuestion($question) {
return $this->attachQuestion($question);
}
public function attachQuestion(PonderQuestion $question = null) {
$this->question = $question; $this->question = $question;
return $this; return $this;
} }
public function getQuestion() { public function getQuestion() {
return $this->question; return $this->assertAttached($this->question);
} }
public function setUserVote($vote) { public function setUserVote($vote) {