Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/differential/query/DifferentialDiffQuery.php
12256 views
1
<?php
2
3
final class DifferentialDiffQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
private $ids;
7
private $phids;
8
private $revisionIDs;
9
private $revisionPHIDs;
10
private $commitPHIDs;
11
private $hasRevision;
12
13
private $needChangesets = false;
14
private $needProperties;
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 withRevisionIDs(array $revision_ids) {
27
$this->revisionIDs = $revision_ids;
28
return $this;
29
}
30
31
public function withRevisionPHIDs(array $revision_phids) {
32
$this->revisionPHIDs = $revision_phids;
33
return $this;
34
}
35
36
public function withCommitPHIDs(array $phids) {
37
$this->commitPHIDs = $phids;
38
return $this;
39
}
40
41
public function withHasRevision($has_revision) {
42
$this->hasRevision = $has_revision;
43
return $this;
44
}
45
46
public function needChangesets($bool) {
47
$this->needChangesets = $bool;
48
return $this;
49
}
50
51
public function needProperties($need_properties) {
52
$this->needProperties = $need_properties;
53
return $this;
54
}
55
56
public function newResultObject() {
57
return new DifferentialDiff();
58
}
59
60
protected function willFilterPage(array $diffs) {
61
$revision_ids = array_filter(mpull($diffs, 'getRevisionID'));
62
63
$revisions = array();
64
if ($revision_ids) {
65
$revisions = id(new DifferentialRevisionQuery())
66
->setViewer($this->getViewer())
67
->withIDs($revision_ids)
68
->execute();
69
}
70
71
foreach ($diffs as $key => $diff) {
72
if (!$diff->getRevisionID()) {
73
continue;
74
}
75
76
$revision = idx($revisions, $diff->getRevisionID());
77
if ($revision) {
78
$diff->attachRevision($revision);
79
continue;
80
}
81
82
unset($diffs[$key]);
83
}
84
85
86
if ($diffs && $this->needChangesets) {
87
$diffs = $this->loadChangesets($diffs);
88
}
89
90
return $diffs;
91
}
92
93
protected function didFilterPage(array $diffs) {
94
if ($this->needProperties) {
95
$properties = id(new DifferentialDiffProperty())->loadAllWhere(
96
'diffID IN (%Ld)',
97
mpull($diffs, 'getID'));
98
99
$properties = mgroup($properties, 'getDiffID');
100
foreach ($diffs as $diff) {
101
$map = idx($properties, $diff->getID(), array());
102
$map = mpull($map, 'getData', 'getName');
103
$diff->attachDiffProperties($map);
104
}
105
}
106
107
return $diffs;
108
}
109
110
private function loadChangesets(array $diffs) {
111
id(new DifferentialChangesetQuery())
112
->setViewer($this->getViewer())
113
->setParentQuery($this)
114
->withDiffs($diffs)
115
->needAttachToDiffs(true)
116
->needHunks(true)
117
->execute();
118
119
return $diffs;
120
}
121
122
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
123
$where = parent::buildWhereClauseParts($conn);
124
125
if ($this->ids !== null) {
126
$where[] = qsprintf(
127
$conn,
128
'id IN (%Ld)',
129
$this->ids);
130
}
131
132
if ($this->phids !== null) {
133
$where[] = qsprintf(
134
$conn,
135
'phid IN (%Ls)',
136
$this->phids);
137
}
138
139
if ($this->revisionIDs !== null) {
140
$where[] = qsprintf(
141
$conn,
142
'revisionID IN (%Ld)',
143
$this->revisionIDs);
144
}
145
146
if ($this->commitPHIDs !== null) {
147
$where[] = qsprintf(
148
$conn,
149
'commitPHID IN (%Ls)',
150
$this->commitPHIDs);
151
}
152
153
if ($this->hasRevision !== null) {
154
if ($this->hasRevision) {
155
$where[] = qsprintf(
156
$conn,
157
'revisionID IS NOT NULL');
158
} else {
159
$where[] = qsprintf(
160
$conn,
161
'revisionID IS NULL');
162
}
163
}
164
165
if ($this->revisionPHIDs !== null) {
166
$viewer = $this->getViewer();
167
168
$revisions = id(new DifferentialRevisionQuery())
169
->setViewer($viewer)
170
->setParentQuery($this)
171
->withPHIDs($this->revisionPHIDs)
172
->execute();
173
$revision_ids = mpull($revisions, 'getID');
174
if (!$revision_ids) {
175
throw new PhabricatorEmptyQueryException();
176
}
177
178
$where[] = qsprintf(
179
$conn,
180
'revisionID IN (%Ls)',
181
$revision_ids);
182
}
183
184
return $where;
185
}
186
187
public function getQueryApplicationClass() {
188
return 'PhabricatorDifferentialApplication';
189
}
190
191
}
192
193