3 * Represents a hide-based boolean filter (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
21 * @author Matthew Flaschen
24 use Wikimedia\Rdbms\IDatabase
;
27 * Represents a hide-based boolean filter (used on ChangesListSpecialPage and descendants)
31 class ChangesListBooleanFilter
extends ChangesListFilter
{
33 * Main unstructured UI i18n key
35 * @var string $showHide
40 * Whether there is a feature designed to replace this filter available on the
43 * @var bool $isReplacedInStructuredUi
45 protected $isReplacedInStructuredUi;
50 * @var bool $defaultValue
52 protected $defaultValue;
55 * Callable used to do the actual query modification; see constructor
57 * @var callable $queryCallable
59 protected $queryCallable;
62 * Value that defined when this filter is considered active
64 * @var bool $activeValue
66 protected $activeValue;
69 * Create a new filter with the specified configuration.
71 * It infers which UI (it can be either or both) to display the filter on based on
72 * which messages are provided.
74 * If 'label' is provided, it will be displayed on the structured UI. If
75 * 'showHide' is provided, it will be displayed on the unstructured UI. Thus,
76 * 'label', 'description', and 'showHide' are optional depending on which UI
79 * @param array $filterDefinition ChangesListFilter definition
80 * * $filterDefinition['name'] string Name. Used as URL parameter.
81 * * $filterDefinition['group'] ChangesListFilterGroup Group. Filter group this
83 * * $filterDefinition['label'] string i18n key of label for structured UI.
84 * * $filterDefinition['description'] string i18n key of description for structured
86 * * $filterDefinition['showHide'] string Main i18n key used for unstructured UI.
87 * * $filterDefinition['isReplacedInStructuredUi'] bool Whether there is an
88 * equivalent feature available in the structured UI; this is optional, defaulting
89 * to true. It does not need to be set if the exact same filter is simply visible
91 * * $filterDefinition['default'] bool Default
92 * * $filterDefinition['activeValue'] bool This filter is considered active when
93 * its value is equal to its activeValue. Default is true.
94 * * $filterDefinition['priority'] int Priority integer. Higher value means higher
95 * up in the group's filter list.
96 * * $filterDefinition['queryCallable'] callable Callable accepting parameters, used
97 * to implement filter's DB query modification. Required, except for legacy
98 * filters that still use the query hooks directly. Callback parameters:
99 * * string $specialPageClassName Class name of current special page
100 * * IContextSource $context Context, for e.g. user
101 * * IDatabase $dbr Database, for addQuotes, makeList, and similar
102 * * array &$tables Array of tables; see IDatabase::select $table
103 * * array &$fields Array of fields; see IDatabase::select $vars
104 * * array &$conds Array of conditions; see IDatabase::select $conds
105 * * array &$query_options Array of query options; see IDatabase::select $options
106 * * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
108 public function __construct( $filterDefinition ) {
109 parent
::__construct( $filterDefinition );
111 if ( isset( $filterDefinition['showHide'] ) ) {
112 $this->showHide
= $filterDefinition['showHide'];
115 $this->isReplacedInStructuredUi
= $filterDefinition['isReplacedInStructuredUi'] ??
false;
117 if ( isset( $filterDefinition['default'] ) ) {
118 $this->setDefault( $filterDefinition['default'] );
120 throw new MWException( 'You must set a default' );
123 if ( isset( $filterDefinition['queryCallable'] ) ) {
124 $this->queryCallable
= $filterDefinition['queryCallable'];
127 $this->activeValue
= $filterDefinition['activeValue'] ??
true;
131 * Get the default value
133 * @param bool $structuredUI Are we currently showing the structured UI
134 * @return bool|null Default value
136 public function getDefault( $structuredUI = false ) {
137 return $this->isReplacedInStructuredUi
&& $structuredUI ?
138 !$this->activeValue
:
143 * Sets default. It must be a boolean.
145 * It will be coerced to boolean.
147 * @param bool $defaultValue
149 public function setDefault( $defaultValue ) {
150 $this->defaultValue
= (bool)$defaultValue;
154 * @return string Main i18n key for unstructured UI
156 public function getShowHide() {
157 return $this->showHide
;
163 public function displaysOnUnstructuredUi() {
164 return (bool)$this->showHide
;
170 public function isFeatureAvailableOnStructuredUi() {
171 return $this->isReplacedInStructuredUi ||
172 parent
::isFeatureAvailableOnStructuredUi();
176 * Modifies the query to include the filter. This is only called if the filter is
177 * in effect (taking into account the default).
179 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
180 * @param ChangesListSpecialPage $specialPage Current special page
181 * @param array &$tables Array of tables; see IDatabase::select $table
182 * @param array &$fields Array of fields; see IDatabase::select $vars
183 * @param array &$conds Array of conditions; see IDatabase::select $conds
184 * @param array &$query_options Array of query options; see IDatabase::select $options
185 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
187 public function modifyQuery( IDatabase
$dbr, ChangesListSpecialPage
$specialPage,
188 &$tables, &$fields, &$conds, &$query_options, &$join_conds
190 if ( $this->queryCallable
=== null ) {
194 ( $this->queryCallable
)(
195 get_class( $specialPage ),
196 $specialPage->getContext(),
209 public function getJsData() {
210 $output = parent
::getJsData();
212 $output['default'] = $this->defaultValue
;
220 public function isSelected( FormOptions
$opts ) {
221 return !$opts[ $this->getName() ] &&
223 $this->getSiblings(),
224 function ( ChangesListBooleanFilter
$sibling ) use ( $opts ) {
225 return $opts[ $sibling->getName() ];
231 * @param FormOptions $opts Query parameters merged with defaults
232 * @param bool $isStructuredUI Whether the structured UI is currently enabled
233 * @return bool Whether this filter should be considered active
235 public function isActive( FormOptions
$opts, $isStructuredUI ) {
236 if ( $this->isReplacedInStructuredUi
&& $isStructuredUI ) {
240 return $opts[ $this->getName() ] === $this->activeValue
;