MediaWiki master
ChangesListStringOptionsFilterGroup.php
Go to the documentation of this file.
1<?php
24
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
162 $this->filters[$filter->getName()] = $filter;
163 }
164
168 public function modifyQuery( IReadableDatabase $dbr, ChangesListSpecialPage $specialPage,
169 &$tables, &$fields, &$conds, &$query_options, &$join_conds,
170 FormOptions $opts, $isStructuredFiltersEnabled
171 ) {
172 // STRING_OPTIONS filter groups are exclusively active on Structured UI
173 if ( !$isStructuredFiltersEnabled ) {
174 return;
175 }
176
177 $value = $opts[ $this->getName() ];
178 $allowedFilterNames = [];
179 foreach ( $this->filters as $filter ) {
180 $allowedFilterNames[] = $filter->getName();
181 }
182
183 if ( $value === self::ALL ) {
184 $selectedValues = $allowedFilterNames;
185 } else {
186 $selectedValues = explode( self::SEPARATOR, strtolower( $value ) );
187
188 // remove values that are not recognized or not currently allowed
189 $selectedValues = array_intersect(
190 $selectedValues,
191 $allowedFilterNames
192 );
193 }
194
195 // If there are now no values, because all are disallowed or invalid (also,
196 // the user may not have selected any), this is a no-op.
197
198 // If everything is unchecked, the group always has no effect, regardless
199 // of full-coverage.
200 if ( count( $selectedValues ) === 0 ) {
201 return;
202 }
203
204 sort( $selectedValues );
205
206 ( $this->queryCallable )(
207 get_class( $specialPage ),
208 $specialPage->getContext(),
209 $dbr,
210 $tables,
211 $fields,
212 $conds,
213 $query_options,
214 $join_conds,
215 $selectedValues
216 );
217 }
218
222 public function getJsData() {
223 $output = parent::getJsData();
224
225 $output['separator'] = self::SEPARATOR;
226 $output['default'] = $this->getDefault();
227
228 return $output;
229 }
230
234 public function addOptions( FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled ) {
235 $opts->add( $this->getName(), $allowDefaults ? $this->getDefault() : '' );
236 }
237}
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.