@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at recaptime-dev/main 178 lines 4.4 kB view raw
1<?php 2 3/** 4 * @extends AlmanacQuery<AlmanacBinding> 5 */ 6final class AlmanacBindingQuery 7 extends AlmanacQuery { 8 9 private $ids; 10 private $phids; 11 private $servicePHIDs; 12 private $devicePHIDs; 13 private $interfacePHIDs; 14 private $isActive; 15 16 public function withIDs(array $ids) { 17 $this->ids = $ids; 18 return $this; 19 } 20 21 public function withPHIDs(array $phids) { 22 $this->phids = $phids; 23 return $this; 24 } 25 26 public function withServicePHIDs(array $phids) { 27 $this->servicePHIDs = $phids; 28 return $this; 29 } 30 31 public function withDevicePHIDs(array $phids) { 32 $this->devicePHIDs = $phids; 33 return $this; 34 } 35 36 public function withInterfacePHIDs(array $phids) { 37 $this->interfacePHIDs = $phids; 38 return $this; 39 } 40 41 public function withIsActive($active) { 42 $this->isActive = $active; 43 return $this; 44 } 45 46 public function newResultObject() { 47 return new AlmanacBinding(); 48 } 49 50 protected function willFilterPage(array $bindings) { 51 $service_phids = mpull($bindings, 'getServicePHID'); 52 $device_phids = mpull($bindings, 'getDevicePHID'); 53 $interface_phids = mpull($bindings, 'getInterfacePHID'); 54 55 $services = id(new AlmanacServiceQuery()) 56 ->setParentQuery($this) 57 ->setViewer($this->getViewer()) 58 ->withPHIDs($service_phids) 59 ->needProperties($this->getNeedProperties()) 60 ->execute(); 61 $services = mpull($services, null, 'getPHID'); 62 63 $devices = id(new AlmanacDeviceQuery()) 64 ->setParentQuery($this) 65 ->setViewer($this->getViewer()) 66 ->withPHIDs($device_phids) 67 ->needProperties($this->getNeedProperties()) 68 ->execute(); 69 $devices = mpull($devices, null, 'getPHID'); 70 71 $interfaces = id(new AlmanacInterfaceQuery()) 72 ->setParentQuery($this) 73 ->setViewer($this->getViewer()) 74 ->withPHIDs($interface_phids) 75 ->needProperties($this->getNeedProperties()) 76 ->execute(); 77 $interfaces = mpull($interfaces, null, 'getPHID'); 78 79 foreach ($bindings as $key => $binding) { 80 $service = idx($services, $binding->getServicePHID()); 81 $device = idx($devices, $binding->getDevicePHID()); 82 $interface = idx($interfaces, $binding->getInterfacePHID()); 83 if (!$service || !$device || !$interface) { 84 $this->didRejectResult($binding); 85 unset($bindings[$key]); 86 continue; 87 } 88 89 $binding->attachService($service); 90 $binding->attachDevice($device); 91 $binding->attachInterface($interface); 92 } 93 94 return $bindings; 95 } 96 97 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { 98 $where = parent::buildWhereClauseParts($conn); 99 100 if ($this->ids !== null) { 101 $where[] = qsprintf( 102 $conn, 103 'binding.id IN (%Ld)', 104 $this->ids); 105 } 106 107 if ($this->phids !== null) { 108 $where[] = qsprintf( 109 $conn, 110 'binding.phid IN (%Ls)', 111 $this->phids); 112 } 113 114 if ($this->servicePHIDs !== null) { 115 $where[] = qsprintf( 116 $conn, 117 'binding.servicePHID IN (%Ls)', 118 $this->servicePHIDs); 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->interfacePHIDs !== null) { 129 $where[] = qsprintf( 130 $conn, 131 'binding.interfacePHID IN (%Ls)', 132 $this->interfacePHIDs); 133 } 134 135 if ($this->isActive !== null) { 136 if ($this->isActive) { 137 $where[] = qsprintf( 138 $conn, 139 '(binding.isDisabled = 0) AND (device.status IN (%Ls))', 140 AlmanacDeviceStatus::getActiveStatusList()); 141 } else { 142 $where[] = qsprintf( 143 $conn, 144 '(binding.isDisabled = 1) OR (device.status IN (%Ls))', 145 AlmanacDeviceStatus::getDisabledStatusList()); 146 } 147 } 148 149 return $where; 150 } 151 152 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { 153 $joins = parent::buildJoinClauseParts($conn); 154 155 if ($this->shouldJoinDeviceTable()) { 156 $device_table = new AlmanacDevice(); 157 $joins[] = qsprintf( 158 $conn, 159 'JOIN %R device ON binding.devicePHID = device.phid', 160 $device_table); 161 } 162 163 return $joins; 164 } 165 166 private function shouldJoinDeviceTable() { 167 if ($this->isActive !== null) { 168 return true; 169 } 170 171 return false; 172 } 173 174 protected function getPrimaryTableAlias() { 175 return 'binding'; 176 } 177 178}