@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
at recaptime-dev/main 153 lines 3.5 kB view raw
1<?php 2 3/** 4 * @extends PhabricatorCursorPagedPolicyAwareQuery<PonderQuestion> 5 */ 6final class PonderQuestionQuery 7 extends PhabricatorCursorPagedPolicyAwareQuery { 8 9 private $ids; 10 private $phids; 11 private $status; 12 private $authorPHIDs; 13 private $answererPHIDs; 14 15 private $needProjectPHIDs; 16 17 private $needAnswers; 18 19 public function withIDs(array $ids) { 20 $this->ids = $ids; 21 return $this; 22 } 23 24 public function withPHIDs(array $phids) { 25 $this->phids = $phids; 26 return $this; 27 } 28 29 public function withAuthorPHIDs(array $phids) { 30 $this->authorPHIDs = $phids; 31 return $this; 32 } 33 34 public function withStatuses($status) { 35 $this->status = $status; 36 return $this; 37 } 38 39 public function withAnswererPHIDs(array $phids) { 40 $this->answererPHIDs = $phids; 41 return $this; 42 } 43 44 public function needAnswers($need_answers) { 45 $this->needAnswers = $need_answers; 46 return $this; 47 } 48 49 public function needProjectPHIDs($need_projects) { 50 $this->needProjectPHIDs = $need_projects; 51 return $this; 52 } 53 54 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { 55 $where = parent::buildWhereClauseParts($conn); 56 57 if ($this->ids !== null) { 58 $where[] = qsprintf( 59 $conn, 60 'q.id IN (%Ld)', 61 $this->ids); 62 } 63 64 if ($this->phids !== null) { 65 $where[] = qsprintf( 66 $conn, 67 'q.phid IN (%Ls)', 68 $this->phids); 69 } 70 71 if ($this->authorPHIDs !== null) { 72 $where[] = qsprintf( 73 $conn, 74 'q.authorPHID IN (%Ls)', 75 $this->authorPHIDs); 76 } 77 78 if ($this->status !== null) { 79 $where[] = qsprintf( 80 $conn, 81 'q.status IN (%Ls)', 82 $this->status); 83 } 84 85 return $where; 86 } 87 88 public function newResultObject() { 89 return new PonderQuestion(); 90 } 91 92 protected function willFilterPage(array $questions) { 93 94 $phids = mpull($questions, 'getPHID'); 95 96 if ($this->needAnswers) { 97 $aquery = id(new PonderAnswerQuery()) 98 ->setViewer($this->getViewer()) 99 ->setOrderVector(array('-id')) 100 ->withQuestionIDs(mpull($questions, 'getID')); 101 102 $answers = $aquery->execute(); 103 $answers = mgroup($answers, 'getQuestionID'); 104 105 foreach ($questions as $question) { 106 $question_answers = idx($answers, $question->getID(), array()); 107 $question->attachAnswers(mpull($question_answers, null, 'getPHID')); 108 } 109 } 110 111 if ($this->needProjectPHIDs) { 112 $edge_query = id(new PhabricatorEdgeQuery()) 113 ->withSourcePHIDs($phids) 114 ->withEdgeTypes( 115 array( 116 PhabricatorProjectObjectHasProjectEdgeType::EDGECONST, 117 )); 118 $edge_query->execute(); 119 120 foreach ($questions as $question) { 121 $project_phids = $edge_query->getDestinationPHIDs( 122 array($question->getPHID())); 123 $question->attachProjectPHIDs($project_phids); 124 } 125 } 126 127 return $questions; 128 } 129 130 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { 131 $joins = parent::buildJoinClauseParts($conn); 132 133 if ($this->answererPHIDs) { 134 $answer_table = new PonderAnswer(); 135 $joins[] = qsprintf( 136 $conn, 137 'JOIN %T a ON a.questionID = q.id AND a.authorPHID IN (%Ls)', 138 $answer_table->getTableName(), 139 $this->answererPHIDs); 140 } 141 142 return $joins; 143 } 144 145 protected function getPrimaryTableAlias() { 146 return 'q'; 147 } 148 149 public function getQueryApplicationClass() { 150 return PhabricatorPonderApplication::class; 151 } 152 153}