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 * List of conflicting groups
111 * @var array $conflictingGroups Array of associative arrays with conflict
112 * information. See setUnidirectionalConflict
114 protected $conflictingGroups = [];
117 * List of conflicting filters
119 * @var array $conflictingFilters Array of associative arrays with conflict
120 * information. See setUnidirectionalConflict
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.
146 public function __construct( array $groupDefinition ) {
147 if ( strpos( $groupDefinition['name'], self
::RESERVED_NAME_CHAR
) !== false ) {
148 throw new MWException( 'Group names may not contain \'' .
149 self
::RESERVED_NAME_CHAR
.
150 '\'. Use the naming convention: \'camelCase\''
154 $this->name
= $groupDefinition['name'];
156 if ( isset( $groupDefinition['title'] ) ) {
157 $this->title
= $groupDefinition['title'];
160 if ( isset ( $groupDefinition['whatsThisHeader'] ) ) {
161 $this->whatsThisHeader
= $groupDefinition['whatsThisHeader'];
162 $this->whatsThisBody
= $groupDefinition['whatsThisBody'];
163 $this->whatsThisUrl
= $groupDefinition['whatsThisUrl'];
164 $this->whatsThisLinkText
= $groupDefinition['whatsThisLinkText'];
167 $this->type
= $groupDefinition['type'];
168 if ( isset( $groupDefinition['priority'] ) ) {
169 $this->priority
= $groupDefinition['priority'];
171 $this->priority
= self
::DEFAULT_PRIORITY
;
174 $this->isFullCoverage
= $groupDefinition['isFullCoverage'];
177 $lowestSpecifiedPriority = -1;
178 foreach ( $groupDefinition['filters'] as $filterDefinition ) {
179 if ( isset( $filterDefinition['priority'] ) ) {
180 $lowestSpecifiedPriority = min( $lowestSpecifiedPriority, $filterDefinition['priority'] );
184 // Convenience feature: If you specify a group (and its filters) all in
185 // one place, you don't have to specify priority. You can just put them
186 // in order. However, if you later add one (e.g. an extension adds a filter
187 // to a core-defined group), you need to specify it.
188 $autoFillPriority = $lowestSpecifiedPriority - 1;
189 foreach ( $groupDefinition['filters'] as $filterDefinition ) {
190 if ( !isset( $filterDefinition['priority'] ) ) {
191 $filterDefinition['priority'] = $autoFillPriority;
194 $filterDefinition['group'] = $this;
196 $filter = $this->createFilter( $filterDefinition );
197 $this->registerFilter( $filter );
202 * Creates a filter of the appropriate type for this group, from the definition
204 * @param array $filterDefinition Filter definition
205 * @return ChangesListFilter Filter
207 abstract protected function createFilter( array $filterDefinition );
210 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
212 * WARNING: This means there is a conflict when both things are *shown*
213 * (not filtered out), even for the hide-based filters. So e.g. conflicting with
214 * 'hideanons' means there is a conflict if only anonymous users are *shown*.
216 * @param ChangesListFilterGroup|ChangesListFilter $other Other
217 * ChangesListFilterGroup or ChangesListFilter
218 * @param string $globalKey i18n key for top-level conflict message
219 * @param string $forwardKey i18n key for conflict message in this
220 * direction (when in UI context of $this object)
221 * @param string $backwardKey i18n key for conflict message in reverse
222 * direction (when in UI context of $other object)
224 public function conflictsWith( $other, $globalKey, $forwardKey,
227 if ( $globalKey === null ||
$forwardKey === null ||
228 $backwardKey === null ) {
230 throw new MWException( 'All messages must be specified' );
233 $this->setUnidirectionalConflict(
239 $other->setUnidirectionalConflict(
247 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with
252 * @param ChangesListFilterGroup|ChangesListFilter $other Other
253 * ChangesListFilterGroup or ChangesListFilter
254 * @param string $globalDescription i18n key for top-level conflict message
255 * @param string $contextDescription i18n key for conflict message in this
256 * direction (when in UI context of $this object)
258 public function setUnidirectionalConflict( $other, $globalDescription,
259 $contextDescription ) {
261 if ( $other instanceof ChangesListFilterGroup
) {
262 $this->conflictingGroups
[] = [
263 'group' => $other->getName(),
264 'globalDescription' => $globalDescription,
265 'contextDescription' => $contextDescription,
267 } elseif ( $other instanceof ChangesListFilter
) {
268 $this->conflictingFilters
[] = [
269 'group' => $other->getGroup()->getName(),
270 'filter' => $other->getName(),
271 'globalDescription' => $globalDescription,
272 'contextDescription' => $contextDescription,
275 throw new MWException( 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' );
280 * @return string Internal name
282 public function getName() {
287 * @return string i18n key for title
289 public function getTitle() {
294 * @return string Type (TYPE constant from a subclass)
296 public function getType() {
301 * @return int Priority. Higher means higher in the group list
303 public function getPriority() {
304 return $this->priority
;
308 * @return array Associative array of ChangesListFilter objects, with filter name as key
310 public function getFilters() {
311 return $this->filters
;
317 * @param string $name Filter name
318 * @return ChangesListFilter Specified filter
320 public function getFilter( $name ) {
321 return $this->filters
[$name];
325 * Check whether the URL parameter is for the group, or for individual filters.
326 * Defaults can also be defined on the group if and only if this is true.
328 * @return bool True if and only if the URL parameter is per-group
330 abstract public function isPerGroupRequestParameter();
333 * Gets the JS data in the format required by the front-end of the structured UI
335 * @param ChangesListSpecialPage $specialPage
336 * @return array|null Associative array, or null if there are no filters that
337 * display in the structured UI. messageKeys is a special top-level value, with
338 * the value being an array of the message keys to send to the client.
340 public function getJsData( ChangesListSpecialPage
$specialPage ) {
342 'name' => $this->name
,
343 'type' => $this->type
,
344 'fullCoverage' => $this->isFullCoverage
,
346 'priority' => $this->priority
,
348 'messageKeys' => [ $this->title
]
351 if ( isset ( $this->whatsThisHeader
) ) {
352 $output['whatsThisHeader'] = $this->whatsThisHeader
;
353 $output['whatsThisBody'] = $this->whatsThisBody
;
354 $output['whatsThisUrl'] = $this->whatsThisUrl
;
355 $output['whatsThisLinkText'] = $this->whatsThisLinkText
;
358 $output['messageKeys'],
359 $output['whatsThisHeader'],
360 $output['whatsThisBody'],
361 $output['whatsThisLinkText']
365 usort( $this->filters
, function ( $a, $b ) {
366 return $b->getPriority() - $a->getPriority();
369 foreach ( $this->filters
as $filterName => $filter ) {
370 if ( $filter->displaysOnStructuredUi( $specialPage ) ) {
371 $filterData = $filter->getJsData();
372 $output['messageKeys'] = array_merge(
373 $output['messageKeys'],
374 $filterData['messageKeys']
376 unset( $filterData['messageKeys'] );
377 $output['filters'][] = $filterData;
381 if ( count( $output['filters'] ) === 0 ) {
385 $output['title'] = $this->title
;
387 $conflicts = array_merge(
388 $this->conflictingGroups
,
389 $this->conflictingFilters
392 foreach ( $conflicts as $conflictInfo ) {
393 $output['conflicts'][] = $conflictInfo;
395 $output['messageKeys'],
396 $conflictInfo['globalDescription'],
397 $conflictInfo['contextDescription']