56 parent::__construct( $main, $action );
61 $services = MediaWikiServices::getInstance();
71 if ( !$user || !$user->isRegistered() ) {
73 [
'apierror-mustbeloggedin', $this->
msg(
'action-editmyoptions' ) ],
'notloggedin'
82 if ( isset( $params[
'optionvalue'] ) && !isset( $params[
'optionname'] ) ) {
83 $this->
dieWithError( [
'apierror-missingparam',
'optionname' ] );
86 $resetKinds = $params[
'resetkinds'];
87 if ( !$params[
'reset'] ) {
92 if ( $params[
'change'] ) {
93 foreach ( $params[
'change'] as $entry ) {
94 $array = explode(
'=', $entry, 2 );
95 $changes[$array[0]] = $array[1] ??
null;
98 if ( isset( $params[
'optionname'] ) ) {
99 $newValue = $params[
'optionvalue'] ??
null;
100 $changes[$params[
'optionname']] = $newValue;
103 $this->
getHookRunner()->onApiOptions( $this, $user, $changes, $resetKinds );
110 if ( !$changed && !count( $changes ) ) {
115 $prefsKinds = $this->userOptionsManager->getOptionKinds( $user, $this->
getContext(), $changes );
118 foreach ( $changes as $key => $value ) {
119 switch ( $prefsKinds[$key] ) {
122 if ( $value ===
null ) {
127 if ( $htmlForm ===
null ) {
129 $htmlForm =
new HTMLForm( [], $this );
131 $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key], $htmlForm );
132 $validation = $field->validate( $value, $this->userOptionsManager->getOptions( $user ) );
135 case 'registered-multiselect':
136 case 'registered-checkmatrix':
139 $value = $value !==
null ? (bool)$value :
null;
143 if ( strlen( $key ) > 255 ) {
145 } elseif ( preg_match(
'/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
146 $validation = $this->
msg(
'apiwarn-validationfailed-badchars' );
151 LoggerFactory::getInstance(
'api-warning' )->info(
152 'ApiOptions: Setting userjs option',
155 'OptionName' => substr( $key, 0, 255 ),
156 'OptionValue' => substr( $value ??
'', 0, 255 ),
157 'OptionSize' => strlen( $value ??
'' ),
158 'OptionValidation' => $validation,
159 'UserId' => $user->getId(),
160 'RequestIP' => $this->getRequest()->getIP(),
161 'RequestUA' => $this->getRequest()->getHeader(
'User-Agent' )
166 $validation = $this->
msg(
'apiwarn-validationfailed-cannotset' );
170 $validation = $this->
msg(
'apiwarn-validationfailed-badpref' );
173 if ( $validation ===
true ) {
194 if ( !$this->userForUpdates ) {
195 $this->userForUpdates = $this->
getUser()->getInstanceForUpdate();
198 return $this->userForUpdates;
224 $this->userOptionsManager->setOption( $this->
getUserForUpdates(), $preference, $value );
243 $optionKinds = $this->userOptionsManager->listOptionKinds();
244 $optionKinds[] =
'all';
270 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
275 'action=options&reset=&token=123ABC'
276 =>
'apihelp-options-example-reset',
277 'action=options&change=skin=vector|hideminor=1&token=123ABC'
278 =>
'apihelp-options-example-change',
279 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
280 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
281 =>
'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.
UserOptionsManager $userOptionsManager
isWriteMode()
Indicates whether this module requires write mode.
getExamplesMessages()
Returns usage examples for this module.
PreferencesFactory $preferencesFactory
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.
User $userForUpdates
User account to modify.
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()
The User object encapsulates all of the user-specific settings (user_id, name, rights,...