39 private $userForUpdates;
56 parent::__construct( $main, $action );
61 $services = MediaWikiServices::getInstance();
62 $this->userOptionsManager = $userOptionsManager ?? $services->getUserOptionsManager();
63 $this->preferencesFactory = $preferencesFactory ?? $services->getPreferencesFactory();
71 if ( !$user || !$user->isNamed() ) {
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 );
117 $htmlForm =
new HTMLForm( DefaultPreferencesFactory::simplifyFormDescriptor( $prefs ), $this );
118 foreach ( $changes as $key => $value ) {
119 switch ( $prefsKinds[$key] ) {
122 if ( $value ===
null ) {
127 $field = $htmlForm->getField( $key );
128 $validation = $field->validate( $value, $this->userOptionsManager->getOptions( $user ) );
131 case 'registered-multiselect':
132 case 'registered-checkmatrix':
136 $value = $value !==
null ? (bool)$value :
null;
140 if ( strlen( $key ) > 255 ) {
142 } elseif ( preg_match(
'/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
143 $validation = $this->
msg(
'apiwarn-validationfailed-badchars' );
148 LoggerFactory::getInstance(
'api-warning' )->info(
149 'ApiOptions: Setting userjs option',
152 'OptionName' => substr( $key, 0, 255 ),
153 'OptionValue' => substr( $value ??
'', 0, 255 ),
154 'OptionSize' => strlen( $value ??
'' ),
155 'OptionValidation' => $validation,
156 'UserId' => $user->getId(),
157 'RequestIP' => $this->getRequest()->getIP(),
158 'RequestUA' => $this->getRequest()->getHeader(
'User-Agent' )
163 $validation = $this->
msg(
'apiwarn-validationfailed-cannotset' );
167 $validation = $this->
msg(
'apiwarn-validationfailed-badpref' );
170 if ( $validation ===
true && is_string( $value ) &&
171 strlen( $value ) > UserOptionsManager::MAX_BYTES_OPTION_VALUE
173 $validation = $this->
msg(
174 'apiwarn-validationfailed-valuetoolong',
178 if ( $validation ===
true ) {
199 if ( !$this->userForUpdates ) {
200 $this->userForUpdates = $this->
getUser()->getInstanceForUpdate();
203 return $this->userForUpdates;
229 $this->userOptionsManager->setOption( $this->
getUserForUpdates(), $preference, $value );
248 $optionKinds = $this->userOptionsManager->listOptionKinds();
249 $optionKinds[] =
'all';
254 ParamValidator::PARAM_TYPE => $optionKinds,
255 ParamValidator::PARAM_DEFAULT =>
'all',
256 ParamValidator::PARAM_ISMULTI =>
true
259 ParamValidator::PARAM_ISMULTI =>
true,
262 ParamValidator::PARAM_TYPE =>
'string',
265 ParamValidator::PARAM_TYPE =>
'string',
275 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
280 'action=options&reset=&token=123ABC'
281 =>
'apihelp-options-example-reset',
282 'action=options&change=skin=vector|hideminor=1&token=123ABC'
283 =>
'apihelp-options-example-change',
284 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
285 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
286 =>
'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)
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()