MediaWiki REL1_40
SpecialPreferences.php
Go to the documentation of this file.
1<?php
28
35
37 private $preferencesFactory;
38
40 private $userOptionsManager;
41
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
52 $services = MediaWikiServices::getInstance();
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( 'mediawiki.notification.convertmessagebox.styles' );
87
88 $out->addHTML(
89 Html::successBox(
90 Html::element(
91 'p',
92 [],
93 $this->msg( 'savedprefs' )->text()
94 ),
95 'mw-preferences-messagebox mw-notify-success'
96 )
97 );
98 }
99
100 $this->addHelpLink( 'Help:Preferences' );
101
102 // Load the user from the primary DB to reduce CAS errors on double post (T95839)
103 if ( $this->getRequest()->wasPosted() ) {
104 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
105 } else {
106 $user = $this->getUser();
107 }
108
109 $htmlForm = $this->getFormObject( $user, $this->getContext() );
110 $sectionTitles = $htmlForm->getPreferenceSections();
111
112 $prefTabs = [];
113 foreach ( $sectionTitles as $key ) {
114 $prefTabs[] = [
115 'name' => $key,
116 'label' => $htmlForm->getLegend( $key ),
117 ];
118 }
119 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
120
121 $out->addHTML( new \OOUI\FieldLayout(
122 new \OOUI\SearchInputWidget( [
123 'placeholder' => $this->msg( 'searchprefs' )->text(),
124 ] ),
125 [
126 'classes' => [ 'mw-prefs-search' ],
127 'label' => $this->msg( 'searchprefs' )->text(),
128 'invisibleLabel' => true,
129 'infusable' => true,
130 ]
131 ) );
132 $htmlForm->show();
133 }
134
141 protected function getFormObject( $user, IContextSource $context ) {
142 $form = $this->preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
143 return $form;
144 }
145
146 protected function showResetForm() {
147 if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
148 throw new PermissionsError( 'editmyoptions' );
149 }
150
151 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
152
153 $desc = [
154 'confirm' => [
155 'type' => 'check',
156 'label-message' => 'prefs-reset-confirm',
157 'required' => true,
158 ],
159 ];
160 // TODO: disable the submit button if the checkbox is not checked
161 HTMLForm::factory( 'ooui', $desc, $this->getContext(), 'prefs-restore' )
162 ->setTitle( $this->getPageTitle( 'reset' ) ) // Reset subpage
163 ->setSubmitTextMsg( 'restoreprefs' )
164 ->setSubmitDestructive()
165 ->setSubmitCallback( [ $this, 'submitReset' ] )
166 ->suppressReset()
167 ->showCancel()
168 ->setCancelTarget( $this->getPageTitle() )
169 ->show();
170 }
171
172 public function submitReset( $formData ) {
173 if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) {
174 throw new PermissionsError( 'editmyoptions' );
175 }
176
177 $user = $this->getUser()->getInstanceForUpdate();
178 $this->userOptionsManager->resetOptions( $user, $this->getContext(), 'all' );
179 $user->saveSettings();
180
181 // Set session data for the success message
182 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
183
184 $url = $this->getPageTitle()->getFullUrlForRedirect();
185 $this->getOutput()->redirect( $url );
186
187 return true;
188 }
189
190 protected function getGroupName() {
191 return 'users';
192 }
193}
This class is a collection of static functions that serve two purposes:
Definition Html.php:55
Service locator for MediaWiki core services.
A service class to control user options.
Show an error when a user tries to do something they do not have the necessary permissions for.
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
requireNamedUser( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in or is a temporary user, throws UserNotLoggedIn.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getAuthority()
Shortcut to get the Authority executing this instance.
getRequest()
Get the WebRequest being used for this instance.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getPageTitle( $subpage=false)
Get a self-referential title object.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
A special page that allows users to change their preferences.
execute( $par)
Default execute method Checks user permissions.
doesWrites()
Indicates whether this special page may perform database writes.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(PreferencesFactory $preferencesFactory=null, UserOptionsManager $userOptionsManager=null)
getFormObject( $user, IContextSource $context)
Get the preferences form to use.
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 ...