Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/almanac/query/AlmanacServiceQuery.php
12256 views
1
<?php
2
3
final class AlmanacServiceQuery
4
extends AlmanacQuery {
5
6
private $ids;
7
private $phids;
8
private $names;
9
private $serviceTypes;
10
private $devicePHIDs;
11
private $namePrefix;
12
private $nameSuffix;
13
14
private $needBindings;
15
private $needActiveBindings;
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 withNames(array $names) {
28
$this->names = $names;
29
return $this;
30
}
31
32
public function withServiceTypes(array $types) {
33
$this->serviceTypes = $types;
34
return $this;
35
}
36
37
public function withDevicePHIDs(array $phids) {
38
$this->devicePHIDs = $phids;
39
return $this;
40
}
41
42
public function withNamePrefix($prefix) {
43
$this->namePrefix = $prefix;
44
return $this;
45
}
46
47
public function withNameSuffix($suffix) {
48
$this->nameSuffix = $suffix;
49
return $this;
50
}
51
52
public function withNameNgrams($ngrams) {
53
return $this->withNgramsConstraint(
54
new AlmanacServiceNameNgrams(),
55
$ngrams);
56
}
57
58
public function needBindings($need_bindings) {
59
$this->needBindings = $need_bindings;
60
return $this;
61
}
62
63
public function needActiveBindings($need_active) {
64
$this->needActiveBindings = $need_active;
65
return $this;
66
}
67
68
public function newResultObject() {
69
return new AlmanacService();
70
}
71
72
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
73
$joins = parent::buildJoinClauseParts($conn);
74
75
if ($this->shouldJoinBindingTable()) {
76
$joins[] = qsprintf(
77
$conn,
78
'JOIN %T binding ON service.phid = binding.servicePHID',
79
id(new AlmanacBinding())->getTableName());
80
}
81
82
return $joins;
83
}
84
85
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
86
$where = parent::buildWhereClauseParts($conn);
87
88
if ($this->ids !== null) {
89
$where[] = qsprintf(
90
$conn,
91
'service.id IN (%Ld)',
92
$this->ids);
93
}
94
95
if ($this->phids !== null) {
96
$where[] = qsprintf(
97
$conn,
98
'service.phid IN (%Ls)',
99
$this->phids);
100
}
101
102
if ($this->names !== null) {
103
$hashes = array();
104
foreach ($this->names as $name) {
105
$hashes[] = PhabricatorHash::digestForIndex($name);
106
}
107
108
$where[] = qsprintf(
109
$conn,
110
'service.nameIndex IN (%Ls)',
111
$hashes);
112
}
113
114
if ($this->serviceTypes !== null) {
115
$where[] = qsprintf(
116
$conn,
117
'service.serviceType IN (%Ls)',
118
$this->serviceTypes);
119
}
120
121
if ($this->devicePHIDs !== null) {
122
$where[] = qsprintf(
123
$conn,
124
'binding.devicePHID IN (%Ls)',
125
$this->devicePHIDs);
126
}
127
128
if ($this->namePrefix !== null) {
129
$where[] = qsprintf(
130
$conn,
131
'service.name LIKE %>',
132
$this->namePrefix);
133
}
134
135
if ($this->nameSuffix !== null) {
136
$where[] = qsprintf(
137
$conn,
138
'service.name LIKE %<',
139
$this->nameSuffix);
140
}
141
142
return $where;
143
}
144
145
protected function willFilterPage(array $services) {
146
$service_map = AlmanacServiceType::getAllServiceTypes();
147
148
foreach ($services as $key => $service) {
149
$implementation = idx($service_map, $service->getServiceType());
150
151
if (!$implementation) {
152
$this->didRejectResult($service);
153
unset($services[$key]);
154
continue;
155
}
156
157
$implementation = clone $implementation;
158
$service->attachServiceImplementation($implementation);
159
}
160
161
return $services;
162
}
163
164
protected function didFilterPage(array $services) {
165
$need_all = $this->needBindings;
166
$need_active = $this->needActiveBindings;
167
168
$need_any = ($need_all || $need_active);
169
$only_active = ($need_active && !$need_all);
170
171
if ($need_any) {
172
$service_phids = mpull($services, 'getPHID');
173
174
$bindings_query = id(new AlmanacBindingQuery())
175
->setViewer($this->getViewer())
176
->withServicePHIDs($service_phids)
177
->needProperties($this->getNeedProperties());
178
179
if ($only_active) {
180
$bindings_query->withIsActive(true);
181
}
182
183
$bindings = $bindings_query->execute();
184
$bindings = mgroup($bindings, 'getServicePHID');
185
186
foreach ($services as $service) {
187
$service_bindings = idx($bindings, $service->getPHID(), array());
188
189
if ($only_active) {
190
$service->attachActiveBindings($service_bindings);
191
} else {
192
$service->attachBindings($service_bindings);
193
}
194
}
195
}
196
197
return parent::didFilterPage($services);
198
}
199
200
private function shouldJoinBindingTable() {
201
return ($this->devicePHIDs !== null);
202
}
203
204
protected function shouldGroupQueryResultRows() {
205
if ($this->shouldJoinBindingTable()) {
206
return true;
207
}
208
209
return parent::shouldGroupQueryResultRows();
210
}
211
212
protected function getPrimaryTableAlias() {
213
return 'service';
214
}
215
216
public function getOrderableColumns() {
217
return parent::getOrderableColumns() + array(
218
'name' => array(
219
'table' => $this->getPrimaryTableAlias(),
220
'column' => 'name',
221
'type' => 'string',
222
'unique' => true,
223
'reverse' => true,
224
),
225
);
226
}
227
228
protected function newPagingMapFromPartialObject($object) {
229
return array(
230
'id' => (int)$object->getID(),
231
'name' => $object->getName(),
232
);
233
}
234
235
public function getBuiltinOrders() {
236
return array(
237
'name' => array(
238
'vector' => array('name'),
239
'name' => pht('Service Name'),
240
),
241
) + parent::getBuiltinOrders();
242
}
243
244
}
245
246