Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / includes / api / ApiOptions.php
1 <?php
2 /**
3 * Copyright © 2012 Szymon Świerkosz beau@adres.pl
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * API module that facilitates the changing of user's preferences.
27 * Requires API write mode to be enabled.
28 *
29 * @ingroup API
30 */
31 class ApiOptions extends ApiBase {
32 /**
33 * Changes preferences of the current user.
34 */
35 public function execute() {
36 if ( $this->getUser()->isAnon() ) {
37 $this->dieWithError(
38 [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin'
39 );
40 }
41
42 $this->checkUserRightsAny( 'editmyoptions' );
43
44 $params = $this->extractRequestParams();
45 $changed = false;
46
47 if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) {
48 $this->dieWithError( [ 'apierror-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->dieWithError(
55 [ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ], 'notloggedin'
56 );
57 }
58
59 if ( $params['reset'] ) {
60 $user->resetOptions( $params['resetkinds'], $this->getContext() );
61 $changed = true;
62 }
63
64 $changes = [];
65 if ( $params['change'] ) {
66 foreach ( $params['change'] as $entry ) {
67 $array = explode( '=', $entry, 2 );
68 $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null;
69 }
70 }
71 if ( isset( $params['optionname'] ) ) {
72 $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null;
73 $changes[$params['optionname']] = $newValue;
74 }
75 if ( !$changed && !count( $changes ) ) {
76 $this->dieWithError( 'apierror-nochanges' );
77 }
78
79 $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
80 $prefs = $preferencesFactory->getFormDescriptor( $user, $this->getContext() );
81 $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes );
82
83 $htmlForm = null;
84 foreach ( $changes as $key => $value ) {
85 switch ( $prefsKinds[$key] ) {
86 case 'registered':
87 // Regular option.
88 if ( $htmlForm === null ) {
89 // We need a dummy HTMLForm for the validate callback...
90 $htmlForm = new HTMLForm( [], $this );
91 }
92 $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key], $htmlForm );
93 $validation = $field->validate( $value, $user->getOptions() );
94 break;
95 case 'registered-multiselect':
96 case 'registered-checkmatrix':
97 // A key for a multiselect or checkmatrix option.
98 $validation = true;
99 $value = $value !== null ? (bool)$value : null;
100 break;
101 case 'userjs':
102 // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts
103 if ( strlen( $key ) > 255 ) {
104 $validation = $this->msg( 'apiwarn-validationfailed-keytoolong', Message::numParam( 255 ) );
105 } elseif ( preg_match( '/[^a-zA-Z0-9_-]/', $key ) !== 0 ) {
106 $validation = $this->msg( 'apiwarn-validationfailed-badchars' );
107 } else {
108 $validation = true;
109 }
110 break;
111 case 'special':
112 $validation = $this->msg( 'apiwarn-validationfailed-cannotset' );
113 break;
114 case 'unused':
115 default:
116 $validation = $this->msg( 'apiwarn-validationfailed-badpref' );
117 break;
118 }
119 if ( $validation === true ) {
120 $user->setOption( $key, $value );
121 $changed = true;
122 } else {
123 $this->addWarning( [ 'apiwarn-validationfailed', wfEscapeWikiText( $key ), $validation ] );
124 }
125 }
126
127 if ( $changed ) {
128 // Commit changes
129 $user->saveSettings();
130 }
131
132 $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
133 }
134
135 public function mustBePosted() {
136 return true;
137 }
138
139 public function isWriteMode() {
140 return true;
141 }
142
143 public function getAllowedParams() {
144 $optionKinds = User::listOptionKinds();
145 $optionKinds[] = 'all';
146
147 return [
148 'reset' => false,
149 'resetkinds' => [
150 ApiBase::PARAM_TYPE => $optionKinds,
151 ApiBase::PARAM_DFLT => 'all',
152 ApiBase::PARAM_ISMULTI => true
153 ],
154 'change' => [
155 ApiBase::PARAM_ISMULTI => true,
156 ],
157 'optionname' => [
158 ApiBase::PARAM_TYPE => 'string',
159 ],
160 'optionvalue' => [
161 ApiBase::PARAM_TYPE => 'string',
162 ],
163 ];
164 }
165
166 public function needsToken() {
167 return 'csrf';
168 }
169
170 public function getHelpUrls() {
171 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
172 }
173
174 protected function getExamplesMessages() {
175 return [
176 'action=options&reset=&token=123ABC'
177 => 'apihelp-options-example-reset',
178 'action=options&change=skin=vector|hideminor=1&token=123ABC'
179 => 'apihelp-options-example-change',
180 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
181 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
182 => 'apihelp-options-example-complex',
183 ];
184 }
185 }