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
24 use MediaWiki\MediaWikiServices
;
27 * A special page that allows users to change their preferences
29 * @ingroup SpecialPage
31 class SpecialPreferences
extends SpecialPage
{
32 function __construct() {
33 parent
::__construct( 'Preferences' );
39 * @deprecated since 1.32
42 public static function isOouiEnabled() {
43 // wfDeprecated( __METHOD__, '1.31' );
47 public function doesWrites() {
51 public function execute( $par ) {
53 $this->outputHeader();
54 $out = $this->getOutput();
55 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
57 $this->requireLogin( 'prefsnologintext2' );
58 $this->checkReadOnly();
60 if ( $par == 'reset' ) {
61 $this->showResetForm();
66 $out->addModules( 'mediawiki.special.preferences.ooui' );
67 $out->addModuleStyles( 'mediawiki.special.preferences.styles.ooui' );
68 $out->addModuleStyles( 'oojs-ui-widgets.styles' );
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( 'mediawiki.notification.convertmessagebox.styles' );
80 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
81 'id' => 'mw-preferences-success',
82 'data-mw-autohide' => 'false',
84 Html
::element( 'p', [], $this->msg( 'savedprefs' )->text() )
89 $this->addHelpLink( 'Help:Preferences' );
91 // Load the user from the master to reduce CAS errors on double post (T95839)
92 if ( $this->getRequest()->wasPosted() ) {
93 $user = $this->getUser()->getInstanceForUpdate() ?
: $this->getUser();
95 $user = $this->getUser();
98 $htmlForm = $this->getFormObject( $user, $this->getContext() );
99 $sectionTitles = $htmlForm->getPreferenceSections();
102 foreach ( $sectionTitles as $key ) {
105 'label' => $htmlForm->getLegend( $key ),
108 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
114 * Get the preferences form to use.
115 * @param User $user The user.
116 * @param IContextSource $context The context.
117 * @return PreferencesFormLegacy|HTMLForm
119 protected function getFormObject( $user, IContextSource
$context ) {
120 $preferencesFactory = MediaWikiServices
::getInstance()->getPreferencesFactory();
121 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormOOUI
::class );
125 protected function showResetForm() {
126 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
127 throw new PermissionsError( 'editmyoptions' );
130 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
132 $context = new DerivativeContext( $this->getContext() );
133 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
134 $htmlForm = HTMLForm
::factory( 'ooui', [], $context, 'prefs-restore' );
136 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
137 $htmlForm->setSubmitDestructive();
138 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
139 $htmlForm->suppressReset();
144 public function submitReset( $formData ) {
145 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
146 throw new PermissionsError( 'editmyoptions' );
149 $user = $this->getUser()->getInstanceForUpdate();
150 $user->resetOptions( 'all', $this->getContext() );
151 $user->saveSettings();
153 // Set session data for the success message
154 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
156 $url = $this->getPageTitle()->getFullUrlForRedirect();
157 $this->getOutput()->redirect( $url );
162 protected function getGroupName() {