Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 134 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
ApiOptionsBase | |
0.00% |
0 / 133 |
|
0.00% |
0 / 16 |
2352 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
272 | |||
runHook | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
shouldIgnoreKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPrefsKinds | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHtmlForm | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
validate | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
240 | |||
getUserForUpdates | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getUserForUpdatesOrNull | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getPreferences | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getUserOptionsManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPreferencesFactory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
resetPreferences | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
setPreference | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
commitChanges | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
2 | |||
needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Copyright © 2012 Szymon Świerkosz beau@adres.pl |
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 | */ |
22 | |
23 | namespace MediaWiki\Api; |
24 | |
25 | use MediaWiki\HTMLForm\HTMLForm; |
26 | use MediaWiki\Logger\LoggerFactory; |
27 | use MediaWiki\Message\Message; |
28 | use MediaWiki\Preferences\DefaultPreferencesFactory; |
29 | use MediaWiki\Preferences\PreferencesFactory; |
30 | use MediaWiki\User\Options\UserOptionsManager; |
31 | use MediaWiki\User\User; |
32 | use Wikimedia\ParamValidator\ParamValidator; |
33 | |
34 | /** |
35 | * The base class for core's ApiOptions and two modules in the GlobalPreferences |
36 | * extension. |
37 | * |
38 | * @ingroup API |
39 | */ |
40 | abstract class ApiOptionsBase extends ApiBase { |
41 | /** @var User User account to modify */ |
42 | private $userForUpdates; |
43 | |
44 | private UserOptionsManager $userOptionsManager; |
45 | private PreferencesFactory $preferencesFactory; |
46 | |
47 | /** @var mixed[][]|null */ |
48 | private $preferences; |
49 | |
50 | /** @var HTMLForm|null */ |
51 | private $htmlForm; |
52 | |
53 | /** @var string[]|null */ |
54 | private $prefsKinds; |
55 | |
56 | public function __construct( |
57 | ApiMain $main, |
58 | string $action, |
59 | UserOptionsManager $userOptionsManager, |
60 | PreferencesFactory $preferencesFactory |
61 | ) { |
62 | parent::__construct( $main, $action ); |
63 | $this->userOptionsManager = $userOptionsManager; |
64 | $this->preferencesFactory = $preferencesFactory; |
65 | } |
66 | |
67 | /** |
68 | * Changes preferences of the current user. |
69 | */ |
70 | public function execute() { |
71 | $user = $this->getUserForUpdatesOrNull(); |
72 | if ( !$user || !$user->isNamed() ) { |
73 | $this->dieWithError( |
74 | [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin' |
75 | ); |
76 | } |
77 | |
78 | $this->checkUserRightsAny( 'editmyoptions' ); |
79 | |
80 | $params = $this->extractRequestParams(); |
81 | $changed = false; |
82 | |
83 | if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) { |
84 | $this->dieWithError( [ 'apierror-missingparam', 'optionname' ] ); |
85 | } |
86 | |
87 | $resetKinds = $params['resetkinds']; |
88 | if ( !$params['reset'] ) { |
89 | $resetKinds = []; |
90 | } |
91 | |
92 | $changes = []; |
93 | if ( $params['change'] ) { |
94 | foreach ( $params['change'] as $entry ) { |
95 | $array = explode( '=', $entry, 2 ); |
96 | $changes[$array[0]] = $array[1] ?? null; |
97 | } |
98 | } |
99 | if ( isset( $params['optionname'] ) ) { |
100 | $newValue = $params['optionvalue'] ?? null; |
101 | $changes[$params['optionname']] = $newValue; |
102 | } |
103 | |
104 | $this->runHook( $user, $changes, $resetKinds ); |
105 | |
106 | if ( $resetKinds ) { |
107 | $this->resetPreferences( $resetKinds ); |
108 | $changed = true; |
109 | } |
110 | |
111 | if ( !$changed && !count( $changes ) ) { |
112 | $this->dieWithError( 'apierror-nochanges' ); |
113 | } |
114 | |
115 | $this->prefsKinds = $this->preferencesFactory->getResetKinds( $user, $this->getContext(), $changes ); |
116 | |
117 | foreach ( $changes as $key => $value ) { |
118 | if ( $this->shouldIgnoreKey( $key ) ) { |
119 | continue; |
120 | } |
121 | $validation = $this->validate( $key, $value ); |
122 | if ( $validation === true ) { |
123 | $this->setPreference( $key, $value ); |
124 | $changed = true; |
125 | } else { |
126 | $this->addWarning( [ 'apiwarn-validationfailed', wfEscapeWikiText( $key ), $validation ] ); |
127 | } |
128 | } |
129 | |
130 | if ( $changed ) { |
131 | $this->commitChanges(); |
132 | } |
133 | |
134 | $this->getResult()->addValue( null, $this->getModuleName(), 'success' ); |
135 | } |
136 | |
137 | /** |
138 | * Run the ApiOptions hook if applicable |
139 | * |
140 | * @param User $user |
141 | * @param string[] $changes |
142 | * @param string[] $resetKinds |
143 | */ |
144 | protected function runHook( $user, $changes, $resetKinds ) { |
145 | } |
146 | |
147 | /** |
148 | * Check whether a key should be ignored. |
149 | * |
150 | * This may be overridden to emit a warning as well as returning true. |
151 | * |
152 | * @param string $key |
153 | * @return bool |
154 | */ |
155 | protected function shouldIgnoreKey( $key ) { |
156 | return false; |
157 | } |
158 | |
159 | /** |
160 | * Get the preference kinds for the current user's options. |
161 | * This can only be called after $this->prefsKinds is set in execute() |
162 | * |
163 | * @return string[] |
164 | */ |
165 | protected function getPrefsKinds(): array { |
166 | return $this->prefsKinds; |
167 | } |
168 | |
169 | /** |
170 | * Get the HTMLForm for the user's preferences |
171 | * |
172 | * @return HTMLForm |
173 | */ |
174 | protected function getHtmlForm() { |
175 | if ( !$this->htmlForm ) { |
176 | $this->htmlForm = new HTMLForm( |
177 | DefaultPreferencesFactory::simplifyFormDescriptor( $this->getPreferences() ), |
178 | $this |
179 | ); |
180 | } |
181 | return $this->htmlForm; |
182 | } |
183 | |
184 | /** |
185 | * Validate a proposed change |
186 | * |
187 | * @param string $key |
188 | * @param mixed &$value |
189 | * @return bool|\MediaWiki\Message\Message|string |
190 | */ |
191 | protected function validate( $key, &$value ) { |
192 | switch ( $this->getPrefsKinds()[$key] ) { |
193 | case 'registered': |
194 | // Regular option. |
195 | if ( $value === null ) { |
196 | // Reset it |
197 | $validation = true; |
198 | } else { |
199 | // Validate |
200 | $field = $this->getHtmlForm()->getField( $key ); |
201 | $validation = $field->validate( |
202 | $value, |
203 | $this->userOptionsManager->getOptions( $this->getUserForUpdates() ) |
204 | ); |
205 | } |
206 | break; |
207 | case 'registered-multiselect': |
208 | case 'registered-checkmatrix': |
209 | // A key for a multiselect or checkmatrix option. |
210 | // TODO: Apply validation properly. |
211 | $validation = true; |
212 | $value = $value !== null ? (bool)$value : null; |
213 | break; |
214 | case 'userjs': |
215 | // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts |
216 | if ( strlen( $key ) > 255 ) { |
217 | $validation = $this->msg( 'apiwarn-validationfailed-keytoolong', Message::numParam( 255 ) ); |
218 | } elseif ( preg_match( '/[^a-zA-Z0-9_-]/', $key ) !== 0 ) { |
219 | $validation = $this->msg( 'apiwarn-validationfailed-badchars' ); |
220 | } else { |
221 | $validation = true; |
222 | } |
223 | |
224 | LoggerFactory::getInstance( 'api-warning' )->info( |
225 | 'ApiOptions: Setting userjs option', |
226 | [ |
227 | 'phab' => 'T259073', |
228 | 'OptionName' => substr( $key, 0, 255 ), |
229 | 'OptionValue' => substr( $value ?? '', 0, 255 ), |
230 | 'OptionSize' => strlen( $value ?? '' ), |
231 | 'OptionValidation' => $validation, |
232 | 'UserId' => $this->getUserForUpdates()->getId(), |
233 | 'RequestIP' => $this->getRequest()->getIP(), |
234 | 'RequestUA' => $this->getRequest()->getHeader( 'User-Agent' ) |
235 | ] |
236 | ); |
237 | break; |
238 | case 'special': |
239 | $validation = $this->msg( 'apiwarn-validationfailed-cannotset' ); |
240 | break; |
241 | case 'unused': |
242 | default: |
243 | $validation = $this->msg( 'apiwarn-validationfailed-badpref' ); |
244 | break; |
245 | } |
246 | if ( $validation === true && is_string( $value ) && |
247 | strlen( $value ) > UserOptionsManager::MAX_BYTES_OPTION_VALUE |
248 | ) { |
249 | $validation = $this->msg( |
250 | 'apiwarn-validationfailed-valuetoolong', |
251 | Message::numParam( UserOptionsManager::MAX_BYTES_OPTION_VALUE ) |
252 | ); |
253 | } |
254 | return $validation; |
255 | } |
256 | |
257 | /** |
258 | * Load the user from the primary to reduce CAS errors on double post (T95839) |
259 | * Will throw if the user is anonymous. |
260 | */ |
261 | protected function getUserForUpdates(): User { |
262 | // @phan-suppress-next-line PhanTypeMismatchReturnNullable |
263 | return $this->getUserForUpdatesOrNull(); |
264 | } |
265 | |
266 | /** |
267 | * Get the user for updates, or null if the user is anonymous |
268 | * |
269 | * @return User|null |
270 | */ |
271 | protected function getUserForUpdatesOrNull(): ?User { |
272 | if ( !$this->userForUpdates ) { |
273 | $this->userForUpdates = $this->getUser()->getInstanceForUpdate(); |
274 | } |
275 | |
276 | return $this->userForUpdates; |
277 | } |
278 | |
279 | /** |
280 | * Returns preferences form descriptor |
281 | * @return mixed[][] |
282 | */ |
283 | protected function getPreferences() { |
284 | if ( !$this->preferences ) { |
285 | $this->preferences = $this->preferencesFactory->getFormDescriptor( |
286 | $this->getUserForUpdates(), |
287 | $this->getContext() |
288 | ); |
289 | } |
290 | return $this->preferences; |
291 | } |
292 | |
293 | protected function getUserOptionsManager(): UserOptionsManager { |
294 | return $this->userOptionsManager; |
295 | } |
296 | |
297 | protected function getPreferencesFactory(): PreferencesFactory { |
298 | return $this->preferencesFactory; |
299 | } |
300 | |
301 | /** |
302 | * Reset preferences of the specified kinds |
303 | * |
304 | * @param string[] $kinds One or more types returned by PreferencesFactory::listResetKinds() or 'all' |
305 | */ |
306 | abstract protected function resetPreferences( array $kinds ); |
307 | |
308 | /** |
309 | * Sets one user preference to be applied by commitChanges() |
310 | * |
311 | * @param string $preference |
312 | * @param mixed $value |
313 | */ |
314 | abstract protected function setPreference( $preference, $value ); |
315 | |
316 | /** |
317 | * Applies changes to user preferences |
318 | */ |
319 | abstract protected function commitChanges(); |
320 | |
321 | public function mustBePosted() { |
322 | return true; |
323 | } |
324 | |
325 | public function isWriteMode() { |
326 | return true; |
327 | } |
328 | |
329 | public function getAllowedParams() { |
330 | $optionKinds = $this->preferencesFactory->listResetKinds(); |
331 | $optionKinds[] = 'all'; |
332 | |
333 | return [ |
334 | 'reset' => false, |
335 | 'resetkinds' => [ |
336 | ParamValidator::PARAM_TYPE => $optionKinds, |
337 | ParamValidator::PARAM_DEFAULT => 'all', |
338 | ParamValidator::PARAM_ISMULTI => true |
339 | ], |
340 | 'change' => [ |
341 | ParamValidator::PARAM_ISMULTI => true, |
342 | ], |
343 | 'optionname' => [ |
344 | ParamValidator::PARAM_TYPE => 'string', |
345 | ], |
346 | 'optionvalue' => [ |
347 | ParamValidator::PARAM_TYPE => 'string', |
348 | ], |
349 | ]; |
350 | } |
351 | |
352 | public function needsToken() { |
353 | return 'csrf'; |
354 | } |
355 | } |
356 | |
357 | /** @deprecated class alias since 1.43 */ |
358 | class_alias( ApiOptionsBase::class, 'ApiOptionsBase' ); |