Fix PreferencesForm alias
[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 *
44 * @since 1.32
45 * @param IContextSource $context The context.
46 * @return bool
47 */
48 public static function isOouiEnabled( IContextSource $context ) {
49 return $context->getRequest()->getFuzzyBool( 'ooui',
50 $context->getConfig()->get( 'OOUIPreferences' )
51 );
52 }
53
54 public function doesWrites() {
55 return true;
56 }
57
58 public function execute( $par ) {
59 $this->oouiEnabled = static::isOouiEnabled( $this->getContext() );
60
61 $this->setHeaders();
62 $this->outputHeader();
63 $out = $this->getOutput();
64 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
65
66 $this->requireLogin( 'prefsnologintext2' );
67 $this->checkReadOnly();
68
69 if ( $par == 'reset' ) {
70 $this->showResetForm();
71
72 return;
73 }
74
75 if ( $this->oouiEnabled ) {
76 $out->addModules( 'mediawiki.special.preferences.ooui' );
77 $out->addModuleStyles( 'mediawiki.special.preferences.styles.ooui' );
78 $out->addModuleStyles( 'oojs-ui-widgets.styles' );
79 } else {
80 $out->addModules( 'mediawiki.special.preferences' );
81 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
82 }
83
84 $session = $this->getRequest()->getSession();
85 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
86 // Remove session data for the success message
87 $session->remove( 'specialPreferencesSaveSuccess' );
88 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
89
90 $out->addHTML(
91 Html::rawElement(
92 'div',
93 [
94 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
95 'id' => 'mw-preferences-success',
96 'data-mw-autohide' => 'false',
97 ],
98 Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
99 )
100 );
101 }
102
103 $this->addHelpLink( 'Help:Preferences' );
104
105 // Load the user from the master to reduce CAS errors on double post (T95839)
106 if ( $this->getRequest()->wasPosted() ) {
107 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
108 } else {
109 $user = $this->getUser();
110 }
111
112 $htmlForm = $this->getFormObject( $user, $this->getContext() );
113 $sectionTitles = $htmlForm->getPreferenceSections();
114
115 if ( $this->oouiEnabled ) {
116 $prefTabs = [];
117 foreach ( $sectionTitles as $key ) {
118 $prefTabs[] = [
119 'name' => $key,
120 'label' => $htmlForm->getLegend( $key ),
121 ];
122 }
123 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
124 } else {
125
126 $prefTabs = '';
127 foreach ( $sectionTitles as $key ) {
128 $prefTabs .= Html::rawElement( 'li',
129 [
130 'role' => 'presentation',
131 'class' => ( $key === 'personal' ) ? 'selected' : null
132 ],
133 Html::rawElement( 'a',
134 [
135 'id' => 'preftab-' . $key,
136 'role' => 'tab',
137 'href' => '#mw-prefsection-' . $key,
138 'aria-controls' => 'mw-prefsection-' . $key,
139 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
140 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
141 ],
142 $htmlForm->getLegend( $key )
143 )
144 );
145 }
146
147 $out->addHTML(
148 Html::rawElement( 'ul',
149 [
150 'id' => 'preftoc',
151 'role' => 'tablist'
152 ],
153 $prefTabs )
154 );
155 }
156
157 $htmlForm->addHiddenField( 'ooui', $this->oouiEnabled ? '1' : '0' );
158
159 $htmlForm->show();
160 }
161
162 /**
163 * Get the preferences form to use.
164 * @param User $user The user.
165 * @param IContextSource $context The context.
166 * @return PreferencesFormLegacy|HTMLForm
167 */
168 protected function getFormObject( $user, IContextSource $context ) {
169 $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
170 if ( $this->oouiEnabled ) {
171 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
172 } else {
173 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormLegacy::class );
174 }
175 return $form;
176 }
177
178 protected function showResetForm() {
179 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
180 throw new PermissionsError( 'editmyoptions' );
181 }
182
183 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
184
185 $context = new DerivativeContext( $this->getContext() );
186 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
187 $htmlForm = HTMLForm::factory(
188 $this->oouiEnabled ? 'ooui' : 'vform', [], $context, 'prefs-restore'
189 );
190
191 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
192 $htmlForm->setSubmitDestructive();
193 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
194 $htmlForm->suppressReset();
195
196 $htmlForm->show();
197 }
198
199 public function submitReset( $formData ) {
200 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
201 throw new PermissionsError( 'editmyoptions' );
202 }
203
204 $user = $this->getUser()->getInstanceForUpdate();
205 $user->resetOptions( 'all', $this->getContext() );
206 $user->saveSettings();
207
208 // Set session data for the success message
209 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
210
211 $url = $this->getPageTitle()->getFullUrlForRedirect();
212 $this->getOutput()->redirect( $url );
213
214 return true;
215 }
216
217 protected function getGroupName() {
218 return 'users';
219 }
220 }