Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/almanac/query/AlmanacInterfaceQuery.php
12256 views
1
<?php
2
3
final class AlmanacInterfaceQuery
4
extends AlmanacQuery {
5
6
private $ids;
7
private $phids;
8
private $networkPHIDs;
9
private $devicePHIDs;
10
private $addresses;
11
12
public function withIDs(array $ids) {
13
$this->ids = $ids;
14
return $this;
15
}
16
17
public function withPHIDs(array $phids) {
18
$this->phids = $phids;
19
return $this;
20
}
21
22
public function withNetworkPHIDs(array $phids) {
23
$this->networkPHIDs = $phids;
24
return $this;
25
}
26
27
public function withDevicePHIDs(array $phids) {
28
$this->devicePHIDs = $phids;
29
return $this;
30
}
31
32
public function withAddresses(array $addresses) {
33
$this->addresses = $addresses;
34
return $this;
35
}
36
37
public function newResultObject() {
38
return new AlmanacInterface();
39
}
40
41
protected function willFilterPage(array $interfaces) {
42
$network_phids = mpull($interfaces, 'getNetworkPHID');
43
$device_phids = mpull($interfaces, 'getDevicePHID');
44
45
$networks = id(new AlmanacNetworkQuery())
46
->setParentQuery($this)
47
->setViewer($this->getViewer())
48
->withPHIDs($network_phids)
49
->needProperties($this->getNeedProperties())
50
->execute();
51
$networks = mpull($networks, null, 'getPHID');
52
53
$devices = id(new AlmanacDeviceQuery())
54
->setParentQuery($this)
55
->setViewer($this->getViewer())
56
->withPHIDs($device_phids)
57
->needProperties($this->getNeedProperties())
58
->execute();
59
$devices = mpull($devices, null, 'getPHID');
60
61
foreach ($interfaces as $key => $interface) {
62
$network = idx($networks, $interface->getNetworkPHID());
63
$device = idx($devices, $interface->getDevicePHID());
64
if (!$network || !$device) {
65
$this->didRejectResult($interface);
66
unset($interfaces[$key]);
67
continue;
68
}
69
70
$interface->attachNetwork($network);
71
$interface->attachDevice($device);
72
}
73
74
return $interfaces;
75
}
76
77
protected function buildSelectClauseParts(AphrontDatabaseConnection $conn) {
78
$select = parent::buildSelectClauseParts($conn);
79
80
if ($this->shouldJoinDeviceTable()) {
81
$select[] = qsprintf($conn, 'device.name');
82
}
83
84
return $select;
85
}
86
87
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
88
$where = parent::buildWhereClauseParts($conn);
89
90
if ($this->ids !== null) {
91
$where[] = qsprintf(
92
$conn,
93
'interface.id IN (%Ld)',
94
$this->ids);
95
}
96
97
if ($this->phids !== null) {
98
$where[] = qsprintf(
99
$conn,
100
'interface.phid IN (%Ls)',
101
$this->phids);
102
}
103
104
if ($this->networkPHIDs !== null) {
105
$where[] = qsprintf(
106
$conn,
107
'interface.networkPHID IN (%Ls)',
108
$this->networkPHIDs);
109
}
110
111
if ($this->devicePHIDs !== null) {
112
$where[] = qsprintf(
113
$conn,
114
'interface.devicePHID IN (%Ls)',
115
$this->devicePHIDs);
116
}
117
118
if ($this->addresses !== null) {
119
$parts = array();
120
foreach ($this->addresses as $address) {
121
$parts[] = qsprintf(
122
$conn,
123
'(interface.networkPHID = %s '.
124
'AND interface.address = %s '.
125
'AND interface.port = %d)',
126
$address->getNetworkPHID(),
127
$address->getAddress(),
128
$address->getPort());
129
}
130
$where[] = qsprintf($conn, '%LO', $parts);
131
}
132
133
return $where;
134
}
135
136
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
137
$joins = parent::buildJoinClauseParts($conn);
138
139
if ($this->shouldJoinDeviceTable()) {
140
$joins[] = qsprintf(
141
$conn,
142
'JOIN %T device ON device.phid = interface.devicePHID',
143
id(new AlmanacDevice())->getTableName());
144
}
145
146
return $joins;
147
}
148
149
protected function shouldGroupQueryResultRows() {
150
if ($this->shouldJoinDeviceTable()) {
151
return true;
152
}
153
154
return parent::shouldGroupQueryResultRows();
155
}
156
157
private function shouldJoinDeviceTable() {
158
$vector = $this->getOrderVector();
159
160
if ($vector->containsKey('name')) {
161
return true;
162
}
163
164
return false;
165
}
166
167
protected function getPrimaryTableAlias() {
168
return 'interface';
169
}
170
171
public function getQueryApplicationClass() {
172
return 'PhabricatorAlmanacApplication';
173
}
174
175
public function getBuiltinOrders() {
176
return array(
177
'name' => array(
178
'vector' => array('name', 'id'),
179
'name' => pht('Device Name'),
180
),
181
) + parent::getBuiltinOrders();
182
}
183
184
public function getOrderableColumns() {
185
return parent::getOrderableColumns() + array(
186
'name' => array(
187
'table' => 'device',
188
'column' => 'name',
189
'type' => 'string',
190
'reverse' => true,
191
),
192
);
193
}
194
195
protected function newPagingMapFromCursorObject(
196
PhabricatorQueryCursor $cursor,
197
array $keys) {
198
199
$interface = $cursor->getObject();
200
201
return array(
202
'id' => (int)$interface->getID(),
203
'name' => $cursor->getRawRowProperty('device.name'),
204
);
205
}
206
207
}
208
209