Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / applications / almanac / query / AlmanacInterfaceQuery.php
blob5738108ffc81937402c2579689738ac368a36c2b
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 loadPage() {
42 return $this->loadStandardPage($this->newResultObject());
45 protected function willFilterPage(array $interfaces) {
46 $network_phids = mpull($interfaces, 'getNetworkPHID');
47 $device_phids = mpull($interfaces, 'getDevicePHID');
49 $networks = id(new AlmanacNetworkQuery())
50 ->setParentQuery($this)
51 ->setViewer($this->getViewer())
52 ->withPHIDs($network_phids)
53 ->needProperties($this->getNeedProperties())
54 ->execute();
55 $networks = mpull($networks, null, 'getPHID');
57 $devices = id(new AlmanacDeviceQuery())
58 ->setParentQuery($this)
59 ->setViewer($this->getViewer())
60 ->withPHIDs($device_phids)
61 ->needProperties($this->getNeedProperties())
62 ->execute();
63 $devices = mpull($devices, null, 'getPHID');
65 foreach ($interfaces as $key => $interface) {
66 $network = idx($networks, $interface->getNetworkPHID());
67 $device = idx($devices, $interface->getDevicePHID());
68 if (!$network || !$device) {
69 $this->didRejectResult($interface);
70 unset($interfaces[$key]);
71 continue;
74 $interface->attachNetwork($network);
75 $interface->attachDevice($device);
78 return $interfaces;
81 protected function buildSelectClauseParts(AphrontDatabaseConnection $conn) {
82 $select = parent::buildSelectClauseParts($conn);
84 if ($this->shouldJoinDeviceTable()) {
85 $select[] = qsprintf($conn, 'device.name');
88 return $select;
91 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
92 $where = parent::buildWhereClauseParts($conn);
94 if ($this->ids !== null) {
95 $where[] = qsprintf(
96 $conn,
97 'interface.id IN (%Ld)',
98 $this->ids);
101 if ($this->phids !== null) {
102 $where[] = qsprintf(
103 $conn,
104 'interface.phid IN (%Ls)',
105 $this->phids);
108 if ($this->networkPHIDs !== null) {
109 $where[] = qsprintf(
110 $conn,
111 'interface.networkPHID IN (%Ls)',
112 $this->networkPHIDs);
115 if ($this->devicePHIDs !== null) {
116 $where[] = qsprintf(
117 $conn,
118 'interface.devicePHID IN (%Ls)',
119 $this->devicePHIDs);
122 if ($this->addresses !== null) {
123 $parts = array();
124 foreach ($this->addresses as $address) {
125 $parts[] = qsprintf(
126 $conn,
127 '(interface.networkPHID = %s '.
128 'AND interface.address = %s '.
129 'AND interface.port = %d)',
130 $address->getNetworkPHID(),
131 $address->getAddress(),
132 $address->getPort());
134 $where[] = qsprintf($conn, '%LO', $parts);
137 return $where;
140 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
141 $joins = parent::buildJoinClauseParts($conn);
143 if ($this->shouldJoinDeviceTable()) {
144 $joins[] = qsprintf(
145 $conn,
146 'JOIN %T device ON device.phid = interface.devicePHID',
147 id(new AlmanacDevice())->getTableName());
150 return $joins;
153 protected function shouldGroupQueryResultRows() {
154 if ($this->shouldJoinDeviceTable()) {
155 return true;
158 return parent::shouldGroupQueryResultRows();
161 private function shouldJoinDeviceTable() {
162 $vector = $this->getOrderVector();
164 if ($vector->containsKey('name')) {
165 return true;
168 return false;
171 protected function getPrimaryTableAlias() {
172 return 'interface';
175 public function getQueryApplicationClass() {
176 return 'PhabricatorAlmanacApplication';
179 public function getBuiltinOrders() {
180 return array(
181 'name' => array(
182 'vector' => array('name', 'id'),
183 'name' => pht('Device Name'),
185 ) + parent::getBuiltinOrders();
188 public function getOrderableColumns() {
189 return parent::getOrderableColumns() + array(
190 'name' => array(
191 'table' => 'device',
192 'column' => 'name',
193 'type' => 'string',
194 'reverse' => true,
199 protected function newPagingMapFromCursorObject(
200 PhabricatorQueryCursor $cursor,
201 array $keys) {
203 $interface = $cursor->getObject();
205 return array(
206 'id' => (int)$interface->getID(),
207 'name' => $cursor->getRawRowProperty('device.name'),