MediaWiki master
SpecialPreferences.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
32use OOUI\FieldLayout;
33use OOUI\SearchInputWidget;
35
42
43 private PreferencesFactory $preferencesFactory;
44 private UserOptionsManager $userOptionsManager;
45
46 public function __construct(
47 ?PreferencesFactory $preferencesFactory = null,
48 ?UserOptionsManager $userOptionsManager = null
49 ) {
50 parent::__construct( 'Preferences' );
51 // This class is extended and therefore falls back to global state - T265924
53 $this->preferencesFactory = $preferencesFactory ?? $services->getPreferencesFactory();
54 $this->userOptionsManager = $userOptionsManager ?? $services->getUserOptionsManager();
55 }
56
57 public function doesWrites() {
58 return true;
59 }
60
61 public function execute( $par ) {
62 $this->setHeaders();
63 $this->outputHeader();
64 $out = $this->getOutput();
65 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
66
67 $this->requireNamedUser( 'prefsnologintext2' );
68 $this->checkReadOnly();
69
70 if ( $par == 'reset' ) {
71 $this->showResetForm();
72
73 return;
74 }
75
76 $out->addModules( 'mediawiki.special.preferences.ooui' );
77 $out->addModuleStyles( [
78 'mediawiki.special.preferences.styles.ooui',
79 'oojs-ui-widgets.styles',
80 ] );
81
82 $session = $this->getRequest()->getSession();
83 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
84 // Remove session data for the success message
85 $session->remove( 'specialPreferencesSaveSuccess' );
86 $out->addModuleStyles( [
87 'mediawiki.codex.messagebox.styles',
88 'mediawiki.notification.convertmessagebox.styles'
89 ] );
90
91 $out->addHTML(
92 Html::successBox(
94 'p',
95 [],
96 $this->msg( 'savedprefs' )->text()
97 ),
98 'mw-preferences-messagebox mw-notify-success'
99 )
100 );
101 }
102
103 $this->addHelpLink( 'Help:Preferences' );
104
105 // Load the user from the primary DB to reduce CAS errors on double post (T95839)
106 if ( $this->getRequest()->wasPosted() ) {
107 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
108 } else {
109 $user = $this->getUser();
110 }
111
112 $htmlForm = $this->getFormObject( $user, $this->getContext() );
113 $sectionTitles = $htmlForm->getPreferenceSections();
114
115 $prefTabs = [];
116 foreach ( $sectionTitles as $key ) {
117 $prefTabs[] = [
118 'name' => $key,
119 'label' => $htmlForm->getLegend( $key ),
120 ];
121 }
122 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
123
124 $out->addHTML( new FieldLayout(
125 new SearchInputWidget( [
126 'placeholder' => $this->msg( 'searchprefs' )->text(),
127 ] ),
128 [
129 'classes' => [ 'mw-prefs-search' ],
130 'label' => $this->msg( 'searchprefs' )->text(),
131 'invisibleLabel' => true,
132 'infusable' => true,
133 ]
134 ) );
135 $htmlForm->show();
136 }
137
144 protected function getFormObject( $user, IContextSource $context ) {
145 $form = $this->preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
146 return $form;
147 }
148
149 protected function showResetForm() {
150 if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
151 throw new PermissionsError( 'editmyoptions' );
152 }
153
154 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
155
156 $desc = [
157 'confirm' => [
158 'type' => 'check',
159 'label-message' => 'prefs-reset-confirm',
160 'required' => true,
161 ],
162 ];
163 // TODO: disable the submit button if the checkbox is not checked
164 HTMLForm::factory( 'ooui', $desc, $this->getContext(), 'prefs-restore' )
165 ->setTitle( $this->getPageTitle( 'reset' ) ) // Reset subpage
166 ->setSubmitTextMsg( 'restoreprefs' )
167 ->setSubmitDestructive()
168 ->setSubmitCallback( $this->submitReset( ... ) )
169 ->showCancel()
170 ->setCancelTarget( $this->getPageTitle() )
171 ->show();
172 }
173
174 public function submitReset( $formData ) {
175 if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
176 throw new PermissionsError( 'editmyoptions' );
177 }
178
179 $user = $this->getUser()->getInstanceForUpdate();
180 $this->userOptionsManager->resetAllOptions( $user );
181 $user->saveSettings();
182
183 // Set session data for the success message
184 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
185
186 $url = $this->getPageTitle()->getFullUrlForRedirect();
187 $this->getOutput()->redirect( $url );
188
189 return true;
190 }
191
192 protected function getGroupName() {
193 return 'login';
194 }
195}
196
201class_alias( SpecialPreferences::class, 'SpecialPreferences' );
Show an error when a user tries to do something they do not have the necessary permissions for.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getUser()
Shortcut to get the User executing this instance.
requireNamedUser( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin', bool $alwaysRedirectToLoginPage=false)
If the user is not logged in or is a temporary user, throws UserNotLoggedIn.
getPageTitle( $subpage=false)
Get a self-referential title object.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getAuthority()
Shortcut to get the Authority executing this instance.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
A special page that allows users to change their preferences.
doesWrites()
Indicates whether POST requests to this special page require write access to the wiki.
__construct(?PreferencesFactory $preferencesFactory=null, ?UserOptionsManager $userOptionsManager=null)
getFormObject( $user, IContextSource $context)
Get the preferences form to use.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
execute( $par)
Default execute method Checks user permissions.
A service class to control user options.
User class for the MediaWiki software.
Definition User.php:123
Form to edit user preferences.
Interface for objects which can provide a MediaWiki context on request.
A PreferencesFactory is a MediaWiki service that provides the definitions of preferences for a given ...
element(SerializerNode $parent, SerializerNode $node, $contents)