3 * Implements Special:Preferences
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * A special page that allows users to change their preferences
27 * @ingroup SpecialPage
29 class SpecialPreferences
extends SpecialPage
{
30 function __construct() {
31 parent
::__construct( 'Preferences' );
34 public function doesWrites() {
38 public function execute( $par ) {
40 $this->outputHeader();
41 $out = $this->getOutput();
42 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
44 $this->requireLogin( 'prefsnologintext2' );
45 $this->checkReadOnly();
47 if ( $par == 'reset' ) {
48 $this->showResetForm();
53 $out->addModules( 'mediawiki.special.preferences.ooui' );
54 $out->addModuleStyles( 'mediawiki.special.preferences.styles.ooui' );
56 $session = $this->getRequest()->getSession();
57 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
58 // Remove session data for the success message
59 $session->remove( 'specialPreferencesSaveSuccess' );
60 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
66 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
67 'id' => 'mw-preferences-success',
68 'data-mw-autohide' => 'false',
70 Html
::element( 'p', [], $this->msg( 'savedprefs' )->text() )
75 $this->addHelpLink( 'Help:Preferences' );
77 // Load the user from the master to reduce CAS errors on double post (T95839)
78 if ( $this->getRequest()->wasPosted() ) {
79 $user = $this->getUser()->getInstanceForUpdate() ?
: $this->getUser();
81 $user = $this->getUser();
84 $htmlForm = $this->getFormObject( $user, $this->getContext() );
85 $htmlForm->setSubmitCallback( [ 'Preferences', 'tryUISubmit' ] );
88 foreach ( $htmlForm->getPreferenceSections() as $key ) {
91 'label' => $htmlForm->getLegend( $key ),
94 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
96 // TODO: Render fake tabs here to avoid FOUC.
97 // $out->addHTML( $fakeTabs );
103 * Get the preferences form to use.
104 * @param User $user The user.
105 * @param IContextSource $context The context.
106 * @return PreferencesForm|HTMLForm
108 protected function getFormObject( $user, IContextSource
$context ) {
109 return Preferences
::getFormObject( $user, $context );
112 protected function showResetForm() {
113 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
114 throw new PermissionsError( 'editmyoptions' );
117 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
119 $context = new DerivativeContext( $this->getContext() );
120 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
121 $htmlForm = HTMLForm
::factory( 'ooui', [], $context, 'prefs-restore' );
123 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
124 $htmlForm->setSubmitDestructive();
125 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
126 $htmlForm->suppressReset();
131 public function submitReset( $formData ) {
132 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
133 throw new PermissionsError( 'editmyoptions' );
136 $user = $this->getUser()->getInstanceForUpdate();
137 $user->resetOptions( 'all', $this->getContext() );
138 $user->saveSettings();
140 // Set session data for the success message
141 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
143 $url = $this->getPageTitle()->getFullUrlForRedirect();
144 $this->getOutput()->redirect( $url );
149 protected function getGroupName() {