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
43 * @param IContextSource $context The context.
46 public static function isOouiEnabled( IContextSource
$context ) {
47 return $context->getRequest()->getFuzzyBool( 'ooui',
48 $context->getConfig()->get( 'OOUIPreferences' )
52 public function doesWrites() {
56 public function execute( $par ) {
57 $this->oouiEnabled
= static::isOouiEnabled( $this->getContext() );
60 $this->outputHeader();
61 $out = $this->getOutput();
62 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
64 $this->requireLogin( 'prefsnologintext2' );
65 $this->checkReadOnly();
67 if ( $par == 'reset' ) {
68 $this->showResetForm();
73 if ( $this->oouiEnabled
) {
74 $out->addModules( 'mediawiki.special.preferences.ooui' );
75 $out->addModuleStyles( 'mediawiki.special.preferences.styles.ooui' );
76 $out->addModuleStyles( 'oojs-ui-widgets.styles' );
78 $out->addModules( 'mediawiki.special.preferences' );
79 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
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' );
92 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
93 'id' => 'mw-preferences-success',
94 'data-mw-autohide' => 'false',
96 Html
::element( 'p', [], $this->msg( 'savedprefs' )->text() )
101 $this->addHelpLink( 'Help:Preferences' );
103 // Load the user from the master to reduce CAS errors on double post (T95839)
104 if ( $this->getRequest()->wasPosted() ) {
105 $user = $this->getUser()->getInstanceForUpdate() ?
: $this->getUser();
107 $user = $this->getUser();
110 $htmlForm = $this->getFormObject( $user, $this->getContext() );
111 $sectionTitles = $htmlForm->getPreferenceSections();
113 if ( $this->oouiEnabled
) {
115 foreach ( $sectionTitles as $key ) {
118 'label' => $htmlForm->getLegend( $key ),
121 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
125 foreach ( $sectionTitles as $key ) {
126 $prefTabs .= Html
::rawElement( 'li',
128 'role' => 'presentation',
129 'class' => ( $key === 'personal' ) ?
'selected' : null
131 Html
::rawElement( 'a',
133 'id' => 'preftab-' . $key,
135 'href' => '#mw-prefsection-' . $key,
136 'aria-controls' => 'mw-prefsection-' . $key,
137 'aria-selected' => ( $key === 'personal' ) ?
'true' : 'false',
138 'tabIndex' => ( $key === 'personal' ) ?
0 : -1,
140 $htmlForm->getLegend( $key )
146 Html
::rawElement( 'ul',
155 $htmlForm->addHiddenField( 'ooui', $this->oouiEnabled ?
'1' : '0' );
161 * Get the preferences form to use.
162 * @param User $user The user.
163 * @param IContextSource $context The context.
164 * @return PreferencesForm|HTMLForm
166 protected function getFormObject( $user, IContextSource
$context ) {
167 $preferencesFactory = MediaWikiServices
::getInstance()->getPreferencesFactory();
168 if ( $this->oouiEnabled
) {
169 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormOOUI
::class );
171 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormLegacy
::class );
176 protected function showResetForm() {
177 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
178 throw new PermissionsError( 'editmyoptions' );
181 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
183 $context = new DerivativeContext( $this->getContext() );
184 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
185 $htmlForm = HTMLForm
::factory(
186 $this->oouiEnabled ?
'ooui' : 'vform', [], $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() {