MediaWiki  1.29.1
ChangesListFilter.php
Go to the documentation of this file.
1 <?php
30 abstract class ChangesListFilter {
36  protected $name;
37 
46  protected $cssClassSuffix;
47 
54 
60  protected $group;
61 
67  protected $label;
68 
74  protected $description;
75 
82  protected $conflictingGroups = [];
83 
90  protected $conflictingFilters = [];
91 
97  protected $subsetFilters = [];
98 
104  protected $priority;
105 
106  const RESERVED_NAME_CHAR = '_';
107 
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 
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 
200  $other,
201  $globalKey,
202  $forwardKey
203  );
204 
205  $other->setUnidirectionalConflict(
206  $this,
207  $globalKey,
208  $backwardKey
209  );
210  }
211 
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 
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 
272  public function getName() {
273  return $this->name;
274  }
275 
279  public function getGroup() {
280  return $this->group;
281  }
282 
286  public function getLabel() {
287  return $this->label;
288  }
289 
293  public function getDescription() {
294  return $this->description;
295  }
296 
302  abstract public function displaysOnUnstructuredUi();
303 
310  public function displaysOnStructuredUi() {
311  return $this->label !== null;
312  }
313 
321  return $this->displaysOnStructuredUi();
322  }
323 
327  public function getPriority() {
328  return $this->priority;
329  }
330 
336  protected function getCssClass() {
337  if ( $this->cssClassSuffix !== null ) {
339  } else {
340  return null;
341  }
342  }
343 
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 
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 
409  abstract public function isSelected( FormOptions $opts );
410 
416  public function getConflictingGroups() {
417  return array_map(
418  function ( $conflictDesc ) {
419  return $conflictDesc[ 'groupObject' ];
420  },
422  );
423  }
424 
430  public function getConflictingFilters() {
431  return array_map(
432  function ( $conflictDesc ) {
433  return $conflictDesc[ 'filterObject' ];
434  },
436  );
437  }
438 
447  if ( $group->anySelected( $opts ) && $this->isSelected( $opts ) ) {
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 
460  return in_array( $group, $this->getConflictingGroups() );
461  }
462 
470  public function activelyInConflictWithFilter( ChangeslistFilter $filter, FormOptions $opts ) {
471  if ( $this->isSelected( $opts ) && $filter->isSelected( $opts ) ) {
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 
495  protected function getSiblings() {
496  return array_filter(
497  $this->getGroup()->getFilters(),
498  function ( $filter ) {
499  return $filter !== $this;
500  }
501  );
502  }
503 }
ChangesListFilter\activelyInConflictWithGroup
activelyInConflictWithGroup(ChangesListFilterGroup $group, FormOptions $opts)
Check if the conflict with a group is currently "active".
Definition: ChangesListFilter.php:446
ChangesListFilter\isSelected
isSelected(FormOptions $opts)
Checks whether this filter is selected in the provided options.
ChangesListFilter\hasConflictWithFilter
hasConflictWithFilter(ChangeslistFilter $filter)
Definition: ChangesListFilter.php:486
RecentChange
Utility class for creating new RC entries.
Definition: RecentChange.php:63
ChangesListFilter\displaysOnUnstructuredUi
displaysOnUnstructuredUi()
Checks whether the filter should display on the unstructured UI.
ChangesListFilter\setAsSupersetOf
setAsSupersetOf(ChangesListFilter $other)
Marks that the current instance is (also) a superset of the filter passed in.
Definition: ChangesListFilter.php:256
ChangesListFilter\$priority
$priority
Priority integer.
Definition: ChangesListFilter.php:104
ChangesListFilter\displaysOnStructuredUi
displaysOnStructuredUi()
Checks whether the filter should display on the structured UI This refers to the exact filter.
Definition: ChangesListFilter.php:310
ChangesListFilter\activelyInConflictWithFilter
activelyInConflictWithFilter(ChangeslistFilter $filter, FormOptions $opts)
Check if the conflict with a filter is currently "active".
Definition: ChangesListFilter.php:470
ChangesListFilter\getName
getName()
Definition: ChangesListFilter.php:272
ChangesListFilter\$isRowApplicableCallable
$isRowApplicableCallable
Callable that returns true if and only if a row is attributed to this filter.
Definition: ChangesListFilter.php:53
ChangesListFilterGroup
Represents a filter group (used on ChangesListSpecialPage and descendants)
Definition: ChangesListFilterGroup.php:35
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ChangesListFilter\$cssClassSuffix
$cssClassSuffix
CSS class suffix used for attribution, e.g.
Definition: ChangesListFilter.php:46
ChangesListFilter\conflictsWith
conflictsWith( $other, $globalKey, $forwardKey, $backwardKey)
Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
Definition: ChangesListFilter.php:190
ChangesListFilter\$conflictingGroups
$conflictingGroups
Definition: ChangesListFilter.php:82
ChangesListFilter\getGroup
getGroup()
Definition: ChangesListFilter.php:279
MWException
MediaWiki exception.
Definition: MWException.php:26
ChangesListFilter\hasConflictWithGroup
hasConflictWithGroup(ChangesListFilterGroup $group)
Definition: ChangesListFilter.php:459
ChangesListFilter\$label
$label
i18n key of label for structured UI
Definition: ChangesListFilter.php:67
ChangesListFilter\$group
$group
Group.
Definition: ChangesListFilter.php:60
ChangesListFilter\getSiblings
getSiblings()
Get filters in the same group.
Definition: ChangesListFilter.php:495
ChangesListFilter\getConflictingGroups
getConflictingGroups()
Get groups conflicting with this filter.
Definition: ChangesListFilter.php:416
ChangesListFilter\getJsData
getJsData()
Gets the JS data required by the front-end of the structured UI.
Definition: ChangesListFilter.php:368
ChangesListFilter\applyCssClassIfNeeded
applyCssClassIfNeeded(IContextSource $ctx, RecentChange $rc, array &$classes)
Add CSS class if needed.
Definition: ChangesListFilter.php:351
ChangesListFilter\getDescription
getDescription()
Definition: ChangesListFilter.php:293
ChangesListFilter\$subsetFilters
$subsetFilters
Definition: ChangesListFilter.php:97
ChangesList\CSS_CLASS_PREFIX
const CSS_CLASS_PREFIX
Definition: ChangesList.php:29
ChangesListFilter\isFeatureAvailableOnStructuredUi
isFeatureAvailableOnStructuredUi()
Checks whether an equivalent feature for this filter is available on the structured UI.
Definition: ChangesListFilter.php:320
$output
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition: hooks.txt:1049
ChangesListFilter\getCssClass
getCssClass()
Gets the CSS class.
Definition: ChangesListFilter.php:336
ChangesListFilter\RESERVED_NAME_CHAR
const RESERVED_NAME_CHAR
Definition: ChangesListFilter.php:106
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
ChangesListFilter\getLabel
getLabel()
Definition: ChangesListFilter.php:286
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ChangesListFilter\setUnidirectionalConflict
setUnidirectionalConflict( $other, $globalDescription, $contextDescription)
Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
Definition: ChangesListFilter.php:224
ChangesListFilter
Represents a filter (used on ChangesListSpecialPage and descendants)
Definition: ChangesListFilter.php:30
ChangesListFilter\$description
$description
i18n key of description for structured UI
Definition: ChangesListFilter.php:74
name
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at name
Definition: design.txt:12
ChangesListFilter\getPriority
getPriority()
Definition: ChangesListFilter.php:327
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
ChangesListFilter\__construct
__construct(array $filterDefinition)
Creates a new filter with the specified configuration, and registers it to the specified group.
Definition: ChangesListFilter.php:138
ChangesListFilter\$name
$name
Filter name.
Definition: ChangesListFilter.php:36
group
no text was provided for refs named< code > blankwithnoreference</code ></span ></li ></ol ></div > ! end ! test with< references/> in group ! wikitext Wikipedia rocks< ref > Proceeds of vol XXI</ref > Wikipedia rocks< ref group=note > Proceeds of vol XXI</ref >< references/>< references group=note/> ! html< p > Wikipedia rocks< sup id="cite_ref-1" class="reference">< a href="#cite_note-1"> &Wikipedia rocks< sup id="cite_ref-2" class="reference">< a href="#cite_note-2"> &</p >< div class="mw-references-wrap">< ol class="references">< li id="cite_note-1">< span class="mw-cite-backlink">< a href="#cite_ref-1"> ↑</a ></span >< span class="reference-text"> Proceeds of vol XXI</span ></li ></ol ></div >< div class="mw-references-wrap">< ol class="references">< li id="cite_note-2">< span class="mw-cite-backlink">< a href="#cite_ref-2"> ↑</a ></span >< span class="reference-text"> Proceeds of vol XXI</span ></li ></ol ></div > ! end ! test with< references/> in group
Definition: citeParserTests.txt:306
ChangesListFilter\getConflictingFilters
getConflictingFilters()
Get filters conflicting with this filter.
Definition: ChangesListFilter.php:430
ChangesListFilter\$conflictingFilters
$conflictingFilters
Definition: ChangesListFilter.php:90
array
the array() calling protocol came about after MediaWiki 1.4rc1.