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