Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / differential / query / DifferentialChangesetQuery.php
blobee29aa1cf88be9f41a5ccb26ee140d360e53dfb9
1 <?php
3 final class DifferentialChangesetQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $ids;
7 private $phids;
8 private $diffPHIDs;
10 private $diffs;
12 private $needAttachToDiffs;
13 private $needHunks;
15 public function withIDs(array $ids) {
16 $this->ids = $ids;
17 return $this;
20 public function withPHIDs(array $phids) {
21 $this->phids = $phids;
22 return $this;
25 public function withDiffs(array $diffs) {
26 assert_instances_of($diffs, 'DifferentialDiff');
27 $this->diffs = $diffs;
28 return $this;
31 public function withDiffPHIDs(array $phids) {
32 $this->diffPHIDs = $phids;
33 return $this;
36 public function needAttachToDiffs($attach) {
37 $this->needAttachToDiffs = $attach;
38 return $this;
41 public function needHunks($need) {
42 $this->needHunks = $need;
43 return $this;
46 protected function willExecute() {
47 // If we fail to load any changesets (which is possible in the case of an
48 // empty commit) we'll never call didFilterPage(). Attach empty changeset
49 // lists now so that we end up with the right result.
50 if ($this->needAttachToDiffs) {
51 foreach ($this->diffs as $diff) {
52 $diff->attachChangesets(array());
57 public function newResultObject() {
58 return new DifferentialChangeset();
61 protected function willFilterPage(array $changesets) {
62 // First, attach all the diffs we already have. We can just do this
63 // directly without worrying about querying for them. When we don't have
64 // a diff, record that we need to load it.
65 if ($this->diffs) {
66 $have_diffs = mpull($this->diffs, null, 'getID');
67 } else {
68 $have_diffs = array();
71 $must_load = array();
72 foreach ($changesets as $key => $changeset) {
73 $diff_id = $changeset->getDiffID();
74 if (isset($have_diffs[$diff_id])) {
75 $changeset->attachDiff($have_diffs[$diff_id]);
76 } else {
77 $must_load[$key] = $changeset;
81 // Load all the diffs we don't have.
82 $need_diff_ids = mpull($must_load, 'getDiffID');
83 $more_diffs = array();
84 if ($need_diff_ids) {
85 $more_diffs = id(new DifferentialDiffQuery())
86 ->setViewer($this->getViewer())
87 ->setParentQuery($this)
88 ->withIDs($need_diff_ids)
89 ->execute();
90 $more_diffs = mpull($more_diffs, null, 'getID');
93 // Attach the diffs we loaded.
94 foreach ($must_load as $key => $changeset) {
95 $diff_id = $changeset->getDiffID();
96 if (isset($more_diffs[$diff_id])) {
97 $changeset->attachDiff($more_diffs[$diff_id]);
98 } else {
99 // We didn't have the diff, and could not load it (it does not exist,
100 // or we can't see it), so filter this result out.
101 unset($changesets[$key]);
105 return $changesets;
108 protected function didFilterPage(array $changesets) {
109 if ($this->needAttachToDiffs) {
110 $changeset_groups = mgroup($changesets, 'getDiffID');
111 foreach ($this->diffs as $diff) {
112 $diff_changesets = idx($changeset_groups, $diff->getID(), array());
113 $diff->attachChangesets($diff_changesets);
117 if ($this->needHunks) {
118 id(new DifferentialHunkQuery())
119 ->setViewer($this->getViewer())
120 ->setParentQuery($this)
121 ->withChangesets($changesets)
122 ->needAttachToChangesets(true)
123 ->execute();
126 return $changesets;
129 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
130 $where = parent::buildWhereClauseParts($conn);
132 if ($this->diffs !== null) {
133 $where[] = qsprintf(
134 $conn,
135 'diffID IN (%Ld)',
136 mpull($this->diffs, 'getID'));
139 if ($this->ids !== null) {
140 $where[] = qsprintf(
141 $conn,
142 'id IN (%Ld)',
143 $this->ids);
146 if ($this->phids !== null) {
147 $where[] = qsprintf(
148 $conn,
149 'phid IN (%Ls)',
150 $this->phids);
153 if ($this->diffPHIDs !== null) {
154 $diff_ids = queryfx_all(
155 $conn,
156 'SELECT id FROM %R WHERE phid IN (%Ls)',
157 new DifferentialDiff(),
158 $this->diffPHIDs);
159 $diff_ids = ipull($diff_ids, 'id', null);
161 if (!$diff_ids) {
162 throw new PhabricatorEmptyQueryException();
165 $where[] = qsprintf(
166 $conn,
167 'diffID IN (%Ld)',
168 $diff_ids);
171 return $where;
174 public function getQueryApplicationClass() {
175 return 'PhabricatorDifferentialApplication';