Merge "Add semantic tags to license info text"
[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 use MediaWiki\MediaWikiServices;
28
29 /**
30 * API module that facilitates the changing of user's preferences.
31 * Requires API write mode to be enabled.
32 *
33 * @ingroup API
34 */
35 class ApiOptions extends ApiBase {
36 /**
37 * Changes preferences of the current user.
38 */
39 public function execute() {
40 if ( $this->getUser()->isAnon() ) {
41 $this->dieWithError(
42 [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin'
43 );
44 }
45
46 $this->checkUserRightsAny( 'editmyoptions' );
47
48 $params = $this->extractRequestParams();
49 $changed = false;
50
51 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
52 $this->dieWithError( [ 'apierror-missingparam', 'optionname' ] );
53 }
54
55 // Load the user from the master to reduce CAS errors on double post (T95839)
56 $user = $this->getUser()->getInstanceForUpdate();
57 if ( !$user ) {
58 $this->dieWithError(
59 [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin'
60 );
61 }
62
63 if ( $params['reset'] ) {
64 $user->resetOptions( $params['resetkinds'], $this->getContext() );
65 $changed = true;
66 }
67
68 $changes = [];
69 if ( $params['change'] ) {
70 foreach ( $params['change'] as $entry ) {
71 $array = explode( '=', $entry, 2 );
72 $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null;
73 }
74 }
75 if ( isset( $params['optionname'] ) ) {
76 $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null;
77 $changes[$params['optionname']] = $newValue;
78 }
79 if ( !$changed && !count( $changes ) ) {
80 $this->dieWithError( 'apierror-nochanges' );
81 }
82
83 $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
84 $prefs = $preferencesFactory->getFormDescriptor( $user, $this->getContext() );
85 $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
86
87 $htmlForm = null;
88 foreach ( $changes as $key => $value ) {
89 switch ( $prefsKinds[$key] ) {
90 case 'registered':
91 // Regular option.
92 if ( $htmlForm === null ) {
93 // We need a dummy HTMLForm for the validate callback...
94 $htmlForm = new HTMLForm( [], $this );
95 }
96 $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key], $htmlForm );
97 $validation = $field->validate( $value, $user->getOptions() );
98 break;
99 case 'registered-multiselect':
100 case 'registered-checkmatrix':
101 // A key for a multiselect or checkmatrix option.
102 $validation = true;
103 $value = $value !== null ? (bool)$value : null;
104 break;
105 case 'userjs':
106 // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts
107 if ( strlen( $key ) > 255 ) {
108 $validation = $this->msg( 'apiwarn-validationfailed-keytoolong', Message::numParam( 255 ) );
109 } elseif ( preg_match( '/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
110 $validation = $this->msg( 'apiwarn-validationfailed-badchars' );
111 } else {
112 $validation = true;
113 }
114 break;
115 case 'special':
116 $validation = $this->msg( 'apiwarn-validationfailed-cannotset' );
117 break;
118 case 'unused':
119 default:
120 $validation = $this->msg( 'apiwarn-validationfailed-badpref' );
121 break;
122 }
123 if ( $validation === true ) {
124 $user->setOption( $key, $value );
125 $changed = true;
126 } else {
127 $this->addWarning( [ 'apiwarn-validationfailed', wfEscapeWikitext( $key ), $validation ] );
128 }
129 }
130
131 if ( $changed ) {
132 // Commit changes
133 $user->saveSettings();
134 }
135
136 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
137 }
138
139 public function mustBePosted() {
140 return true;
141 }
142
143 public function isWriteMode() {
144 return true;
145 }
146
147 public function getAllowedParams() {
148 $optionKinds = User::listOptionKinds();
149 $optionKinds[] = 'all';
150
151 return [
152 'reset' => false,
153 'resetkinds' => [
154 ApiBase::PARAM_TYPE => $optionKinds,
155 ApiBase::PARAM_DFLT => 'all',
156 ApiBase::PARAM_ISMULTI => true
157 ],
158 'change' => [
159 ApiBase::PARAM_ISMULTI => true,
160 ],
161 'optionname' => [
162 ApiBase::PARAM_TYPE => 'string',
163 ],
164 'optionvalue' => [
165 ApiBase::PARAM_TYPE => 'string',
166 ],
167 ];
168 }
169
170 public function needsToken() {
171 return 'csrf';
172 }
173
174 public function getHelpUrls() {
175 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
176 }
177
178 protected function getExamplesMessages() {
179 return [
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',
187 ];
188 }
189 }