Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / almanac / query / AlmanacServiceQuery.php
blobeaca9d4a7670dee2e608ed13cd1dd6f0dd5aea9e
1 <?php
3 final class AlmanacServiceQuery
4 extends AlmanacQuery {
6 private $ids;
7 private $phids;
8 private $names;
9 private $serviceTypes;
10 private $devicePHIDs;
11 private $namePrefix;
12 private $nameSuffix;
14 private $needBindings;
15 private $needActiveBindings;
17 public function withIDs(array $ids) {
18 $this->ids = $ids;
19 return $this;
22 public function withPHIDs(array $phids) {
23 $this->phids = $phids;
24 return $this;
27 public function withNames(array $names) {
28 $this->names = $names;
29 return $this;
32 public function withServiceTypes(array $types) {
33 $this->serviceTypes = $types;
34 return $this;
37 public function withDevicePHIDs(array $phids) {
38 $this->devicePHIDs = $phids;
39 return $this;
42 public function withNamePrefix($prefix) {
43 $this->namePrefix = $prefix;
44 return $this;
47 public function withNameSuffix($suffix) {
48 $this->nameSuffix = $suffix;
49 return $this;
52 public function withNameNgrams($ngrams) {
53 return $this->withNgramsConstraint(
54 new AlmanacServiceNameNgrams(),
55 $ngrams);
58 public function needBindings($need_bindings) {
59 $this->needBindings = $need_bindings;
60 return $this;
63 public function needActiveBindings($need_active) {
64 $this->needActiveBindings = $need_active;
65 return $this;
68 public function newResultObject() {
69 return new AlmanacService();
72 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
73 $joins = parent::buildJoinClauseParts($conn);
75 if ($this->shouldJoinBindingTable()) {
76 $joins[] = qsprintf(
77 $conn,
78 'JOIN %T binding ON service.phid = binding.servicePHID',
79 id(new AlmanacBinding())->getTableName());
82 return $joins;
85 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
86 $where = parent::buildWhereClauseParts($conn);
88 if ($this->ids !== null) {
89 $where[] = qsprintf(
90 $conn,
91 'service.id IN (%Ld)',
92 $this->ids);
95 if ($this->phids !== null) {
96 $where[] = qsprintf(
97 $conn,
98 'service.phid IN (%Ls)',
99 $this->phids);
102 if ($this->names !== null) {
103 $hashes = array();
104 foreach ($this->names as $name) {
105 $hashes[] = PhabricatorHash::digestForIndex($name);
108 $where[] = qsprintf(
109 $conn,
110 'service.nameIndex IN (%Ls)',
111 $hashes);
114 if ($this->serviceTypes !== null) {
115 $where[] = qsprintf(
116 $conn,
117 'service.serviceType IN (%Ls)',
118 $this->serviceTypes);
121 if ($this->devicePHIDs !== null) {
122 $where[] = qsprintf(
123 $conn,
124 'binding.devicePHID IN (%Ls)',
125 $this->devicePHIDs);
128 if ($this->namePrefix !== null) {
129 $where[] = qsprintf(
130 $conn,
131 'service.name LIKE %>',
132 $this->namePrefix);
135 if ($this->nameSuffix !== null) {
136 $where[] = qsprintf(
137 $conn,
138 'service.name LIKE %<',
139 $this->nameSuffix);
142 return $where;
145 protected function willFilterPage(array $services) {
146 $service_map = AlmanacServiceType::getAllServiceTypes();
148 foreach ($services as $key => $service) {
149 $implementation = idx($service_map, $service->getServiceType());
151 if (!$implementation) {
152 $this->didRejectResult($service);
153 unset($services[$key]);
154 continue;
157 $implementation = clone $implementation;
158 $service->attachServiceImplementation($implementation);
161 return $services;
164 protected function didFilterPage(array $services) {
165 $need_all = $this->needBindings;
166 $need_active = $this->needActiveBindings;
168 $need_any = ($need_all || $need_active);
169 $only_active = ($need_active && !$need_all);
171 if ($need_any) {
172 $service_phids = mpull($services, 'getPHID');
174 $bindings_query = id(new AlmanacBindingQuery())
175 ->setViewer($this->getViewer())
176 ->withServicePHIDs($service_phids)
177 ->needProperties($this->getNeedProperties());
179 if ($only_active) {
180 $bindings_query->withIsActive(true);
183 $bindings = $bindings_query->execute();
184 $bindings = mgroup($bindings, 'getServicePHID');
186 foreach ($services as $service) {
187 $service_bindings = idx($bindings, $service->getPHID(), array());
189 if ($only_active) {
190 $service->attachActiveBindings($service_bindings);
191 } else {
192 $service->attachBindings($service_bindings);
197 return parent::didFilterPage($services);
200 private function shouldJoinBindingTable() {
201 return ($this->devicePHIDs !== null);
204 protected function shouldGroupQueryResultRows() {
205 if ($this->shouldJoinBindingTable()) {
206 return true;
209 return parent::shouldGroupQueryResultRows();
212 protected function getPrimaryTableAlias() {
213 return 'service';
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,
228 protected function newPagingMapFromPartialObject($object) {
229 return array(
230 'id' => (int)$object->getID(),
231 'name' => $object->getName(),
235 public function getBuiltinOrders() {
236 return array(
237 'name' => array(
238 'vector' => array('name'),
239 'name' => pht('Service Name'),
241 ) + parent::getBuiltinOrders();