@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<PhabricatorFileImageMacro>
5 */
6final class PhabricatorMacroQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $ids;
10 private $phids;
11 private $authorPHIDs;
12 private $names;
13 private $nameLike;
14 private $namePrefix;
15 private $dateCreatedAfter;
16 private $dateCreatedBefore;
17 private $flagColor;
18
19 private $needFiles;
20
21 private $status = 'status-any';
22 const STATUS_ANY = 'status-any';
23 const STATUS_ACTIVE = 'status-active';
24 const STATUS_DISABLED = 'status-disabled';
25
26 public static function getStatusOptions() {
27 return array(
28 self::STATUS_ACTIVE => pht('Active Macros'),
29 self::STATUS_DISABLED => pht('Disabled Macros'),
30 self::STATUS_ANY => pht('Active and Disabled Macros'),
31 );
32 }
33
34 public static function getFlagColorsOptions() {
35 $options = array(
36 '-1' => pht('(No Filtering)'),
37 '-2' => pht('(Marked With Any Flag)'),
38 );
39
40 foreach (PhabricatorFlagColor::getColorNameMap() as $color => $name) {
41 $options[$color] = $name;
42 }
43
44 return $options;
45 }
46
47 public function withIDs(array $ids) {
48 $this->ids = $ids;
49 return $this;
50 }
51
52 public function withPHIDs(array $phids) {
53 $this->phids = $phids;
54 return $this;
55 }
56
57 public function withAuthorPHIDs(array $author_phids) {
58 $this->authorPHIDs = $author_phids;
59 return $this;
60 }
61
62 public function withNameLike($name) {
63 $this->nameLike = $name;
64 return $this;
65 }
66
67 public function withNames(array $names) {
68 $this->names = $names;
69 return $this;
70 }
71
72 public function withNamePrefix($prefix) {
73 $this->namePrefix = $prefix;
74 return $this;
75 }
76
77 public function withStatus($status) {
78 $this->status = $status;
79 return $this;
80 }
81
82 public function withDateCreatedBefore($date_created_before) {
83 $this->dateCreatedBefore = $date_created_before;
84 return $this;
85 }
86
87 public function withDateCreatedAfter($date_created_after) {
88 $this->dateCreatedAfter = $date_created_after;
89 return $this;
90 }
91
92 public function withFlagColor($flag_color) {
93 $this->flagColor = $flag_color;
94 return $this;
95 }
96
97 public function needFiles($need_files) {
98 $this->needFiles = $need_files;
99 return $this;
100 }
101
102 public function newResultObject() {
103 return new PhabricatorFileImageMacro();
104 }
105
106 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
107 $where = parent::buildWhereClauseParts($conn);
108
109 if ($this->ids !== null) {
110 $where[] = qsprintf(
111 $conn,
112 'm.id IN (%Ld)',
113 $this->ids);
114 }
115
116 if ($this->phids !== null) {
117 $where[] = qsprintf(
118 $conn,
119 'm.phid IN (%Ls)',
120 $this->phids);
121 }
122
123 if ($this->authorPHIDs !== null) {
124 $where[] = qsprintf(
125 $conn,
126 'm.authorPHID IN (%Ls)',
127 $this->authorPHIDs);
128 }
129
130 if (($this->nameLike !== null) && strlen($this->nameLike)) {
131 $where[] = qsprintf(
132 $conn,
133 'm.name LIKE %~',
134 $this->nameLike);
135 }
136
137 if ($this->names !== null) {
138 $where[] = qsprintf(
139 $conn,
140 'm.name IN (%Ls)',
141 $this->names);
142 }
143
144 if (($this->namePrefix !== null) && strlen($this->namePrefix)) {
145 $where[] = qsprintf(
146 $conn,
147 'm.name LIKE %>',
148 $this->namePrefix);
149 }
150
151 switch ($this->status) {
152 case self::STATUS_ACTIVE:
153 $where[] = qsprintf(
154 $conn,
155 'm.isDisabled = 0');
156 break;
157 case self::STATUS_DISABLED:
158 $where[] = qsprintf(
159 $conn,
160 'm.isDisabled = 1');
161 break;
162 case self::STATUS_ANY:
163 break;
164 default:
165 throw new Exception(pht("Unknown status '%s'!", $this->status));
166 }
167
168 if ($this->dateCreatedAfter) {
169 $where[] = qsprintf(
170 $conn,
171 'm.dateCreated >= %d',
172 $this->dateCreatedAfter);
173 }
174
175 if ($this->dateCreatedBefore) {
176 $where[] = qsprintf(
177 $conn,
178 'm.dateCreated <= %d',
179 $this->dateCreatedBefore);
180 }
181
182 if ($this->flagColor != '-1' && $this->flagColor !== null) {
183 if ($this->flagColor == '-2') {
184 $flag_colors = array_keys(PhabricatorFlagColor::getColorNameMap());
185 } else {
186 $flag_colors = array($this->flagColor);
187 }
188 $flags = id(new PhabricatorFlagQuery())
189 ->withOwnerPHIDs(array($this->getViewer()->getPHID()))
190 ->withTypes(array(PhabricatorMacroMacroPHIDType::TYPECONST))
191 ->withColors($flag_colors)
192 ->setViewer($this->getViewer())
193 ->execute();
194
195 if (empty($flags)) {
196 throw new PhabricatorEmptyQueryException(pht('No matching flags.'));
197 } else {
198 $where[] = qsprintf(
199 $conn,
200 'm.phid IN (%Ls)',
201 mpull($flags, 'getObjectPHID'));
202 }
203 }
204
205 return $where;
206 }
207
208 protected function didFilterPage(array $macros) {
209 if ($this->needFiles) {
210 $file_phids = mpull($macros, 'getFilePHID');
211 $files = id(new PhabricatorFileQuery())
212 ->setViewer($this->getViewer())
213 ->setParentQuery($this)
214 ->withPHIDs($file_phids)
215 ->execute();
216 $files = mpull($files, null, 'getPHID');
217
218 foreach ($macros as $key => $macro) {
219 $file = idx($files, $macro->getFilePHID());
220 if (!$file) {
221 unset($macros[$key]);
222 continue;
223 }
224 $macro->attachFile($file);
225 }
226 }
227
228 return $macros;
229 }
230
231 protected function getPrimaryTableAlias() {
232 return 'm';
233 }
234
235 public function getQueryApplicationClass() {
236 return PhabricatorMacroApplication::class;
237 }
238
239 public function getOrderableColumns() {
240 return parent::getOrderableColumns() + array(
241 'name' => array(
242 'table' => 'm',
243 'column' => 'name',
244 'type' => 'string',
245 'reverse' => true,
246 'unique' => true,
247 ),
248 );
249 }
250
251 protected function newPagingMapFromPartialObject($object) {
252 return array(
253 'id' => (int)$object->getID(),
254 'name' => $object->getName(),
255 );
256 }
257
258 public function getBuiltinOrders() {
259 return array(
260 'name' => array(
261 'vector' => array('name'),
262 'name' => pht('Name'),
263 ),
264 ) + parent::getBuiltinOrders();
265 }
266
267}