Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiOptions.php
bloba1ccea47fedf114a3bbcfcb1d5ea07f6b6bc5fb9
1 <?php
2 /**
3 * Copyright © 2012 Szymon Świerkosz beau@adres.pl
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.
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.
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
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\MediaWikiServices;
26 use MediaWiki\Preferences\PreferencesFactory;
27 use MediaWiki\User\Options\UserOptionsManager;
28 use Wikimedia\ParamValidator\ParamValidator;
30 /**
31 * API module that facilitates the changing of user's preferences.
32 * Requires API write mode to be enabled.
34 * @ingroup API
36 class ApiOptions extends ApiOptionsBase {
37 public function __construct(
38 ApiMain $main,
39 string $action,
40 ?UserOptionsManager $userOptionsManager = null,
41 ?PreferencesFactory $preferencesFactory = null
42 ) {
43 /**
44 * This class is extended by GlobalPreferences extension.
45 * So it falls back to the global state.
47 $services = MediaWikiServices::getInstance();
48 $userOptionsManager ??= $services->getUserOptionsManager();
49 $preferencesFactory ??= $services->getPreferencesFactory();
50 parent::__construct( $main, $action, $userOptionsManager, $preferencesFactory );
53 protected function runHook( $user, $changes, $resetKinds ) {
54 $this->getHookRunner()->onApiOptions( $this, $user, $changes, $resetKinds );
57 protected function shouldIgnoreKey( $key ) {
58 $user = $this->getUserForUpdates();
59 $manager = $this->getUserOptionsManager();
60 if ( $this->getGlobalParam() === 'ignore' && $manager->isOptionGlobal( $user, $key ) ) {
61 $this->addWarning( $this->msg( 'apiwarn-global-option-ignored', $key ) );
62 return true;
64 return false;
67 protected function resetPreferences( array $kinds ) {
68 $optionNames = $this->getPreferencesFactory()->getOptionNamesForReset(
69 $this->getUserForUpdates(), $this->getContext(), $kinds );
70 $this->getUserOptionsManager()->resetOptionsByName( $this->getUserForUpdates(), $optionNames );
73 protected function setPreference( $preference, $value ) {
74 $globalUpdateType = [
75 'ignore' => UserOptionsManager::GLOBAL_IGNORE,
76 'update' => UserOptionsManager::GLOBAL_UPDATE,
77 'override' => UserOptionsManager::GLOBAL_OVERRIDE
78 ][ $this->getGlobalParam() ];
80 $this->getUserOptionsManager()->setOption(
81 $this->getUserForUpdates(),
82 $preference,
83 $value,
84 $globalUpdateType
88 private function getGlobalParam() {
89 return $this->extractRequestParams()['global'];
92 protected function commitChanges() {
93 $this->getUserForUpdates()->saveSettings();
96 public function getHelpUrls() {
97 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
100 protected function getExamplesMessages() {
101 return [
102 'action=options&reset=&token=123ABC'
103 => 'apihelp-options-example-reset',
104 'action=options&change=skin=vector|hideminor=1&token=123ABC'
105 => 'apihelp-options-example-change',
106 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
107 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
108 => 'apihelp-options-example-complex',
112 public function getAllowedParams() {
113 return parent::getAllowedParams() + [
114 'global' => [
115 ParamValidator::PARAM_TYPE => [ 'ignore', 'update', 'override' ],
116 ParamValidator::PARAM_DEFAULT => 'ignore'
122 /** @deprecated class alias since 1.43 */
123 class_alias( ApiOptions::class, 'ApiOptions' );