Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / applications / almanac / query / AlmanacServiceQuery.php
blob4e374ec90c4a175713fa0ccd419863daa353bcae
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 loadPage() {
73 return $this->loadStandardPage($this->newResultObject());
76 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
77 $joins = parent::buildJoinClauseParts($conn);
79 if ($this->shouldJoinBindingTable()) {
80 $joins[] = qsprintf(
81 $conn,
82 'JOIN %T binding ON service.phid = binding.servicePHID',
83 id(new AlmanacBinding())->getTableName());
86 return $joins;
89 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
90 $where = parent::buildWhereClauseParts($conn);
92 if ($this->ids !== null) {
93 $where[] = qsprintf(
94 $conn,
95 'service.id IN (%Ld)',
96 $this->ids);
99 if ($this->phids !== null) {
100 $where[] = qsprintf(
101 $conn,
102 'service.phid IN (%Ls)',
103 $this->phids);
106 if ($this->names !== null) {
107 $hashes = array();
108 foreach ($this->names as $name) {
109 $hashes[] = PhabricatorHash::digestForIndex($name);
112 $where[] = qsprintf(
113 $conn,
114 'service.nameIndex IN (%Ls)',
115 $hashes);
118 if ($this->serviceTypes !== null) {
119 $where[] = qsprintf(
120 $conn,
121 'service.serviceType IN (%Ls)',
122 $this->serviceTypes);
125 if ($this->devicePHIDs !== null) {
126 $where[] = qsprintf(
127 $conn,
128 'binding.devicePHID IN (%Ls)',
129 $this->devicePHIDs);
132 if ($this->namePrefix !== null) {
133 $where[] = qsprintf(
134 $conn,
135 'service.name LIKE %>',
136 $this->namePrefix);
139 if ($this->nameSuffix !== null) {
140 $where[] = qsprintf(
141 $conn,
142 'service.name LIKE %<',
143 $this->nameSuffix);
146 return $where;
149 protected function willFilterPage(array $services) {
150 $service_map = AlmanacServiceType::getAllServiceTypes();
152 foreach ($services as $key => $service) {
153 $implementation = idx($service_map, $service->getServiceType());
155 if (!$implementation) {
156 $this->didRejectResult($service);
157 unset($services[$key]);
158 continue;
161 $implementation = clone $implementation;
162 $service->attachServiceImplementation($implementation);
165 return $services;
168 protected function didFilterPage(array $services) {
169 $need_all = $this->needBindings;
170 $need_active = $this->needActiveBindings;
172 $need_any = ($need_all || $need_active);
173 $only_active = ($need_active && !$need_all);
175 if ($need_any) {
176 $service_phids = mpull($services, 'getPHID');
178 $bindings_query = id(new AlmanacBindingQuery())
179 ->setViewer($this->getViewer())
180 ->withServicePHIDs($service_phids)
181 ->needProperties($this->getNeedProperties());
183 if ($only_active) {
184 $bindings_query->withIsActive(true);
187 $bindings = $bindings_query->execute();
188 $bindings = mgroup($bindings, 'getServicePHID');
190 foreach ($services as $service) {
191 $service_bindings = idx($bindings, $service->getPHID(), array());
193 if ($only_active) {
194 $service->attachActiveBindings($service_bindings);
195 } else {
196 $service->attachBindings($service_bindings);
201 return parent::didFilterPage($services);
204 private function shouldJoinBindingTable() {
205 return ($this->devicePHIDs !== null);
208 protected function shouldGroupQueryResultRows() {
209 if ($this->shouldJoinBindingTable()) {
210 return true;
213 return parent::shouldGroupQueryResultRows();
216 protected function getPrimaryTableAlias() {
217 return 'service';
220 public function getOrderableColumns() {
221 return parent::getOrderableColumns() + array(
222 'name' => array(
223 'table' => $this->getPrimaryTableAlias(),
224 'column' => 'name',
225 'type' => 'string',
226 'unique' => true,
227 'reverse' => true,
232 protected function newPagingMapFromPartialObject($object) {
233 return array(
234 'id' => (int)$object->getID(),
235 'name' => $object->getName(),
239 public function getBuiltinOrders() {
240 return array(
241 'name' => array(
242 'vector' => array('name'),
243 'name' => pht('Service Name'),
245 ) + parent::getBuiltinOrders();