MediaWiki  1.34.0
ChangesListStringOptionsFilterGroup.php
Go to the documentation of this file.
1 <?php
25 
41  const TYPE = 'string_options';
42 
46  const SEPARATOR = ';';
47 
51  const ALL = 'all';
52 
59  const NONE = '';
60 
66  protected $defaultValue;
67 
73  protected $queryCallable;
74 
111  public function __construct( array $groupDefinition ) {
112  if ( !isset( $groupDefinition['isFullCoverage'] ) ) {
113  throw new MWException( 'You must specify isFullCoverage' );
114  }
115 
116  $groupDefinition['type'] = self::TYPE;
117 
118  parent::__construct( $groupDefinition );
119 
120  $this->queryCallable = $groupDefinition['queryCallable'];
121 
122  if ( isset( $groupDefinition['default'] ) ) {
123  $this->setDefault( $groupDefinition['default'] );
124  } else {
125  throw new MWException( 'You must specify a default' );
126  }
127  }
128 
134  public function setDefault( $defaultValue ) {
135  $this->defaultValue = $defaultValue;
136  }
137 
143  public function getDefault() {
144  return $this->defaultValue;
145  }
146 
150  protected function createFilter( array $filterDefinition ) {
151  return new ChangesListStringOptionsFilter( $filterDefinition );
152  }
153 
161  $this->filters[$filter->getName()] = $filter;
162  }
163 
167  public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
168  &$tables, &$fields, &$conds, &$query_options, &$join_conds,
169  FormOptions $opts, $isStructuredFiltersEnabled
170  ) {
171  if ( !$this->isActive( $isStructuredFiltersEnabled ) ) {
172  return;
173  }
174 
175  $value = $opts[ $this->getName() ];
176  $allowedFilterNames = [];
177  foreach ( $this->filters as $filter ) {
178  $allowedFilterNames[] = $filter->getName();
179  }
180 
181  if ( $value === self::ALL ) {
182  $selectedValues = $allowedFilterNames;
183  } else {
184  $selectedValues = explode( self::SEPARATOR, strtolower( $value ) );
185 
186  // remove values that are not recognized or not currently allowed
187  $selectedValues = array_intersect(
188  $selectedValues,
189  $allowedFilterNames
190  );
191  }
192 
193  // If there are now no values, because all are disallowed or invalid (also,
194  // the user may not have selected any), this is a no-op.
195 
196  // If everything is unchecked, the group always has no effect, regardless
197  // of full-coverage.
198  if ( count( $selectedValues ) === 0 ) {
199  return;
200  }
201 
202  sort( $selectedValues );
203 
205  get_class( $specialPage ),
206  $specialPage->getContext(),
207  $dbr,
208  $tables,
209  $fields,
210  $conds,
211  $query_options,
212  $join_conds,
213  $selectedValues
214  );
215  }
216 
220  public function getJsData() {
221  $output = parent::getJsData();
222 
223  $output['separator'] = self::SEPARATOR;
224  $output['default'] = $this->getDefault();
225 
226  return $output;
227  }
228 
232  public function addOptions( FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled ) {
233  $opts->add( $this->getName(), $allowDefaults ? $this->getDefault() : '' );
234  }
235 
242  private function isActive( $isStructuredUI ) {
243  // STRING_OPTIONS filter groups are exclusively active on Structured UI
244  return $isStructuredUI;
245  }
246 }
$filter
$filter
Definition: profileinfo.php:344
ChangesListFilterGroup\getName
getName()
Definition: ChangesListFilterGroup.php:282
ChangesListStringOptionsFilter
An individual filter in a ChangesListStringOptionsFilterGroup.
Definition: ChangesListStringOptionsFilter.php:10
ChangesListStringOptionsFilterGroup\createFilter
createFilter(array $filterDefinition)
Creates a filter of the appropriate type for this group, from the definition.Filter definition Change...
Definition: ChangesListStringOptionsFilterGroup.php:150
ChangesListSpecialPage
Special page which uses a ChangesList to show query results.
Definition: ChangesListSpecialPage.php:37
ChangesListStringOptionsFilterGroup\isActive
isActive( $isStructuredUI)
Check if this filter group is currently active.
Definition: ChangesListStringOptionsFilterGroup.php:242
ChangesListFilterGroup
Represents a filter group (used on ChangesListSpecialPage and descendants)
Definition: ChangesListFilterGroup.php: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
FormOptions\add
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
Definition: FormOptions.php:83
MWException
MediaWiki exception.
Definition: MWException.php:26
ChangesListStringOptionsFilterGroup\SEPARATOR
const SEPARATOR
Delimiter.
Definition: ChangesListStringOptionsFilterGroup.php:46
ChangesListStringOptionsFilterGroup\__construct
__construct(array $groupDefinition)
Create a new filter group with the specified configuration.
Definition: ChangesListStringOptionsFilterGroup.php:111
$output
$output
Definition: SyntaxHighlight.php:335
ChangesListStringOptionsFilterGroup\ALL
const ALL
Signifies that all options in the group are selected.
Definition: ChangesListStringOptionsFilterGroup.php:51
ChangesListStringOptionsFilterGroup\modifyQuery
modifyQuery(IDatabase $dbr, ChangesListSpecialPage $specialPage, &$tables, &$fields, &$conds, &$query_options, &$join_conds, FormOptions $opts, $isStructuredFiltersEnabled)
Modifies the query to include the filter group.The modification is only done if the filter group is i...
Definition: ChangesListStringOptionsFilterGroup.php:167
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:692
ChangesListStringOptionsFilterGroup\$defaultValue
$defaultValue
Defaul parameter value.
Definition: ChangesListStringOptionsFilterGroup.php:66
ChangesListStringOptionsFilterGroup\setDefault
setDefault( $defaultValue)
Sets default of filter group.
Definition: ChangesListStringOptionsFilterGroup.php:134
ChangesListStringOptionsFilterGroup\$queryCallable
$queryCallable
Callable used to do the actual query modification; see constructor.
Definition: ChangesListStringOptionsFilterGroup.php:73
ChangesListStringOptionsFilterGroup\getJsData
getJsData()
Gets the JS data in the format required by the front-end of the structured UI.array|null Associative ...
Definition: ChangesListStringOptionsFilterGroup.php:220
ChangesListStringOptionsFilterGroup\NONE
const NONE
Signifies that no options in the group are selected, meaning the group has no effect.
Definition: ChangesListStringOptionsFilterGroup.php:59
ChangesListStringOptionsFilterGroup\getDefault
getDefault()
Gets default of filter group.
Definition: ChangesListStringOptionsFilterGroup.php:143
ChangesListStringOptionsFilterGroup\registerFilter
registerFilter(ChangesListStringOptionsFilter $filter)
Registers a filter in this group.
Definition: ChangesListStringOptionsFilterGroup.php:160
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
ChangesListStringOptionsFilterGroup\addOptions
addOptions(FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled)
All the options represented by this filter group to $opts.
Definition: ChangesListStringOptionsFilterGroup.php:232
ChangesListStringOptionsFilterGroup
Represents a filter group with multiple string options.
Definition: ChangesListStringOptionsFilterGroup.php:37
ChangesListStringOptionsFilterGroup\TYPE
const TYPE
Type marker, used by JavaScript.
Definition: ChangesListStringOptionsFilterGroup.php:41