Merge "Add attributes parameter to ShowSearchHitTitle"
[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->setDefault( $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. It must be a boolean.
160 *
161 * It will be coerced to boolean.
162 *
163 * @param bool $defaultValue
164 */
165 public function setDefault( $defaultValue ) {
166 $this->defaultValue = (bool)$defaultValue;
167 }
168
169 /**
170 * @return string Main i18n key for unstructured UI
171 */
172 public function getShowHide() {
173 return $this->showHide;
174 }
175
176 /**
177 * @inheritDoc
178 */
179 public function displaysOnUnstructuredUi() {
180 return !!$this->showHide;
181 }
182
183 /**
184 * @inheritDoc
185 */
186 public function isFeatureAvailableOnStructuredUi() {
187 return $this->isReplacedInStructuredUi ||
188 parent::isFeatureAvailableOnStructuredUi();
189 }
190
191 /**
192 * Modifies the query to include the filter. This is only called if the filter is
193 * in effect (taking into account the default).
194 *
195 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
196 * @param ChangesListSpecialPage $specialPage Current special page
197 * @param array &$tables Array of tables; see IDatabase::select $table
198 * @param array &$fields Array of fields; see IDatabase::select $vars
199 * @param array &$conds Array of conditions; see IDatabase::select $conds
200 * @param array &$query_options Array of query options; see IDatabase::select $options
201 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
202 */
203 public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
204 &$tables, &$fields, &$conds, &$query_options, &$join_conds
205 ) {
206 if ( $this->queryCallable === null ) {
207 return;
208 }
209
210 call_user_func_array(
211 $this->queryCallable,
212 [
213 get_class( $specialPage ),
214 $specialPage->getContext(),
215 $dbr,
216 &$tables,
217 &$fields,
218 &$conds,
219 &$query_options,
220 &$join_conds
221 ]
222 );
223 }
224
225 /**
226 * @inheritDoc
227 */
228 public function getJsData() {
229 $output = parent::getJsData();
230
231 $output['default'] = $this->defaultValue;
232
233 return $output;
234 }
235
236 /**
237 * @inheritDoc
238 */
239 public function isSelected( FormOptions $opts ) {
240 return !$opts[ $this->getName() ] &&
241 array_filter(
242 $this->getSiblings(),
243 function ( ChangesListBooleanFilter $sibling ) use ( $opts ) {
244 return $opts[ $sibling->getName() ];
245 }
246 );
247 }
248
249 /**
250 * @param FormOptions $opts Query parameters merged with defaults
251 * @param bool $isStructuredUI Whether the structured UI is currently enabled
252 * @return bool Whether this filter should be considered active
253 */
254 public function isActive( FormOptions $opts, $isStructuredUI ) {
255 if ( $this->isReplacedInStructuredUi && $isStructuredUI ) {
256 return false;
257 }
258
259 return $opts[ $this->getName() ] === $this->activeValue;
260 }
261 }