MediaWiki REL1_31
ApiOptions.php
Go to the documentation of this file.
1<?php
24
31class ApiOptions extends ApiBase {
35 public function execute() {
36 if ( $this->getUser()->isAnon() ) {
37 $this->dieWithError(
38 [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin'
39 );
40 }
41
42 $this->checkUserRightsAny( 'editmyoptions' );
43
45 $changed = false;
46
47 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
48 $this->dieWithError( [ 'apierror-missingparam', 'optionname' ] );
49 }
50
51 // Load the user from the master to reduce CAS errors on double post (T95839)
52 $user = $this->getUser()->getInstanceForUpdate();
53 if ( !$user ) {
54 $this->dieWithError(
55 [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin'
56 );
57 }
58
59 if ( $params['reset'] ) {
60 $user->resetOptions( $params['resetkinds'], $this->getContext() );
61 $changed = true;
62 }
63
64 $changes = [];
65 if ( $params['change'] ) {
66 foreach ( $params['change'] as $entry ) {
67 $array = explode( '=', $entry, 2 );
68 $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null;
69 }
70 }
71 if ( isset( $params['optionname'] ) ) {
72 $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null;
73 $changes[$params['optionname']] = $newValue;
74 }
75 if ( !$changed && !count( $changes ) ) {
76 $this->dieWithError( 'apierror-nochanges' );
77 }
78
79 $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
80 $prefs = $preferencesFactory->getFormDescriptor( $user, $this->getContext() );
81 $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
82
83 $htmlForm = null;
84 foreach ( $changes as $key => $value ) {
85 switch ( $prefsKinds[$key] ) {
86 case 'registered':
87 // Regular option.
88 if ( $htmlForm === null ) {
89 // We need a dummy HTMLForm for the validate callback...
90 $htmlForm = new HTMLForm( [], $this );
91 }
92 $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key], $htmlForm );
93 $validation = $field->validate( $value, $user->getOptions() );
94 break;
95 case 'registered-multiselect':
96 case 'registered-checkmatrix':
97 // A key for a multiselect or checkmatrix option.
98 $validation = true;
99 $value = $value !== null ? (bool)$value : null;
100 break;
101 case 'userjs':
102 // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts
103 if ( strlen( $key ) > 255 ) {
104 $validation = $this->msg( 'apiwarn-validationfailed-keytoolong', Message::numParam( 255 ) );
105 } elseif ( preg_match( '/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
106 $validation = $this->msg( 'apiwarn-validationfailed-badchars' );
107 } else {
108 $validation = true;
109 }
110 break;
111 case 'special':
112 $validation = $this->msg( 'apiwarn-validationfailed-cannotset' );
113 break;
114 case 'unused':
115 default:
116 $validation = $this->msg( 'apiwarn-validationfailed-badpref' );
117 break;
118 }
119 if ( $validation === true ) {
120 $user->setOption( $key, $value );
121 $changed = true;
122 } else {
123 $this->addWarning( [ 'apiwarn-validationfailed', wfEscapeWikiText( $key ), $validation ] );
124 }
125 }
126
127 if ( $changed ) {
128 // Commit changes
129 $user->saveSettings();
130 }
131
132 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
133 }
134
135 public function mustBePosted() {
136 return true;
137 }
138
139 public function isWriteMode() {
140 return true;
141 }
142
143 public function getAllowedParams() {
144 $optionKinds = User::listOptionKinds();
145 $optionKinds[] = 'all';
146
147 return [
148 'reset' => false,
149 'resetkinds' => [
150 ApiBase::PARAM_TYPE => $optionKinds,
151 ApiBase::PARAM_DFLT => 'all',
153 ],
154 'change' => [
156 ],
157 'optionname' => [
158 ApiBase::PARAM_TYPE => 'string',
159 ],
160 'optionvalue' => [
161 ApiBase::PARAM_TYPE => 'string',
162 ],
163 ];
164 }
165
166 public function needsToken() {
167 return 'csrf';
168 }
169
170 public function getHelpUrls() {
171 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
172 }
173
174 protected function getExamplesMessages() {
175 return [
176 'action=options&reset=&token=123ABC'
177 => 'apihelp-options-example-reset',
178 'action=options&change=skin=vector|hideminor=1&token=123ABC'
179 => 'apihelp-options-example-change',
180 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
181 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
182 => 'apihelp-options-example-complex',
183 ];
184 }
185}
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.
Definition ApiBase.php:37
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:2006
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:1895
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:87
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:48
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:749
getResult()
Get the result object.
Definition ApiBase.php:641
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1819
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:521
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:51
API module that facilitates the changing of user's preferences.
isWriteMode()
Indicates whether this module requires write mode.
getExamplesMessages()
Returns usage examples for this module.
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...
mustBePosted()
Indicates whether this module must be called with a POST request.
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
getContext()
Get the base IContextSource object.
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition HTMLForm.php:130
MediaWikiServices is the service locator for the application scope of MediaWiki.
static listOptionKinds()
Return a list of the types of user options currently returned by User::getOptionKinds().
Definition User.php:3324
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
$params