Merge "Rephrase enotif_lastdiff and enotif_lastvisited"
[lhc/web/wiklou.git] / includes / changes / ChangesListFilter.php
1 <?php
2 /**
3 * Represents a filter (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 /**
26 * Represents a filter (used on ChangesListSpecialPage and descendants)
27 *
28 * @since 1.29
29 */
30 abstract class ChangesListFilter {
31 /**
32 * Filter name
33 *
34 * @var string $name
35 */
36 protected $name;
37
38 /**
39 * CSS class suffix used for attribution, e.g. 'bot'.
40 *
41 * In this example, if bot actions are included in the result set, this CSS class
42 * will then be included in all bot-flagged actions.
43 *
44 * @var string|null $cssClassSuffix
45 */
46 protected $cssClassSuffix;
47
48 /**
49 * Callable that returns true if and only if a row is attributed to this filter
50 *
51 * @var callable $isRowApplicableCallable
52 */
53 protected $isRowApplicableCallable;
54
55 /**
56 * Group. ChangesListFilterGroup this belongs to
57 *
58 * @var ChangesListFilterGroup $group
59 */
60 protected $group;
61
62 /**
63 * i18n key of label for structured UI
64 *
65 * @var string $label
66 */
67 protected $label;
68
69 /**
70 * i18n key of description for structured UI
71 *
72 * @var string $description
73 */
74 protected $description;
75
76 /**
77 * List of conflicting groups
78 *
79 * @var array $conflictingGroups Array of associative arrays with conflict
80 * information. See setUnidirectionalConflict
81 */
82 protected $conflictingGroups = [];
83
84 /**
85 * List of conflicting filters
86 *
87 * @var array $conflictingFilters Array of associative arrays with conflict
88 * information. See setUnidirectionalConflict
89 */
90 protected $conflictingFilters = [];
91
92 /**
93 * List of filters that are a subset of the current filter
94 *
95 * @var array $subsetFilters Array of associative arrays with subset information
96 */
97 protected $subsetFilters = [];
98
99 /**
100 * Priority integer. Higher value means higher up in the group's filter list.
101 *
102 * @var string $priority
103 */
104 protected $priority;
105
106 const RESERVED_NAME_CHAR = '_';
107
108 /**
109 * Creates a new filter with the specified configuration, and registers it to the
110 * specified group.
111 *
112 * It infers which UI (it can be either or both) to display the filter on based on
113 * which messages are provided.
114 *
115 * If 'label' is provided, it will be displayed on the structured UI. Thus,
116 * 'label', 'description', and sub-class parameters are optional depending on which
117 * UI it's for.
118 *
119 * @param array $filterDefinition ChangesListFilter definition
120 *
121 * $filterDefinition['name'] string Name of filter; use lowercase with no
122 * punctuation
123 * $filterDefinition['cssClassSuffix'] string CSS class suffix, used to mark
124 * that a particular row belongs to this filter (when a row is included by the
125 * filter) (optional)
126 * $filterDefinition['isRowApplicableCallable'] Callable taking two parameters, the
127 * IContextSource, and the RecentChange object for the row, and returning true if
128 * the row is attributed to this filter. The above CSS class will then be
129 * automatically added (optional, required if cssClassSuffix is used).
130 * $filterDefinition['group'] ChangesListFilterGroup Group. Filter group this
131 * belongs to.
132 * $filterDefinition['label'] string i18n key of label for structured UI.
133 * $filterDefinition['description'] string i18n key of description for structured
134 * UI.
135 * $filterDefinition['priority'] int Priority integer. Higher value means higher
136 * up in the group's filter list.
137 */
138 public function __construct( array $filterDefinition ) {
139 if ( isset( $filterDefinition['group'] ) ) {
140 $this->group = $filterDefinition['group'];
141 } else {
142 throw new MWException( 'You must use \'group\' to specify the ' .
143 'ChangesListFilterGroup this filter belongs to' );
144 }
145
146 if ( strpos( $filterDefinition['name'], self::RESERVED_NAME_CHAR ) !== false ) {
147 throw new MWException( 'Filter names may not contain \'' .
148 self::RESERVED_NAME_CHAR .
149 '\'. Use the naming convention: \'lowercase\''
150 );
151 }
152
153 if ( $this->group->getFilter( $filterDefinition['name'] ) ) {
154 throw new MWException( 'Two filters in a group cannot have the ' .
155 "same name: '{$filterDefinition['name']}'" );
156 }
157
158 $this->name = $filterDefinition['name'];
159
160 if ( isset( $filterDefinition['cssClassSuffix'] ) ) {
161 $this->cssClassSuffix = $filterDefinition['cssClassSuffix'];
162 $this->isRowApplicableCallable = $filterDefinition['isRowApplicableCallable'];
163 }
164
165 if ( isset( $filterDefinition['label'] ) ) {
166 $this->label = $filterDefinition['label'];
167 $this->description = $filterDefinition['description'];
168 }
169
170 $this->priority = $filterDefinition['priority'];
171
172 $this->group->registerFilter( $this );
173 }
174
175 /**
176 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
177 *
178 * WARNING: This means there is a conflict when both things are *shown*
179 * (not filtered out), even for the hide-based filters. So e.g. conflicting with
180 * 'hideanons' means there is a conflict if only anonymous users are *shown*.
181 *
182 * @param ChangesListFilterGroup|ChangesListFilter $other Other
183 * ChangesListFilterGroup or ChangesListFilter
184 * @param string $globalKey i18n key for top-level conflict message
185 * @param string $forwardKey i18n key for conflict message in this
186 * direction (when in UI context of $this object)
187 * @param string $backwardKey i18n key for conflict message in reverse
188 * direction (when in UI context of $other object)
189 */
190 public function conflictsWith( $other, $globalKey, $forwardKey,
191 $backwardKey ) {
192
193 if ( $globalKey === null || $forwardKey === null ||
194 $backwardKey === null ) {
195
196 throw new MWException( 'All messages must be specified' );
197 }
198
199 $this->setUnidirectionalConflict(
200 $other,
201 $globalKey,
202 $forwardKey
203 );
204
205 $other->setUnidirectionalConflict(
206 $this,
207 $globalKey,
208 $backwardKey
209 );
210 }
211
212 /**
213 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with
214 * this object.
215 *
216 * Internal use ONLY.
217 *
218 * @param ChangesListFilterGroup|ChangesListFilter $other Other
219 * ChangesListFilterGroup or ChangesListFilter
220 * @param string $globalDescription i18n key for top-level conflict message
221 * @param string $contextDescription i18n key for conflict message in this
222 * direction (when in UI context of $this object)
223 */
224 public function setUnidirectionalConflict( $other, $globalDescription,
225 $contextDescription ) {
226
227 if ( $other instanceof ChangesListFilterGroup ) {
228 $this->conflictingGroups[] = [
229 'group' => $other->getName(),
230 'groupObject' => $other,
231 'globalDescription' => $globalDescription,
232 'contextDescription' => $contextDescription,
233 ];
234 } elseif ( $other instanceof ChangesListFilter ) {
235 $this->conflictingFilters[] = [
236 'group' => $other->getGroup()->getName(),
237 'filter' => $other->getName(),
238 'filterObject' => $other,
239 'globalDescription' => $globalDescription,
240 'contextDescription' => $contextDescription,
241 ];
242 } else {
243 throw new MWException( 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' );
244 }
245 }
246
247 /**
248 * Marks that the current instance is (also) a superset of the filter passed in.
249 * This can be called more than once.
250 *
251 * This means that anything in the results for the other filter is also in the
252 * results for this one.
253 *
254 * @param ChangesListFilter The filter the current instance is a superset of
255 */
256 public function setAsSupersetOf( ChangesListFilter $other ) {
257 if ( $other->getGroup() !== $this->getGroup() ) {
258 throw new MWException( 'Supersets can only be defined for filters in the same group' );
259 }
260
261 $this->subsetFilters[] = [
262 // It's always the same group, but this makes the representation
263 // more consistent with conflicts.
264 'group' => $other->getGroup()->getName(),
265 'filter' => $other->getName(),
266 ];
267 }
268
269 /**
270 * @return string Name, e.g. hideanons
271 */
272 public function getName() {
273 return $this->name;
274 }
275
276 /**
277 * @return ChangesListFilterGroup Group this belongs to
278 */
279 public function getGroup() {
280 return $this->group;
281 }
282
283 /**
284 * @return string i18n key of label for structured UI
285 */
286 public function getLabel() {
287 return $this->label;
288 }
289
290 /**
291 * @return string i18n key of description for structured UI
292 */
293 public function getDescription() {
294 return $this->description;
295 }
296
297 /**
298 * Checks whether the filter should display on the unstructured UI
299 *
300 * @return bool Whether to display
301 */
302 abstract public function displaysOnUnstructuredUi();
303
304 /**
305 * Checks whether the filter should display on the structured UI
306 * This refers to the exact filter. See also isFeatureAvailableOnStructuredUi.
307 *
308 * @return bool Whether to display
309 */
310 public function displaysOnStructuredUi() {
311 return $this->label !== null;
312 }
313
314 /**
315 * Checks whether an equivalent feature for this filter is available on the
316 * structured UI.
317 *
318 * This can either be the exact filter, or a new filter that replaces it.
319 */
320 public function isFeatureAvailableOnStructuredUi() {
321 return $this->displaysOnStructuredUi();
322 }
323
324 /**
325 * @return int Priority. Higher value means higher up in the group list
326 */
327 public function getPriority() {
328 return $this->priority;
329 }
330
331 /**
332 * Gets the CSS class
333 *
334 * @return string|null CSS class, or null if not defined
335 */
336 protected function getCssClass() {
337 if ( $this->cssClassSuffix !== null ) {
338 return ChangesList::CSS_CLASS_PREFIX . $this->cssClassSuffix;
339 } else {
340 return null;
341 }
342 }
343
344 /**
345 * Add CSS class if needed
346 *
347 * @param IContextSource $ctx Context source
348 * @param RecentChange $rc Recent changes object
349 * @param Non-associative array of CSS class names; appended to if needed
350 */
351 public function applyCssClassIfNeeded( IContextSource $ctx, RecentChange $rc, array &$classes ) {
352 if ( $this->isRowApplicableCallable === null ) {
353 return;
354 }
355
356 if ( call_user_func( $this->isRowApplicableCallable, $ctx, $rc ) ) {
357 $classes[] = $this->getCssClass();
358 }
359 }
360
361 /**
362 * Gets the JS data required by the front-end of the structured UI
363 *
364 * @return array Associative array Data required by the front-end. messageKeys is
365 * a special top-level value, with the value being an array of the message keys to
366 * send to the client.
367 */
368 public function getJsData() {
369 $output = [
370 'name' => $this->getName(),
371 'label' => $this->getLabel(),
372 'description' => $this->getDescription(),
373 'cssClass' => $this->getCssClass(),
374 'priority' => $this->priority,
375 'subset' => $this->subsetFilters,
376 'conflicts' => [],
377 ];
378
379 $output['messageKeys'] = [
380 $this->getLabel(),
381 $this->getDescription(),
382 ];
383
384 $conflicts = array_merge(
385 $this->conflictingGroups,
386 $this->conflictingFilters
387 );
388
389 foreach ( $conflicts as $conflictInfo ) {
390 unset( $conflictInfo['filterObject'] );
391 unset( $conflictInfo['groupObject'] );
392 $output['conflicts'][] = $conflictInfo;
393 array_push(
394 $output['messageKeys'],
395 $conflictInfo['globalDescription'],
396 $conflictInfo['contextDescription']
397 );
398 }
399
400 return $output;
401 }
402
403 /**
404 * Checks whether this filter is selected in the provided options
405 *
406 * @param FormOptions $opts
407 * @return bool
408 */
409 abstract public function isSelected( FormOptions $opts );
410
411 /**
412 * Get groups conflicting with this filter
413 *
414 * @return ChangesListFilterGroup[]
415 */
416 public function getConflictingGroups() {
417 return array_map(
418 function ( $conflictDesc ) {
419 return $conflictDesc[ 'groupObject' ];
420 },
421 $this->conflictingGroups
422 );
423 }
424
425 /**
426 * Get filters conflicting with this filter
427 *
428 * @return ChangesListFilter[]
429 */
430 public function getConflictingFilters() {
431 return array_map(
432 function ( $conflictDesc ) {
433 return $conflictDesc[ 'filterObject' ];
434 },
435 $this->conflictingFilters
436 );
437 }
438
439 /**
440 * Check if the conflict with a group is currently "active"
441 *
442 * @param ChangesListFilterGroup $group
443 * @param FormOptions $opts
444 * @return bool
445 */
446 public function activelyInConflictWithGroup( ChangesListFilterGroup $group, FormOptions $opts ) {
447 if ( $group->anySelected( $opts ) && $this->isSelected( $opts ) ) {
448 /** @var ChangesListFilter $siblingFilter */
449 foreach ( $this->getSiblings() as $siblingFilter ) {
450 if ( $siblingFilter->isSelected( $opts ) && !$siblingFilter->hasConflictWithGroup( $group ) ) {
451 return false;
452 }
453 }
454 return true;
455 }
456 return false;
457 }
458
459 private function hasConflictWithGroup( ChangesListFilterGroup $group ) {
460 return in_array( $group, $this->getConflictingGroups() );
461 }
462
463 /**
464 * Check if the conflict with a filter is currently "active"
465 *
466 * @param ChangesListFilter $filter
467 * @param FormOptions $opts
468 * @return bool
469 */
470 public function activelyInConflictWithFilter( ChangeslistFilter $filter, FormOptions $opts ) {
471 if ( $this->isSelected( $opts ) && $filter->isSelected( $opts ) ) {
472 /** @var ChangesListFilter $siblingFilter */
473 foreach ( $this->getSiblings() as $siblingFilter ) {
474 if (
475 $siblingFilter->isSelected( $opts ) &&
476 !$siblingFilter->hasConflictWithFilter( $filter )
477 ) {
478 return false;
479 }
480 }
481 return true;
482 }
483 return false;
484 }
485
486 private function hasConflictWithFilter( ChangeslistFilter $filter ) {
487 return in_array( $filter, $this->getConflictingFilters() );
488 }
489
490 /**
491 * Get filters in the same group
492 *
493 * @return ChangesListFilter[]
494 */
495 protected function getSiblings() {
496 return array_filter(
497 $this->getGroup()->getFilters(),
498 function ( $filter ) {
499 return $filter !== $this;
500 }
501 );
502 }
503 }