Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / releeph / query / ReleephRequestQuery.php
blob3042260387a3c2292cc6f865f60f4e0b4cfbb682
1 <?php
3 final class ReleephRequestQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $requestedCommitPHIDs;
7 private $ids;
8 private $phids;
9 private $severities;
10 private $requestorPHIDs;
11 private $branchIDs;
12 private $requestedObjectPHIDs;
14 const STATUS_ALL = 'status-all';
15 const STATUS_OPEN = 'status-open';
16 const STATUS_REQUESTED = 'status-requested';
17 const STATUS_NEEDS_PULL = 'status-needs-pull';
18 const STATUS_REJECTED = 'status-rejected';
19 const STATUS_ABANDONED = 'status-abandoned';
20 const STATUS_PULLED = 'status-pulled';
21 const STATUS_NEEDS_REVERT = 'status-needs-revert';
22 const STATUS_REVERTED = 'status-reverted';
24 private $status = self::STATUS_ALL;
26 public function withIDs(array $ids) {
27 $this->ids = $ids;
28 return $this;
31 public function withPHIDs(array $phids) {
32 $this->phids = $phids;
33 return $this;
36 public function withBranchIDs(array $branch_ids) {
37 $this->branchIDs = $branch_ids;
38 return $this;
41 public function withStatus($status) {
42 $this->status = $status;
43 return $this;
46 public function withRequestedCommitPHIDs(array $requested_commit_phids) {
47 $this->requestedCommitPHIDs = $requested_commit_phids;
48 return $this;
51 public function withRequestorPHIDs(array $phids) {
52 $this->requestorPHIDs = $phids;
53 return $this;
56 public function withSeverities(array $severities) {
57 $this->severities = $severities;
58 return $this;
61 public function withRequestedObjectPHIDs(array $phids) {
62 $this->requestedObjectPHIDs = $phids;
63 return $this;
66 protected function loadPage() {
67 $table = new ReleephRequest();
68 $conn_r = $table->establishConnection('r');
70 $data = queryfx_all(
71 $conn_r,
72 'SELECT * FROM %T %Q %Q %Q',
73 $table->getTableName(),
74 $this->buildWhereClause($conn_r),
75 $this->buildOrderClause($conn_r),
76 $this->buildLimitClause($conn_r));
78 return $table->loadAllFromArray($data);
81 protected function willFilterPage(array $requests) {
82 // Load requested objects: you must be able to see an object to see
83 // requests for it.
84 $object_phids = mpull($requests, 'getRequestedObjectPHID');
85 $objects = id(new PhabricatorObjectQuery())
86 ->setViewer($this->getViewer())
87 ->setParentQuery($this)
88 ->withPHIDs($object_phids)
89 ->execute();
91 foreach ($requests as $key => $request) {
92 $object_phid = $request->getRequestedObjectPHID();
93 $object = idx($objects, $object_phid);
94 if (!$object) {
95 unset($requests[$key]);
96 continue;
98 $request->attachRequestedObject($object);
101 if ($this->severities) {
102 $severities = array_fuse($this->severities);
103 foreach ($requests as $key => $request) {
105 // NOTE: Facebook uses a custom field here.
106 if (ReleephDefaultFieldSelector::isFacebook()) {
107 $severity = $request->getDetail('severity');
108 } else {
109 $severity = $request->getDetail('releeph:severity');
112 if (empty($severities[$severity])) {
113 unset($requests[$key]);
118 $branch_ids = array_unique(mpull($requests, 'getBranchID'));
119 $branches = id(new ReleephBranchQuery())
120 ->withIDs($branch_ids)
121 ->setViewer($this->getViewer())
122 ->execute();
123 $branches = mpull($branches, null, 'getID');
124 foreach ($requests as $key => $request) {
125 $branch = idx($branches, $request->getBranchID());
126 if (!$branch) {
127 unset($requests[$key]);
128 continue;
130 $request->attachBranch($branch);
133 // TODO: These should be serviced by the query, but are not currently
134 // denormalized anywhere. For now, filter them here instead. Note that
135 // we must perform this filtering *after* querying and attaching branches,
136 // because request status depends on the product.
138 $keep_status = array_fuse($this->getKeepStatusConstants());
139 if ($keep_status) {
140 foreach ($requests as $key => $request) {
141 if (empty($keep_status[$request->getStatus()])) {
142 unset($requests[$key]);
147 return $requests;
150 protected function buildWhereClause(AphrontDatabaseConnection $conn) {
151 $where = array();
153 if ($this->ids !== null) {
154 $where[] = qsprintf(
155 $conn,
156 'id IN (%Ld)',
157 $this->ids);
160 if ($this->phids !== null) {
161 $where[] = qsprintf(
162 $conn,
163 'phid IN (%Ls)',
164 $this->phids);
167 if ($this->branchIDs !== null) {
168 $where[] = qsprintf(
169 $conn,
170 'branchID IN (%Ld)',
171 $this->branchIDs);
174 if ($this->requestedCommitPHIDs !== null) {
175 $where[] = qsprintf(
176 $conn,
177 'requestCommitPHID IN (%Ls)',
178 $this->requestedCommitPHIDs);
181 if ($this->requestorPHIDs !== null) {
182 $where[] = qsprintf(
183 $conn,
184 'requestUserPHID IN (%Ls)',
185 $this->requestorPHIDs);
188 if ($this->requestedObjectPHIDs !== null) {
189 $where[] = qsprintf(
190 $conn,
191 'requestedObjectPHID IN (%Ls)',
192 $this->requestedObjectPHIDs);
195 $where[] = $this->buildPagingClause($conn);
197 return $this->formatWhereClause($conn, $where);
200 private function getKeepStatusConstants() {
201 switch ($this->status) {
202 case self::STATUS_ALL:
203 return array();
204 case self::STATUS_OPEN:
205 return array(
206 ReleephRequestStatus::STATUS_REQUESTED,
207 ReleephRequestStatus::STATUS_NEEDS_PICK,
208 ReleephRequestStatus::STATUS_NEEDS_REVERT,
210 case self::STATUS_REQUESTED:
211 return array(
212 ReleephRequestStatus::STATUS_REQUESTED,
214 case self::STATUS_NEEDS_PULL:
215 return array(
216 ReleephRequestStatus::STATUS_NEEDS_PICK,
218 case self::STATUS_REJECTED:
219 return array(
220 ReleephRequestStatus::STATUS_REJECTED,
222 case self::STATUS_ABANDONED:
223 return array(
224 ReleephRequestStatus::STATUS_ABANDONED,
226 case self::STATUS_PULLED:
227 return array(
228 ReleephRequestStatus::STATUS_PICKED,
230 case self::STATUS_NEEDS_REVERT:
231 return array(
232 ReleephRequestStatus::STATUS_NEEDS_REVERT,
234 case self::STATUS_REVERTED:
235 return array(
236 ReleephRequestStatus::STATUS_REVERTED,
238 default:
239 throw new Exception(pht("Unknown status '%s'!", $this->status));
243 public function getQueryApplicationClass() {
244 return 'PhabricatorReleephApplication';