MediaWiki  1.33.0
ChangesListFilterGroup.php
Go to the documentation of this file.
1 <?php
24 // TODO: Might want to make a super-class or trait to share behavior (especially re
25 // conflicts) between ChangesListFilter and ChangesListFilterGroup.
26 // What to call it. FilterStructure? That would also let me make
27 // setUnidirectionalConflict protected.
28 
30 
36 abstract class ChangesListFilterGroup {
42  protected $name;
43 
49  protected $title;
50 
56  protected $whatsThisHeader;
57 
63  protected $whatsThisBody;
64 
70  protected $whatsThisUrl;
71 
77  protected $whatsThisLinkText;
78 
84  protected $type;
85 
92  protected $priority;
93 
99  protected $filters;
100 
107  protected $isFullCoverage;
108 
115  protected $conflictingGroups = [];
116 
123  protected $conflictingFilters = [];
124 
125  const DEFAULT_PRIORITY = -100;
126 
127  const RESERVED_NAME_CHAR = '_';
128 
155  public function __construct( array $groupDefinition ) {
156  if ( strpos( $groupDefinition['name'], self::RESERVED_NAME_CHAR ) !== false ) {
157  throw new MWException( 'Group names may not contain \'' .
158  self::RESERVED_NAME_CHAR .
159  '\'. Use the naming convention: \'camelCase\''
160  );
161  }
162 
163  $this->name = $groupDefinition['name'];
164 
165  if ( isset( $groupDefinition['title'] ) ) {
166  $this->title = $groupDefinition['title'];
167  }
168 
169  if ( isset( $groupDefinition['whatsThisHeader'] ) ) {
170  $this->whatsThisHeader = $groupDefinition['whatsThisHeader'];
171  $this->whatsThisBody = $groupDefinition['whatsThisBody'];
172  $this->whatsThisUrl = $groupDefinition['whatsThisUrl'];
173  $this->whatsThisLinkText = $groupDefinition['whatsThisLinkText'];
174  }
175 
176  $this->type = $groupDefinition['type'];
177  $this->priority = $groupDefinition['priority'] ?? self::DEFAULT_PRIORITY;
178 
179  $this->isFullCoverage = $groupDefinition['isFullCoverage'];
180 
181  $this->filters = [];
182  $lowestSpecifiedPriority = -1;
183  foreach ( $groupDefinition['filters'] as $filterDefinition ) {
184  if ( isset( $filterDefinition['priority'] ) ) {
185  $lowestSpecifiedPriority = min( $lowestSpecifiedPriority, $filterDefinition['priority'] );
186  }
187  }
188 
189  // Convenience feature: If you specify a group (and its filters) all in
190  // one place, you don't have to specify priority. You can just put them
191  // in order. However, if you later add one (e.g. an extension adds a filter
192  // to a core-defined group), you need to specify it.
193  $autoFillPriority = $lowestSpecifiedPriority - 1;
194  foreach ( $groupDefinition['filters'] as $filterDefinition ) {
195  if ( !isset( $filterDefinition['priority'] ) ) {
196  $filterDefinition['priority'] = $autoFillPriority;
197  $autoFillPriority--;
198  }
199  $filterDefinition['group'] = $this;
200 
201  $filter = $this->createFilter( $filterDefinition );
202  $this->registerFilter( $filter );
203  }
204  }
205 
212  abstract protected function createFilter( array $filterDefinition );
213 
228  public function conflictsWith( $other, $globalKey, $forwardKey, $backwardKey ) {
229  if ( $globalKey === null || $forwardKey === null || $backwardKey === null ) {
230  throw new MWException( 'All messages must be specified' );
231  }
232 
234  $other,
235  $globalKey,
236  $forwardKey
237  );
238 
239  $other->setUnidirectionalConflict(
240  $this,
241  $globalKey,
242  $backwardKey
243  );
244  }
245 
257  public function setUnidirectionalConflict( $other, $globalDescription, $contextDescription ) {
258  if ( $other instanceof ChangesListFilterGroup ) {
259  $this->conflictingGroups[] = [
260  'group' => $other->getName(),
261  'groupObject' => $other,
262  'globalDescription' => $globalDescription,
263  'contextDescription' => $contextDescription,
264  ];
265  } elseif ( $other instanceof ChangesListFilter ) {
266  $this->conflictingFilters[] = [
267  'group' => $other->getGroup()->getName(),
268  'filter' => $other->getName(),
269  'filterObject' => $other,
270  'globalDescription' => $globalDescription,
271  'contextDescription' => $contextDescription,
272  ];
273  } else {
274  throw new MWException( 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' );
275  }
276  }
277 
281  public function getName() {
282  return $this->name;
283  }
284 
288  public function getTitle() {
289  return $this->title;
290  }
291 
295  public function getType() {
296  return $this->type;
297  }
298 
302  public function getPriority() {
303  return $this->priority;
304  }
305 
310  public function getFilters() {
311  return $this->filters;
312  }
313 
320  public function getFilter( $name ) {
321  return $this->filters[$name] ?? null;
322  }
323 
331  public function getJsData() {
332  $output = [
333  'name' => $this->name,
334  'type' => $this->type,
335  'fullCoverage' => $this->isFullCoverage,
336  'filters' => [],
337  'priority' => $this->priority,
338  'conflicts' => [],
339  'messageKeys' => [ $this->title ]
340  ];
341 
342  if ( isset( $this->whatsThisHeader ) ) {
343  $output['whatsThisHeader'] = $this->whatsThisHeader;
344  $output['whatsThisBody'] = $this->whatsThisBody;
345  $output['whatsThisUrl'] = $this->whatsThisUrl;
346  $output['whatsThisLinkText'] = $this->whatsThisLinkText;
347 
348  array_push(
349  $output['messageKeys'],
350  $output['whatsThisHeader'],
351  $output['whatsThisBody'],
352  $output['whatsThisLinkText']
353  );
354  }
355 
356  usort( $this->filters, function ( $a, $b ) {
357  return $b->getPriority() <=> $a->getPriority();
358  } );
359 
360  foreach ( $this->filters as $filterName => $filter ) {
361  if ( $filter->displaysOnStructuredUi() ) {
362  $filterData = $filter->getJsData();
363  $output['messageKeys'] = array_merge(
364  $output['messageKeys'],
365  $filterData['messageKeys']
366  );
367  unset( $filterData['messageKeys'] );
368  $output['filters'][] = $filterData;
369  }
370  }
371 
372  if ( count( $output['filters'] ) === 0 ) {
373  return null;
374  }
375 
376  $output['title'] = $this->title;
377 
378  $conflicts = array_merge(
379  $this->conflictingGroups,
380  $this->conflictingFilters
381  );
382 
383  foreach ( $conflicts as $conflictInfo ) {
384  unset( $conflictInfo['filterObject'] );
385  unset( $conflictInfo['groupObject'] );
386  $output['conflicts'][] = $conflictInfo;
387  array_push(
388  $output['messageKeys'],
389  $conflictInfo['globalDescription'],
390  $conflictInfo['contextDescription']
391  );
392  }
393 
394  return $output;
395  }
396 
402  public function getConflictingGroups() {
403  return array_map(
404  function ( $conflictDesc ) {
405  return $conflictDesc[ 'groupObject' ];
406  },
408  );
409  }
410 
416  public function getConflictingFilters() {
417  return array_map(
418  function ( $conflictDesc ) {
419  return $conflictDesc[ 'filterObject' ];
420  },
422  );
423  }
424 
431  public function anySelected( FormOptions $opts ) {
432  return (bool)count( array_filter(
433  $this->getFilters(),
434  function ( ChangesListFilter $filter ) use ( $opts ) {
435  return $filter->isSelected( $opts );
436  }
437  ) );
438  }
439 
456  abstract public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
457  &$tables, &$fields, &$conds, &$query_options, &$join_conds,
458  FormOptions $opts, $isStructuredFiltersEnabled );
459 
467  abstract public function addOptions( FormOptions $opts, $allowDefaults,
468  $isStructuredFiltersEnabled );
469 }
$filter
$filter
Definition: profileinfo.php:341
ChangesListFilterGroup\addOptions
addOptions(FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled)
All the options represented by this filter group to $opts.
ChangesListFilterGroup\modifyQuery
modifyQuery(IDatabase $dbr, ChangesListSpecialPage $specialPage, &$tables, &$fields, &$conds, &$query_options, &$join_conds, FormOptions $opts, $isStructuredFiltersEnabled)
Modifies the query to include the filter group.
ChangesListFilterGroup\getType
getType()
Definition: ChangesListFilterGroup.php:295
ChangesListFilterGroup\getName
getName()
Definition: ChangesListFilterGroup.php:281
ChangesListFilterGroup\$type
$type
Type, from a TYPE constant of a subclass.
Definition: ChangesListFilterGroup.php:84
ChangesListFilterGroup\$name
$name
Name (internal identifier)
Definition: ChangesListFilterGroup.php:42
captcha-old.count
count
Definition: captcha-old.py:249
ChangesListSpecialPage
Special page which uses a ChangesList to show query results.
Definition: ChangesListSpecialPage.php:36
$tables
this hook is for auditing only 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 & $tables
Definition: hooks.txt:979
ChangesListFilterGroup\createFilter
createFilter(array $filterDefinition)
Creates a filter of the appropriate type for this group, from the definition.
ChangesListFilterGroup\getFilter
getFilter( $name)
Get filter by name.
Definition: ChangesListFilterGroup.php:320
ChangesListFilterGroup\getTitle
getTitle()
Definition: ChangesListFilterGroup.php:288
ChangesListFilterGroup
Represents a filter group (used on ChangesListSpecialPage and descendants)
Definition: ChangesListFilterGroup.php:36
ChangesListFilterGroup\conflictsWith
conflictsWith( $other, $globalKey, $forwardKey, $backwardKey)
Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
Definition: ChangesListFilterGroup.php:228
ChangesListFilterGroup\$whatsThisUrl
$whatsThisUrl
URL of What's This? link.
Definition: ChangesListFilterGroup.php:70
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
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
$dbr
$dbr
Definition: testCompression.php:50
name
and how to run hooks for an and one after Each event has a name
Definition: hooks.txt:6
MWException
MediaWiki exception.
Definition: MWException.php:26
ChangesListFilterGroup\__construct
__construct(array $groupDefinition)
Create a new filter group with the specified configuration.
Definition: ChangesListFilterGroup.php:155
ChangesListFilterGroup\$priority
$priority
Priority integer.
Definition: ChangesListFilterGroup.php:92
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ChangesListFilterGroup\getConflictingFilters
getConflictingFilters()
Get filters conflicting with this filter group.
Definition: ChangesListFilterGroup.php:416
ChangesListFilterGroup\DEFAULT_PRIORITY
const DEFAULT_PRIORITY
Definition: ChangesListFilterGroup.php:125
ChangesListFilterGroup\setUnidirectionalConflict
setUnidirectionalConflict( $other, $globalDescription, $contextDescription)
Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
Definition: ChangesListFilterGroup.php:257
$output
$output
Definition: SyntaxHighlight.php:334
ChangesListFilterGroup\$isFullCoverage
$isFullCoverage
Whether this group is full coverage.
Definition: ChangesListFilterGroup.php:107
ChangesListFilterGroup\getFilters
getFilters()
Definition: ChangesListFilterGroup.php:310
ChangesListFilterGroup\$whatsThisHeader
$whatsThisHeader
i18n key for header of What's This?
Definition: ChangesListFilterGroup.php:56
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
ChangesListFilterGroup\getJsData
getJsData()
Gets the JS data in the format required by the front-end of the structured UI.
Definition: ChangesListFilterGroup.php:331
title
title
Definition: parserTests.txt:245
ChangesListFilterGroup\getPriority
getPriority()
Definition: ChangesListFilterGroup.php:302
ChangesListFilterGroup\anySelected
anySelected(FormOptions $opts)
Check if any filter in this group is selected.
Definition: ChangesListFilterGroup.php:431
ChangesListFilterGroup\$whatsThisLinkText
$whatsThisLinkText
i18n key for What's This? link
Definition: ChangesListFilterGroup.php:77
ChangesListFilterGroup\$conflictingFilters
$conflictingFilters
Array of associative arrays with conflict information.
Definition: ChangesListFilterGroup.php:123
ChangesListFilterGroup\$filters
$filters
Associative array of filters, as ChangesListFilter objects, with filter name as key.
Definition: ChangesListFilterGroup.php:99
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
Represents a filter (used on ChangesListSpecialPage and descendants)
Definition: ChangesListFilter.php:29
ChangesListFilterGroup\$whatsThisBody
$whatsThisBody
i18n key for body of What's This?
Definition: ChangesListFilterGroup.php:63
ChangesListFilterGroup\RESERVED_NAME_CHAR
const RESERVED_NAME_CHAR
Definition: ChangesListFilterGroup.php:127
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
ChangesListFilterGroup\$conflictingGroups
$conflictingGroups
Array of associative arrays with conflict information.
Definition: ChangesListFilterGroup.php:115
ChangesListFilterGroup\$title
$title
i18n key for title
Definition: ChangesListFilterGroup.php:49
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:22
ChangesListFilterGroup\getConflictingGroups
getConflictingGroups()
Get groups conflicting with this filter group.
Definition: ChangesListFilterGroup.php:402