@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
1<?php
2
3/**
4 * @extends PhabricatorCursorPagedPolicyAwareQuery<HarbormasterBuildPlan>
5 */
6final class HarbormasterBuildPlanQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $ids;
10 private $phids;
11 private $statuses;
12 private $datasourceQuery;
13 private $planAutoKeys;
14 private $needBuildSteps;
15
16 public function withIDs(array $ids) {
17 $this->ids = $ids;
18 return $this;
19 }
20
21 public function withPHIDs(array $phids) {
22 $this->phids = $phids;
23 return $this;
24 }
25
26 public function withStatuses(array $statuses) {
27 $this->statuses = $statuses;
28 return $this;
29 }
30
31 public function withDatasourceQuery($query) {
32 $this->datasourceQuery = $query;
33 return $this;
34 }
35
36 public function withPlanAutoKeys(array $keys) {
37 $this->planAutoKeys = $keys;
38 return $this;
39 }
40
41 public function withNameNgrams($ngrams) {
42 return $this->withNgramsConstraint(
43 new HarbormasterBuildPlanNameNgrams(),
44 $ngrams);
45 }
46
47 public function needBuildSteps($need) {
48 $this->needBuildSteps = $need;
49 return $this;
50 }
51
52 public function newResultObject() {
53 return new HarbormasterBuildPlan();
54 }
55
56 protected function didFilterPage(array $page) {
57 if ($this->needBuildSteps) {
58 $plan_phids = mpull($page, 'getPHID');
59
60 $steps = id(new HarbormasterBuildStepQuery())
61 ->setParentQuery($this)
62 ->setViewer($this->getViewer())
63 ->withBuildPlanPHIDs($plan_phids)
64 ->execute();
65 $steps = mgroup($steps, 'getBuildPlanPHID');
66
67 foreach ($page as $plan) {
68 $plan_steps = idx($steps, $plan->getPHID(), array());
69 $plan->attachBuildSteps($plan_steps);
70 }
71 }
72
73 return $page;
74 }
75
76 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
77 $where = parent::buildWhereClauseParts($conn);
78
79 if ($this->ids !== null) {
80 $where[] = qsprintf(
81 $conn,
82 'plan.id IN (%Ld)',
83 $this->ids);
84 }
85
86 if ($this->phids !== null) {
87 $where[] = qsprintf(
88 $conn,
89 'plan.phid IN (%Ls)',
90 $this->phids);
91 }
92
93 if ($this->statuses !== null) {
94 $where[] = qsprintf(
95 $conn,
96 'plan.planStatus IN (%Ls)',
97 $this->statuses);
98 }
99
100 if (!phutil_nonempty_string($this->datasourceQuery)) {
101 $where[] = qsprintf(
102 $conn,
103 'plan.name LIKE %>',
104 $this->datasourceQuery);
105 }
106
107 if ($this->planAutoKeys !== null) {
108 $where[] = qsprintf(
109 $conn,
110 'plan.planAutoKey IN (%Ls)',
111 $this->planAutoKeys);
112 }
113
114 return $where;
115 }
116
117 protected function getPrimaryTableAlias() {
118 return 'plan';
119 }
120
121 public function getQueryApplicationClass() {
122 return PhabricatorHarbormasterApplication::class;
123 }
124
125 public function getOrderableColumns() {
126 return parent::getOrderableColumns() + array(
127 'name' => array(
128 'column' => 'name',
129 'type' => 'string',
130 'reverse' => true,
131 ),
132 );
133 }
134
135 protected function newPagingMapFromPartialObject($object) {
136 return array(
137 'id' => (int)$object->getID(),
138 'name' => $object->getName(),
139 );
140 }
141
142 public function getBuiltinOrders() {
143 return array(
144 'name' => array(
145 'vector' => array('name', 'id'),
146 'name' => pht('Name'),
147 ),
148 ) + parent::getBuiltinOrders();
149 }
150
151}