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' );
36 public function doesWrites() {
40 public function execute( $par ) {
42 $this->outputHeader();
43 $out = $this->getOutput();
44 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
46 $this->requireLogin( 'prefsnologintext2' );
47 $this->checkReadOnly();
49 if ( $par == 'reset' ) {
50 $this->showResetForm();
55 $out->addModules( 'mediawiki.special.preferences' );
56 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
58 $session = $this->getRequest()->getSession();
59 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
60 // Remove session data for the success message
61 $session->remove( 'specialPreferencesSaveSuccess' );
62 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
68 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
69 'id' => 'mw-preferences-success',
70 'data-mw-autohide' => 'false',
72 Html
::element( 'p', [], $this->msg( 'savedprefs' )->text() )
77 $this->addHelpLink( 'Help:Preferences' );
79 // Load the user from the master to reduce CAS errors on double post (T95839)
80 if ( $this->getRequest()->wasPosted() ) {
81 $user = $this->getUser()->getInstanceForUpdate() ?
: $this->getUser();
83 $user = $this->getUser();
86 $htmlForm = $this->getFormObject( $user, $this->getContext() );
87 $sectionTitles = $htmlForm->getPreferenceSections();
90 foreach ( $sectionTitles as $key ) {
91 $prefTabs .= Html
::rawElement( 'li',
93 'role' => 'presentation',
94 'class' => ( $key === 'personal' ) ?
'selected' : null
96 Html
::rawElement( 'a',
98 'id' => 'preftab-' . $key,
100 'href' => '#mw-prefsection-' . $key,
101 'aria-controls' => 'mw-prefsection-' . $key,
102 'aria-selected' => ( $key === 'personal' ) ?
'true' : 'false',
103 'tabIndex' => ( $key === 'personal' ) ?
0 : -1,
105 $htmlForm->getLegend( $key )
111 Html
::rawElement( 'ul',
122 * Get the preferences form to use.
123 * @param User $user The user.
124 * @param IContextSource $context The context.
125 * @return PreferencesForm|HTMLForm
127 protected function getFormObject( $user, IContextSource
$context ) {
128 $preferencesFactory = MediaWikiServices
::getInstance()->getPreferencesFactory();
129 $form = $preferencesFactory->getForm( $user, $context );
133 protected function showResetForm() {
134 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
135 throw new PermissionsError( 'editmyoptions' );
138 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
140 $context = new DerivativeContext( $this->getContext() );
141 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
142 $htmlForm = new HTMLForm( [], $context, 'prefs-restore' );
144 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
145 $htmlForm->setSubmitDestructive();
146 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
147 $htmlForm->suppressReset();
152 public function submitReset( $formData ) {
153 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
154 throw new PermissionsError( 'editmyoptions' );
157 $user = $this->getUser()->getInstanceForUpdate();
158 $user->resetOptions( 'all', $this->getContext() );
159 $user->saveSettings();
161 // Set session data for the success message
162 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
164 $url = $this->getPageTitle()->getFullUrlForRedirect();
165 $this->getOutput()->redirect( $url );
170 protected function getGroupName() {