MediaWiki master
SpecialPreferences.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
31use OOUI\FieldLayout;
32use OOUI\SearchInputWidget;
35
42
43 private PreferencesFactory $preferencesFactory;
44 private UserOptionsManager $userOptionsManager;
45
50 public function __construct(
51 PreferencesFactory $preferencesFactory = null,
52 UserOptionsManager $userOptionsManager = null
53 ) {
54 parent::__construct( 'Preferences' );
55 // This class is extended and therefore falls back to global state - T265924
57 $this->preferencesFactory = $preferencesFactory ?? $services->getPreferencesFactory();
58 $this->userOptionsManager = $userOptionsManager ?? $services->getUserOptionsManager();
59 }
60
61 public function doesWrites() {
62 return true;
63 }
64
65 public function execute( $par ) {
66 $this->setHeaders();
67 $this->outputHeader();
68 $out = $this->getOutput();
69 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
70
71 $this->requireNamedUser( 'prefsnologintext2' );
72 $this->checkReadOnly();
73
74 if ( $par == 'reset' ) {
75 $this->showResetForm();
76
77 return;
78 }
79
80 $out->addModules( 'mediawiki.special.preferences.ooui' );
81 $out->addModuleStyles( [
82 'mediawiki.special.preferences.styles.ooui',
83 'oojs-ui-widgets.styles',
84 ] );
85
86 $session = $this->getRequest()->getSession();
87 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
88 // Remove session data for the success message
89 $session->remove( 'specialPreferencesSaveSuccess' );
90 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
91
92 $out->addHTML(
93 Html::successBox(
95 'p',
96 [],
97 $this->msg( 'savedprefs' )->text()
98 ),
99 'mw-preferences-messagebox mw-notify-success'
100 )
101 );
102 }
103
104 $this->addHelpLink( 'Help:Preferences' );
105
106 // Load the user from the primary DB to reduce CAS errors on double post (T95839)
107 if ( $this->getRequest()->wasPosted() ) {
108 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
109 } else {
110 $user = $this->getUser();
111 }
112
113 $htmlForm = $this->getFormObject( $user, $this->getContext() );
114 $sectionTitles = $htmlForm->getPreferenceSections();
115
116 $prefTabs = [];
117 foreach ( $sectionTitles as $key ) {
118 $prefTabs[] = [
119 'name' => $key,
120 'label' => $htmlForm->getLegend( $key ),
121 ];
122 }
123 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
124
125 $out->addHTML( new FieldLayout(
126 new SearchInputWidget( [
127 'placeholder' => $this->msg( 'searchprefs' )->text(),
128 ] ),
129 [
130 'classes' => [ 'mw-prefs-search' ],
131 'label' => $this->msg( 'searchprefs' )->text(),
132 'invisibleLabel' => true,
133 'infusable' => true,
134 ]
135 ) );
136 $htmlForm->show();
137 }
138
145 protected function getFormObject( $user, IContextSource $context ) {
146 $form = $this->preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
147 return $form;
148 }
149
150 protected function showResetForm() {
151 if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
152 throw new PermissionsError( 'editmyoptions' );
153 }
154
155 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
156
157 $desc = [
158 'confirm' => [
159 'type' => 'check',
160 'label-message' => 'prefs-reset-confirm',
161 'required' => true,
162 ],
163 ];
164 // TODO: disable the submit button if the checkbox is not checked
165 HTMLForm::factory( 'ooui', $desc, $this->getContext(), 'prefs-restore' )
166 ->setTitle( $this->getPageTitle( 'reset' ) ) // Reset subpage
167 ->setSubmitTextMsg( 'restoreprefs' )
168 ->setSubmitDestructive()
169 ->setSubmitCallback( [ $this, 'submitReset' ] )
170 ->showCancel()
171 ->setCancelTarget( $this->getPageTitle() )
172 ->show();
173 }
174
175 public function submitReset( $formData ) {
176 if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
177 throw new PermissionsError( 'editmyoptions' );
178 }
179
180 $user = $this->getUser()->getInstanceForUpdate();
181 $this->userOptionsManager->resetAllOptions( $user );
182 $user->saveSettings();
183
184 // Set session data for the success message
185 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
186
187 $url = $this->getPageTitle()->getFullUrlForRedirect();
188 $this->getOutput()->redirect( $url );
189
190 return true;
191 }
192
193 protected function getGroupName() {
194 return 'login';
195 }
196}
197
202class_alias( SpecialPreferences::class, 'SpecialPreferences' );
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:208
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 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 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)