Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / almanac / query / AlmanacInterfaceQuery.php
blobdbbc0cd53e67733ae2ab3922f42e4218e52ee430
1 <?php
3 final class AlmanacInterfaceQuery
4 extends AlmanacQuery {
6 private $ids;
7 private $phids;
8 private $networkPHIDs;
9 private $devicePHIDs;
10 private $addresses;
12 public function withIDs(array $ids) {
13 $this->ids = $ids;
14 return $this;
17 public function withPHIDs(array $phids) {
18 $this->phids = $phids;
19 return $this;
22 public function withNetworkPHIDs(array $phids) {
23 $this->networkPHIDs = $phids;
24 return $this;
27 public function withDevicePHIDs(array $phids) {
28 $this->devicePHIDs = $phids;
29 return $this;
32 public function withAddresses(array $addresses) {
33 $this->addresses = $addresses;
34 return $this;
37 public function newResultObject() {
38 return new AlmanacInterface();
41 protected function willFilterPage(array $interfaces) {
42 $network_phids = mpull($interfaces, 'getNetworkPHID');
43 $device_phids = mpull($interfaces, 'getDevicePHID');
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');
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');
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;
70 $interface->attachNetwork($network);
71 $interface->attachDevice($device);
74 return $interfaces;
77 protected function buildSelectClauseParts(AphrontDatabaseConnection $conn) {
78 $select = parent::buildSelectClauseParts($conn);
80 if ($this->shouldJoinDeviceTable()) {
81 $select[] = qsprintf($conn, 'device.name');
84 return $select;
87 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
88 $where = parent::buildWhereClauseParts($conn);
90 if ($this->ids !== null) {
91 $where[] = qsprintf(
92 $conn,
93 'interface.id IN (%Ld)',
94 $this->ids);
97 if ($this->phids !== null) {
98 $where[] = qsprintf(
99 $conn,
100 'interface.phid IN (%Ls)',
101 $this->phids);
104 if ($this->networkPHIDs !== null) {
105 $where[] = qsprintf(
106 $conn,
107 'interface.networkPHID IN (%Ls)',
108 $this->networkPHIDs);
111 if ($this->devicePHIDs !== null) {
112 $where[] = qsprintf(
113 $conn,
114 'interface.devicePHID IN (%Ls)',
115 $this->devicePHIDs);
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());
130 $where[] = qsprintf($conn, '%LO', $parts);
133 return $where;
136 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
137 $joins = parent::buildJoinClauseParts($conn);
139 if ($this->shouldJoinDeviceTable()) {
140 $joins[] = qsprintf(
141 $conn,
142 'JOIN %T device ON device.phid = interface.devicePHID',
143 id(new AlmanacDevice())->getTableName());
146 return $joins;
149 protected function shouldGroupQueryResultRows() {
150 if ($this->shouldJoinDeviceTable()) {
151 return true;
154 return parent::shouldGroupQueryResultRows();
157 private function shouldJoinDeviceTable() {
158 $vector = $this->getOrderVector();
160 if ($vector->containsKey('name')) {
161 return true;
164 return false;
167 protected function getPrimaryTableAlias() {
168 return 'interface';
171 public function getQueryApplicationClass() {
172 return 'PhabricatorAlmanacApplication';
175 public function getBuiltinOrders() {
176 return array(
177 'name' => array(
178 'vector' => array('name', 'id'),
179 'name' => pht('Device Name'),
181 ) + parent::getBuiltinOrders();
184 public function getOrderableColumns() {
185 return parent::getOrderableColumns() + array(
186 'name' => array(
187 'table' => 'device',
188 'column' => 'name',
189 'type' => 'string',
190 'reverse' => true,
195 protected function newPagingMapFromCursorObject(
196 PhabricatorQueryCursor $cursor,
197 array $keys) {
199 $interface = $cursor->getObject();
201 return array(
202 'id' => (int)$interface->getID(),
203 'name' => $cursor->getRawRowProperty('device.name'),