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
{
33 * @var bool Whether OOUI should be enabled here
35 private $oouiEnabled = false;
37 function __construct() {
38 parent
::__construct( 'Preferences' );
42 * Check if OOUI mode is enabled, by config or query string
45 * @param IContextSource $context The context.
48 public static function isOouiEnabled( IContextSource
$context ) {
49 return $context->getRequest()->getFuzzyBool( 'ooui',
50 $context->getConfig()->get( 'OOUIPreferences' )
54 public function doesWrites() {
58 public function execute( $par ) {
59 $this->oouiEnabled
= static::isOouiEnabled( $this->getContext() );
62 $this->outputHeader();
63 $out = $this->getOutput();
64 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
66 $this->requireLogin( 'prefsnologintext2' );
67 $this->checkReadOnly();
69 if ( $par == 'reset' ) {
70 $this->showResetForm();
75 if ( $this->oouiEnabled
) {
76 $out->addModules( 'mediawiki.special.preferences.ooui' );
77 $out->addModuleStyles( 'mediawiki.special.preferences.styles.ooui' );
78 $out->addModuleStyles( 'oojs-ui-widgets.styles' );
80 $out->addModules( 'mediawiki.special.preferences' );
81 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
84 $session = $this->getRequest()->getSession();
85 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
86 // Remove session data for the success message
87 $session->remove( 'specialPreferencesSaveSuccess' );
88 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
94 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
95 'id' => 'mw-preferences-success',
96 'data-mw-autohide' => 'false',
98 Html
::element( 'p', [], $this->msg( 'savedprefs' )->text() )
103 $this->addHelpLink( 'Help:Preferences' );
105 // Load the user from the master to reduce CAS errors on double post (T95839)
106 if ( $this->getRequest()->wasPosted() ) {
107 $user = $this->getUser()->getInstanceForUpdate() ?
: $this->getUser();
109 $user = $this->getUser();
112 $htmlForm = $this->getFormObject( $user, $this->getContext() );
113 $sectionTitles = $htmlForm->getPreferenceSections();
115 if ( $this->oouiEnabled
) {
117 foreach ( $sectionTitles as $key ) {
120 'label' => $htmlForm->getLegend( $key ),
123 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
127 foreach ( $sectionTitles as $key ) {
128 $prefTabs .= Html
::rawElement( 'li',
130 'role' => 'presentation',
131 'class' => ( $key === 'personal' ) ?
'selected' : null
135 'id' => 'preftab-' . $key,
137 'href' => '#mw-prefsection-' . $key,
138 'aria-controls' => 'mw-prefsection-' . $key,
139 'aria-selected' => ( $key === 'personal' ) ?
'true' : 'false',
140 'tabIndex' => ( $key === 'personal' ) ?
0 : -1,
142 $htmlForm->getLegend( $key )
148 Html
::rawElement( 'ul',
157 $htmlForm->addHiddenField( 'ooui', $this->oouiEnabled ?
'1' : '0' );
163 * Get the preferences form to use.
164 * @param User $user The user.
165 * @param IContextSource $context The context.
166 * @return PreferencesFormLegacy|HTMLForm
168 protected function getFormObject( $user, IContextSource
$context ) {
169 $preferencesFactory = MediaWikiServices
::getInstance()->getPreferencesFactory();
170 if ( $this->oouiEnabled
) {
171 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormOOUI
::class );
173 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormLegacy
::class );
178 protected function showResetForm() {
179 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
180 throw new PermissionsError( 'editmyoptions' );
183 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
185 $context = new DerivativeContext( $this->getContext() );
186 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
187 $htmlForm = HTMLForm
::factory( 'ooui', [], $context, 'prefs-restore' );
189 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
190 $htmlForm->setSubmitDestructive();
191 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
192 $htmlForm->suppressReset();
197 public function submitReset( $formData ) {
198 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
199 throw new PermissionsError( 'editmyoptions' );
202 $user = $this->getUser()->getInstanceForUpdate();
203 $user->resetOptions( 'all', $this->getContext() );
204 $user->saveSettings();
206 // Set session data for the success message
207 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
209 $url = $this->getPageTitle()->getFullUrlForRedirect();
210 $this->getOutput()->redirect( $url );
215 protected function getGroupName() {