Preferences: Use session data instead of URL parameter for success
[lhc/web/wiklou.git] / includes / specials / SpecialPreferences.php
1 <?php
2 /**
3 * Implements Special:Preferences
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that allows users to change their preferences
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialPreferences extends SpecialPage {
30 function __construct() {
31 parent::__construct( 'Preferences' );
32 }
33
34 public function execute( $par ) {
35 $this->setHeaders();
36 $this->outputHeader();
37 $out = $this->getOutput();
38 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
39
40 $this->requireLogin( 'prefsnologintext2' );
41 $this->checkReadOnly();
42
43 if ( $par == 'reset' ) {
44 $this->showResetForm();
45
46 return;
47 }
48
49 $out->addModules( 'mediawiki.special.preferences' );
50 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
51
52 $request = $this->getRequest();
53 if ( $request->getSessionData( 'specialPreferencesSaveSuccess' ) ) {
54 // Remove session data for the success message
55 $request->setSessionData( 'specialPreferencesSaveSuccess', null );
56
57 $out->wrapWikiMsg(
58 Html::rawElement(
59 'div',
60 array(
61 'class' => 'mw-preferences-messagebox successbox',
62 'id' => 'mw-preferences-success'
63 ),
64 Html::element( 'p', array(), '$1' )
65 ),
66 'savedprefs'
67 );
68 }
69
70 $this->addHelpLink( 'Help:Preferences' );
71
72 // Load the user from the master to reduce CAS errors on double post (T95839)
73 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
74
75 $htmlForm = Preferences::getFormObject( $user, $this->getContext() );
76 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryUISubmit' ) );
77 $sectionTitles = $htmlForm->getPreferenceSections();
78
79 $prefTabs = '';
80 foreach ( $sectionTitles as $key ) {
81 $prefTabs .= Html::rawElement( 'li',
82 array(
83 'role' => 'presentation',
84 'class' => ( $key === 'personal' ) ? 'selected' : null
85 ),
86 Html::rawElement( 'a',
87 array(
88 'id' => 'preftab-' . $key,
89 'role' => 'tab',
90 'href' => '#mw-prefsection-' . $key,
91 'aria-controls' => 'mw-prefsection-' . $key,
92 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
93 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
94 ),
95 $htmlForm->getLegend( $key )
96 )
97 );
98 }
99
100 $out->addHTML(
101 Html::rawElement( 'ul',
102 array(
103 'id' => 'preftoc',
104 'role' => 'tablist'
105 ),
106 $prefTabs )
107 );
108 $htmlForm->show();
109 }
110
111 private function showResetForm() {
112 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
113 throw new PermissionsError( 'editmyoptions' );
114 }
115
116 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
117
118 $context = new DerivativeContext( $this->getContext() );
119 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
120 $htmlForm = new HTMLForm( array(), $context, 'prefs-restore' );
121
122 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
123 $htmlForm->setSubmitDestructive();
124 $htmlForm->setSubmitCallback( array( $this, 'submitReset' ) );
125 $htmlForm->suppressReset();
126
127 $htmlForm->show();
128 }
129
130 public function submitReset( $formData ) {
131 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
132 throw new PermissionsError( 'editmyoptions' );
133 }
134
135 $user = $this->getUser()->getInstanceForUpdate();
136 $user->resetOptions( 'all', $this->getContext() );
137 $user->saveSettings();
138
139 // Set session data for the success message
140 $this->getRequest()->setSessionData( 'specialPreferencesSaveSuccess', 1 );
141
142 $url = $this->getPageTitle()->getFullURL();
143 $this->getOutput()->redirect( $url );
144
145 return true;
146 }
147
148 protected function getGroupName() {
149 return 'users';
150 }
151 }