Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/harbormaster/query/HarbormasterBuildTargetQuery.php
12256 views
1
<?php
2
3
final class HarbormasterBuildTargetQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
private $ids;
7
private $phids;
8
private $buildPHIDs;
9
private $buildGenerations;
10
private $dateCreatedMin;
11
private $dateCreatedMax;
12
private $dateStartedMin;
13
private $dateStartedMax;
14
private $dateCompletedMin;
15
private $dateCompletedMax;
16
private $statuses;
17
18
private $needBuildSteps;
19
20
public function withIDs(array $ids) {
21
$this->ids = $ids;
22
return $this;
23
}
24
25
public function withPHIDs(array $phids) {
26
$this->phids = $phids;
27
return $this;
28
}
29
30
public function withBuildPHIDs(array $build_phids) {
31
$this->buildPHIDs = $build_phids;
32
return $this;
33
}
34
35
public function withBuildGenerations(array $build_generations) {
36
$this->buildGenerations = $build_generations;
37
return $this;
38
}
39
40
public function withDateCreatedBetween($min, $max) {
41
$this->dateCreatedMin = $min;
42
$this->dateCreatedMax = $max;
43
return $this;
44
}
45
46
public function withDateStartedBetween($min, $max) {
47
$this->dateStartedMin = $min;
48
$this->dateStartedMax = $max;
49
return $this;
50
}
51
52
public function withDateCompletedBetween($min, $max) {
53
$this->dateCompletedMin = $min;
54
$this->dateCompletedMax = $max;
55
return $this;
56
}
57
58
public function withTargetStatuses(array $statuses) {
59
$this->statuses = $statuses;
60
return $this;
61
}
62
63
public function needBuildSteps($need_build_steps) {
64
$this->needBuildSteps = $need_build_steps;
65
return $this;
66
}
67
68
public function newResultObject() {
69
return new HarbormasterBuildTarget();
70
}
71
72
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
73
$where = parent::buildWhereClauseParts($conn);
74
75
if ($this->ids !== null) {
76
$where[] = qsprintf(
77
$conn,
78
'id IN (%Ld)',
79
$this->ids);
80
}
81
82
if ($this->phids !== null) {
83
$where[] = qsprintf(
84
$conn,
85
'phid in (%Ls)',
86
$this->phids);
87
}
88
89
if ($this->buildPHIDs !== null) {
90
$where[] = qsprintf(
91
$conn,
92
'buildPHID in (%Ls)',
93
$this->buildPHIDs);
94
}
95
96
if ($this->buildGenerations !== null) {
97
$where[] = qsprintf(
98
$conn,
99
'buildGeneration in (%Ld)',
100
$this->buildGenerations);
101
}
102
103
if ($this->dateCreatedMin !== null) {
104
$where[] = qsprintf(
105
$conn,
106
'dateCreated >= %d',
107
$this->dateCreatedMin);
108
}
109
110
if ($this->dateCreatedMax !== null) {
111
$where[] = qsprintf(
112
$conn,
113
'dateCreated <= %d',
114
$this->dateCreatedMax);
115
}
116
117
if ($this->dateStartedMin !== null) {
118
$where[] = qsprintf(
119
$conn,
120
'dateStarted >= %d',
121
$this->dateStartedMin);
122
}
123
124
if ($this->dateStartedMax !== null) {
125
$where[] = qsprintf(
126
$conn,
127
'dateStarted <= %d',
128
$this->dateStartedMax);
129
}
130
131
if ($this->dateCompletedMin !== null) {
132
$where[] = qsprintf(
133
$conn,
134
'dateCompleted >= %d',
135
$this->dateCompletedMin);
136
}
137
138
if ($this->dateCompletedMax !== null) {
139
$where[] = qsprintf(
140
$conn,
141
'dateCompleted <= %d',
142
$this->dateCompletedMax);
143
}
144
145
if ($this->statuses !== null) {
146
$where[] = qsprintf(
147
$conn,
148
'targetStatus IN (%Ls)',
149
$this->statuses);
150
}
151
152
return $where;
153
}
154
155
protected function didFilterPage(array $page) {
156
if ($this->needBuildSteps) {
157
$step_phids = array();
158
159
foreach ($page as $target) {
160
$step_phids[] = $target->getBuildStepPHID();
161
}
162
163
$steps = id(new HarbormasterBuildStepQuery())
164
->setViewer($this->getViewer())
165
->setParentQuery($this)
166
->withPHIDs($step_phids)
167
->execute();
168
169
$steps = mpull($steps, null, 'getPHID');
170
171
foreach ($page as $target) {
172
$target->attachBuildStep(
173
idx($steps, $target->getBuildStepPHID()));
174
}
175
}
176
177
return $page;
178
}
179
180
protected function willFilterPage(array $page) {
181
$builds = array();
182
183
$build_phids = array_filter(mpull($page, 'getBuildPHID'));
184
if ($build_phids) {
185
$builds = id(new PhabricatorObjectQuery())
186
->setViewer($this->getViewer())
187
->withPHIDs($build_phids)
188
->setParentQuery($this)
189
->execute();
190
$builds = mpull($builds, null, 'getPHID');
191
}
192
193
foreach ($page as $key => $build_target) {
194
$build_phid = $build_target->getBuildPHID();
195
if (empty($builds[$build_phid])) {
196
unset($page[$key]);
197
continue;
198
}
199
$build_target->attachBuild($builds[$build_phid]);
200
}
201
202
return $page;
203
}
204
205
public function getQueryApplicationClass() {
206
return 'PhabricatorHarbormasterApplication';
207
}
208
209
}
210
211