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
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.
31 * Represents a filter group (used on ChangesListSpecialPage and descendants)
35 abstract class ChangesListFilterGroup
{
37 * Name (internal identifier)
51 * i18n key for header of What's This?
53 * @var string|null $whatsThisHeader
55 protected $whatsThisHeader;
58 * i18n key for body of What's This?
60 * @var string|null $whatsThisBody
62 protected $whatsThisBody;
65 * URL of What's This? link
67 * @var string|null $whatsThisUrl
69 protected $whatsThisUrl;
72 * i18n key for What's This? link
74 * @var string|null $whatsThisLinkText
76 protected $whatsThisLinkText;
79 * Type, from a TYPE constant of a subclass
86 * Priority integer. Higher values means higher up in the
89 * @var string $priority
94 * Associative array of filters, as ChangesListFilter objects, with filter name as key
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"
149 * * $groupDefinition['whatsThisUrl'] string URL for main link of "What's This"
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'];
179 $this->priority
= self
::DEFAULT_PRIORITY
;
182 $this->isFullCoverage
= $groupDefinition['isFullCoverage'];
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;
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,
235 if ( $globalKey === null ||
$forwardKey === null ||
236 $backwardKey === null ) {
238 throw new MWException( 'All messages must be specified' );
241 $this->setUnidirectionalConflict(
247 $other->setUnidirectionalConflict(
255 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with
260 * @param ChangesListFilterGroup|ChangesListFilter $other Other
261 * ChangesListFilterGroup or ChangesListFilter
262 * @param string $globalDescription i18n key for top-level conflict message
263 * @param string $contextDescription i18n key for conflict message in this
264 * direction (when in UI context of $this object)
266 public function setUnidirectionalConflict( $other, $globalDescription,
267 $contextDescription ) {
269 if ( $other instanceof ChangesListFilterGroup
) {
270 $this->conflictingGroups
[] = [
271 'group' => $other->getName(),
272 'groupObject' => $other,
273 'globalDescription' => $globalDescription,
274 'contextDescription' => $contextDescription,
276 } elseif ( $other instanceof ChangesListFilter
) {
277 $this->conflictingFilters
[] = [
278 'group' => $other->getGroup()->getName(),
279 'filter' => $other->getName(),
280 'filterObject' => $other,
281 'globalDescription' => $globalDescription,
282 'contextDescription' => $contextDescription,
285 throw new MWException( 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' );
290 * @return string Internal name
292 public function getName() {
297 * @return string i18n key for title
299 public function getTitle() {
304 * @return string Type (TYPE constant from a subclass)
306 public function getType() {
311 * @return int Priority. Higher means higher in the group list
313 public function getPriority() {
314 return $this->priority
;
318 * @return ChangesListFilter[] Associative array of ChangesListFilter objects, with
321 public function getFilters() {
322 return $this->filters
;
328 * @param string $name Filter name
329 * @return ChangesListFilter|null Specified filter, or null if it is not registered
331 public function getFilter( $name ) {
332 return isset( $this->filters
[$name] ) ?
$this->filters
[$name] : null;
336 * Check whether the URL parameter is for the group, or for individual filters.
337 * Defaults can also be defined on the group if and only if this is true.
339 * @return bool True if and only if the URL parameter is per-group
341 abstract public function isPerGroupRequestParameter();
344 * Gets the JS data in the format required by the front-end of the structured UI
346 * @return array|null Associative array, or null if there are no filters that
347 * display in the structured UI. messageKeys is a special top-level value, with
348 * the value being an array of the message keys to send to the client.
350 public function getJsData() {
352 'name' => $this->name
,
353 'type' => $this->type
,
354 'fullCoverage' => $this->isFullCoverage
,
356 'priority' => $this->priority
,
358 'messageKeys' => [ $this->title
]
361 if ( isset ( $this->whatsThisHeader
) ) {
362 $output['whatsThisHeader'] = $this->whatsThisHeader
;
363 $output['whatsThisBody'] = $this->whatsThisBody
;
364 $output['whatsThisUrl'] = $this->whatsThisUrl
;
365 $output['whatsThisLinkText'] = $this->whatsThisLinkText
;
368 $output['messageKeys'],
369 $output['whatsThisHeader'],
370 $output['whatsThisBody'],
371 $output['whatsThisLinkText']
375 usort( $this->filters
, function ( $a, $b ) {
376 return $b->getPriority() - $a->getPriority();
379 foreach ( $this->filters
as $filterName => $filter ) {
380 if ( $filter->displaysOnStructuredUi() ) {
381 $filterData = $filter->getJsData();
382 $output['messageKeys'] = array_merge(
383 $output['messageKeys'],
384 $filterData['messageKeys']
386 unset( $filterData['messageKeys'] );
387 $output['filters'][] = $filterData;
391 if ( count( $output['filters'] ) === 0 ) {
395 $output['title'] = $this->title
;
397 $conflicts = array_merge(
398 $this->conflictingGroups
,
399 $this->conflictingFilters
402 foreach ( $conflicts as $conflictInfo ) {
403 $output['conflicts'][] = $conflictInfo;
404 unset( $conflictInfo['filterObject'] );
405 unset( $conflictInfo['groupObject'] );
407 $output['messageKeys'],
408 $conflictInfo['globalDescription'],
409 $conflictInfo['contextDescription']
417 * Get groups conflicting with this filter group
419 * @return ChangesListFilterGroup[]
421 public function getConflictingGroups() {
423 function ( $conflictDesc ) {
424 return $conflictDesc[ 'groupObject' ];
426 $this->conflictingGroups
431 * Get filters conflicting with this filter group
433 * @return ChangesListFilter[]
435 public function getConflictingFilters() {
437 function ( $conflictDesc ) {
438 return $conflictDesc[ 'filterObject' ];
440 $this->conflictingFilters
445 * Check if any filter in this group is selected
447 * @param FormOptions $opts
450 public function anySelected( FormOptions
$opts ) {
451 return !!count( array_filter(
453 function ( ChangesListFilter
$filter ) use ( $opts ) {
454 return $filter->isSelected( $opts );