3 * Represents a filter group (used on ChangesListSpecialPage and descendants)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 * @author Matthew Flaschen
25 // TODO: Might want to make a super-class or trait to share behavior (especially re
26 // conflicts) between ChangesListFilter and ChangesListFilterGroup.
27 // What to call it. FilterStructure? That would also let me make
28 // setUnidirectionalConflict protected.
30 use Wikimedia\Rdbms\IDatabase
;
33 * Represents a filter group (used on ChangesListSpecialPage and descendants)
37 abstract class ChangesListFilterGroup
{
39 * Name (internal identifier)
53 * i18n key for header of What's This?
55 * @var string|null $whatsThisHeader
57 protected $whatsThisHeader;
60 * i18n key for body of What's This?
62 * @var string|null $whatsThisBody
64 protected $whatsThisBody;
67 * URL of What's This? link
69 * @var string|null $whatsThisUrl
71 protected $whatsThisUrl;
74 * i18n key for What's This? link
76 * @var string|null $whatsThisLinkText
78 protected $whatsThisLinkText;
81 * Type, from a TYPE constant of a subclass
88 * Priority integer. Higher values means higher up in the
91 * @var string $priority
96 * Associative array of filters, as ChangesListFilter objects, with filter name as key
103 * Whether this group is full coverage. This means that checking every item in the
104 * group means no changes list (e.g. RecentChanges) entries are filtered out.
106 * @var bool $isFullCoverage
108 protected $isFullCoverage;
111 * Array of associative arrays with conflict information. See
112 * setUnidirectionalConflict
114 * @var array $conflictingGroups
116 protected $conflictingGroups = [];
119 * Array of associative arrays with conflict information. See
120 * setUnidirectionalConflict
122 * @var array $conflictingFilters
124 protected $conflictingFilters = [];
126 const DEFAULT_PRIORITY
= -100;
128 const RESERVED_NAME_CHAR
= '_';
131 * Create a new filter group with the specified configuration
133 * @param array $groupDefinition Configuration of group
134 * * $groupDefinition['name'] string Group name; use camelCase with no punctuation
135 * * $groupDefinition['title'] string i18n key for title (optional, can be omitted
136 * only if none of the filters in the group display in the structured UI)
137 * * $groupDefinition['type'] string A type constant from a subclass of this one
138 * * $groupDefinition['priority'] int Priority integer. Higher value means higher
139 * up in the group list (optional, defaults to -100).
140 * * $groupDefinition['filters'] array Numeric array of filter definitions, each of which
141 * is an associative array to be passed to the filter constructor. However,
142 * 'priority' is optional for the filters. Any filter that has priority unset
143 * will be put to the bottom, in the order given.
144 * * $groupDefinition['isFullCoverage'] bool Whether the group is full coverage;
145 * if true, this means that checking every item in the group means no
146 * changes list entries are filtered out.
147 * * $groupDefinition['whatsThisHeader'] string i18n key for header of "What's
148 * This" popup (optional).
149 * * $groupDefinition['whatsThisBody'] string i18n key for body of "What's This"
151 * * $groupDefinition['whatsThisUrl'] string URL for main link of "What's This"
153 * * $groupDefinition['whatsThisLinkText'] string i18n key of text for main link of
154 * "What's This" popup (optional).
156 public function __construct( array $groupDefinition ) {
157 if ( strpos( $groupDefinition['name'], self
::RESERVED_NAME_CHAR
) !== false ) {
158 throw new MWException( 'Group names may not contain \'' .
159 self
::RESERVED_NAME_CHAR
.
160 '\'. Use the naming convention: \'camelCase\''
164 $this->name
= $groupDefinition['name'];
166 if ( isset( $groupDefinition['title'] ) ) {
167 $this->title
= $groupDefinition['title'];
170 if ( isset( $groupDefinition['whatsThisHeader'] ) ) {
171 $this->whatsThisHeader
= $groupDefinition['whatsThisHeader'];
172 $this->whatsThisBody
= $groupDefinition['whatsThisBody'];
173 $this->whatsThisUrl
= $groupDefinition['whatsThisUrl'];
174 $this->whatsThisLinkText
= $groupDefinition['whatsThisLinkText'];
177 $this->type
= $groupDefinition['type'];
178 if ( isset( $groupDefinition['priority'] ) ) {
179 $this->priority
= $groupDefinition['priority'];
181 $this->priority
= self
::DEFAULT_PRIORITY
;
184 $this->isFullCoverage
= $groupDefinition['isFullCoverage'];
187 $lowestSpecifiedPriority = -1;
188 foreach ( $groupDefinition['filters'] as $filterDefinition ) {
189 if ( isset( $filterDefinition['priority'] ) ) {
190 $lowestSpecifiedPriority = min( $lowestSpecifiedPriority, $filterDefinition['priority'] );
194 // Convenience feature: If you specify a group (and its filters) all in
195 // one place, you don't have to specify priority. You can just put them
196 // in order. However, if you later add one (e.g. an extension adds a filter
197 // to a core-defined group), you need to specify it.
198 $autoFillPriority = $lowestSpecifiedPriority - 1;
199 foreach ( $groupDefinition['filters'] as $filterDefinition ) {
200 if ( !isset( $filterDefinition['priority'] ) ) {
201 $filterDefinition['priority'] = $autoFillPriority;
204 $filterDefinition['group'] = $this;
206 $filter = $this->createFilter( $filterDefinition );
207 $this->registerFilter( $filter );
212 * Creates a filter of the appropriate type for this group, from the definition
214 * @param array $filterDefinition Filter definition
215 * @return ChangesListFilter Filter
217 abstract protected function createFilter( array $filterDefinition );
220 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with this object.
222 * WARNING: This means there is a conflict when both things are *shown*
223 * (not filtered out), even for the hide-based filters. So e.g. conflicting with
224 * 'hideanons' means there is a conflict if only anonymous users are *shown*.
226 * @param ChangesListFilterGroup|ChangesListFilter $other
227 * @param string $globalKey i18n key for top-level conflict message
228 * @param string $forwardKey i18n key for conflict message in this
229 * direction (when in UI context of $this object)
230 * @param string $backwardKey i18n key for conflict message in reverse
231 * direction (when in UI context of $other object)
233 public function conflictsWith( $other, $globalKey, $forwardKey, $backwardKey ) {
234 if ( $globalKey === null ||
$forwardKey === null ||
$backwardKey === null ) {
235 throw new MWException( 'All messages must be specified' );
238 $this->setUnidirectionalConflict(
244 $other->setUnidirectionalConflict(
252 * Marks that the given ChangesListFilterGroup or ChangesListFilter conflicts with
257 * @param ChangesListFilterGroup|ChangesListFilter $other
258 * @param string $globalDescription i18n key for top-level conflict message
259 * @param string $contextDescription i18n key for conflict message in this
260 * direction (when in UI context of $this object)
262 public function setUnidirectionalConflict( $other, $globalDescription, $contextDescription ) {
263 if ( $other instanceof ChangesListFilterGroup
) {
264 $this->conflictingGroups
[] = [
265 'group' => $other->getName(),
266 'groupObject' => $other,
267 'globalDescription' => $globalDescription,
268 'contextDescription' => $contextDescription,
270 } elseif ( $other instanceof ChangesListFilter
) {
271 $this->conflictingFilters
[] = [
272 'group' => $other->getGroup()->getName(),
273 'filter' => $other->getName(),
274 'filterObject' => $other,
275 'globalDescription' => $globalDescription,
276 'contextDescription' => $contextDescription,
279 throw new MWException( 'You can only pass in a ChangesListFilterGroup or a ChangesListFilter' );
284 * @return string Internal name
286 public function getName() {
291 * @return string i18n key for title
293 public function getTitle() {
298 * @return string Type (TYPE constant from a subclass)
300 public function getType() {
305 * @return int Priority. Higher means higher in the group list
307 public function getPriority() {
308 return $this->priority
;
312 * @return ChangesListFilter[] Associative array of ChangesListFilter objects, with
315 public function getFilters() {
316 return $this->filters
;
322 * @param string $name Filter name
323 * @return ChangesListFilter|null Specified filter, or null if it is not registered
325 public function getFilter( $name ) {
326 return isset( $this->filters
[$name] ) ?
$this->filters
[$name] : null;
330 * Gets the JS data in the format required by the front-end of the structured UI
332 * @return array|null Associative array, or null if there are no filters that
333 * display in the structured UI. messageKeys is a special top-level value, with
334 * the value being an array of the message keys to send to the client.
336 public function getJsData() {
338 'name' => $this->name
,
339 'type' => $this->type
,
340 'fullCoverage' => $this->isFullCoverage
,
342 'priority' => $this->priority
,
344 'messageKeys' => [ $this->title
]
347 if ( isset( $this->whatsThisHeader
) ) {
348 $output['whatsThisHeader'] = $this->whatsThisHeader
;
349 $output['whatsThisBody'] = $this->whatsThisBody
;
350 $output['whatsThisUrl'] = $this->whatsThisUrl
;
351 $output['whatsThisLinkText'] = $this->whatsThisLinkText
;
354 $output['messageKeys'],
355 $output['whatsThisHeader'],
356 $output['whatsThisBody'],
357 $output['whatsThisLinkText']
361 usort( $this->filters
, function ( $a, $b ) {
362 return $b->getPriority() - $a->getPriority();
365 foreach ( $this->filters
as $filterName => $filter ) {
366 if ( $filter->displaysOnStructuredUi() ) {
367 $filterData = $filter->getJsData();
368 $output['messageKeys'] = array_merge(
369 $output['messageKeys'],
370 $filterData['messageKeys']
372 unset( $filterData['messageKeys'] );
373 $output['filters'][] = $filterData;
377 if ( count( $output['filters'] ) === 0 ) {
381 $output['title'] = $this->title
;
383 $conflicts = array_merge(
384 $this->conflictingGroups
,
385 $this->conflictingFilters
388 foreach ( $conflicts as $conflictInfo ) {
389 unset( $conflictInfo['filterObject'] );
390 unset( $conflictInfo['groupObject'] );
391 $output['conflicts'][] = $conflictInfo;
393 $output['messageKeys'],
394 $conflictInfo['globalDescription'],
395 $conflictInfo['contextDescription']
403 * Get groups conflicting with this filter group
405 * @return ChangesListFilterGroup[]
407 public function getConflictingGroups() {
409 function ( $conflictDesc ) {
410 return $conflictDesc[ 'groupObject' ];
412 $this->conflictingGroups
417 * Get filters conflicting with this filter group
419 * @return ChangesListFilter[]
421 public function getConflictingFilters() {
423 function ( $conflictDesc ) {
424 return $conflictDesc[ 'filterObject' ];
426 $this->conflictingFilters
431 * Check if any filter in this group is selected
433 * @param FormOptions $opts
436 public function anySelected( FormOptions
$opts ) {
437 return !!count( array_filter(
439 function ( ChangesListFilter
$filter ) use ( $opts ) {
440 return $filter->isSelected( $opts );
446 * Modifies the query to include the filter group.
448 * The modification is only done if the filter group is in effect. This means that
449 * one or more valid and allowed filters were selected.
451 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
452 * @param ChangesListSpecialPage $specialPage Current special page
453 * @param array &$tables Array of tables; see IDatabase::select $table
454 * @param array &$fields Array of fields; see IDatabase::select $vars
455 * @param array &$conds Array of conditions; see IDatabase::select $conds
456 * @param array &$query_options Array of query options; see IDatabase::select $options
457 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
458 * @param FormOptions $opts Wrapper for the current request options and their defaults
459 * @param bool $isStructuredFiltersEnabled True if the Structured UI is currently enabled
461 abstract public function modifyQuery( IDatabase
$dbr, ChangesListSpecialPage
$specialPage,
462 &$tables, &$fields, &$conds, &$query_options, &$join_conds,
463 FormOptions
$opts, $isStructuredFiltersEnabled );
466 * All the options represented by this filter group to $opts
468 * @param FormOptions $opts
469 * @param bool $allowDefaults
470 * @param bool $isStructuredFiltersEnabled
472 abstract public function addOptions( FormOptions
$opts, $allowDefaults,
473 $isStructuredFiltersEnabled );