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