Debian Codegraph @

includes/recentchanges/ChangesListFilterGroup.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\Html\FormOptions;
22 use MediaWiki\SpecialPage\ChangesListSpecialPage;
23 use Wikimedia\Rdbms\IReadableDatabase;
24
25 /**
26 * Represents a filter group (used on ChangesListSpecialPage and descendants)
27 *
28 * @todo Might want to make a super-class or trait to share behavior (especially re
29 * conflicts) between ChangesListFilter and ChangesListFilterGroup.
30 * What to call it. FilterStructure? That would also let me make
31 * setUnidirectionalConflict protected.
32 *
33 * @since 1.29
34 * @ingroup RecentChanges
35 * @author Matthew Flaschen
36 * @method registerFilter($filter)
37 */
38 abstract class ChangesListFilterGroup {
39 /**
40 * Name (internal identifier)
41 *
42 * @var string
43 */
44 protected $name;
45
46 /**
47 * i18n key for title
48 *
49 * @var string
50 */
51 protected $title;
52
53 /**
54 * i18n key for header of What's This?
55 *
56 * @var string|null
57 */
58 protected $whatsThisHeader;
59
60 /**
61 * i18n key for body of What's This?
62 *
63 * @var string|null
64 */
65 protected $whatsThisBody;
66
67 /**
68 * URL of What's This? link
69 *
70 * @var string|null
71 */
72 protected $whatsThisUrl;
73
74 /**
75 * i18n key for What's This? link
76 *
77 * @var string|null
78 */
79 protected $whatsThisLinkText;
80
81 /**
82 * Type, from a TYPE constant of a subclass
83 *
84 * @var string
85 */
86 protected $type;
87
88 /**
89 * Priority integer. Higher values means higher up in the
90 * group list.
91 *
92 * @var int
93 */
94 protected $priority;
95
96 /**
97 * Associative array of filters, as ChangesListFilter objects, with filter name as key
98 *
99 * @var ChangesListFilter[]
100 */
101 protected $filters;
102
103 /**
104 * Whether this group is full coverage. This means that checking every item in the
105 * group means no changes list (e.g. RecentChanges) entries are filtered out.
106 *
107 * @var bool
108 */
109 protected $isFullCoverage;
110
111 /**
112 * Array of associative arrays with conflict information. See
113 * setUnidirectionalConflict
114 *
115 * @var array
116 */
117 protected $conflictingGroups = [];
118
119 /**
120 * Array of associative arrays with conflict information. See
121 * setUnidirectionalConflict
122 *
123 * @var array
124 */
125 protected $conflictingFilters = [];
126
127 private const DEFAULT_PRIORITY = -100;
128
129 private const RESERVED_NAME_CHAR = '_';
130
131 /**
132 * Create a new filter group with the specified configuration
133 *
134 * @param array $groupDefinition Configuration of group
135 * * $groupDefinition['name'] string Group name; use camelCase with no punctuation
136 * * $groupDefinition['title'] string i18n key for title (optional, can be omitted
137 * only if none of the filters in the group display in the structured UI)
138 * * $groupDefinition['type'] string A type constant from a subclass of this one
139 * * $groupDefinition['priority'] int Priority integer. Higher value means higher
140 * up in the group list (optional, defaults to -100).
141 * * $groupDefinition['filters'] array Numeric array of filter definitions, each of which
142 * is an associative array to be passed to the filter constructor. However,
143 * 'priority' is optional for the filters. Any filter that has priority unset
144 * will be put to the bottom, in the order given.
145 * * $groupDefinition['isFullCoverage'] bool Whether the group is full coverage;
146 * if true, this means that checking every item in the group means no
147 * changes list entries are filtered out.
148 * * $groupDefinition['whatsThisHeader'] string i18n key for header of "What's
149 * This" popup (optional).
150 * * $groupDefinition['whatsThisBody'] string i18n key for body of "What's This"
151 * popup (optional).
152 * * $groupDefinition['whatsThisUrl'] string URL for main link of "What's This"
153 * popup (optional).
154 * * $groupDefinition['whatsThisLinkText'] string i18n key of text for main link of
155 * "What's This" popup (optional).
156 */
157 public function __construct( array $groupDefinition ) {
158 if ( strpos( $groupDefinition['name'], self::RESERVED_NAME_CHAR ) !== false ) {
159 throw new InvalidArgumentException( 'Group names may not contain \'' .
160 self::RESERVED_NAME_CHAR .
161 '\'. Use the naming convention: \'camelCase\''
162 );
163 }
164
165 $this->name = $groupDefinition['name'];
166
167 if ( isset( $groupDefinition['title'] ) ) {
168 $this->title = $groupDefinition['title'];
169 }
170
171 if ( isset( $groupDefinition['whatsThisHeader'] ) ) {
172 $this->whatsThisHeader = $groupDefinition['whatsThisHeader'];
173 $this->whatsThisBody = $groupDefinition['whatsThisBody'];
174 $this->whatsThisUrl = $groupDefinition['whatsThisUrl'];
175 $this->whatsThisLinkText = $groupDefinition['whatsThisLinkText'];
176 }
177
178 $this->type = $groupDefinition['type'];
179 $this->priority = $groupDefinition['priority'] ?? self::DEFAULT_PRIORITY;
180
181 $this->isFullCoverage = $groupDefinition['isFullCoverage'];
182
183 $this->filters = [];
184 $lowestSpecifiedPriority = -1;
185 foreach ( $groupDefinition['filters'] as $filterDefinition ) {
186 if ( isset( $filterDefinition['priority'] ) ) {
187 $lowestSpecifiedPriority = min( $lowestSpecifiedPriority, $filterDefinition['priority'] );
188 }
189 }
190
191 // Convenience feature: If you specify a group (and its filters) all in
192 // one place, you don't have to specify priority. You can just put them
193 // in order. However, if you later add one (e.g. an extension adds a filter
194 // to a core-defined group), you need to specify it.
195 $autoFillPriority = $lowestSpecifiedPriority - 1;
196 foreach ( $groupDefinition['filters'] as $filterDefinition ) {
197 if ( !isset( $filterDefinition['priority'] ) ) {
198 $filterDefinition['priority'] = $autoFillPriority;
199 $autoFillPriority--;
200 }
201 $filterDefinition['group'] = $this;
202
203 $filter = $this->createFilter( $filterDefinition );
204 $this->registerFilter( $filter );
205 }
206 }
207
208 /**
209 * Creates a filter of the appropriate type for this group, from the definition
210 *
211 * @param array $filterDefinition
212 * @return ChangesListFilter Filter
213 */
214 abstract protected function createFilter( array $filterDefinition );
215
216 /**
217 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
218 *
219 * WARNING: This means there is a conflict when both things are *shown*
220 * (not filtered out), even for the hide-based filters. So e.g. conflicting with
221 * 'hideanons' means there is a conflict if only anonymous users are *shown*.
222 *
223 * @param ChangesListFilterGroup|ChangesListFilter $other
224 * @param string $globalKey i18n key for top-level conflict message
225 * @param string $forwardKey i18n key for conflict message in this
226 * direction (when in UI context of $this object)
227 * @param string $backwardKey i18n key for conflict message in reverse
228 * direction (when in UI context of $other object)
229 */
230 public function conflictsWith( $other, string $globalKey, string $forwardKey, string $backwardKey ) {
231 $this->setUnidirectionalConflict(
232 $other,
233 $globalKey,
234 $forwardKey
235 );
236
237 $other->setUnidirectionalConflict(
238 $this,
239 $globalKey,
240 $backwardKey
241 );
242 }
243
244 /**
245 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with
246 * this object.
247 *
248 * Internal use ONLY.
249 *
250 * @param ChangesListFilterGroup|ChangesListFilter $other
251 * @param string $globalDescription i18n key for top-level conflict message
252 * @param string $contextDescription i18n key for conflict message in this
253 * direction (when in UI context of $this object)
254 */
255 public function setUnidirectionalConflict( $other, $globalDescription, $contextDescription ) {
256 if ( $other instanceof ChangesListFilterGroup ) {
257 $this->conflictingGroups[] = [
258 'group' => $other->getName(),
259 'groupObject' => $other,
260 'globalDescription' => $globalDescription,
261 'contextDescription' => $contextDescription,
262 ];
263 } elseif ( $other instanceof ChangesListFilter ) {
264 $this->conflictingFilters[] = [
265 'group' => $other->getGroup()->getName(),
266 'filter' => $other->getName(),
267 'filterObject' => $other,
268 'globalDescription' => $globalDescription,
269 'contextDescription' => $contextDescription,
270 ];
271 } else {
272 throw new InvalidArgumentException(
273 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter'
274 );
275 }
276 }
277
278 /**
279 * @return string Internal name
280 */
281 public function getName() {
282 return $this->name;
283 }
284
285 /**
286 * @return string i18n key for title
287 */
288 public function getTitle() {
289 return $this->title;
290 }
291
292 /**
293 * @return string Type (TYPE constant from a subclass)
294 */
295 public function getType() {
296 return $this->type;
297 }
298
299 /**
300 * @return int Priority. Higher means higher in the group list
301 */
302 public function getPriority() {
303 return $this->priority;
304 }
305
306 /**
307 * @return ChangesListFilter[] Associative array of ChangesListFilter objects, with
308 * filter name as key
309 */
310 public function getFilters() {
311 return $this->filters;
312 }
313
314 /**
315 * Get filter by name
316 *
317 * @param string $name Filter name
318 * @return ChangesListFilter|null Specified filter, or null if it is not registered
319 */
320 public function getFilter( $name ) {
321 return $this->filters[$name] ?? null;
322 }
323
324 /**
325 * Gets the JS data in the format required by the front-end of the structured UI
326 *
327 * @return array|null Associative array, or null if there are no filters that
328 * display in the structured UI. messageKeys is a special top-level value, with
329 * the value being an array of the message keys to send to the client.
330 */
331 public function getJsData() {
332 $output = [
333 'name' => $this->name,
334 'type' => $this->type,
335 'fullCoverage' => $this->isFullCoverage,
336 'filters' => [],
337 'priority' => $this->priority,
338 'conflicts' => [],
339 'messageKeys' => [ $this->title ]
340 ];
341
342 if ( isset( $this->whatsThisHeader ) ) {
343 $output['whatsThisHeader'] = $this->whatsThisHeader;
344 $output['whatsThisBody'] = $this->whatsThisBody;
345 $output['whatsThisUrl'] = $this->whatsThisUrl;
346 $output['whatsThisLinkText'] = $this->whatsThisLinkText;
347
348 array_push(
349 $output['messageKeys'],
350 $output['whatsThisHeader'],
351 $output['whatsThisBody'],
352 $output['whatsThisLinkText']
353 );
354 }
355
356 usort( $this->filters, static function ( ChangesListFilter $a, ChangesListFilter $b ) {
357 return $b->getPriority() <=> $a->getPriority();
358 } );
359
360 foreach ( $this->filters as $filter ) {
361 if ( $filter->displaysOnStructuredUi() ) {
362 $filterData = $filter->getJsData();
363 $output['messageKeys'] = array_merge(
364 $output['messageKeys'],
365 $filterData['messageKeys']
366 );
367 unset( $filterData['messageKeys'] );
368 $output['filters'][] = $filterData;
369 }
370 }
371
372 if ( count( $output['filters'] ) === 0 ) {
373 return null;
374 }
375
376 $output['title'] = $this->title;
377
378 $conflicts = array_merge(
379 $this->conflictingGroups,
380 $this->conflictingFilters
381 );
382
383 foreach ( $conflicts as $conflictInfo ) {
384 unset( $conflictInfo['filterObject'] );
385 unset( $conflictInfo['groupObject'] );
386 $output['conflicts'][] = $conflictInfo;
387 array_push(
388 $output['messageKeys'],
389 $conflictInfo['globalDescription'],
390 $conflictInfo['contextDescription']
391 );
392 }
393
394 return $output;
395 }
396
397 /**
398 * Get groups conflicting with this filter group
399 *
400 * @return ChangesListFilterGroup[]
401 */
402 public function getConflictingGroups() {
403 return array_column( $this->conflictingGroups, 'groupObject' );
404 }
405
406 /**
407 * Get filters conflicting with this filter group
408 *
409 * @return ChangesListFilter[]
410 */
411 public function getConflictingFilters() {
412 return array_column( $this->conflictingFilters, 'filterObject' );
413 }
414
415 /**
416 * Check if any filter in this group is selected
417 *
418 * @param FormOptions $opts
419 * @return bool
420 */
421 public function anySelected( FormOptions $opts ) {
422 return (bool)count( array_filter(
423 $this->getFilters(),
424 static function ( ChangesListFilter $filter ) use ( $opts ) {
425 return $filter->isSelected( $opts );
426 }
427 ) );
428 }
429
430 /**
431 * Modifies the query to include the filter group.
432 *
433 * The modification is only done if the filter group is in effect. This means that
434 * one or more valid and allowed filters were selected.
435 *
436 * @param IReadableDatabase $dbr Database, for addQuotes, makeList, and similar
437 * @param ChangesListSpecialPage $specialPage Current special page
438 * @param array &$tables Array of tables; see IDatabase::select $table
439 * @param array &$fields Array of fields; see IDatabase::select $vars
440 * @param array &$conds Array of conditions; see IDatabase::select $conds
441 * @param array &$query_options Array of query options; see IDatabase::select $options
442 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
443 * @param FormOptions $opts Wrapper for the current request options and their defaults
444 * @param bool $isStructuredFiltersEnabled True if the Structured UI is currently enabled
445 */
446 abstract public function modifyQuery( IReadableDatabase $dbr, ChangesListSpecialPage $specialPage,
447 &$tables, &$fields, &$conds, &$query_options, &$join_conds,
448 FormOptions $opts, $isStructuredFiltersEnabled );
449
450 /**
451 * All the options represented by this filter group to $opts
452 *
453 * @param FormOptions $opts
454 * @param bool $allowDefaults
455 * @param bool $isStructuredFiltersEnabled
456 */
457 abstract public function addOptions( FormOptions $opts, $allowDefaults,
458 $isStructuredFiltersEnabled );
459 }