MediaWiki master
ApiOptions.php
Go to the documentation of this file.
1<?php
27
41 public function __construct(
42 ApiMain $main,
43 $action,
44 UserOptionsManager $userOptionsManager = null,
45 PreferencesFactory $preferencesFactory = null
46 ) {
51 $services = MediaWikiServices::getInstance();
52 $userOptionsManager ??= $services->getUserOptionsManager();
53 $preferencesFactory ??= $services->getPreferencesFactory();
54 parent::__construct( $main, $action, $userOptionsManager, $preferencesFactory );
55 }
56
57 protected function runHook( $user, $changes, $resetKinds ) {
58 $this->getHookRunner()->onApiOptions( $this, $user, $changes, $resetKinds );
59 }
60
61 protected function shouldIgnoreKey( $key ) {
62 $user = $this->getUserForUpdates();
63 $manager = $this->getUserOptionsManager();
64 if ( $this->getGlobalParam() === 'ignore' && $manager->isOptionGlobal( $user, $key ) ) {
65 $this->addWarning( $this->msg( 'apiwarn-global-option-ignored', $key ) );
66 return true;
67 }
68 return false;
69 }
70
71 protected function resetPreferences( array $kinds ) {
72 $optionNames = $this->getPreferencesFactory()->getOptionNamesForReset(
73 $this->getUserForUpdates(), $this->getContext(), $kinds );
74 $this->getUserOptionsManager()->resetOptionsByName( $this->getUserForUpdates(), $optionNames );
75 }
76
77 protected function setPreference( $preference, $value ) {
78 $globalUpdateType = [
79 'ignore' => UserOptionsManager::GLOBAL_IGNORE,
80 'update' => UserOptionsManager::GLOBAL_UPDATE,
81 'override' => UserOptionsManager::GLOBAL_OVERRIDE
82 ][ $this->getGlobalParam() ];
83
84 $this->getUserOptionsManager()->setOption(
85 $this->getUserForUpdates(),
86 $preference,
87 $value,
88 $globalUpdateType
89 );
90 }
91
92 private function getGlobalParam() {
93 return $this->extractRequestParams()['global'];
94 }
95
96 protected function commitChanges() {
97 $this->getUserForUpdates()->saveSettings();
98 }
99
100 public function getHelpUrls() {
101 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
102 }
103
104 protected function getExamplesMessages() {
105 return [
106 'action=options&reset=&token=123ABC'
107 => 'apihelp-options-example-reset',
108 'action=options&change=skin=vector|hideminor=1&token=123ABC'
109 => 'apihelp-options-example-change',
110 'action=options&reset=&change=skin=monobook&optionname=nickname&' .
111 'optionvalue=[[User:Beau|Beau]]%20([[User_talk:Beau|talk]])&token=123ABC'
112 => 'apihelp-options-example-complex',
113 ];
114 }
115
116 public function getAllowedParams() {
117 return parent::getAllowedParams() + [
118 'global' => [
119 ParamValidator::PARAM_TYPE => [ 'ignore', 'update', 'override' ],
120 ParamValidator::PARAM_DEFAULT => 'ignore'
121 ]
122 ];
123 }
124}
getContext()
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1458
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:766
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:67
The base class for core's ApiOptions and two modules in the GlobalPreferences extension.
getUserForUpdates()
Load the user from the primary to reduce CAS errors on double post (T95839) Will throw if the user is...
API module that facilitates the changing of user's preferences.
resetPreferences(array $kinds)
Reset preferences of the specified kinds.
commitChanges()
Applies changes to user preferences.
getExamplesMessages()
Returns usage examples for this module.
setPreference( $preference, $value)
Sets one user preference to be applied by commitChanges()
runHook( $user, $changes, $resetKinds)
Run the ApiOptions hook if applicable.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
shouldIgnoreKey( $key)
Check whether a key should be ignored.
__construct(ApiMain $main, $action, UserOptionsManager $userOptionsManager=null, PreferencesFactory $preferencesFactory=null)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Service locator for MediaWiki core services.
A service class to control user options.
Service for formatting and validating API parameters.
A PreferencesFactory is a MediaWiki service that provides the definitions of preferences for a given ...