MediaWiki master
ChangesListStringOptionsFilterGroup.php
Go to the documentation of this file.
1<?php
8
9use InvalidArgumentException;
13
31 public const TYPE = 'string_options';
32
36 public const SEPARATOR = ';';
37
41 public const ALL = 'all';
42
49 public const NONE = '';
50
56 protected $defaultValue;
57
63 protected $queryCallable;
64
101 public function __construct( array $groupDefinition ) {
102 if ( !isset( $groupDefinition['isFullCoverage'] ) ) {
103 throw new InvalidArgumentException( 'You must specify isFullCoverage' );
104 }
105
106 $groupDefinition['type'] = self::TYPE;
107
108 parent::__construct( $groupDefinition );
109
110 $this->queryCallable = $groupDefinition['queryCallable'] ?? null;
111
112 if ( isset( $groupDefinition['default'] ) ) {
113 $this->setDefault( $groupDefinition['default'] );
114 } else {
115 throw new InvalidArgumentException( 'You must specify a default' );
116 }
117 }
118
124 public function setDefault( $defaultValue ) {
125 if ( !is_string( $defaultValue ) ) {
126 throw new InvalidArgumentException(
127 "Can't set the default of filter options group \"{$this->getName()}\"" .
128 ' to a value of type "' . gettype( $defaultValue ) . ': string expected' );
129 }
130 $this->defaultValue = $defaultValue;
131 }
132
138 public function getDefault() {
139 return $this->defaultValue;
140 }
141
145 protected function createFilter( array $filterDefinition ) {
146 return new ChangesListStringOptionsFilter( $filterDefinition );
147 }
148
156 $this->filters[$filter->getName()] = $filter;
157 }
158
162 public function modifyQuery( IReadableDatabase $dbr, ChangesListSpecialPage $specialPage,
163 &$tables, &$fields, &$conds, &$query_options, &$join_conds,
164 FormOptions $opts, $isStructuredFiltersEnabled
165 ) {
166 // STRING_OPTIONS filter groups are exclusively active on Structured UI
167 if ( !$isStructuredFiltersEnabled ) {
168 return;
169 }
170 if ( !$this->queryCallable ) {
171 return;
172 }
173
174 $value = $opts[ $this->getName() ];
175 $allowedFilterNames = [];
176 foreach ( $this->filters as $filter ) {
177 $allowedFilterNames[] = $filter->getName();
178 }
179
180 if ( $value === self::ALL ) {
181 $selectedValues = $allowedFilterNames;
182 } else {
183 $selectedValues = explode( self::SEPARATOR, strtolower( $value ) );
184
185 // remove values that are not recognized or not currently allowed
186 $selectedValues = array_intersect(
187 $selectedValues,
188 $allowedFilterNames
189 );
190 }
191
192 // If there are now no values, because all are disallowed or invalid (also,
193 // the user may not have selected any), this is a no-op.
194
195 // If everything is unchecked, the group always has no effect, regardless
196 // of full-coverage.
197 if ( count( $selectedValues ) === 0 ) {
198 return;
199 }
200
201 sort( $selectedValues );
202
204 get_class( $specialPage ),
205 $specialPage->getContext(),
206 $dbr,
207 $tables,
208 $fields,
209 $conds,
210 $query_options,
211 $join_conds,
212 $selectedValues
213 );
214 }
215
219 public function getJsData() {
220 $output = parent::getJsData();
221
222 $output['separator'] = self::SEPARATOR;
223 $output['default'] = $this->getDefault();
224
225 return $output;
226 }
227
231 public function addOptions( FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled ) {
232 $opts->add( $this->getName(), $allowDefaults ? $this->getDefault() : '' );
233 }
234}
235
237class_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)
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)
Add 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 (legacy interface).The modification is only done if th...
callable null $queryCallable
Callable used to do the actual query modification; see constructor.
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.