57 parent::__construct( $main, $action );
62 $services = MediaWikiServices::getInstance();
72 if ( !$user || !$user->isRegistered() ) {
74 [
'apierror-mustbeloggedin', $this->
msg(
'action-editmyoptions' ) ],
'notloggedin'
83 if ( isset( $params[
'optionvalue'] ) && !isset( $params[
'optionname'] ) ) {
84 $this->
dieWithError( [
'apierror-missingparam',
'optionname' ] );
87 $resetKinds = $params[
'resetkinds'];
88 if ( !$params[
'reset'] ) {
93 if ( $params[
'change'] ) {
94 foreach ( $params[
'change'] as $entry ) {
95 $array = explode(
'=', $entry, 2 );
96 $changes[$array[0]] = $array[1] ??
null;
99 if ( isset( $params[
'optionname'] ) ) {
100 $newValue = $params[
'optionvalue'] ??
null;
101 $changes[$params[
'optionname']] = $newValue;
104 $this->
getHookRunner()->onApiOptions( $this, $user, $changes, $resetKinds );
111 if ( !$changed && !count( $changes ) ) {
116 $prefsKinds = $this->userOptionsManager->getOptionKinds( $user, $this->
getContext(), $changes );
118 $htmlForm =
new HTMLForm( DefaultPreferencesFactory::simplifyFormDescriptor( $prefs ), $this );
119 foreach ( $changes as $key => $value ) {
120 switch ( $prefsKinds[$key] ) {
123 if ( $value ===
null ) {
128 $field = $htmlForm->getField( $key );
129 $validation = $field->validate( $value, $this->userOptionsManager->getOptions( $user ) );
132 case 'registered-multiselect':
133 case 'registered-checkmatrix':
137 $value = $value !==
null ? (bool)$value :
null;
141 if ( strlen( $key ) > 255 ) {
143 } elseif ( preg_match(
'/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
144 $validation = $this->
msg(
'apiwarn-validationfailed-badchars' );
149 LoggerFactory::getInstance(
'api-warning' )->info(
150 'ApiOptions: Setting userjs option',
153 'OptionName' => substr( $key, 0, 255 ),
154 'OptionValue' => substr( $value, 0, 255 ),
155 'OptionSize' => strlen( $value ),
156 'OptionValidation' => $validation,
157 'UserId' => $user->getId(),
158 'RequestIP' => $this->getRequest()->getIP(),
159 'RequestUA' => $this->getRequest()->getHeader(
'User-Agent' )
164 $validation = $this->
msg(
'apiwarn-validationfailed-cannotset' );
168 $validation = $this->
msg(
'apiwarn-validationfailed-badpref' );
171 if ( $validation ===
true ) {
192 if ( !$this->userForUpdates ) {
193 $this->userForUpdates = $this->
getUser()->getInstanceForUpdate();
222 $this->userOptionsManager->setOption( $this->
getUserForUpdates(), $preference, $value );
241 $optionKinds = $this->userOptionsManager->listOptionKinds();
242 $optionKinds[] =
'all';
268 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
273 'action=options&reset=&token=123ABC'
274 =>
'apihelp-options-example-reset',
275 'action=options&change=skin=vector|hideminor=1&token=123ABC'
276 =>
'apihelp-options-example-change',
277 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
278 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
279 =>
'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.
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
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()
getContext()
Get the base IContextSource object.