Special:Preferences: Construct fake tabs to avoid FOUC
[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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * A special page that allows users to change their preferences
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialPreferences extends SpecialPage {
32 /**
33 * @var bool Whether OOUI should be enabled here
34 */
35 private $oouiEnabled = false;
36
37 function __construct() {
38 parent::__construct( 'Preferences' );
39 }
40
41 /**
42 * Check if OOUI mode is enabled, by config or query string
43 * @param IContextSource $context The context.
44 * @return bool
45 */
46 public static function isOouiEnabled( IContextSource $context ) {
47 return $context->getRequest()->getFuzzyBool( 'ooui',
48 $context->getConfig()->get( 'OOUIPreferences' )
49 );
50 }
51
52 public function doesWrites() {
53 return true;
54 }
55
56 public function execute( $par ) {
57 $this->oouiEnabled = static::isOouiEnabled( $this->getContext() );
58
59 $this->setHeaders();
60 $this->outputHeader();
61 $out = $this->getOutput();
62 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
63
64 $this->requireLogin( 'prefsnologintext2' );
65 $this->checkReadOnly();
66
67 if ( $par == 'reset' ) {
68 $this->showResetForm();
69
70 return;
71 }
72
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' );
77 } else {
78 $out->addModules( 'mediawiki.special.preferences' );
79 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
80 }
81
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' );
87
88 $out->addHTML(
89 Html::rawElement(
90 'div',
91 [
92 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
93 'id' => 'mw-preferences-success',
94 'data-mw-autohide' => 'false',
95 ],
96 Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
97 )
98 );
99 }
100
101 $this->addHelpLink( 'Help:Preferences' );
102
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();
106 } else {
107 $user = $this->getUser();
108 }
109
110 $htmlForm = $this->getFormObject( $user, $this->getContext() );
111 $sectionTitles = $htmlForm->getPreferenceSections();
112
113 if ( $this->oouiEnabled ) {
114 $prefTabs = [];
115 foreach ( $sectionTitles as $key ) {
116 $prefTabs[] = [
117 'name' => $key,
118 'label' => $htmlForm->getLegend( $key ),
119 ];
120 }
121 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
122 } else {
123
124 $prefTabs = '';
125 foreach ( $sectionTitles as $key ) {
126 $prefTabs .= Html::rawElement( 'li',
127 [
128 'role' => 'presentation',
129 'class' => ( $key === 'personal' ) ? 'selected' : null
130 ],
131 Html::rawElement( 'a',
132 [
133 'id' => 'preftab-' . $key,
134 'role' => 'tab',
135 'href' => '#mw-prefsection-' . $key,
136 'aria-controls' => 'mw-prefsection-' . $key,
137 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
138 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
139 ],
140 $htmlForm->getLegend( $key )
141 )
142 );
143 }
144
145 $out->addHTML(
146 Html::rawElement( 'ul',
147 [
148 'id' => 'preftoc',
149 'role' => 'tablist'
150 ],
151 $prefTabs )
152 );
153 }
154
155 $htmlForm->addHiddenField( 'ooui', $this->oouiEnabled ? '1' : '0' );
156
157 $htmlForm->show();
158 }
159
160 /**
161 * Get the preferences form to use.
162 * @param User $user The user.
163 * @param IContextSource $context The context.
164 * @return PreferencesForm|HTMLForm
165 */
166 protected function getFormObject( $user, IContextSource $context ) {
167 $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
168 if ( $this->oouiEnabled ) {
169 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
170 } else {
171 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormLegacy::class );
172 }
173 return $form;
174 }
175
176 protected function showResetForm() {
177 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
178 throw new PermissionsError( 'editmyoptions' );
179 }
180
181 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
182
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'
187 );
188
189 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
190 $htmlForm->setSubmitDestructive();
191 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
192 $htmlForm->suppressReset();
193
194 $htmlForm->show();
195 }
196
197 public function submitReset( $formData ) {
198 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
199 throw new PermissionsError( 'editmyoptions' );
200 }
201
202 $user = $this->getUser()->getInstanceForUpdate();
203 $user->resetOptions( 'all', $this->getContext() );
204 $user->saveSettings();
205
206 // Set session data for the success message
207 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
208
209 $url = $this->getPageTitle()->getFullUrlForRedirect();
210 $this->getOutput()->redirect( $url );
211
212 return true;
213 }
214
215 protected function getGroupName() {
216 return 'users';
217 }
218 }