rdbms: Avoid selectDB() call in LoadMonitor new connections
[mediawiki.git] / includes / changes / ChangesListBooleanFilter.php
blob01e67f5079b2fb40fe2a889a11e5074fef7641c4
1 <?php
2 /**
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
20 * @file
21 * @license GPL 2+
22 * @author Matthew Flaschen
25 use Wikimedia\Rdbms\IDatabase;
27 /**
28 * An individual filter in a boolean group
30 * @since 1.29
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).
40 /**
41 * Main unstructured UI i18n key
43 * @var string $showHide
45 protected $showHide;
47 /**
48 * Whether there is a feature designed to replace this filter available on the
49 * structured UI
51 * @var bool $isReplacedInStructuredUi
53 protected $isReplacedInStructuredUi;
55 /**
56 * Default
58 * @var bool $defaultValue
60 protected $defaultValue;
62 /**
63 * Callable used to do the actual query modification; see constructor
65 * @var callable $queryCallable
67 protected $queryCallable;
69 /**
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
78 * it's for.
80 * @param array $filterDefinition ChangesListFilter definition
81 * * $filterDefinition['name'] string Name. Used as URL parameter.
82 * * $filterDefinition['group'] ChangesListFilterGroup Group. Filter group this
83 * belongs to.
84 * * $filterDefinition['label'] string i18n key of label for structured UI.
85 * * $filterDefinition['description'] string i18n key of description for structured
86 * UI.
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
91 * on both.
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'];
116 } else {
117 $this->isReplacedInStructuredUi = false;
120 if ( isset( $filterDefinition['default'] ) ) {
121 $this->defaultValue = $filterDefinition['default'];
122 } else {
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 ?
139 false :
140 $this->defaultValue;
144 * Sets default
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;
160 * @inheritDoc
162 public function displaysOnUnstructuredUi() {
163 return !!$this->showHide;
167 * @inheritDoc
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 ) {
190 return;
193 call_user_func_array(
194 $this->queryCallable,
196 get_class( $specialPage ),
197 $specialPage->getContext(),
198 $dbr,
199 &$tables,
200 &$fields,
201 &$conds,
202 &$query_options,
203 &$join_conds
209 * @inheritDoc
211 public function getJsData() {
212 $output = parent::getJsData();
214 $output['default'] = $this->defaultValue;
216 return $output;
220 * @inheritDoc
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() ];
226 } );