MediaWiki  REL1_31
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  if ( isset( $groupDefinition['priority'] ) ) {
178  $this->priority = $groupDefinition['priority'];
179  } else {
180  $this->priority = self::DEFAULT_PRIORITY;
181  }
182 
183  $this->isFullCoverage = $groupDefinition['isFullCoverage'];
184 
185  $this->filters = [];
186  $lowestSpecifiedPriority = -1;
187  foreach ( $groupDefinition['filters'] as $filterDefinition ) {
188  if ( isset( $filterDefinition['priority'] ) ) {
189  $lowestSpecifiedPriority = min( $lowestSpecifiedPriority, $filterDefinition['priority'] );
190  }
191  }
192 
193  // Convenience feature: If you specify a group (and its filters) all in
194  // one place, you don't have to specify priority. You can just put them
195  // in order. However, if you later add one (e.g. an extension adds a filter
196  // to a core-defined group), you need to specify it.
197  $autoFillPriority = $lowestSpecifiedPriority - 1;
198  foreach ( $groupDefinition['filters'] as $filterDefinition ) {
199  if ( !isset( $filterDefinition['priority'] ) ) {
200  $filterDefinition['priority'] = $autoFillPriority;
201  $autoFillPriority--;
202  }
203  $filterDefinition['group'] = $this;
204 
205  $filter = $this->createFilter( $filterDefinition );
206  $this->registerFilter( $filter );
207  }
208  }
209 
216  abstract protected function createFilter( array $filterDefinition );
217 
232  public function conflictsWith( $other, $globalKey, $forwardKey, $backwardKey ) {
233  if ( $globalKey === null || $forwardKey === null || $backwardKey === null ) {
234  throw new MWException( 'All messages must be specified' );
235  }
236 
238  $other,
239  $globalKey,
240  $forwardKey
241  );
242 
243  $other->setUnidirectionalConflict(
244  $this,
245  $globalKey,
246  $backwardKey
247  );
248  }
249 
261  public function setUnidirectionalConflict( $other, $globalDescription, $contextDescription ) {
262  if ( $other instanceof ChangesListFilterGroup ) {
263  $this->conflictingGroups[] = [
264  'group' => $other->getName(),
265  'groupObject' => $other,
266  'globalDescription' => $globalDescription,
267  'contextDescription' => $contextDescription,
268  ];
269  } elseif ( $other instanceof ChangesListFilter ) {
270  $this->conflictingFilters[] = [
271  'group' => $other->getGroup()->getName(),
272  'filter' => $other->getName(),
273  'filterObject' => $other,
274  'globalDescription' => $globalDescription,
275  'contextDescription' => $contextDescription,
276  ];
277  } else {
278  throw new MWException( 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' );
279  }
280  }
281 
285  public function getName() {
286  return $this->name;
287  }
288 
292  public function getTitle() {
293  return $this->title;
294  }
295 
299  public function getType() {
300  return $this->type;
301  }
302 
306  public function getPriority() {
307  return $this->priority;
308  }
309 
314  public function getFilters() {
315  return $this->filters;
316  }
317 
324  public function getFilter( $name ) {
325  return isset( $this->filters[$name] ) ? $this->filters[$name] : null;
326  }
327 
335  public function getJsData() {
336  $output = [
337  'name' => $this->name,
338  'type' => $this->type,
339  'fullCoverage' => $this->isFullCoverage,
340  'filters' => [],
341  'priority' => $this->priority,
342  'conflicts' => [],
343  'messageKeys' => [ $this->title ]
344  ];
345 
346  if ( isset( $this->whatsThisHeader ) ) {
347  $output['whatsThisHeader'] = $this->whatsThisHeader;
348  $output['whatsThisBody'] = $this->whatsThisBody;
349  $output['whatsThisUrl'] = $this->whatsThisUrl;
350  $output['whatsThisLinkText'] = $this->whatsThisLinkText;
351 
352  array_push(
353  $output['messageKeys'],
354  $output['whatsThisHeader'],
355  $output['whatsThisBody'],
356  $output['whatsThisLinkText']
357  );
358  }
359 
360  usort( $this->filters, function ( $a, $b ) {
361  return $b->getPriority() - $a->getPriority();
362  } );
363 
364  foreach ( $this->filters as $filterName => $filter ) {
365  if ( $filter->displaysOnStructuredUi() ) {
366  $filterData = $filter->getJsData();
367  $output['messageKeys'] = array_merge(
368  $output['messageKeys'],
369  $filterData['messageKeys']
370  );
371  unset( $filterData['messageKeys'] );
372  $output['filters'][] = $filterData;
373  }
374  }
375 
376  if ( count( $output['filters'] ) === 0 ) {
377  return null;
378  }
379 
380  $output['title'] = $this->title;
381 
382  $conflicts = array_merge(
383  $this->conflictingGroups,
384  $this->conflictingFilters
385  );
386 
387  foreach ( $conflicts as $conflictInfo ) {
388  unset( $conflictInfo['filterObject'] );
389  unset( $conflictInfo['groupObject'] );
390  $output['conflicts'][] = $conflictInfo;
391  array_push(
392  $output['messageKeys'],
393  $conflictInfo['globalDescription'],
394  $conflictInfo['contextDescription']
395  );
396  }
397 
398  return $output;
399  }
400 
406  public function getConflictingGroups() {
407  return array_map(
408  function ( $conflictDesc ) {
409  return $conflictDesc[ 'groupObject' ];
410  },
412  );
413  }
414 
420  public function getConflictingFilters() {
421  return array_map(
422  function ( $conflictDesc ) {
423  return $conflictDesc[ 'filterObject' ];
424  },
426  );
427  }
428 
435  public function anySelected( FormOptions $opts ) {
436  return !!count( array_filter(
437  $this->getFilters(),
438  function ( ChangesListFilter $filter ) use ( $opts ) {
439  return $filter->isSelected( $opts );
440  }
441  ) );
442  }
443 
460  abstract public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
461  &$tables, &$fields, &$conds, &$query_options, &$join_conds,
462  FormOptions $opts, $isStructuredFiltersEnabled );
463 
471  abstract public function addOptions( FormOptions $opts, $allowDefaults,
472  $isStructuredFiltersEnabled );
473 }
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:299
ChangesListFilter\isSelected
isSelected(FormOptions $opts)
Checks whether this filter is selected in the provided options.
ChangesListFilterGroup\getName
getName()
Definition: ChangesListFilterGroup.php:285
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 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:30
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
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
array
the array() calling protocol came about after MediaWiki 1.4rc1.
$output
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2255
ChangesListSpecialPage
Special page which uses a ChangesList to show query results.
Definition: ChangesListSpecialPage.php:35
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:324
ChangesListFilterGroup\getTitle
getTitle()
Definition: ChangesListFilterGroup.php:292
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:232
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:37
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
$dbr
$dbr
Definition: testCompression.php:50
MWException
MediaWiki exception.
Definition: MWException.php:26
title
title
Definition: parserTests.txt:219
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
ChangesListFilterGroup\getConflictingFilters
getConflictingFilters()
Get filters conflicting with this filter group.
Definition: ChangesListFilterGroup.php:420
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:261
ChangesListFilterGroup\$isFullCoverage
$isFullCoverage
Whether this group is full coverage.
Definition: ChangesListFilterGroup.php:107
ChangesListFilterGroup\getFilters
getFilters()
Definition: ChangesListFilterGroup.php:314
ChangesListFilterGroup\$whatsThisHeader
$whatsThisHeader
i18n key for header of What's This?
Definition: ChangesListFilterGroup.php:56
ChangesListFilterGroup\getJsData
getJsData()
Gets the JS data in the format required by the front-end of the structured UI.
Definition: ChangesListFilterGroup.php:335
ChangesListFilterGroup\getPriority
getPriority()
Definition: ChangesListFilterGroup.php:306
ChangesListFilterGroup\anySelected
anySelected(FormOptions $opts)
Check if any filter in this group is selected.
Definition: ChangesListFilterGroup.php:435
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
$tables
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 & $tables
Definition: hooks.txt:1015
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:22
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
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
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
ChangesListFilterGroup\getConflictingGroups
getConflictingGroups()
Get groups conflicting with this filter group.
Definition: ChangesListFilterGroup.php:406