@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * @extends PhabricatorCursorPagedPolicyAwareQuery<DifferentialHunk>
5 */
6final class DifferentialHunkQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $changesets;
10 private $shouldAttachToChangesets;
11
12 /**
13 * @param array<DifferentialChangeset> $changesets
14 */
15 public function withChangesets(array $changesets) {
16 assert_instances_of($changesets, DifferentialChangeset::class);
17 $this->changesets = $changesets;
18 return $this;
19 }
20
21 public function needAttachToChangesets($attach) {
22 $this->shouldAttachToChangesets = $attach;
23 return $this;
24 }
25
26 protected function willExecute() {
27 // If we fail to load any hunks at all (for example, because all of
28 // the requested changesets are directories or empty files and have no
29 // hunks) we'll never call didFilterPage(), and thus never have an
30 // opportunity to attach hunks. Attach empty hunk lists now so that we
31 // end up with the right result.
32 if ($this->shouldAttachToChangesets) {
33 foreach ($this->changesets as $changeset) {
34 $changeset->attachHunks(array());
35 }
36 }
37 }
38
39 public function newResultObject() {
40 return new DifferentialHunk();
41 }
42
43 protected function willFilterPage(array $hunks) {
44 $changesets = mpull($this->changesets, null, 'getID');
45 foreach ($hunks as $key => $hunk) {
46 $changeset = idx($changesets, $hunk->getChangesetID());
47 if (!$changeset) {
48 unset($hunks[$key]);
49 }
50 $hunk->attachChangeset($changeset);
51 }
52
53 return $hunks;
54 }
55
56 protected function didFilterPage(array $hunks) {
57 if ($this->shouldAttachToChangesets) {
58 $hunk_groups = mgroup($hunks, 'getChangesetID');
59 foreach ($this->changesets as $changeset) {
60 $hunks = idx($hunk_groups, $changeset->getID(), array());
61 $changeset->attachHunks($hunks);
62 }
63 }
64
65 return $hunks;
66 }
67
68 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
69 $where = parent::buildWhereClauseParts($conn);
70
71 if (!$this->changesets) {
72 throw new Exception(
73 pht(
74 'You must load hunks via changesets, with %s!',
75 'withChangesets()'));
76 }
77
78 $where[] = qsprintf(
79 $conn,
80 'changesetID IN (%Ld)',
81 mpull($this->changesets, 'getID'));
82
83 return $where;
84 }
85
86 public function getQueryApplicationClass() {
87 return PhabricatorDifferentialApplication::class;
88 }
89
90 protected function getDefaultOrderVector() {
91 // TODO: Do we need this?
92 return array('-id');
93 }
94
95}