ApiOptions.php 5.3 KB

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