rdbms: Avoid selectDB() call in LoadMonitor new connections
[mediawiki.git] / includes / changes / ChangesListFilterGroup.php
blob57627525a34eaf3cb19cdf03ba7d1fbd32a851a0
1 <?php
2 /**
3 * Represents a filter group (used on ChangesListSpecialPage and descendants)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @license GPL 2+
22 * @author Matthew Flaschen
25 // TODO: Might want to make a super-class or trait to share behavior (especially re
26 // conflicts) between ChangesListFilter and ChangesListFilterGroup.
27 // What to call it. FilterStructure? That would also let me make
28 // setUnidirectionalConflict protected.
30 /**
31 * Represents a filter group (used on ChangesListSpecialPage and descendants)
33 * @since 1.29
35 abstract class ChangesListFilterGroup {
36 /**
37 * Name (internal identifier)
39 * @var string $name
41 protected $name;
43 /**
44 * i18n key for title
46 * @var string $title
48 protected $title;
50 /**
51 * i18n key for header of What's This?
53 * @var string|null $whatsThisHeader
55 protected $whatsThisHeader;
57 /**
58 * i18n key for body of What's This?
60 * @var string|null $whatsThisBody
62 protected $whatsThisBody;
64 /**
65 * URL of What's This? link
67 * @var string|null $whatsThisUrl
69 protected $whatsThisUrl;
71 /**
72 * i18n key for What's This? link
74 * @var string|null $whatsThisLinkText
76 protected $whatsThisLinkText;
78 /**
79 * Type, from a TYPE constant of a subclass
81 * @var string $type
83 protected $type;
85 /**
86 * Priority integer. Higher values means higher up in the
87 * group list.
89 * @var string $priority
91 protected $priority;
93 /**
94 * Associative array of filters, as ChangesListFilter objects, with filter name as key
96 * @var array $filters
98 protected $filters;
101 * Whether this group is full coverage. This means that checking every item in the
102 * group means no changes list (e.g. RecentChanges) entries are filtered out.
104 * @var bool $isFullCoverage
106 protected $isFullCoverage;
109 * Array of associative arrays with conflict information. See
110 * setUnidirectionalConflict
112 * @var array $conflictingGroups
114 protected $conflictingGroups = [];
117 * Array of associative arrays with conflict information. See
118 * setUnidirectionalConflict
120 * @var array $conflictingFilters
122 protected $conflictingFilters = [];
124 const DEFAULT_PRIORITY = -100;
126 const RESERVED_NAME_CHAR = '_';
129 * Create a new filter group with the specified configuration
131 * @param array $groupDefinition Configuration of group
132 * * $groupDefinition['name'] string Group name; use camelCase with no punctuation
133 * * $groupDefinition['title'] string i18n key for title (optional, can be omitted
134 * only if none of the filters in the group display in the structured UI)
135 * * $groupDefinition['type'] string A type constant from a subclass of this one
136 * * $groupDefinition['priority'] int Priority integer. Higher value means higher
137 * up in the group list (optional, defaults to -100).
138 * * $groupDefinition['filters'] array Numeric array of filter definitions, each of which
139 * is an associative array to be passed to the filter constructor. However,
140 * 'priority' is optional for the filters. Any filter that has priority unset
141 * will be put to the bottom, in the order given.
142 * * $groupDefinition['isFullCoverage'] bool Whether the group is full coverage;
143 * if true, this means that checking every item in the group means no
144 * changes list entries are filtered out.
145 * * $groupDefinition['whatsThisHeader'] string i18n key for header of "What's
146 * This" popup (optional).
147 * * $groupDefinition['whatsThisBody'] string i18n key for body of "What's This"
148 * popup (optional).
149 * * $groupDefinition['whatsThisUrl'] string URL for main link of "What's This"
150 * popup (optional).
151 * * $groupDefinition['whatsThisLinkText'] string i18n key of text for main link of
152 * "What's This" popup (optional).
154 public function __construct( array $groupDefinition ) {
155 if ( strpos( $groupDefinition['name'], self::RESERVED_NAME_CHAR ) !== false ) {
156 throw new MWException( 'Group names may not contain \'' .
157 self::RESERVED_NAME_CHAR .
158 '\'. Use the naming convention: \'camelCase\''
162 $this->name = $groupDefinition['name'];
164 if ( isset( $groupDefinition['title'] ) ) {
165 $this->title = $groupDefinition['title'];
168 if ( isset( $groupDefinition['whatsThisHeader'] ) ) {
169 $this->whatsThisHeader = $groupDefinition['whatsThisHeader'];
170 $this->whatsThisBody = $groupDefinition['whatsThisBody'];
171 $this->whatsThisUrl = $groupDefinition['whatsThisUrl'];
172 $this->whatsThisLinkText = $groupDefinition['whatsThisLinkText'];
175 $this->type = $groupDefinition['type'];
176 if ( isset( $groupDefinition['priority'] ) ) {
177 $this->priority = $groupDefinition['priority'];
178 } else {
179 $this->priority = self::DEFAULT_PRIORITY;
182 $this->isFullCoverage = $groupDefinition['isFullCoverage'];
184 $this->filters = [];
185 $lowestSpecifiedPriority = -1;
186 foreach ( $groupDefinition['filters'] as $filterDefinition ) {
187 if ( isset( $filterDefinition['priority'] ) ) {
188 $lowestSpecifiedPriority = min( $lowestSpecifiedPriority, $filterDefinition['priority'] );
192 // Convenience feature: If you specify a group (and its filters) all in
193 // one place, you don't have to specify priority. You can just put them
194 // in order. However, if you later add one (e.g. an extension adds a filter
195 // to a core-defined group), you need to specify it.
196 $autoFillPriority = $lowestSpecifiedPriority - 1;
197 foreach ( $groupDefinition['filters'] as $filterDefinition ) {
198 if ( !isset( $filterDefinition['priority'] ) ) {
199 $filterDefinition['priority'] = $autoFillPriority;
200 $autoFillPriority--;
202 $filterDefinition['group'] = $this;
204 $filter = $this->createFilter( $filterDefinition );
205 $this->registerFilter( $filter );
210 * Creates a filter of the appropriate type for this group, from the definition
212 * @param array $filterDefinition Filter definition
213 * @return ChangesListFilter Filter
215 abstract protected function createFilter( array $filterDefinition );
218 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
220 * WARNING: This means there is a conflict when both things are *shown*
221 * (not filtered out), even for the hide-based filters. So e.g. conflicting with
222 * 'hideanons' means there is a conflict if only anonymous users are *shown*.
224 * @param ChangesListFilterGroup|ChangesListFilter $other Other
225 * ChangesListFilterGroup or ChangesListFilter
226 * @param string $globalKey i18n key for top-level conflict message
227 * @param string $forwardKey i18n key for conflict message in this
228 * direction (when in UI context of $this object)
229 * @param string $backwardKey i18n key for conflict message in reverse
230 * direction (when in UI context of $other object)
232 public function conflictsWith( $other, $globalKey, $forwardKey, $backwardKey ) {
233 if ( $globalKey === null || $forwardKey === null || $backwardKey === null ) {
234 throw new MWException( 'All messages must be specified' );
237 $this->setUnidirectionalConflict(
238 $other,
239 $globalKey,
240 $forwardKey
243 $other->setUnidirectionalConflict(
244 $this,
245 $globalKey,
246 $backwardKey
251 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with
252 * this object.
254 * Internal use ONLY.
256 * @param ChangesListFilterGroup|ChangesListFilter $other Other
257 * ChangesListFilterGroup or ChangesListFilter
258 * @param string $globalDescription i18n key for top-level conflict message
259 * @param string $contextDescription i18n key for conflict message in this
260 * direction (when in UI context of $this object)
262 public function setUnidirectionalConflict( $other, $globalDescription, $contextDescription ) {
263 if ( $other instanceof ChangesListFilterGroup ) {
264 $this->conflictingGroups[] = [
265 'group' => $other->getName(),
266 'groupObject' => $other,
267 'globalDescription' => $globalDescription,
268 'contextDescription' => $contextDescription,
270 } elseif ( $other instanceof ChangesListFilter ) {
271 $this->conflictingFilters[] = [
272 'group' => $other->getGroup()->getName(),
273 'filter' => $other->getName(),
274 'filterObject' => $other,
275 'globalDescription' => $globalDescription,
276 'contextDescription' => $contextDescription,
278 } else {
279 throw new MWException( 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' );
284 * @return string Internal name
286 public function getName() {
287 return $this->name;
291 * @return string i18n key for title
293 public function getTitle() {
294 return $this->title;
298 * @return string Type (TYPE constant from a subclass)
300 public function getType() {
301 return $this->type;
305 * @return int Priority. Higher means higher in the group list
307 public function getPriority() {
308 return $this->priority;
312 * @return ChangesListFilter[] Associative array of ChangesListFilter objects, with
313 * filter name as key
315 public function getFilters() {
316 return $this->filters;
320 * Get filter by name
322 * @param string $name Filter name
323 * @return ChangesListFilter|null Specified filter, or null if it is not registered
325 public function getFilter( $name ) {
326 return isset( $this->filters[$name] ) ? $this->filters[$name] : null;
330 * Check whether the URL parameter is for the group, or for individual filters.
331 * Defaults can also be defined on the group if and only if this is true.
333 * @return bool True if and only if the URL parameter is per-group
335 abstract public function isPerGroupRequestParameter();
338 * Gets the JS data in the format required by the front-end of the structured UI
340 * @return array|null Associative array, or null if there are no filters that
341 * display in the structured UI. messageKeys is a special top-level value, with
342 * the value being an array of the message keys to send to the client.
344 public function getJsData() {
345 $output = [
346 'name' => $this->name,
347 'type' => $this->type,
348 'fullCoverage' => $this->isFullCoverage,
349 'filters' => [],
350 'priority' => $this->priority,
351 'conflicts' => [],
352 'messageKeys' => [ $this->title ]
355 if ( isset( $this->whatsThisHeader ) ) {
356 $output['whatsThisHeader'] = $this->whatsThisHeader;
357 $output['whatsThisBody'] = $this->whatsThisBody;
358 $output['whatsThisUrl'] = $this->whatsThisUrl;
359 $output['whatsThisLinkText'] = $this->whatsThisLinkText;
361 array_push(
362 $output['messageKeys'],
363 $output['whatsThisHeader'],
364 $output['whatsThisBody'],
365 $output['whatsThisLinkText']
369 usort( $this->filters, function ( $a, $b ) {
370 return $b->getPriority() - $a->getPriority();
371 } );
373 foreach ( $this->filters as $filterName => $filter ) {
374 if ( $filter->displaysOnStructuredUi() ) {
375 $filterData = $filter->getJsData();
376 $output['messageKeys'] = array_merge(
377 $output['messageKeys'],
378 $filterData['messageKeys']
380 unset( $filterData['messageKeys'] );
381 $output['filters'][] = $filterData;
385 if ( count( $output['filters'] ) === 0 ) {
386 return null;
389 $output['title'] = $this->title;
391 $conflicts = array_merge(
392 $this->conflictingGroups,
393 $this->conflictingFilters
396 foreach ( $conflicts as $conflictInfo ) {
397 $output['conflicts'][] = $conflictInfo;
398 unset( $conflictInfo['filterObject'] );
399 unset( $conflictInfo['groupObject'] );
400 array_push(
401 $output['messageKeys'],
402 $conflictInfo['globalDescription'],
403 $conflictInfo['contextDescription']
407 return $output;
411 * Get groups conflicting with this filter group
413 * @return ChangesListFilterGroup[]
415 public function getConflictingGroups() {
416 return array_map(
417 function ( $conflictDesc ) {
418 return $conflictDesc[ 'groupObject' ];
420 $this->conflictingGroups
425 * Get filters conflicting with this filter group
427 * @return ChangesListFilter[]
429 public function getConflictingFilters() {
430 return array_map(
431 function ( $conflictDesc ) {
432 return $conflictDesc[ 'filterObject' ];
434 $this->conflictingFilters
439 * Check if any filter in this group is selected
441 * @param FormOptions $opts
442 * @return bool
444 public function anySelected( FormOptions $opts ) {
445 return !!count( array_filter(
446 $this->getFilters(),
447 function ( ChangesListFilter $filter ) use ( $opts ) {
448 return $filter->isSelected( $opts );
450 ) );