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 * Value that defined when this filter is considered active
72 * @var bool $activeValue
74 protected $activeValue;
77 * Whether this filter is visible somewhere (legacy form or structured UI).
79 * @var bool $isVisible
84 * Create a new filter with the specified configuration.
86 * It infers which UI (it can be either or both) to display the filter on based on
87 * which messages are provided.
89 * If 'label' is provided, it will be displayed on the structured UI. If
90 * 'showHide' is provided, it will be displayed on the unstructured UI. Thus,
91 * 'label', 'description', and 'showHide' are optional depending on which UI
94 * @param array $filterDefinition ChangesListFilter definition
95 * * $filterDefinition['name'] string Name. Used as URL parameter.
96 * * $filterDefinition['group'] ChangesListFilterGroup Group. Filter group this
98 * * $filterDefinition['label'] string i18n key of label for structured UI.
99 * * $filterDefinition['description'] string i18n key of description for structured
101 * * $filterDefinition['showHide'] string Main i18n key used for unstructured UI.
102 * * $filterDefinition['isReplacedInStructuredUi'] bool Whether there is an
103 * equivalent feature available in the structured UI; this is optional, defaulting
104 * to true. It does not need to be set if the exact same filter is simply visible
106 * * $filterDefinition['default'] bool Default
107 * * $filterDefinition['activeValue'] bool This filter is considered active when
108 * its value is equal to its activeValue. Default is true.
109 * * $filterDefinition['isVisible'] bool This filter is visible in the legacy form or
110 * structured UI. Default is true.
111 * * $filterDefinition['priority'] int Priority integer. Higher value means higher
112 * up in the group's filter list.
113 * * $filterDefinition['queryCallable'] callable Callable accepting parameters, used
114 * to implement filter's DB query modification. Required, except for legacy
115 * filters that still use the query hooks directly. Callback parameters:
116 * * string $specialPageClassName Class name of current special page
117 * * IContextSource $context Context, for e.g. user
118 * * IDatabase $dbr Database, for addQuotes, makeList, and similar
119 * * array &$tables Array of tables; see IDatabase::select $table
120 * * array &$fields Array of fields; see IDatabase::select $vars
121 * * array &$conds Array of conditions; see IDatabase::select $conds
122 * * array &$query_options Array of query options; see IDatabase::select $options
123 * * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
125 public function __construct( $filterDefinition ) {
126 parent
::__construct( $filterDefinition );
128 if ( isset( $filterDefinition['showHide'] ) ) {
129 $this->showHide
= $filterDefinition['showHide'];
132 if ( isset( $filterDefinition['isReplacedInStructuredUi'] ) ) {
133 $this->isReplacedInStructuredUi
= $filterDefinition['isReplacedInStructuredUi'];
135 $this->isReplacedInStructuredUi
= false;
138 if ( isset( $filterDefinition['default'] ) ) {
139 $this->defaultValue
= $filterDefinition['default'];
141 throw new MWException( 'You must set a default' );
144 if ( isset( $filterDefinition['queryCallable'] ) ) {
145 $this->queryCallable
= $filterDefinition['queryCallable'];
148 if ( isset( $filterDefinition['activeValue'] ) ) {
149 $this->activeValue
= $filterDefinition['activeValue'];
151 $this->activeValue
= true;
154 if ( isset( $filterDefinition['isVisible'] ) ) {
155 $this->isVisible
= $filterDefinition['isVisible'];
157 $this->isVisible
= true;
162 * Get the default value
164 * @param bool $structuredUI Are we currently showing the structured UI
165 * @return bool|null Default value
167 public function getDefault( $structuredUI = false ) {
168 return $this->isReplacedInStructuredUi
&& $structuredUI ?
169 !$this->activeValue
:
176 * @param bool $defaultValue
178 public function setDefault( $defaultValue ) {
179 $this->defaultValue
= $defaultValue;
183 * @return string Main i18n key for unstructured UI
185 public function getShowHide() {
186 return $this->showHide
;
192 public function displaysOnUnstructuredUi() {
193 return !!$this->showHide
;
199 public function isFeatureAvailableOnStructuredUi() {
200 return $this->isReplacedInStructuredUi ||
201 parent
::isFeatureAvailableOnStructuredUi();
205 * Modifies the query to include the filter. This is only called if the filter is
206 * in effect (taking into account the default).
208 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
209 * @param ChangesListSpecialPage $specialPage Current special page
210 * @param array &$tables Array of tables; see IDatabase::select $table
211 * @param array &$fields Array of fields; see IDatabase::select $vars
212 * @param array &$conds Array of conditions; see IDatabase::select $conds
213 * @param array &$query_options Array of query options; see IDatabase::select $options
214 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
216 public function modifyQuery( IDatabase
$dbr, ChangesListSpecialPage
$specialPage,
217 &$tables, &$fields, &$conds, &$query_options, &$join_conds
219 if ( $this->queryCallable
=== null ) {
223 call_user_func_array(
224 $this->queryCallable
,
226 get_class( $specialPage ),
227 $specialPage->getContext(),
241 public function getJsData() {
242 $output = parent
::getJsData();
244 $output['default'] = $this->defaultValue
;
252 public function isSelected( FormOptions
$opts ) {
253 return !$opts[ $this->getName() ] &&
254 array_filter( $this->getSiblings(), function ( $sibling ) use ( $opts ) {
255 return $opts[ $sibling->getName() ];
260 * @param FormOptions $opts Query parameters merged with defaults
261 * @param bool $isStructuredUI Whether the structured UI is currently enabled
262 * @return bool Whether this filter should be considered active
264 public function isActive( FormOptions
$opts, $isStructuredUI ) {
265 if ( $this->isReplacedInStructuredUi
&& $isStructuredUI ) {
269 return $opts[ $this->getName() ] === $this->activeValue
;
273 * @return bool Whether this filter is visible anywhere
275 public function isVisible() {
276 return $this->isVisible
;