(bug 42638) Fix API action=options&reset=1 & unit tests
[mediawiki.git] / includes / api / ApiOptions.php
blob265c2ccb674cd4473c42c0157fa09bf87c17b63c
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();
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 foreach ( $changes as $key => $value ) {
78 if ( !isset( $prefs[$key] ) ) {
79 $this->setWarning( "Not a valid preference: $key" );
80 continue;
82 $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key] );
83 $validation = $field->validate( $value, $user->getOptions() );
84 if ( $validation === true ) {
85 $user->setOption( $key, $value );
86 $changed = true;
87 } else {
88 $this->setWarning( "Validation error for '$key': $validation" );
92 if ( $changed ) {
93 // Commit changes
94 $user->saveSettings();
97 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
100 public function mustBePosted() {
101 return true;
104 public function isWriteMode() {
105 return true;
108 public function getAllowedParams() {
109 return array(
110 'token' => array(
111 ApiBase::PARAM_TYPE => 'string',
112 ApiBase::PARAM_REQUIRED => true
114 'reset' => false,
115 'change' => array(
116 ApiBase::PARAM_ISMULTI => true,
118 'optionname' => array(
119 ApiBase::PARAM_TYPE => 'string',
121 'optionvalue' => array(
122 ApiBase::PARAM_TYPE => 'string',
127 public function getResultProperties() {
128 return array(
129 '' => array(
130 '*' => array(
131 ApiBase::PROP_TYPE => array(
132 'success'
139 public function getParamDescription() {
140 return array(
141 'token' => 'An options token previously obtained through the action=tokens',
142 'reset' => 'Resets all preferences to the site defaults',
143 'change' => 'List of changes, formatted name=value (e.g. skin=vector), value cannot contain pipe characters',
144 'optionname' => 'A name of a option which should have an optionvalue set',
145 'optionvalue' => 'A value of the option specified by the optionname, can contain pipe characters',
149 public function getDescription() {
150 return 'Change preferences of the current user';
153 public function getPossibleErrors() {
154 return array_merge( parent::getPossibleErrors(), array(
155 array( 'code' => 'notloggedin', 'info' => 'Anonymous users cannot change preferences' ),
156 array( 'code' => 'nochanges', 'info' => 'No changes were requested' ),
157 ) );
160 public function needsToken() {
161 return true;
164 public function getTokenSalt() {
165 return '';
168 public function getHelpUrls() {
169 return 'https://www.mediawiki.org/wiki/API:Options';
172 public function getExamples() {
173 return array(
174 'api.php?action=options&reset=&token=123ABC',
175 'api.php?action=options&change=skin=vector|hideminor=1&token=123ABC',
176 'api.php?action=options&reset=&change=skin=monobook&optionname=nickname&optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC',
180 public function getVersion() {
181 return __CLASS__ . ': $Id$';