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 if ( $this->getUser()->isAnon() ) {
39 $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' );
40 } elseif ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
41 $this->dieUsage( "You don't have permission to edit your options", 'permissiondenied' );
44 $params = $this->extractRequestParams();
47 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
48 $this->dieUsageMsg( [ 'missingparam', 'optionname' ] );
51 // Load the user from the master to reduce CAS errors on double post (T95839)
52 $user = $this->getUser()->getInstanceForUpdate();
54 $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' );
57 if ( $params['reset'] ) {
58 $user->resetOptions( $params['resetkinds'], $this->getContext() );
63 if ( count( $params['change'] ) ) {
64 foreach ( $params['change'] as $entry ) {
65 $array = explode( '=', $entry, 2 );
66 $changes[$array[0]] = isset( $array[1] ) ?
$array[1] : null;
69 if ( isset( $params['optionname'] ) ) {
70 $newValue = isset( $params['optionvalue'] ) ?
$params['optionvalue'] : null;
71 $changes[$params['optionname']] = $newValue;
73 if ( !$changed && !count( $changes ) ) {
74 $this->dieUsage( 'No changes were requested', 'nochanges' );
77 $prefs = Preferences
::getPreferences( $user, $this->getContext() );
78 $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
81 foreach ( $changes as $key => $value ) {
82 switch ( $prefsKinds[$key] ) {
85 if ( $htmlForm === null ) {
86 // We need a dummy HTMLForm for the validate callback...
87 $htmlForm = new HTMLForm( [], $this );
89 $field = HTMLForm
::loadInputFromParameters( $key, $prefs[$key], $htmlForm );
90 $validation = $field->validate( $value, $user->getOptions() );
92 case 'registered-multiselect':
93 case 'registered-checkmatrix':
94 // A key for a multiselect or checkmatrix option.
96 $value = $value !== null ?
(bool)$value : null;
99 // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts
100 if ( strlen( $key ) > 255 ) {
101 $validation = 'key too long (no more than 255 bytes allowed)';
102 } elseif ( preg_match( '/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
103 $validation = 'invalid key (only a-z, A-Z, 0-9, _, - allowed)';
109 $validation = 'cannot be set by this module';
113 $validation = 'not a valid preference';
116 if ( $validation === true ) {
117 $user->setOption( $key, $value );
120 $this->setWarning( "Validation error for '$key': $validation" );
126 $user->saveSettings();
129 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
132 public function mustBePosted() {
136 public function isWriteMode() {
140 public function getAllowedParams() {
141 $optionKinds = User
::listOptionKinds();
142 $optionKinds[] = 'all';
147 ApiBase
::PARAM_TYPE
=> $optionKinds,
148 ApiBase
::PARAM_DFLT
=> 'all',
149 ApiBase
::PARAM_ISMULTI
=> true
152 ApiBase
::PARAM_ISMULTI
=> true,
155 ApiBase
::PARAM_TYPE
=> 'string',
158 ApiBase
::PARAM_TYPE
=> 'string',
163 public function needsToken() {
167 public function getHelpUrls() {
168 return 'https://www.mediawiki.org/wiki/API:Options';
171 protected function getExamplesMessages() {
173 'action=options&reset=&token=123ABC'
174 => 'apihelp-options-example-reset',
175 'action=options&change=skin=vector|hideminor=1&token=123ABC'
176 => 'apihelp-options-example-change',
177 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
178 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
179 => 'apihelp-options-example-complex',