Merge "Align "What's this" vertically"
[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 // This can sometimes be different on Special:RecentChanges
34 // and Special:Watchlist, due to the double-legacy hooks
35 // (SpecialRecentChangesFilters and SpecialWatchlistFilters)
36
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
42 *
43 * @var string $showHide
44 */
45 protected $showHide;
46
47 /**
48 * Whether there is a feature designed to replace this filter available on the
49 * structured UI
50 *
51 * @var bool $isReplacedInStructuredUi
52 */
53 protected $isReplacedInStructuredUi;
54
55 /**
56 * Default
57 *
58 * @var bool $defaultValue
59 */
60 protected $defaultValue;
61
62 /**
63 * Callable used to do the actual query modification; see constructor
64 *
65 * @var callable $queryCallable
66 */
67 protected $queryCallable;
68
69 /**
70 * Value that defined when this filter is considered active
71 *
72 * @var bool $activeValue
73 */
74 protected $activeValue;
75
76 /**
77 * Create a new filter with the specified configuration.
78 *
79 * It infers which UI (it can be either or both) to display the filter on based on
80 * which messages are provided.
81 *
82 * If 'label' is provided, it will be displayed on the structured UI. If
83 * 'showHide' is provided, it will be displayed on the unstructured UI. Thus,
84 * 'label', 'description', and 'showHide' are optional depending on which UI
85 * it's for.
86 *
87 * @param array $filterDefinition ChangesListFilter definition
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['activeValue'] bool This filter is considered active when
101 * its value is equal to its activeValue. Default is true.
102 * * $filterDefinition['priority'] int Priority integer. Higher value means higher
103 * up in the group's filter list.
104 * * $filterDefinition['queryCallable'] callable Callable accepting parameters, used
105 * to implement filter's DB query modification. Required, except for legacy
106 * filters that still use the query hooks directly. Callback parameters:
107 * * string $specialPageClassName Class name of current special page
108 * * IContextSource $context Context, for e.g. user
109 * * IDatabase $dbr Database, for addQuotes, makeList, and similar
110 * * array &$tables Array of tables; see IDatabase::select $table
111 * * array &$fields Array of fields; see IDatabase::select $vars
112 * * array &$conds Array of conditions; see IDatabase::select $conds
113 * * array &$query_options Array of query options; see IDatabase::select $options
114 * * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
115 */
116 public function __construct( $filterDefinition ) {
117 parent::__construct( $filterDefinition );
118
119 if ( isset( $filterDefinition['showHide'] ) ) {
120 $this->showHide = $filterDefinition['showHide'];
121 }
122
123 if ( isset( $filterDefinition['isReplacedInStructuredUi'] ) ) {
124 $this->isReplacedInStructuredUi = $filterDefinition['isReplacedInStructuredUi'];
125 } else {
126 $this->isReplacedInStructuredUi = false;
127 }
128
129 if ( isset( $filterDefinition['default'] ) ) {
130 $this->defaultValue = $filterDefinition['default'];
131 } else {
132 throw new MWException( 'You must set a default' );
133 }
134
135 if ( isset( $filterDefinition['queryCallable'] ) ) {
136 $this->queryCallable = $filterDefinition['queryCallable'];
137 }
138
139 if ( isset( $filterDefinition['activeValue'] ) ) {
140 $this->activeValue = $filterDefinition['activeValue'];
141 } else {
142 $this->activeValue = true;
143 }
144 }
145
146 /**
147 * Get the default value
148 *
149 * @param bool $structuredUI Are we currently showing the structured UI
150 * @return bool|null Default value
151 */
152 public function getDefault( $structuredUI = false ) {
153 return $this->isReplacedInStructuredUi && $structuredUI ?
154 !$this->activeValue :
155 $this->defaultValue;
156 }
157
158 /**
159 * Sets default
160 *
161 * @param bool $defaultValue
162 */
163 public function setDefault( $defaultValue ) {
164 $this->defaultValue = $defaultValue;
165 }
166
167 /**
168 * @return string Main i18n key for unstructured UI
169 */
170 public function getShowHide() {
171 return $this->showHide;
172 }
173
174 /**
175 * @inheritDoc
176 */
177 public function displaysOnUnstructuredUi() {
178 return !!$this->showHide;
179 }
180
181 /**
182 * @inheritDoc
183 */
184 public function isFeatureAvailableOnStructuredUi() {
185 return $this->isReplacedInStructuredUi ||
186 parent::isFeatureAvailableOnStructuredUi();
187 }
188
189 /**
190 * Modifies the query to include the filter. This is only called if the filter is
191 * in effect (taking into account the default).
192 *
193 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
194 * @param ChangesListSpecialPage $specialPage Current special page
195 * @param array &$tables Array of tables; see IDatabase::select $table
196 * @param array &$fields Array of fields; see IDatabase::select $vars
197 * @param array &$conds Array of conditions; see IDatabase::select $conds
198 * @param array &$query_options Array of query options; see IDatabase::select $options
199 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
200 */
201 public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
202 &$tables, &$fields, &$conds, &$query_options, &$join_conds
203 ) {
204 if ( $this->queryCallable === null ) {
205 return;
206 }
207
208 call_user_func_array(
209 $this->queryCallable,
210 [
211 get_class( $specialPage ),
212 $specialPage->getContext(),
213 $dbr,
214 &$tables,
215 &$fields,
216 &$conds,
217 &$query_options,
218 &$join_conds
219 ]
220 );
221 }
222
223 /**
224 * @inheritDoc
225 */
226 public function getJsData() {
227 $output = parent::getJsData();
228
229 $output['default'] = $this->defaultValue;
230
231 return $output;
232 }
233
234 /**
235 * @inheritDoc
236 */
237 public function isSelected( FormOptions $opts ) {
238 return !$opts[ $this->getName() ] &&
239 array_filter( $this->getSiblings(), function ( $sibling ) use ( $opts ) {
240 return $opts[ $sibling->getName() ];
241 } );
242 }
243
244 /**
245 * @param FormOptions $opts Query parameters merged with defaults
246 * @param bool $isStructuredUI Whether the structured UI is currently enabled
247 * @return bool Whether this filter should be considered active
248 */
249 public function isActive( FormOptions $opts, $isStructuredUI ) {
250 if ( $this->isReplacedInStructuredUi && $isStructuredUI ) {
251 return false;
252 }
253
254 return $opts[ $this->getName() ] === $this->activeValue;
255 }
256 }