Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
21.74% covered (danger)
21.74%
5 / 23
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChangesListBooleanFilterGroup
22.73% covered (danger)
22.73%
5 / 22
50.00% covered (danger)
50.00%
3 / 6
90.98
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 createFilter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefault
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 registerFilter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 modifyQuery
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 addOptions
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\RecentChanges;
4
5use InvalidArgumentException;
6use MediaWiki\Html\FormOptions;
7use MediaWiki\SpecialPage\ChangesListSpecialPage;
8use Wikimedia\Rdbms\IReadableDatabase;
9
10/**
11 * If the group is active, any unchecked filters will
12 * translate to hide parameters in the URL.  E.g. if 'Human (not bot)' is checked,
13 * but 'Bot' is unchecked, hidebots=1 will be sent.
14 *
15 * @since 1.29
16 * @ingroup RecentChanges
17 * @method ChangesListBooleanFilter[] getFilters()
18 * @method ChangesListBooleanFilter|null getFilter( string $name )
19 */
20class ChangesListBooleanFilterGroup extends ChangesListFilterGroup {
21    /**
22     * Type marker, used by JavaScript
23     */
24    public const TYPE = 'send_unselected_if_any';
25
26    /**
27     * Create a new filter group with the specified configuration
28     *
29     * @param array $groupDefinition Configuration of group
30     * * $groupDefinition['name'] string Group name
31     * * $groupDefinition['title'] string i18n key for title (optional, can be omitted
32     *     only if none of the filters in the group display in the structured UI)
33     * * $groupDefinition['priority'] int Priority integer.  Higher means higher in the
34     *     group list.
35     * * $groupDefinition['filters'] array Numeric array of filter definitions, each of which
36     *     is an associative array to be passed to the filter constructor.  However,
37     *    'priority' is optional for the filters.  Any filter that has priority unset
38     *     will be put to the bottom, in the order given.
39     * * $groupDefinition['whatsThisHeader'] string i18n key for header of "What's
40     *     This" popup (optional).
41     * * $groupDefinition['whatsThisBody'] string i18n key for body of "What's This"
42     *     popup (optional).
43     * * $groupDefinition['whatsThisUrl'] string URL for main link of "What's This"
44     *     popup (optional).
45     * * $groupDefinition['whatsThisLinkText'] string i18n key of text for main link of
46     *     "What's This" popup (optional).
47     */
48    public function __construct( array $groupDefinition ) {
49        $groupDefinition['isFullCoverage'] = true;
50        $groupDefinition['type'] = self::TYPE;
51
52        parent::__construct( $groupDefinition );
53    }
54
55    /**
56     * @inheritDoc
57     */
58    protected function createFilter( array $filterDefinition ) {
59        return new ChangesListBooleanFilter( $filterDefinition );
60    }
61
62    /**
63     * @since 1.45
64     * @param array $defaultValue
65     */
66    public function setDefault( $defaultValue ) {
67        if ( !is_array( $defaultValue ) ) {
68            throw new InvalidArgumentException(
69                "Can't set the default of filter options group \"{$this->getName()}\"" .
70                ' to a value of type "' . gettype( $defaultValue ) . ': expected bool[]' );
71        }
72        foreach ( $defaultValue as $name => $value ) {
73            if ( !is_bool( $value ) ) {
74                throw new InvalidArgumentException(
75                    "Can't set the default of filter option \"{$this->getName()}/$name\"" .
76                    ' to a value of type "' . gettype( $value ) . ': expected bool' );
77            }
78            $this->getFilter( $name )?->setDefault( $value );
79        }
80    }
81
82    /**
83     * Registers a filter in this group
84     *
85     * @param ChangesListBooleanFilter $filter
86     * @suppress PhanParamSignaturePHPDocMismatchHasParamType,PhanParamSignatureMismatch
87     */
88    public function registerFilter( ChangesListBooleanFilter $filter ) {
89        $this->filters[$filter->getName()] = $filter;
90    }
91
92    /**
93     * @inheritDoc
94     */
95    public function modifyQuery( IReadableDatabase $dbr, ChangesListSpecialPage $specialPage,
96        &$tables, &$fields, &$conds, &$query_options, &$join_conds,
97        FormOptions $opts, $isStructuredFiltersEnabled
98    ) {
99        foreach ( $this->getFilters() as $filter ) {
100            if ( $filter->isActive( $opts, $isStructuredFiltersEnabled ) ) {
101                $filter->modifyQuery( $dbr, $specialPage, $tables, $fields, $conds,
102                    $query_options, $join_conds );
103            }
104        }
105    }
106
107    /**
108     * @inheritDoc
109     */
110    public function addOptions( FormOptions $opts, $allowDefaults, $isStructuredFiltersEnabled ) {
111        foreach ( $this->getFilters() as $filter ) {
112            $defaultValue = $allowDefaults ? $filter->getDefault( $isStructuredFiltersEnabled ) : false;
113            $opts->add( $filter->getName(), $defaultValue );
114        }
115    }
116}
117
118/** @deprecated class alias since 1.44 */
119class_alias( ChangesListBooleanFilterGroup::class, 'ChangesListBooleanFilterGroup' );