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 if ( isset( $filterDefinition['isReplacedInStructuredUi'] ) ) {
116 $this->isReplacedInStructuredUi
= $filterDefinition['isReplacedInStructuredUi'];
118 $this->isReplacedInStructuredUi
= false;
121 if ( isset( $filterDefinition['default'] ) ) {
122 $this->setDefault( $filterDefinition['default'] );
124 throw new MWException( 'You must set a default' );
127 if ( isset( $filterDefinition['queryCallable'] ) ) {
128 $this->queryCallable
= $filterDefinition['queryCallable'];
131 if ( isset( $filterDefinition['activeValue'] ) ) {
132 $this->activeValue
= $filterDefinition['activeValue'];
134 $this->activeValue
= true;
139 * Get the default value
141 * @param bool $structuredUI Are we currently showing the structured UI
142 * @return bool|null Default value
144 public function getDefault( $structuredUI = false ) {
145 return $this->isReplacedInStructuredUi
&& $structuredUI ?
146 !$this->activeValue
:
151 * Sets default. It must be a boolean.
153 * It will be coerced to boolean.
155 * @param bool $defaultValue
157 public function setDefault( $defaultValue ) {
158 $this->defaultValue
= (bool)$defaultValue;
162 * @return string Main i18n key for unstructured UI
164 public function getShowHide() {
165 return $this->showHide
;
171 public function displaysOnUnstructuredUi() {
172 return !!$this->showHide
;
178 public function isFeatureAvailableOnStructuredUi() {
179 return $this->isReplacedInStructuredUi ||
180 parent
::isFeatureAvailableOnStructuredUi();
184 * Modifies the query to include the filter. This is only called if the filter is
185 * in effect (taking into account the default).
187 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
188 * @param ChangesListSpecialPage $specialPage Current special page
189 * @param array &$tables Array of tables; see IDatabase::select $table
190 * @param array &$fields Array of fields; see IDatabase::select $vars
191 * @param array &$conds Array of conditions; see IDatabase::select $conds
192 * @param array &$query_options Array of query options; see IDatabase::select $options
193 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
195 public function modifyQuery( IDatabase
$dbr, ChangesListSpecialPage
$specialPage,
196 &$tables, &$fields, &$conds, &$query_options, &$join_conds
198 if ( $this->queryCallable
=== null ) {
202 ( $this->queryCallable
)(
203 get_class( $specialPage ),
204 $specialPage->getContext(),
217 public function getJsData() {
218 $output = parent
::getJsData();
220 $output['default'] = $this->defaultValue
;
228 public function isSelected( FormOptions
$opts ) {
229 return !$opts[ $this->getName() ] &&
231 $this->getSiblings(),
232 function ( ChangesListBooleanFilter
$sibling ) use ( $opts ) {
233 return $opts[ $sibling->getName() ];
239 * @param FormOptions $opts Query parameters merged with defaults
240 * @param bool $isStructuredUI Whether the structured UI is currently enabled
241 * @return bool Whether this filter should be considered active
243 public function isActive( FormOptions
$opts, $isStructuredUI ) {
244 if ( $this->isReplacedInStructuredUi
&& $isStructuredUI ) {
248 return $opts[ $this->getName() ] === $this->activeValue
;