@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<PhabricatorRepositoryRefCursor>
5 */
6final class PhabricatorRepositoryRefCursorQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $ids;
10 private $phids;
11 private $repositoryPHIDs;
12 private $refTypes;
13 private $refNames;
14 private $datasourceQuery;
15 private $needPositions;
16
17 public function withIDs(array $ids) {
18 $this->ids = $ids;
19 return $this;
20 }
21
22 public function withPHIDs(array $phids) {
23 $this->phids = $phids;
24 return $this;
25 }
26
27 public function withRepositoryPHIDs(array $phids) {
28 $this->repositoryPHIDs = $phids;
29 return $this;
30 }
31
32 public function withRefTypes(array $types) {
33 $this->refTypes = $types;
34 return $this;
35 }
36
37 public function withRefNames(array $names) {
38 $this->refNames = $names;
39 return $this;
40 }
41
42 public function withDatasourceQuery($query) {
43 $this->datasourceQuery = $query;
44 return $this;
45 }
46
47 public function needPositions($need) {
48 $this->needPositions = $need;
49 return $this;
50 }
51
52 public function newResultObject() {
53 return new PhabricatorRepositoryRefCursor();
54 }
55
56 protected function willFilterPage(array $refs) {
57 $repository_phids = mpull($refs, 'getRepositoryPHID');
58
59 $repositories = id(new PhabricatorRepositoryQuery())
60 ->setViewer($this->getViewer())
61 ->setParentQuery($this)
62 ->withPHIDs($repository_phids)
63 ->execute();
64 $repositories = mpull($repositories, null, 'getPHID');
65
66 foreach ($refs as $key => $ref) {
67 $repository = idx($repositories, $ref->getRepositoryPHID());
68 if (!$repository) {
69 $this->didRejectResult($ref);
70 unset($refs[$key]);
71 continue;
72 }
73 $ref->attachRepository($repository);
74 }
75
76 if (!$refs) {
77 return $refs;
78 }
79
80 if ($this->needPositions) {
81 $positions = id(new PhabricatorRepositoryRefPosition())->loadAllWhere(
82 'cursorID IN (%Ld)',
83 mpull($refs, 'getID'));
84 $positions = mgroup($positions, 'getCursorID');
85
86 foreach ($refs as $key => $ref) {
87 $ref_positions = idx($positions, $ref->getID(), array());
88 $ref->attachPositions($ref_positions);
89 }
90 }
91
92 return $refs;
93 }
94
95 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
96 $where = parent::buildWhereClauseParts($conn);
97
98 if ($this->ids !== null) {
99 $where[] = qsprintf(
100 $conn,
101 'id IN (%Ld)',
102 $this->ids);
103 }
104
105 if ($this->phids !== null) {
106 $where[] = qsprintf(
107 $conn,
108 'phid IN (%Ls)',
109 $this->phids);
110 }
111
112 if ($this->repositoryPHIDs !== null) {
113 $where[] = qsprintf(
114 $conn,
115 'repositoryPHID IN (%Ls)',
116 $this->repositoryPHIDs);
117 }
118
119 if ($this->refTypes !== null) {
120 $where[] = qsprintf(
121 $conn,
122 'refType IN (%Ls)',
123 $this->refTypes);
124 }
125
126 if ($this->refNames !== null) {
127 $name_hashes = array();
128 foreach ($this->refNames as $name) {
129 $name_hashes[] = PhabricatorHash::digestForIndex($name);
130 }
131
132 $where[] = qsprintf(
133 $conn,
134 'refNameHash IN (%Ls)',
135 $name_hashes);
136 }
137
138 if (phutil_nonempty_string($this->datasourceQuery)) {
139 $where[] = qsprintf(
140 $conn,
141 'refNameRaw LIKE %>',
142 $this->datasourceQuery);
143 }
144
145 return $where;
146 }
147
148 public function getQueryApplicationClass() {
149 return PhabricatorDiffusionApplication::class;
150 }
151
152}