MediaWiki  1.23.13
ApiOptions.php
Go to the documentation of this file.
1 <?php
33 class ApiOptions extends ApiBase {
37  public function execute() {
38  $user = $this->getUser();
39 
40  if ( $user->isAnon() ) {
41  $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' );
42  }
43 
44  if ( !$user->isAllowed( 'editmyoptions' ) ) {
45  $this->dieUsage( 'You don\'t have permission to edit your options', 'permissiondenied' );
46  }
47 
48  $params = $this->extractRequestParams();
49  $changed = false;
50 
51  if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
52  $this->dieUsageMsg( array( 'missingparam', 'optionname' ) );
53  }
54 
55  if ( $params['reset'] ) {
56  $user->resetOptions( $params['resetkinds'], $this->getContext() );
57  $changed = true;
58  }
59 
60  $changes = array();
61  if ( count( $params['change'] ) ) {
62  foreach ( $params['change'] as $entry ) {
63  $array = explode( '=', $entry, 2 );
64  $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null;
65  }
66  }
67  if ( isset( $params['optionname'] ) ) {
68  $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null;
69  $changes[$params['optionname']] = $newValue;
70  }
71  if ( !$changed && !count( $changes ) ) {
72  $this->dieUsage( 'No changes were requested', 'nochanges' );
73  }
74 
75  $prefs = Preferences::getPreferences( $user, $this->getContext() );
76  $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
77 
78  foreach ( $changes as $key => $value ) {
79  switch ( $prefsKinds[$key] ) {
80  case 'registered':
81  // Regular option.
82  $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key] );
83  $validation = $field->validate( $value, $user->getOptions() );
84  break;
85  case 'registered-multiselect':
86  case 'registered-checkmatrix':
87  // A key for a multiselect or checkmatrix option.
88  $validation = true;
89  $value = $value !== null ? (bool)$value : null;
90  break;
91  case 'userjs':
92  // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts
93  if ( strlen( $key ) > 255 ) {
94  $validation = "key too long (no more than 255 bytes allowed)";
95  } elseif ( preg_match( "/[^a-zA-Z0-9_-]/", $key ) !== 0 ) {
96  $validation = "invalid key (only a-z, A-Z, 0-9, _, - allowed)";
97  } else {
98  $validation = true;
99  }
100  break;
101  case 'special':
102  $validation = "cannot be set by this module";
103  break;
104  case 'unused':
105  default:
106  $validation = "not a valid preference";
107  break;
108  }
109  if ( $validation === true ) {
110  $user->setOption( $key, $value );
111  $changed = true;
112  } else {
113  $this->setWarning( "Validation error for '$key': $validation" );
114  }
115  }
116 
117  if ( $changed ) {
118  // Commit changes
119  $user->saveSettings();
120  }
121 
122  $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
123  }
124 
125  public function mustBePosted() {
126  return true;
127  }
128 
129  public function isWriteMode() {
130  return true;
131  }
132 
133  public function getAllowedParams() {
134  $optionKinds = User::listOptionKinds();
135  $optionKinds[] = 'all';
136 
137  return array(
138  'token' => array(
139  ApiBase::PARAM_TYPE => 'string',
141  ),
142  'reset' => false,
143  'resetkinds' => array(
144  ApiBase::PARAM_TYPE => $optionKinds,
145  ApiBase::PARAM_DFLT => 'all',
146  ApiBase::PARAM_ISMULTI => true
147  ),
148  'change' => array(
149  ApiBase::PARAM_ISMULTI => true,
150  ),
151  'optionname' => array(
152  ApiBase::PARAM_TYPE => 'string',
153  ),
154  'optionvalue' => array(
155  ApiBase::PARAM_TYPE => 'string',
156  ),
157  );
158  }
159 
160  public function getResultProperties() {
161  return array(
162  '' => array(
163  '*' => array(
165  'success'
166  )
167  )
168  )
169  );
170  }
171 
172  public function getParamDescription() {
173  return array(
174  'token' => 'An options token previously obtained through the action=tokens',
175  'reset' => 'Resets preferences to the site defaults',
176  'resetkinds' => 'List of types of options to reset when the "reset" option is set',
177  'change' => array( 'List of changes, formatted name=value (e.g. skin=vector), ' .
178  'value cannot contain pipe characters. If no value is given (not ',
179  'even an equals sign), e.g., optionname|otheroption|..., the ' .
180  'option will be reset to its default value'
181  ),
182  'optionname' => 'A name of a option which should have an optionvalue set',
183  'optionvalue' => 'A value of the option specified by the optionname, ' .
184  'can contain pipe characters',
185  );
186  }
187 
188  public function getDescription() {
189  return array(
190  'Change preferences of the current user.',
191  'Only options which are registered in core or in one of installed extensions,',
192  'or as options with keys prefixed with \'userjs-\' (intended to be used by user',
193  'scripts), can be set.'
194  );
195  }
196 
197  public function getPossibleErrors() {
198  return array_merge( parent::getPossibleErrors(), array(
199  array( 'code' => 'notloggedin', 'info' => 'Anonymous users cannot change preferences' ),
200  array( 'code' => 'nochanges', 'info' => 'No changes were requested' ),
201  ) );
202  }
203 
204  public function needsToken() {
205  return true;
206  }
207 
208  public function getTokenSalt() {
209  return '';
210  }
211 
212  public function getHelpUrls() {
213  return 'https://www.mediawiki.org/wiki/API:Options';
214  }
215 
216  public function getExamples() {
217  return array(
218  'api.php?action=options&reset=&token=123ABC',
219  'api.php?action=options&change=skin=vector|hideminor=1&token=123ABC',
220  'api.php?action=options&reset=&change=skin=monobook&optionname=nickname&' .
221  'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC',
222  );
223  }
224 }
Preferences\getPreferences
static getPreferences( $user, IContextSource $context)
Definition: Preferences.php:78
ContextSource\getContext
getContext()
Get the RequestContext object.
Definition: ContextSource.php:40
ApiOptions\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiOptions.php:188
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:62
ApiOptions\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiOptions.php:129
ApiBase\dieUsageMsg
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition: ApiBase.php:1933
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:205
$params
$params
Definition: styleTest.css.php:40
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:132
ApiOptions\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiOptions.php:216
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiOptions\getResultProperties
getResultProperties()
Returns possible properties in the result, grouped by the value of the prop parameter that shows them...
Definition: ApiOptions.php:160
ApiOptions\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiOptions.php:133
ApiOptions\execute
execute()
Changes preferences of the current user.
Definition: ApiOptions.php:37
ApiOptions\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiOptions.php:125
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiBase\PROP_TYPE
const PROP_TYPE
Definition: ApiBase.php:74
ApiOptions
API module that facilitates the changing of user's preferences.
Definition: ApiOptions.php:33
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
ApiOptions\needsToken
needsToken()
Returns whether this module requires a token to execute It is used to show possible errors in action=...
Definition: ApiOptions.php:204
$value
$value
Definition: styleTest.css.php:45
ApiBase\dieUsage
dieUsage( $description, $errorCode, $httpRespCode=0, $extradata=null)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition: ApiBase.php:1363
ApiOptions\getPossibleErrors
getPossibleErrors()
Returns a list of all possible errors returned by the module.
Definition: ApiOptions.php:197
HTMLForm\loadInputFromParameters
static loadInputFromParameters( $fieldname, $descriptor)
Initialise a new Object for the field.
Definition: HTMLForm.php:351
ApiOptions\getTokenSalt
getTokenSalt()
Returns the token salt if there is one, '' if the module doesn't require a salt, else false if the mo...
Definition: ApiOptions.php:208
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
ApiOptions\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiOptions.php:172
ApiBase\setWarning
setWarning( $warning)
Set warning section for this module.
Definition: ApiBase.php:245
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:46
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:148
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:48
ApiOptions\getHelpUrls
getHelpUrls()
Definition: ApiOptions.php:212
User\listOptionKinds
static listOptionKinds()
Return a list of the types of user options currently returned by User::getOptionKinds().
Definition: User.php:2580
$changed
$changed
Definition: UtfNormalGenerate.php:130