Block: Remove old IP addresses from default autoblock_whitelist message
[lhc/web/wiklou.git] / includes / changes / ChangesListBooleanFilter.php
1 <?php
2 /**
3 * Represents a hide-based boolean filter (used on ChangesListSpecialPage and descendants)
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @license GPL 2+
22 * @author Matthew Flaschen
23 */
24
25 use Wikimedia\Rdbms\IDatabase;
26
27 /**
28 * An individual filter in a boolean group
29 *
30 * @since 1.29
31 */
32 class ChangesListBooleanFilter extends ChangesListFilter {
33 /**
34 * Name. Used as URL parameter
35 *
36 * @var string $name
37 */
38
39 // This can sometimes be different on Special:RecentChanges
40 // and Special:Watchlist, due to the double-legacy hooks
41 // (SpecialRecentChangesFilters and SpecialWatchlistFilters)
42
43 // but there will be separate sets of ChangesListFilterGroup and ChangesListFilter instances
44 // for those pages (it should work even if they're both loaded
45 // at once, but that can't happen).
46 /**
47 * Main unstructured UI i18n key
48 *
49 * @var string $showHide
50 */
51 protected $showHide;
52
53 /**
54 * Whether there is a feature designed to replace this filter available on the
55 * structured UI
56 *
57 * @var bool $isReplacedInStructuredUi
58 */
59 protected $isReplacedInStructuredUi;
60
61 /**
62 * Default
63 *
64 * @var bool $defaultValue
65 */
66 protected $defaultValue;
67
68 /**
69 * Callable used to do the actual query modification; see constructor
70 *
71 * @var callable $queryCallable
72 */
73 protected $queryCallable;
74
75 /**
76 * Create a new filter with the specified configuration.
77 *
78 * It infers which UI (it can be either or both) to display the filter on based on
79 * which messages are provided.
80 *
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
84 * it's for.
85 *
86 * @param array $filterDefinition ChangesListFilter definition
87 *
88 * $filterDefinition['name'] string Name. Used as URL parameter.
89 * $filterDefinition['group'] ChangesListFilterGroup Group. Filter group this
90 * belongs to.
91 * $filterDefinition['label'] string i18n key of label for structured UI.
92 * $filterDefinition['description'] string i18n key of description for structured
93 * UI.
94 * $filterDefinition['showHide'] string Main i18n key used for unstructured UI.
95 * $filterDefinition['isReplacedInStructuredUi'] bool Whether there is an
96 * equivalent feature available in the structured UI; this is optional, defaulting
97 * to true. It does not need to be set if the exact same filter is simply visible
98 * on both.
99 * $filterDefinition['default'] bool Default
100 * $filterDefinition['priority'] int Priority integer. Higher value means higher
101 * up in the group's filter list.
102 * $filterDefinition['queryCallable'] callable Callable accepting parameters, used
103 * to implement filter's DB query modification. Callback parameters:
104 * string $specialPageClassName Class name of current special page
105 * IContextSource $context Context, for e.g. user
106 * IDatabase $dbr Database, for addQuotes, makeList, and similar
107 * array &$tables Array of tables; see IDatabase::select $table
108 * array &$fields Array of fields; see IDatabase::select $vars
109 * array &$conds Array of conditions; see IDatabase::select $conds
110 * array &$query_options Array of query options; see IDatabase::select $options
111 * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
112 * Optional only for legacy filters that still use the query hooks directly
113 */
114 public function __construct( $filterDefinition ) {
115 parent::__construct( $filterDefinition );
116
117 if ( isset( $filterDefinition['showHide'] ) ) {
118 $this->showHide = $filterDefinition['showHide'];
119 }
120
121 if ( isset( $filterDefinition['isReplacedInStructuredUi'] ) ) {
122 $this->isReplacedInStructuredUi = $filterDefinition['isReplacedInStructuredUi'];
123 } else {
124 $this->isReplacedInStructuredUi = false;
125 }
126
127 if ( isset( $filterDefinition['default'] ) ) {
128 $this->defaultValue = $filterDefinition['default'];
129 } else {
130 throw new MWException( 'You must set a default' );
131 }
132
133 if ( isset( $filterDefinition['queryCallable'] ) ) {
134 $this->queryCallable = $filterDefinition['queryCallable'];
135 }
136 }
137
138 /**
139 * Get the default value
140 *
141 * @param bool $structuredUI Are we currently showing the structured UI
142 * @return bool|null Default value
143 */
144 public function getDefault( $structuredUI = false ) {
145 return $this->isReplacedInStructuredUi && $structuredUI ?
146 false :
147 $this->defaultValue;
148 }
149
150 /**
151 * Sets default
152 *
153 * @param bool Default value
154 */
155 public function setDefault( $defaultValue ) {
156 $this->defaultValue = $defaultValue;
157 }
158
159 /**
160 * @return string Main i18n key for unstructured UI
161 */
162 public function getShowHide() {
163 return $this->showHide;
164 }
165
166 /**
167 * @inheritdoc
168 */
169 public function displaysOnUnstructuredUi() {
170 return !!$this->showHide;
171 }
172
173 /**
174 * @inheritdoc
175 */
176 public function isFeatureAvailableOnStructuredUi() {
177 return $this->isReplacedInStructuredUi ||
178 parent::isFeatureAvailableOnStructuredUi();
179 }
180
181 /**
182 * Modifies the query to include the filter. This is only called if the filter is
183 * in effect (taking into account the default).
184 *
185 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
186 * @param ChangesListSpecialPage $specialPage Current special page
187 * @param array &$tables Array of tables; see IDatabase::select $table
188 * @param array &$fields Array of fields; see IDatabase::select $vars
189 * @param array &$conds Array of conditions; see IDatabase::select $conds
190 * @param array &$query_options Array of query options; see IDatabase::select $options
191 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
192 */
193 public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
194 &$tables, &$fields, &$conds, &$query_options, &$join_conds ) {
195
196 if ( $this->queryCallable === null ) {
197 return;
198 }
199
200 call_user_func_array(
201 $this->queryCallable,
202 [
203 get_class( $specialPage ),
204 $specialPage->getContext(),
205 $dbr,
206 &$tables,
207 &$fields,
208 &$conds,
209 &$query_options,
210 &$join_conds
211 ]
212 );
213 }
214
215 /**
216 * @inheritdoc
217 */
218 public function getJsData() {
219 $output = parent::getJsData();
220
221 $output['default'] = $this->defaultValue;
222
223 return $output;
224 }
225
226 }