MediaWiki master
ChangesListStringOptionsFilterGroup.php
Go to the documentation of this file.
1<?php
22
23use InvalidArgumentException;
27
45 public const TYPE = 'string_options';
46
50 public const SEPARATOR = ';';
51
55 public const ALL = 'all';
56
63 public const NONE = '';
64
70 protected $defaultValue;
71
77 protected $queryCallable;
78
115 public function __construct( array $groupDefinition ) {
116 if ( !isset( $groupDefinition['isFullCoverage'] ) ) {
117 throw new InvalidArgumentException( 'You must specify isFullCoverage' );
118 }
119
120 $groupDefinition['type'] = self::TYPE;
121
122 parent::__construct( $groupDefinition );
123
124 $this->queryCallable = $groupDefinition['queryCallable'];
125
126 if ( isset( $groupDefinition['default'] ) ) {
127 $this->setDefault( $groupDefinition['default'] );
128 } else {
129 throw new InvalidArgumentException( 'You must specify a default' );
130 }
131 }
132
138 public function setDefault( $defaultValue ) {
139 $this->defaultValue = $defaultValue;
140 }
141
147 public function getDefault() {
148 return $this->defaultValue;
149 }
150
154 protected function createFilter( array $filterDefinition ) {
155 return new ChangesListStringOptionsFilter( $filterDefinition );
156 }
157
165 $this->filters[$filter->getName()] = $filter;
166 }
167
171 public function modifyQuery( IReadableDatabase $dbr, ChangesListSpecialPage $specialPage,
172 &$tables, &$fields, &$conds, &$query_options, &$join_conds,
173 FormOptions $opts, $isStructuredFiltersEnabled
174 ) {
175 // STRING_OPTIONS filter groups are exclusively active on Structured UI
176 if ( !$isStructuredFiltersEnabled ) {
177 return;
178 }
179
180 $value = $opts[ $this->getName() ];
181 $allowedFilterNames = [];
182 foreach ( $this->filters as $filter ) {
183 $allowedFilterNames[] = $filter->getName();
184 }
185
186 if ( $value === self::ALL ) {
187 $selectedValues = $allowedFilterNames;
188 } else {
189 $selectedValues = explode( self::SEPARATOR, strtolower( $value ) );
190
191 // remove values that are not recognized or not currently allowed
192 $selectedValues = array_intersect(
193 $selectedValues,
194 $allowedFilterNames
195 );
196 }
197
198 // If there are now no values, because all are disallowed or invalid (also,
199 // the user may not have selected any), this is a no-op.
200
201 // If everything is unchecked, the group always has no effect, regardless
202 // of full-coverage.
203 if ( count( $selectedValues ) === 0 ) {
204 return;
205 }
206
207 sort( $selectedValues );
208
210 get_class( $specialPage ),
211 $specialPage->getContext(),
212 $dbr,
213 $tables,
214 $fields,
215 $conds,
216 $query_options,
217 $join_conds,
218 $selectedValues
219 );
220 }
221
225 public function getJsData() {
226 $output = parent::getJsData();
227
228 $output['separator'] = self::SEPARATOR;
229 $output['default'] = $this->getDefault();
230
231 return $output;
232 }
233
237 public function addOptions( FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled ) {
238 $opts->add( $this->getName(), $allowDefaults ? $this->getDefault() : '' );
239 }
240}
241
243class_alias( ChangesListStringOptionsFilterGroup::class, 'ChangesListStringOptionsFilterGroup' );
Helper class to keep track of options when mixing links and form elements.
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
Represents a filter group (used on ChangesListSpecialPage and descendants)
callable $queryCallable
Callable used to do the actual query modification; see constructor.
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.
__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.
addOptions(FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled)
All the options represented by this filter group to $opts.
createFilter(array $filterDefinition)
Creates a filter of the appropriate type for this group, from the definition.ChangesListFilter Filter
modifyQuery(IReadableDatabase $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...
An individual filter in a ChangesListStringOptionsFilterGroup.
Special page which uses a ChangesList to show query results.
getContext()
Gets the context this SpecialPage is executed in.
A database connection without write operations.