Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / differential / query / DifferentialDiffQuery.php
blob04019df1e08ef4078e0af6560a6a7758afbad19e
1 <?php
3 final class DifferentialDiffQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $ids;
7 private $phids;
8 private $revisionIDs;
9 private $revisionPHIDs;
10 private $commitPHIDs;
11 private $hasRevision;
13 private $needChangesets = false;
14 private $needProperties;
16 public function withIDs(array $ids) {
17 $this->ids = $ids;
18 return $this;
21 public function withPHIDs(array $phids) {
22 $this->phids = $phids;
23 return $this;
26 public function withRevisionIDs(array $revision_ids) {
27 $this->revisionIDs = $revision_ids;
28 return $this;
31 public function withRevisionPHIDs(array $revision_phids) {
32 $this->revisionPHIDs = $revision_phids;
33 return $this;
36 public function withCommitPHIDs(array $phids) {
37 $this->commitPHIDs = $phids;
38 return $this;
41 public function withHasRevision($has_revision) {
42 $this->hasRevision = $has_revision;
43 return $this;
46 public function needChangesets($bool) {
47 $this->needChangesets = $bool;
48 return $this;
51 public function needProperties($need_properties) {
52 $this->needProperties = $need_properties;
53 return $this;
56 public function newResultObject() {
57 return new DifferentialDiff();
60 protected function willFilterPage(array $diffs) {
61 $revision_ids = array_filter(mpull($diffs, 'getRevisionID'));
63 $revisions = array();
64 if ($revision_ids) {
65 $revisions = id(new DifferentialRevisionQuery())
66 ->setViewer($this->getViewer())
67 ->withIDs($revision_ids)
68 ->execute();
71 foreach ($diffs as $key => $diff) {
72 if (!$diff->getRevisionID()) {
73 continue;
76 $revision = idx($revisions, $diff->getRevisionID());
77 if ($revision) {
78 $diff->attachRevision($revision);
79 continue;
82 unset($diffs[$key]);
86 if ($diffs && $this->needChangesets) {
87 $diffs = $this->loadChangesets($diffs);
90 return $diffs;
93 protected function didFilterPage(array $diffs) {
94 if ($this->needProperties) {
95 $properties = id(new DifferentialDiffProperty())->loadAllWhere(
96 'diffID IN (%Ld)',
97 mpull($diffs, 'getID'));
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);
107 return $diffs;
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();
119 return $diffs;
122 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
123 $where = parent::buildWhereClauseParts($conn);
125 if ($this->ids !== null) {
126 $where[] = qsprintf(
127 $conn,
128 'id IN (%Ld)',
129 $this->ids);
132 if ($this->phids !== null) {
133 $where[] = qsprintf(
134 $conn,
135 'phid IN (%Ls)',
136 $this->phids);
139 if ($this->revisionIDs !== null) {
140 $where[] = qsprintf(
141 $conn,
142 'revisionID IN (%Ld)',
143 $this->revisionIDs);
146 if ($this->commitPHIDs !== null) {
147 $where[] = qsprintf(
148 $conn,
149 'commitPHID IN (%Ls)',
150 $this->commitPHIDs);
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');
165 if ($this->revisionPHIDs !== null) {
166 $viewer = $this->getViewer();
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();
178 $where[] = qsprintf(
179 $conn,
180 'revisionID IN (%Ls)',
181 $revision_ids);
184 return $where;
187 public function getQueryApplicationClass() {
188 return 'PhabricatorDifferentialApplication';