MediaWiki master
ChangesListStringOptionsFilterGroup.php
Go to the documentation of this file.
1<?php
27
43 public const TYPE = 'string_options';
44
48 public const SEPARATOR = ';';
49
53 public const ALL = 'all';
54
61 public const NONE = '';
62
68 protected $defaultValue;
69
75 protected $queryCallable;
76
113 public function __construct( array $groupDefinition ) {
114 if ( !isset( $groupDefinition['isFullCoverage'] ) ) {
115 throw new InvalidArgumentException( 'You must specify isFullCoverage' );
116 }
117
118 $groupDefinition['type'] = self::TYPE;
119
120 parent::__construct( $groupDefinition );
121
122 $this->queryCallable = $groupDefinition['queryCallable'];
123
124 if ( isset( $groupDefinition['default'] ) ) {
125 $this->setDefault( $groupDefinition['default'] );
126 } else {
127 throw new InvalidArgumentException( 'You must specify a default' );
128 }
129 }
130
136 public function setDefault( $defaultValue ) {
137 $this->defaultValue = $defaultValue;
138 }
139
145 public function getDefault() {
146 return $this->defaultValue;
147 }
148
152 protected function createFilter( array $filterDefinition ) {
153 return new ChangesListStringOptionsFilter( $filterDefinition );
154 }
155
163 $this->filters[$filter->getName()] = $filter;
164 }
165
169 public function modifyQuery( IReadableDatabase $dbr, ChangesListSpecialPage $specialPage,
170 &$tables, &$fields, &$conds, &$query_options, &$join_conds,
171 FormOptions $opts, $isStructuredFiltersEnabled
172 ) {
173 // STRING_OPTIONS filter groups are exclusively active on Structured UI
174 if ( !$isStructuredFiltersEnabled ) {
175 return;
176 }
177
178 $value = $opts[ $this->getName() ];
179 $allowedFilterNames = [];
180 foreach ( $this->filters as $filter ) {
181 $allowedFilterNames[] = $filter->getName();
182 }
183
184 if ( $value === self::ALL ) {
185 $selectedValues = $allowedFilterNames;
186 } else {
187 $selectedValues = explode( self::SEPARATOR, strtolower( $value ) );
188
189 // remove values that are not recognized or not currently allowed
190 $selectedValues = array_intersect(
191 $selectedValues,
192 $allowedFilterNames
193 );
194 }
195
196 // If there are now no values, because all are disallowed or invalid (also,
197 // the user may not have selected any), this is a no-op.
198
199 // If everything is unchecked, the group always has no effect, regardless
200 // of full-coverage.
201 if ( count( $selectedValues ) === 0 ) {
202 return;
203 }
204
205 sort( $selectedValues );
206
207 ( $this->queryCallable )(
208 get_class( $specialPage ),
209 $specialPage->getContext(),
210 $dbr,
211 $tables,
212 $fields,
213 $conds,
214 $query_options,
215 $join_conds,
216 $selectedValues
217 );
218 }
219
223 public function getJsData() {
224 $output = parent::getJsData();
225
226 $output['separator'] = self::SEPARATOR;
227 $output['default'] = $this->getDefault();
228
229 return $output;
230 }
231
235 public function addOptions( FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled ) {
236 $opts->add( $this->getName(), $allowDefaults ? $this->getDefault() : '' );
237 }
238}
Represents a filter group (used on ChangesListSpecialPage and descendants)
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(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...
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.
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
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.