MediaWiki master
SpecialPreferences.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
34use OOUI\FieldLayout;
35use OOUI\SearchInputWidget;
38
45
46 private PreferencesFactory $preferencesFactory;
47 private UserOptionsManager $userOptionsManager;
48
53 public function __construct(
54 PreferencesFactory $preferencesFactory = null,
55 UserOptionsManager $userOptionsManager = null
56 ) {
57 parent::__construct( 'Preferences' );
58 // This class is extended and therefore falls back to global state - T265924
60 $this->preferencesFactory = $preferencesFactory ?? $services->getPreferencesFactory();
61 $this->userOptionsManager = $userOptionsManager ?? $services->getUserOptionsManager();
62 }
63
64 public function doesWrites() {
65 return true;
66 }
67
68 public function execute( $par ) {
69 $this->setHeaders();
70 $this->outputHeader();
71 $out = $this->getOutput();
72 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
73
74 $this->requireNamedUser( 'prefsnologintext2' );
75 $this->checkReadOnly();
76
77 if ( $par == 'reset' ) {
78 $this->showResetForm();
79
80 return;
81 }
82
83 $out->addModules( 'mediawiki.special.preferences.ooui' );
84 $out->addModuleStyles( [
85 'mediawiki.special.preferences.styles.ooui',
86 'oojs-ui-widgets.styles',
87 ] );
88
89 $session = $this->getRequest()->getSession();
90 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
91 // Remove session data for the success message
92 $session->remove( 'specialPreferencesSaveSuccess' );
93 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
94
95 $out->addHTML(
96 Html::successBox(
98 'p',
99 [],
100 $this->msg( 'savedprefs' )->text()
101 ),
102 'mw-preferences-messagebox mw-notify-success'
103 )
104 );
105 }
106
107 $this->addHelpLink( 'Help:Preferences' );
108
109 // Load the user from the primary DB to reduce CAS errors on double post (T95839)
110 if ( $this->getRequest()->wasPosted() ) {
111 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
112 } else {
113 $user = $this->getUser();
114 }
115
116 $htmlForm = $this->getFormObject( $user, $this->getContext() );
117 $sectionTitles = $htmlForm->getPreferenceSections();
118
119 $prefTabs = [];
120 foreach ( $sectionTitles as $key ) {
121 $prefTabs[] = [
122 'name' => $key,
123 'label' => $htmlForm->getLegend( $key ),
124 ];
125 }
126 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
127
128 $out->addHTML( new FieldLayout(
129 new SearchInputWidget( [
130 'placeholder' => $this->msg( 'searchprefs' )->text(),
131 ] ),
132 [
133 'classes' => [ 'mw-prefs-search' ],
134 'label' => $this->msg( 'searchprefs' )->text(),
135 'invisibleLabel' => true,
136 'infusable' => true,
137 ]
138 ) );
139 $htmlForm->show();
140 }
141
148 protected function getFormObject( $user, IContextSource $context ) {
149 $form = $this->preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
150 return $form;
151 }
152
153 protected function showResetForm() {
154 if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
155 throw new PermissionsError( 'editmyoptions' );
156 }
157
158 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
159
160 $desc = [
161 'confirm' => [
162 'type' => 'check',
163 'label-message' => 'prefs-reset-confirm',
164 'required' => true,
165 ],
166 ];
167 // TODO: disable the submit button if the checkbox is not checked
168 HTMLForm::factory( 'ooui', $desc, $this->getContext(), 'prefs-restore' )
169 ->setTitle( $this->getPageTitle( 'reset' ) ) // Reset subpage
170 ->setSubmitTextMsg( 'restoreprefs' )
171 ->setSubmitDestructive()
172 ->setSubmitCallback( [ $this, 'submitReset' ] )
173 ->showCancel()
174 ->setCancelTarget( $this->getPageTitle() )
175 ->show();
176 }
177
178 public function submitReset( $formData ) {
179 if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
180 throw new PermissionsError( 'editmyoptions' );
181 }
182
183 $user = $this->getUser()->getInstanceForUpdate();
184 $this->userOptionsManager->resetOptions( $user, $this->getContext(), 'all' );
185 $user->saveSettings();
186
187 // Set session data for the success message
188 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
189
190 $url = $this->getPageTitle()->getFullUrlForRedirect();
191 $this->getOutput()->redirect( $url );
192
193 return true;
194 }
195
196 protected function getGroupName() {
197 return 'login';
198 }
199}
200
205class_alias( SpecialPreferences::class, 'SpecialPreferences' );
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
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.
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.
requireNamedUser( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in or is a temporary user, throws UserNotLoggedIn.
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 Per default the message key is the canonical name o...
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 this special page may perform database writes.
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...
__construct(PreferencesFactory $preferencesFactory=null, UserOptionsManager $userOptionsManager=null)
execute( $par)
Default execute method Checks user permissions.
A service class to control user options.
internal since 1.36
Definition User.php:93
Show an error when a user tries to do something they do not have the necessary permissions for.
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)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...