Merge "RC Filters: watch filter highlight"
[lhc/web/wiklou.git] / includes / changes / ChangesListFilterGroup.php
1 <?php
2 /**
3 * Represents a filter group (used on ChangesListSpecialPage and descendants)
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @license GPL 2+
22 * @author Matthew Flaschen
23 */
24
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.
29
30 /**
31 * Represents a filter group (used on ChangesListSpecialPage and descendants)
32 *
33 * @since 1.29
34 */
35 abstract class ChangesListFilterGroup {
36 /**
37 * Name (internal identifier)
38 *
39 * @var string $name
40 */
41 protected $name;
42
43 /**
44 * i18n key for title
45 *
46 * @var string $title
47 */
48 protected $title;
49
50 /**
51 * i18n key for header of What's This?
52 *
53 * @var string|null $whatsThisHeader
54 */
55 protected $whatsThisHeader;
56
57 /**
58 * i18n key for body of What's This?
59 *
60 * @var string|null $whatsThisBody
61 */
62 protected $whatsThisBody;
63
64 /**
65 * URL of What's This? link
66 *
67 * @var string|null $whatsThisUrl
68 */
69 protected $whatsThisUrl;
70
71 /**
72 * i18n key for What's This? link
73 *
74 * @var string|null $whatsThisLinkText
75 */
76 protected $whatsThisLinkText;
77
78 /**
79 * Type, from a TYPE constant of a subclass
80 *
81 * @var string $type
82 */
83 protected $type;
84
85 /**
86 * Priority integer. Higher values means higher up in the
87 * group list.
88 *
89 * @var string $priority
90 */
91 protected $priority;
92
93 /**
94 * Associative array of filters, as ChangesListFilter objects, with filter name as key
95 *
96 * @var array $filters
97 */
98 protected $filters;
99
100 /**
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.
103 *
104 * @var bool $isFullCoverage
105 */
106 protected $isFullCoverage;
107
108 /**
109 * List of conflicting groups
110 *
111 * @var array $conflictingGroups Array of associative arrays with conflict
112 * information. See setUnidirectionalConflict
113 */
114 protected $conflictingGroups = [];
115
116 /**
117 * List of conflicting filters
118 *
119 * @var array $conflictingFilters Array of associative arrays with conflict
120 * information. See setUnidirectionalConflict
121 */
122 protected $conflictingFilters = [];
123
124 const DEFAULT_PRIORITY = -100;
125
126 const RESERVED_NAME_CHAR = '_';
127
128 /**
129 * Create a new filter group with the specified configuration
130 *
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).
153 */
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\''
159 );
160 }
161
162 $this->name = $groupDefinition['name'];
163
164 if ( isset( $groupDefinition['title'] ) ) {
165 $this->title = $groupDefinition['title'];
166 }
167
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'];
173 }
174
175 $this->type = $groupDefinition['type'];
176 if ( isset( $groupDefinition['priority'] ) ) {
177 $this->priority = $groupDefinition['priority'];
178 } else {
179 $this->priority = self::DEFAULT_PRIORITY;
180 }
181
182 $this->isFullCoverage = $groupDefinition['isFullCoverage'];
183
184 $this->filters = [];
185 $lowestSpecifiedPriority = -1;
186 foreach ( $groupDefinition['filters'] as $filterDefinition ) {
187 if ( isset( $filterDefinition['priority'] ) ) {
188 $lowestSpecifiedPriority = min( $lowestSpecifiedPriority, $filterDefinition['priority'] );
189 }
190 }
191
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--;
201 }
202 $filterDefinition['group'] = $this;
203
204 $filter = $this->createFilter( $filterDefinition );
205 $this->registerFilter( $filter );
206 }
207 }
208
209 /**
210 * Creates a filter of the appropriate type for this group, from the definition
211 *
212 * @param array $filterDefinition Filter definition
213 * @return ChangesListFilter Filter
214 */
215 abstract protected function createFilter( array $filterDefinition );
216
217 /**
218 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
219 *
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*.
223 *
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)
231 */
232 public function conflictsWith( $other, $globalKey, $forwardKey,
233 $backwardKey ) {
234
235 if ( $globalKey === null || $forwardKey === null ||
236 $backwardKey === null ) {
237
238 throw new MWException( 'All messages must be specified' );
239 }
240
241 $this->setUnidirectionalConflict(
242 $other,
243 $globalKey,
244 $forwardKey
245 );
246
247 $other->setUnidirectionalConflict(
248 $this,
249 $globalKey,
250 $backwardKey
251 );
252 }
253
254 /**
255 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with
256 * this object.
257 *
258 * Internal use ONLY.
259 *
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)
265 */
266 public function setUnidirectionalConflict( $other, $globalDescription,
267 $contextDescription ) {
268
269 if ( $other instanceof ChangesListFilterGroup ) {
270 $this->conflictingGroups[] = [
271 'group' => $other->getName(),
272 'groupObject' => $other,
273 'globalDescription' => $globalDescription,
274 'contextDescription' => $contextDescription,
275 ];
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,
283 ];
284 } else {
285 throw new MWException( 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' );
286 }
287 }
288
289 /**
290 * @return string Internal name
291 */
292 public function getName() {
293 return $this->name;
294 }
295
296 /**
297 * @return string i18n key for title
298 */
299 public function getTitle() {
300 return $this->title;
301 }
302
303 /**
304 * @return string Type (TYPE constant from a subclass)
305 */
306 public function getType() {
307 return $this->type;
308 }
309
310 /**
311 * @return int Priority. Higher means higher in the group list
312 */
313 public function getPriority() {
314 return $this->priority;
315 }
316
317 /**
318 * @return array Associative array of ChangesListFilter objects, with filter name as key
319 */
320 public function getFilters() {
321 return $this->filters;
322 }
323
324 /**
325 * Get filter by name
326 *
327 * @param string $name Filter name
328 * @return ChangesListFilter|null Specified filter, or null if it is not registered
329 */
330 public function getFilter( $name ) {
331 return isset( $this->filters[$name] ) ? $this->filters[$name] : null;
332 }
333
334 /**
335 * Check whether the URL parameter is for the group, or for individual filters.
336 * Defaults can also be defined on the group if and only if this is true.
337 *
338 * @return bool True if and only if the URL parameter is per-group
339 */
340 abstract public function isPerGroupRequestParameter();
341
342 /**
343 * Gets the JS data in the format required by the front-end of the structured UI
344 *
345 * @return array|null Associative array, or null if there are no filters that
346 * display in the structured UI. messageKeys is a special top-level value, with
347 * the value being an array of the message keys to send to the client.
348 */
349 public function getJsData() {
350 $output = [
351 'name' => $this->name,
352 'type' => $this->type,
353 'fullCoverage' => $this->isFullCoverage,
354 'filters' => [],
355 'priority' => $this->priority,
356 'conflicts' => [],
357 'messageKeys' => [ $this->title ]
358 ];
359
360 if ( isset ( $this->whatsThisHeader ) ) {
361 $output['whatsThisHeader'] = $this->whatsThisHeader;
362 $output['whatsThisBody'] = $this->whatsThisBody;
363 $output['whatsThisUrl'] = $this->whatsThisUrl;
364 $output['whatsThisLinkText'] = $this->whatsThisLinkText;
365
366 array_push(
367 $output['messageKeys'],
368 $output['whatsThisHeader'],
369 $output['whatsThisBody'],
370 $output['whatsThisLinkText']
371 );
372 }
373
374 usort( $this->filters, function ( $a, $b ) {
375 return $b->getPriority() - $a->getPriority();
376 } );
377
378 foreach ( $this->filters as $filterName => $filter ) {
379 if ( $filter->displaysOnStructuredUi() ) {
380 $filterData = $filter->getJsData();
381 $output['messageKeys'] = array_merge(
382 $output['messageKeys'],
383 $filterData['messageKeys']
384 );
385 unset( $filterData['messageKeys'] );
386 $output['filters'][] = $filterData;
387 }
388 }
389
390 if ( count( $output['filters'] ) === 0 ) {
391 return null;
392 }
393
394 $output['title'] = $this->title;
395
396 $conflicts = array_merge(
397 $this->conflictingGroups,
398 $this->conflictingFilters
399 );
400
401 foreach ( $conflicts as $conflictInfo ) {
402 $output['conflicts'][] = $conflictInfo;
403 unset( $conflictInfo['filterObject'] );
404 unset( $conflictInfo['groupObject'] );
405 array_push(
406 $output['messageKeys'],
407 $conflictInfo['globalDescription'],
408 $conflictInfo['contextDescription']
409 );
410 }
411
412 return $output;
413 }
414
415 /**
416 * Get groups conflicting with this filter group
417 *
418 * @return ChangesListFilterGroup[]
419 */
420 public function getConflictingGroups() {
421 return array_map(
422 function ( $conflictDesc ) {
423 return $conflictDesc[ 'groupObject' ];
424 },
425 $this->conflictingGroups
426 );
427 }
428
429 /**
430 * Get filters conflicting with this filter group
431 *
432 * @return ChangesListFilter[]
433 */
434 public function getConflictingFilters() {
435 return array_map(
436 function ( $conflictDesc ) {
437 return $conflictDesc[ 'filterObject' ];
438 },
439 $this->conflictingFilters
440 );
441 }
442
443 /**
444 * Check if any filter in this group is selected
445 *
446 * @param FormOptions $opts
447 * @return bool
448 */
449 public function anySelected( FormOptions $opts ) {
450 return !!count( array_filter(
451 $this->getFilters(),
452 function ( ChangesListFilter $filter ) use ( $opts ) {
453 return $filter->isSelected( $opts );
454 }
455 ) );
456 }
457 }