Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/drydock/query/DrydockBlueprintQuery.php
12256 views
1
<?php
2
3
final class DrydockBlueprintQuery extends DrydockQuery {
4
5
private $ids;
6
private $phids;
7
private $blueprintClasses;
8
private $datasourceQuery;
9
private $disabled;
10
private $authorizedPHIDs;
11
12
private $identifiers;
13
private $identifierIDs;
14
private $identifierPHIDs;
15
private $identifierMap;
16
17
public function withIDs(array $ids) {
18
$this->ids = $ids;
19
return $this;
20
}
21
22
public function withPHIDs(array $phids) {
23
$this->phids = $phids;
24
return $this;
25
}
26
27
public function withBlueprintClasses(array $classes) {
28
$this->blueprintClasses = $classes;
29
return $this;
30
}
31
32
public function withDatasourceQuery($query) {
33
$this->datasourceQuery = $query;
34
return $this;
35
}
36
37
public function withDisabled($disabled) {
38
$this->disabled = $disabled;
39
return $this;
40
}
41
42
public function withAuthorizedPHIDs(array $phids) {
43
$this->authorizedPHIDs = $phids;
44
return $this;
45
}
46
47
public function withNameNgrams($ngrams) {
48
return $this->withNgramsConstraint(
49
new DrydockBlueprintNameNgrams(),
50
$ngrams);
51
}
52
53
public function withIdentifiers(array $identifiers) {
54
if (!$identifiers) {
55
throw new Exception(
56
pht(
57
'Can not issue a query with an empty identifier list.'));
58
}
59
60
$this->identifiers = $identifiers;
61
62
$ids = array();
63
$phids = array();
64
65
foreach ($identifiers as $identifier) {
66
if (ctype_digit($identifier)) {
67
$ids[] = $identifier;
68
} else {
69
$phids[] = $identifier;
70
}
71
}
72
73
$this->identifierIDs = $ids;
74
$this->identifierPHIDs = $phids;
75
76
return $this;
77
}
78
79
public function getIdentifierMap() {
80
if ($this->identifierMap === null) {
81
throw new Exception(
82
pht(
83
'Execute a query with identifiers before getting the '.
84
'identifier map.'));
85
}
86
87
return $this->identifierMap;
88
}
89
90
public function newResultObject() {
91
return new DrydockBlueprint();
92
}
93
94
protected function getPrimaryTableAlias() {
95
return 'blueprint';
96
}
97
98
protected function willExecute() {
99
if ($this->identifiers) {
100
$this->identifierMap = array();
101
} else {
102
$this->identifierMap = null;
103
}
104
}
105
106
protected function willFilterPage(array $blueprints) {
107
$impls = DrydockBlueprintImplementation::getAllBlueprintImplementations();
108
foreach ($blueprints as $key => $blueprint) {
109
$impl = idx($impls, $blueprint->getClassName());
110
if (!$impl) {
111
$this->didRejectResult($blueprint);
112
unset($blueprints[$key]);
113
continue;
114
}
115
$impl = clone $impl;
116
$blueprint->attachImplementation($impl);
117
}
118
119
if ($this->identifiers) {
120
$id_map = mpull($blueprints, null, 'getID');
121
$phid_map = mpull($blueprints, null, 'getPHID');
122
123
$map = $this->identifierMap;
124
125
foreach ($this->identifierIDs as $id) {
126
if (isset($id_map[$id])) {
127
$map[$id] = $id_map[$id];
128
}
129
}
130
131
foreach ($this->identifierPHIDs as $phid) {
132
if (isset($phid_map[$phid])) {
133
$map[$phid] = $phid_map[$phid];
134
}
135
}
136
137
// Just for consistency, reorder the map to match input order.
138
$map = array_select_keys($map, $this->identifiers);
139
140
$this->identifierMap = $map;
141
}
142
143
return $blueprints;
144
}
145
146
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
147
$where = parent::buildWhereClauseParts($conn);
148
149
if ($this->ids !== null) {
150
$where[] = qsprintf(
151
$conn,
152
'blueprint.id IN (%Ld)',
153
$this->ids);
154
}
155
156
if ($this->phids !== null) {
157
$where[] = qsprintf(
158
$conn,
159
'blueprint.phid IN (%Ls)',
160
$this->phids);
161
}
162
163
if ($this->datasourceQuery !== null) {
164
$where[] = qsprintf(
165
$conn,
166
'blueprint.blueprintName LIKE %>',
167
$this->datasourceQuery);
168
}
169
170
if ($this->blueprintClasses !== null) {
171
$where[] = qsprintf(
172
$conn,
173
'blueprint.className IN (%Ls)',
174
$this->blueprintClasses);
175
}
176
177
if ($this->disabled !== null) {
178
$where[] = qsprintf(
179
$conn,
180
'blueprint.isDisabled = %d',
181
(int)$this->disabled);
182
}
183
184
if ($this->identifiers !== null) {
185
$parts = array();
186
187
if ($this->identifierIDs) {
188
$parts[] = qsprintf(
189
$conn,
190
'blueprint.id IN (%Ld)',
191
$this->identifierIDs);
192
}
193
194
if ($this->identifierPHIDs) {
195
$parts[] = qsprintf(
196
$conn,
197
'blueprint.phid IN (%Ls)',
198
$this->identifierPHIDs);
199
}
200
201
$where[] = qsprintf(
202
$conn,
203
'%LO',
204
$parts);
205
}
206
207
return $where;
208
}
209
210
protected function shouldGroupQueryResultRows() {
211
if ($this->authorizedPHIDs !== null) {
212
return true;
213
}
214
return parent::shouldGroupQueryResultRows();
215
}
216
217
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
218
$joins = parent::buildJoinClauseParts($conn);
219
220
if ($this->authorizedPHIDs !== null) {
221
$joins[] = qsprintf(
222
$conn,
223
'JOIN %T authorization
224
ON authorization.blueprintPHID = blueprint.phid
225
AND authorization.objectPHID IN (%Ls)
226
AND authorization.objectAuthorizationState = %s
227
AND authorization.blueprintAuthorizationState = %s',
228
id(new DrydockAuthorization())->getTableName(),
229
$this->authorizedPHIDs,
230
DrydockAuthorization::OBJECTAUTH_ACTIVE,
231
DrydockAuthorization::BLUEPRINTAUTH_AUTHORIZED);
232
}
233
234
return $joins;
235
}
236
237
}
238
239