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
28 * API module that facilitates the changing of user's preferences.
29 * Requires API write mode to be enabled.
33 class ApiOptions
extends ApiBase
{
35 * Changes preferences of the current user.
37 public function execute() {
38 $user = $this->getUser();
40 if ( $user->isAnon() ) {
41 $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' );
44 if ( !$user->isAllowed( 'editmyoptions' ) ) {
45 $this->dieUsage( 'You don\'t have permission to edit your options', 'permissiondenied' );
48 $params = $this->extractRequestParams();
51 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
52 $this->dieUsageMsg( array( 'missingparam', 'optionname' ) );
55 // Load the user from the master to reduce CAS errors on double post (T95839)
56 if ( wfGetLB()->getServerCount() > 1 ) {
57 $user = User
::newFromId( $user->getId() );
58 if ( !$user->loadFromId( User
::READ_EXCLUSIVE
) ) {
59 $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' );
63 if ( $params['reset'] ) {
64 $user->resetOptions( $params['resetkinds'], $this->getContext() );
69 if ( count( $params['change'] ) ) {
70 foreach ( $params['change'] as $entry ) {
71 $array = explode( '=', $entry, 2 );
72 $changes[$array[0]] = isset( $array[1] ) ?
$array[1] : null;
75 if ( isset( $params['optionname'] ) ) {
76 $newValue = isset( $params['optionvalue'] ) ?
$params['optionvalue'] : null;
77 $changes[$params['optionname']] = $newValue;
79 if ( !$changed && !count( $changes ) ) {
80 $this->dieUsage( 'No changes were requested', 'nochanges' );
83 $prefs = Preferences
::getPreferences( $user, $this->getContext() );
84 $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
87 foreach ( $changes as $key => $value ) {
88 switch ( $prefsKinds[$key] ) {
91 if ( $htmlForm === null ) {
92 // We need a dummy HTMLForm for the validate callback...
93 $htmlForm = new HTMLForm( array(), $this );
95 $field = HTMLForm
::loadInputFromParameters( $key, $prefs[$key] );
96 $field->mParent
= $htmlForm;
97 $validation = $field->validate( $value, $user->getOptions() );
99 case 'registered-multiselect':
100 case 'registered-checkmatrix':
101 // A key for a multiselect or checkmatrix option.
103 $value = $value !== null ?
(bool)$value : null;
106 // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts
107 if ( strlen( $key ) > 255 ) {
108 $validation = "key too long (no more than 255 bytes allowed)";
109 } elseif ( preg_match( "/[^a-zA-Z0-9_-]/", $key ) !== 0 ) {
110 $validation = "invalid key (only a-z, A-Z, 0-9, _, - allowed)";
116 $validation = "cannot be set by this module";
120 $validation = "not a valid preference";
123 if ( $validation === true ) {
124 $user->setOption( $key, $value );
127 $this->setWarning( "Validation error for '$key': $validation" );
133 $user->saveSettings();
136 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
139 public function mustBePosted() {
143 public function isWriteMode() {
147 public function getAllowedParams() {
148 $optionKinds = User
::listOptionKinds();
149 $optionKinds[] = 'all';
153 'resetkinds' => array(
154 ApiBase
::PARAM_TYPE
=> $optionKinds,
155 ApiBase
::PARAM_DFLT
=> 'all',
156 ApiBase
::PARAM_ISMULTI
=> true
159 ApiBase
::PARAM_ISMULTI
=> true,
161 'optionname' => array(
162 ApiBase
::PARAM_TYPE
=> 'string',
164 'optionvalue' => array(
165 ApiBase
::PARAM_TYPE
=> 'string',
170 public function needsToken() {
174 public function getHelpUrls() {
175 return 'https://www.mediawiki.org/wiki/API:Options';
178 protected function getExamplesMessages() {
180 'action=options&reset=&token=123ABC'
181 => 'apihelp-options-example-reset',
182 'action=options&change=skin=vector|hideminor=1&token=123ABC'
183 => 'apihelp-options-example-change',
184 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
185 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
186 => 'apihelp-options-example-complex',