Remove product literal strings in "pht()", part 6
[phabricator.git] / src / applications / differential / query / DifferentialChangesetQuery.php
blobe2357f92782b4653861e2a4f0919cd251feb268f
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 loadPage() {
62 return $this->loadStandardPage($this->newResultObject());
65 protected function willFilterPage(array $changesets) {
66 // First, attach all the diffs we already have. We can just do this
67 // directly without worrying about querying for them. When we don't have
68 // a diff, record that we need to load it.
69 if ($this->diffs) {
70 $have_diffs = mpull($this->diffs, null, 'getID');
71 } else {
72 $have_diffs = array();
75 $must_load = array();
76 foreach ($changesets as $key => $changeset) {
77 $diff_id = $changeset->getDiffID();
78 if (isset($have_diffs[$diff_id])) {
79 $changeset->attachDiff($have_diffs[$diff_id]);
80 } else {
81 $must_load[$key] = $changeset;
85 // Load all the diffs we don't have.
86 $need_diff_ids = mpull($must_load, 'getDiffID');
87 $more_diffs = array();
88 if ($need_diff_ids) {
89 $more_diffs = id(new DifferentialDiffQuery())
90 ->setViewer($this->getViewer())
91 ->setParentQuery($this)
92 ->withIDs($need_diff_ids)
93 ->execute();
94 $more_diffs = mpull($more_diffs, null, 'getID');
97 // Attach the diffs we loaded.
98 foreach ($must_load as $key => $changeset) {
99 $diff_id = $changeset->getDiffID();
100 if (isset($more_diffs[$diff_id])) {
101 $changeset->attachDiff($more_diffs[$diff_id]);
102 } else {
103 // We didn't have the diff, and could not load it (it does not exist,
104 // or we can't see it), so filter this result out.
105 unset($changesets[$key]);
109 return $changesets;
112 protected function didFilterPage(array $changesets) {
113 if ($this->needAttachToDiffs) {
114 $changeset_groups = mgroup($changesets, 'getDiffID');
115 foreach ($this->diffs as $diff) {
116 $diff_changesets = idx($changeset_groups, $diff->getID(), array());
117 $diff->attachChangesets($diff_changesets);
121 if ($this->needHunks) {
122 id(new DifferentialHunkQuery())
123 ->setViewer($this->getViewer())
124 ->setParentQuery($this)
125 ->withChangesets($changesets)
126 ->needAttachToChangesets(true)
127 ->execute();
130 return $changesets;
133 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
134 $where = parent::buildWhereClauseParts($conn);
136 if ($this->diffs !== null) {
137 $where[] = qsprintf(
138 $conn,
139 'diffID IN (%Ld)',
140 mpull($this->diffs, 'getID'));
143 if ($this->ids !== null) {
144 $where[] = qsprintf(
145 $conn,
146 'id IN (%Ld)',
147 $this->ids);
150 if ($this->phids !== null) {
151 $where[] = qsprintf(
152 $conn,
153 'phid IN (%Ls)',
154 $this->phids);
157 if ($this->diffPHIDs !== null) {
158 $diff_ids = queryfx_all(
159 $conn,
160 'SELECT id FROM %R WHERE phid IN (%Ls)',
161 new DifferentialDiff(),
162 $this->diffPHIDs);
163 $diff_ids = ipull($diff_ids, 'id', null);
165 if (!$diff_ids) {
166 throw new PhabricatorEmptyQueryException();
169 $where[] = qsprintf(
170 $conn,
171 'diffID IN (%Ld)',
172 $diff_ids);
175 return $where;
178 public function getQueryApplicationClass() {
179 return 'PhabricatorDifferentialApplication';