@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<DivinerLiveBook>
5 */
6final class DivinerBookQuery extends PhabricatorCursorPagedPolicyAwareQuery {
7
8 private $ids;
9 private $phids;
10 private $names;
11 private $nameLike;
12 private $namePrefix;
13 private $repositoryPHIDs;
14
15 private $needProjectPHIDs;
16 private $needRepositories;
17
18 public function withIDs(array $ids) {
19 $this->ids = $ids;
20 return $this;
21 }
22
23 public function withPHIDs(array $phids) {
24 $this->phids = $phids;
25 return $this;
26 }
27
28 public function withNameLike($name) {
29 $this->nameLike = $name;
30 return $this;
31 }
32
33 public function withNames(array $names) {
34 $this->names = $names;
35 return $this;
36 }
37
38 public function withNamePrefix($prefix) {
39 $this->namePrefix = $prefix;
40 return $this;
41 }
42
43 public function withRepositoryPHIDs(array $repository_phids) {
44 $this->repositoryPHIDs = $repository_phids;
45 return $this;
46 }
47
48 public function needProjectPHIDs($need_phids) {
49 $this->needProjectPHIDs = $need_phids;
50 return $this;
51 }
52
53 public function needRepositories($need_repositories) {
54 $this->needRepositories = $need_repositories;
55 return $this;
56 }
57
58 protected function loadPage() {
59 $table = new DivinerLiveBook();
60 $conn_r = $table->establishConnection('r');
61
62 $data = queryfx_all(
63 $conn_r,
64 'SELECT * FROM %T %Q %Q %Q',
65 $table->getTableName(),
66 $this->buildWhereClause($conn_r),
67 $this->buildOrderClause($conn_r),
68 $this->buildLimitClause($conn_r));
69
70 return $table->loadAllFromArray($data);
71 }
72
73 /**
74 * @param array<DivinerLiveBook> $books
75 */
76 protected function didFilterPage(array $books) {
77 assert_instances_of($books, DivinerLiveBook::class);
78
79 if ($this->needRepositories) {
80 $repositories = id(new PhabricatorRepositoryQuery())
81 ->setViewer($this->getViewer())
82 ->withPHIDs(mpull($books, 'getRepositoryPHID'))
83 ->execute();
84 $repositories = mpull($repositories, null, 'getPHID');
85
86 foreach ($books as $key => $book) {
87 if ($book->getRepositoryPHID() === null) {
88 $book->attachRepository(null);
89 continue;
90 }
91
92 $repository = idx($repositories, $book->getRepositoryPHID());
93
94 if (!$repository) {
95 $this->didRejectResult($book);
96 unset($books[$key]);
97 continue;
98 }
99
100 $book->attachRepository($repository);
101 }
102 }
103
104 if ($this->needProjectPHIDs) {
105 $edge_query = id(new PhabricatorEdgeQuery())
106 ->withSourcePHIDs(mpull($books, 'getPHID'))
107 ->withEdgeTypes(
108 array(
109 PhabricatorProjectObjectHasProjectEdgeType::EDGECONST,
110 ));
111 $edge_query->execute();
112
113 foreach ($books as $book) {
114 $project_phids = $edge_query->getDestinationPHIDs(
115 array(
116 $book->getPHID(),
117 ));
118 $book->attachProjectPHIDs($project_phids);
119 }
120 }
121
122 return $books;
123 }
124
125 protected function buildWhereClause(AphrontDatabaseConnection $conn) {
126 $where = array();
127
128 if ($this->ids) {
129 $where[] = qsprintf(
130 $conn,
131 'id IN (%Ld)',
132 $this->ids);
133 }
134
135 if ($this->phids) {
136 $where[] = qsprintf(
137 $conn,
138 'phid IN (%Ls)',
139 $this->phids);
140 }
141
142 if (phutil_nonempty_string($this->nameLike)) {
143 $where[] = qsprintf(
144 $conn,
145 'name LIKE %~',
146 $this->nameLike);
147 }
148
149 if ($this->names !== null) {
150 $where[] = qsprintf(
151 $conn,
152 'name IN (%Ls)',
153 $this->names);
154 }
155
156 if (phutil_nonempty_string($this->namePrefix)) {
157 $where[] = qsprintf(
158 $conn,
159 'name LIKE %>',
160 $this->namePrefix);
161 }
162
163 if ($this->repositoryPHIDs !== null) {
164 $where[] = qsprintf(
165 $conn,
166 'repositoryPHID IN (%Ls)',
167 $this->repositoryPHIDs);
168 }
169
170 $where[] = $this->buildPagingClause($conn);
171
172 return $this->formatWhereClause($conn, $where);
173 }
174
175 public function getQueryApplicationClass() {
176 return PhabricatorDivinerApplication::class;
177 }
178
179 public function getOrderableColumns() {
180 return parent::getOrderableColumns() + array(
181 'name' => array(
182 'column' => 'name',
183 'type' => 'string',
184 'reverse' => true,
185 'unique' => true,
186 ),
187 );
188 }
189
190 protected function newPagingMapFromPartialObject($object) {
191 return array(
192 'id' => (int)$object->getID(),
193 'name' => $object->getName(),
194 );
195 }
196
197 public function getBuiltinOrders() {
198 return array(
199 'name' => array(
200 'vector' => array('name'),
201 'name' => pht('Name'),
202 ),
203 ) + parent::getBuiltinOrders();
204 }
205
206}