Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / api / ApiOptions.php
blobeff05debd311641ac5565134dd0edba603a94e43
1 <?php
2 /**
5 * Created on Apr 15, 2012
7 * Copyright © 2012 Szymon Świerkosz beau@adres.pl
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * API module that facilitates the changing of user's preferences.
29 * Requires API write mode to be enabled.
31 * @ingroup API
33 class ApiOptions extends ApiBase {
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
39 /**
40 * Changes preferences of the current user.
42 public function execute() {
43 $user = $this->getUser();
45 if ( $user->isAnon() ) {
46 $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' );
49 $params = $this->extractRequestParams();
50 $changed = false;
52 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
53 $this->dieUsageMsg( array( 'missingparam', 'optionname' ) );
56 if ( $params['reset'] ) {
57 $user->resetOptions( $params['resetkinds'] );
58 $changed = true;
61 $changes = array();
62 if ( count( $params['change'] ) ) {
63 foreach ( $params['change'] as $entry ) {
64 $array = explode( '=', $entry, 2 );
65 $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null;
68 if ( isset( $params['optionname'] ) ) {
69 $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null;
70 $changes[$params['optionname']] = $newValue;
72 if ( !$changed && !count( $changes ) ) {
73 $this->dieUsage( 'No changes were requested', 'nochanges' );
76 $prefs = Preferences::getPreferences( $user, $this->getContext() );
77 $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
79 foreach ( $changes as $key => $value ) {
80 switch ( $prefsKinds[$key] ) {
81 case 'registered':
82 // Regular option.
83 $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key] );
84 $validation = $field->validate( $value, $user->getOptions() );
85 break;
86 case 'registered-multiselect':
87 // A key for a multiselect 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;
100 break;
101 case 'unused':
102 default:
103 $validation = "not a valid preference";
104 break;
106 if ( $validation === true ) {
107 $user->setOption( $key, $value );
108 $changed = true;
109 } else {
110 $this->setWarning( "Validation error for '$key': $validation" );
114 if ( $changed ) {
115 // Commit changes
116 $user->saveSettings();
119 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
122 public function mustBePosted() {
123 return true;
126 public function isWriteMode() {
127 return true;
130 public function getAllowedParams() {
131 $optionKinds = User::listOptionKinds();
132 $optionKinds[] = 'all';
134 return array(
135 'token' => array(
136 ApiBase::PARAM_TYPE => 'string',
137 ApiBase::PARAM_REQUIRED => true
139 'reset' => false,
140 'resetkinds' => array(
141 ApiBase::PARAM_TYPE => $optionKinds,
142 ApiBase::PARAM_DFLT => 'all',
143 ApiBase::PARAM_ISMULTI => true
145 'change' => array(
146 ApiBase::PARAM_ISMULTI => true,
148 'optionname' => array(
149 ApiBase::PARAM_TYPE => 'string',
151 'optionvalue' => array(
152 ApiBase::PARAM_TYPE => 'string',
157 public function getResultProperties() {
158 return array(
159 '' => array(
160 '*' => array(
161 ApiBase::PROP_TYPE => array(
162 'success'
169 public function getParamDescription() {
170 return array(
171 'token' => 'An options token previously obtained through the action=tokens',
172 'reset' => 'Resets preferences to the site defaults',
173 'resetkinds' => 'List of types of options to reset when the "reset" option is set',
174 'change' => 'List of changes, formatted name=value (e.g. skin=vector), value cannot contain pipe characters. If no value is given (not even an equals sign), e.g., optionname|otheroption|..., the option will be reset to its default value',
175 'optionname' => 'A name of a option which should have an optionvalue set',
176 'optionvalue' => 'A value of the option specified by the optionname, can contain pipe characters',
180 public function getDescription() {
181 return array(
182 'Change preferences of the current user',
183 'Only options which are registered in core or in one of installed extensions,',
184 'or as options with keys prefixed with \'userjs-\' (intended to be used by user scripts), can be set.'
188 public function getPossibleErrors() {
189 return array_merge( parent::getPossibleErrors(), array(
190 array( 'code' => 'notloggedin', 'info' => 'Anonymous users cannot change preferences' ),
191 array( 'code' => 'nochanges', 'info' => 'No changes were requested' ),
192 ) );
195 public function needsToken() {
196 return true;
199 public function getTokenSalt() {
200 return '';
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/API:Options';
207 public function getExamples() {
208 return array(
209 'api.php?action=options&reset=&token=123ABC',
210 'api.php?action=options&change=skin=vector|hideminor=1&token=123ABC',
211 'api.php?action=options&reset=&change=skin=monobook&optionname=nickname&optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC',