MediaWiki master
ApiOptions.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
29
37 public function __construct(
38 ApiMain $main,
39 string $action,
40 ?UserOptionsManager $userOptionsManager = null,
41 ?PreferencesFactory $preferencesFactory = null
42 ) {
48 $userOptionsManager ??= $services->getUserOptionsManager();
49 $preferencesFactory ??= $services->getPreferencesFactory();
50 parent::__construct( $main, $action, $userOptionsManager, $preferencesFactory );
51 }
52
53 protected function runHook( $user, $changes, $resetKinds ) {
54 $this->getHookRunner()->onApiOptions( $this, $user, $changes, $resetKinds );
55 }
56
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;
63 }
64 return false;
65 }
66
67 protected function resetPreferences( array $kinds ) {
68 $optionNames = $this->getPreferencesFactory()->getOptionNamesForReset(
69 $this->getUserForUpdates(), $this->getContext(), $kinds );
70 $this->getUserOptionsManager()->resetOptionsByName( $this->getUserForUpdates(), $optionNames );
71 }
72
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() ];
79
80 $this->getUserOptionsManager()->setOption(
81 $this->getUserForUpdates(),
82 $preference,
83 $value,
84 $globalUpdateType
85 );
86 }
87
88 private function getGlobalParam() {
89 return $this->extractRequestParams()['global'];
90 }
91
92 protected function commitChanges() {
93 $this->getUserForUpdates()->saveSettings();
94 }
95
96 public function getHelpUrls() {
97 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Options';
98 }
99
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',
109 ];
110 }
111
112 public function getAllowedParams() {
113 return parent::getAllowedParams() + [
114 'global' => [
115 ParamValidator::PARAM_TYPE => [ 'ignore', 'update', 'override' ],
116 ParamValidator::PARAM_DEFAULT => 'ignore'
117 ]
118 ];
119 }
120}
121
123class_alias( ApiOptions::class, 'ApiOptions' );
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:795
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1495
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
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.
runHook( $user, $changes, $resetKinds)
Run the ApiOptions hook if applicable.
getHelpUrls()
Return links to more detailed help pages about the module.
shouldIgnoreKey( $key)
Check whether a key should be ignored.
resetPreferences(array $kinds)
Reset preferences of the specified kinds.
__construct(ApiMain $main, string $action, ?UserOptionsManager $userOptionsManager=null, ?PreferencesFactory $preferencesFactory=null)
commitChanges()
Applies changes to user preferences.
setPreference( $preference, $value)
Sets one user preference to be applied by commitChanges()
getExamplesMessages()
Returns usage examples for this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
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 ...