Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / specials / SpecialPreferences.php
blobeee5b641a36ce3829fdf09b5474a830e2d3aa6eb
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
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() {
35 return true;
38 public function execute( $par ) {
39 $this->setHeaders();
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();
50 return;
53 $out->addModules( 'mediawiki.special.preferences' );
54 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
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' );
62 $out->addHTML(
63 Html::rawElement(
64 'div',
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();
80 } else {
81 $user = $this->getUser();
84 $htmlForm = Preferences::getFormObject( $user, $this->getContext() );
85 $htmlForm->setSubmitCallback( [ 'Preferences', 'tryUISubmit' ] );
86 $sectionTitles = $htmlForm->getPreferenceSections();
88 $prefTabs = '';
89 foreach ( $sectionTitles as $key ) {
90 $prefTabs .= Html::rawElement( 'li',
92 'role' => 'presentation',
93 'class' => ( $key === 'personal' ) ? 'selected' : null
95 Html::rawElement( 'a',
97 'id' => 'preftab-' . $key,
98 'role' => 'tab',
99 'href' => '#mw-prefsection-' . $key,
100 'aria-controls' => 'mw-prefsection-' . $key,
101 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
102 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
104 $htmlForm->getLegend( $key )
109 $out->addHTML(
110 Html::rawElement( 'ul',
112 'id' => 'preftoc',
113 'role' => 'tablist'
115 $prefTabs )
117 $htmlForm->show();
120 private function showResetForm() {
121 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
122 throw new PermissionsError( 'editmyoptions' );
125 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
127 $context = new DerivativeContext( $this->getContext() );
128 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
129 $htmlForm = new HTMLForm( [], $context, 'prefs-restore' );
131 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
132 $htmlForm->setSubmitDestructive();
133 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
134 $htmlForm->suppressReset();
136 $htmlForm->show();
139 public function submitReset( $formData ) {
140 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
141 throw new PermissionsError( 'editmyoptions' );
144 $user = $this->getUser()->getInstanceForUpdate();
145 $user->resetOptions( 'all', $this->getContext() );
146 $user->saveSettings();
148 // Set session data for the success message
149 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
151 $url = $this->getPageTitle()->getFullURL();
152 $this->getOutput()->redirect( $url );
154 return true;
157 protected function getGroupName() {
158 return 'users';