Merge "Use single quotes in API where possible"
[lhc/web/wiklou.git] / includes / api / ApiOptions.php
1 <?php
2 /**
3 *
4 *
5 * Created on Apr 15, 2012
6 *
7 * Copyright © 2012 Szymon Świerkosz beau@adres.pl
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * API module that facilitates the changing of user's preferences.
29 * Requires API write mode to be enabled.
30 *
31 * @ingroup API
32 */
33 class ApiOptions extends ApiBase {
34 /**
35 * Changes preferences of the current user.
36 */
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' );
42 }
43
44 $params = $this->extractRequestParams();
45 $changed = false;
46
47 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
48 $this->dieUsageMsg( [ 'missingparam', 'optionname' ] );
49 }
50
51 // Load the user from the master to reduce CAS errors on double post (T95839)
52 $user = $this->getUser()->getInstanceForUpdate();
53 if ( !$user ) {
54 $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' );
55 }
56
57 if ( $params['reset'] ) {
58 $user->resetOptions( $params['resetkinds'], $this->getContext() );
59 $changed = true;
60 }
61
62 $changes = [];
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;
67 }
68 }
69 if ( isset( $params['optionname'] ) ) {
70 $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null;
71 $changes[$params['optionname']] = $newValue;
72 }
73 if ( !$changed && !count( $changes ) ) {
74 $this->dieUsage( 'No changes were requested', 'nochanges' );
75 }
76
77 $prefs = Preferences::getPreferences( $user, $this->getContext() );
78 $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
79
80 $htmlForm = null;
81 foreach ( $changes as $key => $value ) {
82 switch ( $prefsKinds[$key] ) {
83 case 'registered':
84 // Regular option.
85 if ( $htmlForm === null ) {
86 // We need a dummy HTMLForm for the validate callback...
87 $htmlForm = new HTMLForm( [], $this );
88 }
89 $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key] );
90 $field->mParent = $htmlForm;
91 $validation = $field->validate( $value, $user->getOptions() );
92 break;
93 case 'registered-multiselect':
94 case 'registered-checkmatrix':
95 // A key for a multiselect or checkmatrix option.
96 $validation = true;
97 $value = $value !== null ? (bool)$value : null;
98 break;
99 case 'userjs':
100 // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts
101 if ( strlen( $key ) > 255 ) {
102 $validation = 'key too long (no more than 255 bytes allowed)';
103 } elseif ( preg_match( '/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
104 $validation = 'invalid key (only a-z, A-Z, 0-9, _, - allowed)';
105 } else {
106 $validation = true;
107 }
108 break;
109 case 'special':
110 $validation = 'cannot be set by this module';
111 break;
112 case 'unused':
113 default:
114 $validation = 'not a valid preference';
115 break;
116 }
117 if ( $validation === true ) {
118 $user->setOption( $key, $value );
119 $changed = true;
120 } else {
121 $this->setWarning( "Validation error for '$key': $validation" );
122 }
123 }
124
125 if ( $changed ) {
126 // Commit changes
127 $user->saveSettings();
128 }
129
130 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
131 }
132
133 public function mustBePosted() {
134 return true;
135 }
136
137 public function isWriteMode() {
138 return true;
139 }
140
141 public function getAllowedParams() {
142 $optionKinds = User::listOptionKinds();
143 $optionKinds[] = 'all';
144
145 return [
146 'reset' => false,
147 'resetkinds' => [
148 ApiBase::PARAM_TYPE => $optionKinds,
149 ApiBase::PARAM_DFLT => 'all',
150 ApiBase::PARAM_ISMULTI => true
151 ],
152 'change' => [
153 ApiBase::PARAM_ISMULTI => true,
154 ],
155 'optionname' => [
156 ApiBase::PARAM_TYPE => 'string',
157 ],
158 'optionvalue' => [
159 ApiBase::PARAM_TYPE => 'string',
160 ],
161 ];
162 }
163
164 public function needsToken() {
165 return 'csrf';
166 }
167
168 public function getHelpUrls() {
169 return 'https://www.mediawiki.org/wiki/API:Options';
170 }
171
172 protected function getExamplesMessages() {
173 return [
174 'action=options&reset=&token=123ABC'
175 => 'apihelp-options-example-reset',
176 'action=options&change=skin=vector|hideminor=1&token=123ABC'
177 => 'apihelp-options-example-change',
178 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
179 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
180 => 'apihelp-options-example-complex',
181 ];
182 }
183 }