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:
parent
7522b45ef2
commit
f98ca03d7d
3 changed files with 59 additions and 45 deletions
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue