38 private $userForUpdates;
41 private $userOptionsManager;
44 private $preferencesFactory;
58 parent::__construct( $main, $action );
63 $services = MediaWikiServices::getInstance();
64 $this->userOptionsManager = $userOptionsManager ?? $services->getUserOptionsManager();
65 $this->preferencesFactory = $preferencesFactory ?? $services->getPreferencesFactory();
73 if ( !$user || !$user->isRegistered() ) {
75 [
'apierror-mustbeloggedin', $this->
msg(
'action-editmyoptions' ) ],
'notloggedin'
84 if ( isset( $params[
'optionvalue'] ) && !isset( $params[
'optionname'] ) ) {
85 $this->
dieWithError( [
'apierror-missingparam',
'optionname' ] );
88 $resetKinds = $params[
'resetkinds'];
89 if ( !$params[
'reset'] ) {
94 if ( $params[
'change'] ) {
95 foreach ( $params[
'change'] as $entry ) {
96 $array = explode(
'=', $entry, 2 );
97 $changes[$array[0]] = $array[1] ??
null;
100 if ( isset( $params[
'optionname'] ) ) {
101 $newValue = $params[
'optionvalue'] ??
null;
102 $changes[$params[
'optionname']] = $newValue;
105 $this->
getHookRunner()->onApiOptions( $this, $user, $changes, $resetKinds );
112 if ( !$changed && !count( $changes ) ) {
117 $prefsKinds = $this->userOptionsManager->getOptionKinds( $user, $this->
getContext(), $changes );
119 $htmlForm =
new HTMLForm( DefaultPreferencesFactory::simplifyFormDescriptor( $prefs ), $this );
120 foreach ( $changes as $key => $value ) {
121 switch ( $prefsKinds[$key] ) {
124 if ( $value ===
null ) {
129 $field = $htmlForm->getField( $key );
130 $validation = $field->validate( $value, $this->userOptionsManager->getOptions( $user ) );
133 case 'registered-multiselect':
134 case 'registered-checkmatrix':
138 $value = $value !==
null ? (bool)$value :
null;
142 if ( strlen( $key ) > 255 ) {
144 } elseif ( preg_match(
'/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
145 $validation = $this->
msg(
'apiwarn-validationfailed-badchars' );
150 LoggerFactory::getInstance(
'api-warning' )->info(
151 'ApiOptions: Setting userjs option',
154 'OptionName' => substr( $key, 0, 255 ),
155 'OptionValue' => substr( $value ??
'', 0, 255 ),
156 'OptionSize' => strlen( $value ??
'' ),
157 'OptionValidation' => $validation,
158 'UserId' => $user->getId(),
159 'RequestIP' => $this->getRequest()->getIP(),
160 'RequestUA' => $this->getRequest()->getHeader(
'User-Agent' )
165 $validation = $this->
msg(
'apiwarn-validationfailed-cannotset' );
169 $validation = $this->
msg(
'apiwarn-validationfailed-badpref' );
172 if ( $validation ===
true && is_string( $value ) &&
173 strlen( $value ) > UserOptionsManager::MAX_BYTES_OPTION_VALUE
175 $validation = $this->
msg(
176 'apiwarn-validationfailed-valuetoolong',
180 if ( $validation ===
true ) {
201 if ( !$this->userForUpdates ) {
202 $this->userForUpdates = $this->
getUser()->getInstanceForUpdate();
205 return $this->userForUpdates;
231 $this->userOptionsManager->setOption( $this->
getUserForUpdates(), $preference, $value );
250 $optionKinds = $this->userOptionsManager->listOptionKinds();
251 $optionKinds[] =
'all';
256 ParamValidator::PARAM_TYPE => $optionKinds,
257 ParamValidator::PARAM_DEFAULT =>
'all',
258 ParamValidator::PARAM_ISMULTI =>
true
261 ParamValidator::PARAM_ISMULTI =>
true,
264 ParamValidator::PARAM_TYPE =>
'string',
267 ParamValidator::PARAM_TYPE =>
'string',
277 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
282 'action=options&reset=&token=123ABC'
283 =>
'apihelp-options-example-reset',
284 'action=options&change=skin=vector|hideminor=1&token=123ABC'
285 =>
'apihelp-options-example-change',
286 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
287 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
288 =>
'apihelp-options-example-complex',
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
getModuleName()
Get the name of the module being executed by this instance.
getHookRunner()
Get an ApiHookRunner for running core API hooks.
This is the main API class, used for both external and internal processing.
API module that facilitates the changing of user's preferences.
resetPreferences(array $kinds)
commitChanges()
Applies changes to user preferences.
isWriteMode()
Indicates whether this module requires write mode.
getExamplesMessages()
Returns usage examples for this module.
setPreference( $preference, $value)
Sets one user preference to be applied by commitChanges()
needsToken()
Returns the token type this module requires in order to execute.
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Changes preferences of the current user.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
__construct(ApiMain $main, $action, UserOptionsManager $userOptionsManager=null, PreferencesFactory $preferencesFactory=null)
getPreferences()
Returns preferences form descriptor.
mustBePosted()
Indicates whether this module must be called with a POST request.
getUserForUpdates()
Load the user from the primary to reduce CAS errors on double post (T95839)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()