d0c4b7752752ee2c7aba68a3add5e5caf40a9ede
[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['isAllowedCallable'] callable Callable taking two parameters,
101 * the class name of the special page and an IContextSource, and returning true
102 * if and only if the current user is permitted to use this filter on the current
103 * wiki. If it returns false, it will both hide the UI (in all UIs) and prevent
104 * the DB query modification from taking effect. (optional, defaults to allowed)
105 * $filterDefinition['priority'] int Priority integer. Higher value means higher
106 * up in the group's filter list.
107 * $filterDefinition['queryCallable'] callable Callable accepting parameters, used
108 * to implement filter's DB query modification. Callback parameters:
109 * string $specialPageClassName Class name of current special page
110 * IContextSource $context Context, for e.g. user
111 * IDatabase $dbr Database, for addQuotes, makeList, and similar
112 * array &$tables Array of tables; see IDatabase::select $table
113 * array &$fields Array of fields; see IDatabase::select $vars
114 * array &$conds Array of conditions; see IDatabase::select $conds
115 * array &$query_options Array of query options; see IDatabase::select $options
116 * array &$join_conds Array of join conditions; see IDatabase::select $join_conds
117 * Optional only for legacy filters that still use the query hooks directly
118 */
119 public function __construct( $filterDefinition ) {
120 parent::__construct( $filterDefinition );
121
122 if ( isset( $filterDefinition['showHide'] ) ) {
123 $this->showHide = $filterDefinition['showHide'];
124 }
125
126 if ( isset( $filterDefinition['isReplacedInStructuredUi'] ) ) {
127 $this->isReplacedInStructuredUi = $filterDefinition['isReplacedInStructuredUi'];
128 } else {
129 $this->isReplacedInStructuredUi = false;
130 }
131
132 if ( isset( $filterDefinition['default'] ) ) {
133 $this->defaultValue = $filterDefinition['default'];
134 } else {
135 throw new MWException( 'You must set a default' );
136 }
137
138 if ( isset( $filterDefinition['queryCallable'] ) ) {
139 $this->queryCallable = $filterDefinition['queryCallable'];
140 }
141 }
142
143 /**
144 * @return bool|null Default value
145 */
146 public function getDefault() {
147 return $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( ChangesListSpecialPage $specialPage ) {
170 return $this->showHide &&
171 $this->isAllowed( $specialPage );
172 }
173
174 /**
175 * @inheritdoc
176 */
177 public function isFeatureAvailableOnStructuredUi( ChangesListSpecialPage $specialPage ) {
178 return $this->isReplacedInStructuredUi ||
179 parent::isFeatureAvailableOnStructuredUi( $specialPage );
180 }
181
182 /**
183 * Modifies the query to include the filter. This is only called if the filter is
184 * in effect (taking into account the default).
185 *
186 * @param IDatabase $dbr Database, for addQuotes, makeList, and similar
187 * @param ChangesListSpecialPage $specialPage Current special page
188 * @param array &$tables Array of tables; see IDatabase::select $table
189 * @param array &$fields Array of fields; see IDatabase::select $vars
190 * @param array &$conds Array of conditions; see IDatabase::select $conds
191 * @param array &$query_options Array of query options; see IDatabase::select $options
192 * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
193 */
194 public function modifyQuery( IDatabase $dbr, ChangesListSpecialPage $specialPage,
195 &$tables, &$fields, &$conds, &$query_options, &$join_conds ) {
196
197 if ( $this->queryCallable === null ) {
198 return;
199 }
200
201 call_user_func_array(
202 $this->queryCallable,
203 [
204 get_class( $specialPage ),
205 $specialPage->getContext(),
206 $dbr,
207 &$tables,
208 &$fields,
209 &$conds,
210 &$query_options,
211 &$join_conds
212 ]
213 );
214 }
215
216 /**
217 * @inheritdoc
218 */
219 public function getJsData() {
220 $output = parent::getJsData();
221
222 $output['default'] = $this->defaultValue;
223
224 return $output;
225 }
226
227 }