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
{
32 // This can sometimes be different on Special:RecentChanges
33 // and Special:Watchlist, due to the double-legacy hooks
34 // (SpecialRecentChangesFilters and SpecialWatchlistFilters)
36 // but there will be separate sets of ChangesListFilterGroup and ChangesListFilter instances
37 // for those pages (it should work even if they're both loaded
38 // at once, but that can't happen).
40 * Main unstructured UI i18n key
42 * @var string $showHide
47 * Whether there is a feature designed to replace this filter available on the
50 * @var bool $isReplacedInStructuredUi
52 protected $isReplacedInStructuredUi;
57 * @var bool $defaultValue
59 protected $defaultValue;
62 * Callable used to do the actual query modification; see constructor
64 * @var callable $queryCallable
66 protected $queryCallable;
69 * Value that defined when this filter is considered active
71 * @var bool $activeValue
73 protected $activeValue;
76 * Create a new filter with the specified configuration.
78 * It infers which UI (it can be either or both) to display the filter on based on
79 * which messages are provided.
81 * If 'label' is provided, it will be displayed on the structured UI. If
82 * 'showHide' is provided, it will be displayed on the unstructured UI. Thus,
83 * 'label', 'description', and 'showHide' are optional depending on which UI
86 * @param array $filterDefinition ChangesListFilter definition
87 * * $filterDefinition['name'] string Name. Used as URL parameter.
88 * * $filterDefinition['group'] ChangesListFilterGroup Group. Filter group this
90 * * $filterDefinition['label'] string i18n key of label for structured UI.
91 * * $filterDefinition['description'] string i18n key of description for structured
93 * * $filterDefinition['showHide'] string Main i18n key used for unstructured UI.
94 * * $filterDefinition['isReplacedInStructuredUi'] bool Whether there is an
95 * equivalent feature available in the structured UI; this is optional, defaulting
96 * to true. It does not need to be set if the exact same filter is simply visible
98 * * $filterDefinition['default'] bool Default
99 * * $filterDefinition['activeValue'] bool This filter is considered active when
100 * its value is equal to its activeValue. Default is true.
101 * * $filterDefinition['priority'] int Priority integer. Higher value means higher
102 * up in the group's filter list.
103 * * $filterDefinition['queryCallable'] callable Callable accepting parameters, used
104 * to implement filter's DB query modification. Required, except for legacy
105 * filters that still use the query hooks directly. Callback parameters:
106 * * string $specialPageClassName Class name of current special page
107 * * IContextSource $context Context, for e.g. user
108 * * IDatabase $dbr Database, for addQuotes, makeList, and similar
109 * * array &$tables Array of tables; see IDatabase::select $table
110 * * array &$fields Array of fields; see IDatabase::select $vars
111 * * array &$conds Array of conditions; see IDatabase::select $conds
112 * * array &$query_options Array of query options; see IDatabase::select $options
113 * * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
115 public function __construct( $filterDefinition ) {
116 parent
::__construct( $filterDefinition );
118 if ( isset( $filterDefinition['showHide'] ) ) {
119 $this->showHide
= $filterDefinition['showHide'];
122 if ( isset( $filterDefinition['isReplacedInStructuredUi'] ) ) {
123 $this->isReplacedInStructuredUi
= $filterDefinition['isReplacedInStructuredUi'];
125 $this->isReplacedInStructuredUi
= false;
128 if ( isset( $filterDefinition['default'] ) ) {
129 $this->setDefault( $filterDefinition['default'] );
131 throw new MWException( 'You must set a default' );
134 if ( isset( $filterDefinition['queryCallable'] ) ) {
135 $this->queryCallable
= $filterDefinition['queryCallable'];
138 if ( isset( $filterDefinition['activeValue'] ) ) {
139 $this->activeValue
= $filterDefinition['activeValue'];
141 $this->activeValue
= true;
146 * Get the default value
148 * @param bool $structuredUI Are we currently showing the structured UI
149 * @return bool|null Default value
151 public function getDefault( $structuredUI = false ) {
152 return $this->isReplacedInStructuredUi
&& $structuredUI ?
153 !$this->activeValue
:
158 * Sets default. It must be a boolean.
160 * It will be coerced to boolean.
162 * @param bool $defaultValue
164 public function setDefault( $defaultValue ) {
165 $this->defaultValue
= (bool)$defaultValue;
169 * @return string Main i18n key for unstructured UI
171 public function getShowHide() {
172 return $this->showHide
;
178 public function displaysOnUnstructuredUi() {
179 return !!$this->showHide
;
185 public function isFeatureAvailableOnStructuredUi() {
186 return $this->isReplacedInStructuredUi ||
187 parent
::isFeatureAvailableOnStructuredUi();
191 * Modifies the query to include the filter. This is only called if the filter is
192 * in effect (taking into account the default).
194 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
195 * @param ChangesListSpecialPage $specialPage Current special page
196 * @param array &$tables Array of tables; see IDatabase::select $table
197 * @param array &$fields Array of fields; see IDatabase::select $vars
198 * @param array &$conds Array of conditions; see IDatabase::select $conds
199 * @param array &$query_options Array of query options; see IDatabase::select $options
200 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
202 public function modifyQuery( IDatabase
$dbr, ChangesListSpecialPage
$specialPage,
203 &$tables, &$fields, &$conds, &$query_options, &$join_conds
205 if ( $this->queryCallable
=== null ) {
209 ( $this->queryCallable
)(
210 get_class( $specialPage ),
211 $specialPage->getContext(),
224 public function getJsData() {
225 $output = parent
::getJsData();
227 $output['default'] = $this->defaultValue
;
235 public function isSelected( FormOptions
$opts ) {
236 return !$opts[ $this->getName() ] &&
238 $this->getSiblings(),
239 function ( ChangesListBooleanFilter
$sibling ) use ( $opts ) {
240 return $opts[ $sibling->getName() ];
246 * @param FormOptions $opts Query parameters merged with defaults
247 * @param bool $isStructuredUI Whether the structured UI is currently enabled
248 * @return bool Whether this filter should be considered active
250 public function isActive( FormOptions
$opts, $isStructuredUI ) {
251 if ( $this->isReplacedInStructuredUi
&& $isStructuredUI ) {
255 return $opts[ $this->getName() ] === $this->activeValue
;