@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
1<?php
2
3/**
4 * @extends DrydockQuery<DrydockResource>
5 */
6final class DrydockResourceQuery extends DrydockQuery {
7
8 private $ids;
9 private $phids;
10 private $statuses;
11 private $types;
12 private $blueprintPHIDs;
13 private $datasourceQuery;
14 private $needUnconsumedCommands;
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 withTypes(array $types) {
27 $this->types = $types;
28 return $this;
29 }
30
31 public function withStatuses(array $statuses) {
32 $this->statuses = $statuses;
33 return $this;
34 }
35
36 public function withBlueprintPHIDs(array $blueprint_phids) {
37 $this->blueprintPHIDs = $blueprint_phids;
38 return $this;
39 }
40
41 public function withDatasourceQuery($query) {
42 $this->datasourceQuery = $query;
43 return $this;
44 }
45
46 public function needUnconsumedCommands($need) {
47 $this->needUnconsumedCommands = $need;
48 return $this;
49 }
50
51 public function newResultObject() {
52 return new DrydockResource();
53 }
54
55 protected function willFilterPage(array $resources) {
56 $blueprint_phids = mpull($resources, 'getBlueprintPHID');
57
58 $blueprints = id(new DrydockBlueprintQuery())
59 ->setViewer($this->getViewer())
60 ->withPHIDs($blueprint_phids)
61 ->execute();
62 $blueprints = mpull($blueprints, null, 'getPHID');
63
64 foreach ($resources as $key => $resource) {
65 $blueprint = idx($blueprints, $resource->getBlueprintPHID());
66 if (!$blueprint) {
67 $this->didRejectResult($resource);
68 unset($resources[$key]);
69 continue;
70 }
71 $resource->attachBlueprint($blueprint);
72 }
73
74 return $resources;
75 }
76
77 protected function didFilterPage(array $resources) {
78 if ($this->needUnconsumedCommands) {
79 $commands = id(new DrydockCommandQuery())
80 ->setViewer($this->getViewer())
81 ->setParentQuery($this)
82 ->withTargetPHIDs(mpull($resources, 'getPHID'))
83 ->withConsumed(false)
84 ->execute();
85 $commands = mgroup($commands, 'getTargetPHID');
86
87 foreach ($resources as $resource) {
88 $list = idx($commands, $resource->getPHID(), array());
89 $resource->attachUnconsumedCommands($list);
90 }
91 }
92
93 return $resources;
94 }
95
96 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
97 $where = parent::buildWhereClauseParts($conn);
98
99 if ($this->ids !== null) {
100 $where[] = qsprintf(
101 $conn,
102 'resource.id IN (%Ld)',
103 $this->ids);
104 }
105
106 if ($this->phids !== null) {
107 $where[] = qsprintf(
108 $conn,
109 'resource.phid IN (%Ls)',
110 $this->phids);
111 }
112
113 if ($this->types !== null) {
114 $where[] = qsprintf(
115 $conn,
116 'resource.type IN (%Ls)',
117 $this->types);
118 }
119
120 if ($this->statuses !== null) {
121 $where[] = qsprintf(
122 $conn,
123 'resource.status IN (%Ls)',
124 $this->statuses);
125 }
126
127 if ($this->blueprintPHIDs !== null) {
128 $where[] = qsprintf(
129 $conn,
130 'resource.blueprintPHID IN (%Ls)',
131 $this->blueprintPHIDs);
132 }
133
134 if ($this->datasourceQuery !== null) {
135 $where[] = qsprintf(
136 $conn,
137 'resource.name LIKE %>',
138 $this->datasourceQuery);
139 }
140
141 return $where;
142 }
143
144 protected function getPrimaryTableAlias() {
145 return 'resource';
146 }
147
148}