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 public function __construct( $main, $action ) {
36 parent
::__construct( $main, $action );
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();
52 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
53 $this->dieUsageMsg( array( 'missingparam', 'optionname' ) );
56 if ( $params['reset'] ) {
57 $user->resetOptions();
60 if ( count( $params['change'] ) ) {
61 foreach ( $params['change'] as $entry ) {
62 $array = explode( '=', $entry, 2 );
63 $user->setOption( $array[0], isset( $array[1] ) ?
$array[1] : null );
67 if ( isset( $params['optionname'] ) ) {
68 $newValue = isset( $params['optionvalue'] ) ?
$params['optionvalue'] : null;
69 $user->setOption( $params['optionname'], $newValue );
75 $user->saveSettings();
77 $this->dieUsage( 'No changes were requested', 'nochanges' );
80 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
83 public function mustBePosted() {
87 public function isWriteMode() {
91 public function getAllowedParams() {
94 ApiBase
::PARAM_TYPE
=> 'string',
95 ApiBase
::PARAM_REQUIRED
=> true
99 ApiBase
::PARAM_ISMULTI
=> true,
101 'optionname' => array(
102 ApiBase
::PARAM_TYPE
=> 'string',
104 'optionvalue' => array(
105 ApiBase
::PARAM_TYPE
=> 'string',
110 public function getParamDescription() {
112 'token' => 'An options token previously obtained through the action=tokens',
113 'reset' => 'Resets all preferences to the site defaults',
114 'change' => 'Pipe-separated list of changes, formatted name=value (e.g. skin=vector), value cannot contain pipe characters',
115 'optionname' => 'A name of a option which should have an optionvalue set',
116 'optionvalue' => 'A value of the option specified by the optionname, can contain pipe characters',
120 public function getDescription() {
121 return 'Change preferences of the current user';
124 public function getPossibleErrors() {
125 return array_merge( parent
::getPossibleErrors(), array(
126 array( 'notloggedin' ),
127 array( 'nochanges' ),
131 public function needsToken() {
135 public function getTokenSalt() {
139 public function getHelpUrls() {
140 return 'https://www.mediawiki.org/wiki/API:Options';
143 public function getExamples() {
145 'api.php?action=options&reset=&token=123ABC',
146 'api.php?action=options&change=skin=vector|hideminor=1&token=123ABC',
147 'api.php?action=options&reset=&change=skin=monobook&optionname=nickname&optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC',
151 public function getVersion() {
152 return __CLASS__
. ': $Id$';