Merge "parserTests.php: fix three bitrot bugs with --record"
[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 doesWrites() {
35 return true;
36 }
37
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.
43
44 $this->requireLogin( 'prefsnologintext2' );
45 $this->checkReadOnly();
46
47 if ( $par == 'reset' ) {
48 $this->showResetForm();
49
50 return;
51 }
52
53 $out->addModules( 'mediawiki.special.preferences' );
54 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
55
56 $request = $this->getRequest();
57 if ( $request->getSessionData( 'specialPreferencesSaveSuccess' ) ) {
58 // Remove session data for the success message
59 $request->setSessionData( 'specialPreferencesSaveSuccess', null );
60
61 $out->wrapWikiMsg(
62 Html::rawElement(
63 'div',
64 [
65 'class' => 'mw-preferences-messagebox successbox',
66 'id' => 'mw-preferences-success'
67 ],
68 Html::element( 'p', [], '$1' )
69 ),
70 'savedprefs'
71 );
72 }
73
74 $this->addHelpLink( 'Help:Preferences' );
75
76 // Load the user from the master to reduce CAS errors on double post (T95839)
77 if ( $this->getRequest()->wasPosted() ) {
78 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
79 } else {
80 $user = $this->getUser();
81 }
82
83 $htmlForm = Preferences::getFormObject( $user, $this->getContext() );
84 $htmlForm->setSubmitCallback( [ 'Preferences', 'tryUISubmit' ] );
85 $sectionTitles = $htmlForm->getPreferenceSections();
86
87 $prefTabs = '';
88 foreach ( $sectionTitles as $key ) {
89 $prefTabs .= Html::rawElement( 'li',
90 [
91 'role' => 'presentation',
92 'class' => ( $key === 'personal' ) ? 'selected' : null
93 ],
94 Html::rawElement( 'a',
95 [
96 'id' => 'preftab-' . $key,
97 'role' => 'tab',
98 'href' => '#mw-prefsection-' . $key,
99 'aria-controls' => 'mw-prefsection-' . $key,
100 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
101 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
102 ],
103 $htmlForm->getLegend( $key )
104 )
105 );
106 }
107
108 $out->addHTML(
109 Html::rawElement( 'ul',
110 [
111 'id' => 'preftoc',
112 'role' => 'tablist'
113 ],
114 $prefTabs )
115 );
116 $htmlForm->show();
117 }
118
119 private function showResetForm() {
120 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
121 throw new PermissionsError( 'editmyoptions' );
122 }
123
124 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
125
126 $context = new DerivativeContext( $this->getContext() );
127 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
128 $htmlForm = new HTMLForm( [], $context, 'prefs-restore' );
129
130 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
131 $htmlForm->setSubmitDestructive();
132 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
133 $htmlForm->suppressReset();
134
135 $htmlForm->show();
136 }
137
138 public function submitReset( $formData ) {
139 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
140 throw new PermissionsError( 'editmyoptions' );
141 }
142
143 $user = $this->getUser()->getInstanceForUpdate();
144 $user->resetOptions( 'all', $this->getContext() );
145 $user->saveSettings();
146
147 // Set session data for the success message
148 $this->getRequest()->setSessionData( 'specialPreferencesSaveSuccess', 1 );
149
150 $url = $this->getPageTitle()->getFullURL();
151 $this->getOutput()->redirect( $url );
152
153 return true;
154 }
155
156 protected function getGroupName() {
157 return 'users';
158 }
159 }