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
22 * @author Matthew Flaschen
25 use Wikimedia\Rdbms\IDatabase
;
28 * An individual filter in a boolean group
32 class ChangesListBooleanFilter
extends ChangesListFilter
{
33 // This can sometimes be different on Special:RecentChanges
34 // and Special:Watchlist, due to the double-legacy hooks
35 // (SpecialRecentChangesFilters and SpecialWatchlistFilters)
37 // but there will be separate sets of ChangesListFilterGroup and ChangesListFilter instances
38 // for those pages (it should work even if they're both loaded
39 // at once, but that can't happen).
41 * Main unstructured UI i18n key
43 * @var string $showHide
48 * Whether there is a feature designed to replace this filter available on the
51 * @var bool $isReplacedInStructuredUi
53 protected $isReplacedInStructuredUi;
58 * @var bool $defaultValue
60 protected $defaultValue;
63 * Callable used to do the actual query modification; see constructor
65 * @var callable $queryCallable
67 protected $queryCallable;
70 * Create a new filter with the specified configuration.
72 * It infers which UI (it can be either or both) to display the filter on based on
73 * which messages are provided.
75 * If 'label' is provided, it will be displayed on the structured UI. If
76 * 'showHide' is provided, it will be displayed on the unstructured UI. Thus,
77 * 'label', 'description', and 'showHide' are optional depending on which UI
80 * @param array $filterDefinition ChangesListFilter definition
81 * * $filterDefinition['name'] string Name. Used as URL parameter.
82 * * $filterDefinition['group'] ChangesListFilterGroup Group. Filter group this
84 * * $filterDefinition['label'] string i18n key of label for structured UI.
85 * * $filterDefinition['description'] string i18n key of description for structured
87 * * $filterDefinition['showHide'] string Main i18n key used for unstructured UI.
88 * * $filterDefinition['isReplacedInStructuredUi'] bool Whether there is an
89 * equivalent feature available in the structured UI; this is optional, defaulting
90 * to true. It does not need to be set if the exact same filter is simply visible
92 * * $filterDefinition['default'] bool Default
93 * * $filterDefinition['priority'] int Priority integer. Higher value means higher
94 * up in the group's filter list.
95 * * $filterDefinition['queryCallable'] callable Callable accepting parameters, used
96 * to implement filter's DB query modification. Required, except for legacy
97 * filters that still use the query hooks directly. Callback parameters:
98 * * string $specialPageClassName Class name of current special page
99 * * IContextSource $context Context, for e.g. user
100 * * IDatabase $dbr Database, for addQuotes, makeList, and similar
101 * * array &$tables Array of tables; see IDatabase::select $table
102 * * array &$fields Array of fields; see IDatabase::select $vars
103 * * array &$conds Array of conditions; see IDatabase::select $conds
104 * * array &$query_options Array of query options; see IDatabase::select $options
105 * * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
107 public function __construct( $filterDefinition ) {
108 parent
::__construct( $filterDefinition );
110 if ( isset( $filterDefinition['showHide'] ) ) {
111 $this->showHide
= $filterDefinition['showHide'];
114 if ( isset( $filterDefinition['isReplacedInStructuredUi'] ) ) {
115 $this->isReplacedInStructuredUi
= $filterDefinition['isReplacedInStructuredUi'];
117 $this->isReplacedInStructuredUi
= false;
120 if ( isset( $filterDefinition['default'] ) ) {
121 $this->defaultValue
= $filterDefinition['default'];
123 throw new MWException( 'You must set a default' );
126 if ( isset( $filterDefinition['queryCallable'] ) ) {
127 $this->queryCallable
= $filterDefinition['queryCallable'];
132 * Get the default value
134 * @param bool $structuredUI Are we currently showing the structured UI
135 * @return bool|null Default value
137 public function getDefault( $structuredUI = false ) {
138 return $this->isReplacedInStructuredUi
&& $structuredUI ?
146 * @param bool $defaultValue
148 public function setDefault( $defaultValue ) {
149 $this->defaultValue
= $defaultValue;
153 * @return string Main i18n key for unstructured UI
155 public function getShowHide() {
156 return $this->showHide
;
162 public function displaysOnUnstructuredUi() {
163 return !!$this->showHide
;
169 public function isFeatureAvailableOnStructuredUi() {
170 return $this->isReplacedInStructuredUi ||
171 parent
::isFeatureAvailableOnStructuredUi();
175 * Modifies the query to include the filter. This is only called if the filter is
176 * in effect (taking into account the default).
178 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
179 * @param ChangesListSpecialPage $specialPage Current special page
180 * @param array &$tables Array of tables; see IDatabase::select $table
181 * @param array &$fields Array of fields; see IDatabase::select $vars
182 * @param array &$conds Array of conditions; see IDatabase::select $conds
183 * @param array &$query_options Array of query options; see IDatabase::select $options
184 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
186 public function modifyQuery( IDatabase
$dbr, ChangesListSpecialPage
$specialPage,
187 &$tables, &$fields, &$conds, &$query_options, &$join_conds
189 if ( $this->queryCallable
=== null ) {
193 call_user_func_array(
194 $this->queryCallable
,
196 get_class( $specialPage ),
197 $specialPage->getContext(),
211 public function getJsData() {
212 $output = parent
::getJsData();
214 $output['default'] = $this->defaultValue
;
222 public function isSelected( FormOptions
$opts ) {
223 return !$opts[ $this->getName() ] &&
224 array_filter( $this->getSiblings(), function ( $sibling ) use ( $opts ) {
225 return $opts[ $sibling->getName() ];