3 final class DifferentialChangesetQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery
{
12 private $needAttachToDiffs;
15 public function withIDs(array $ids) {
20 public function withPHIDs(array $phids) {
21 $this->phids
= $phids;
25 public function withDiffs(array $diffs) {
26 assert_instances_of($diffs, 'DifferentialDiff');
27 $this->diffs
= $diffs;
31 public function withDiffPHIDs(array $phids) {
32 $this->diffPHIDs
= $phids;
36 public function needAttachToDiffs($attach) {
37 $this->needAttachToDiffs
= $attach;
41 public function needHunks($need) {
42 $this->needHunks
= $need;
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.
70 $have_diffs = mpull($this->diffs
, null, 'getID');
72 $have_diffs = 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]);
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();
89 $more_diffs = id(new DifferentialDiffQuery())
90 ->setViewer($this->getViewer())
91 ->setParentQuery($this)
92 ->withIDs($need_diff_ids)
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]);
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]);
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)
133 protected function buildWhereClauseParts(AphrontDatabaseConnection
$conn) {
134 $where = parent
::buildWhereClauseParts($conn);
136 if ($this->diffs
!== null) {
140 mpull($this->diffs
, 'getID'));
143 if ($this->ids
!== null) {
150 if ($this->phids
!== null) {
157 if ($this->diffPHIDs
!== null) {
158 $diff_ids = queryfx_all(
160 'SELECT id FROM %R WHERE phid IN (%Ls)',
161 new DifferentialDiff(),
163 $diff_ids = ipull($diff_ids, 'id', null);
166 throw new PhabricatorEmptyQueryException();
178 public function getQueryApplicationClass() {
179 return 'PhabricatorDifferentialApplication';