Merge "Special:Preferences: Use OOjs UI"
[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.ooui' );
54 $out->addModuleStyles( 'mediawiki.special.preferences.styles.ooui' );
55
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' );
61
62 $out->addHTML(
63 Html::rawElement(
64 'div',
65 [
66 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
67 'id' => 'mw-preferences-success',
68 'data-mw-autohide' => 'false',
69 ],
70 Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
71 )
72 );
73 }
74
75 $this->addHelpLink( 'Help:Preferences' );
76
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();
82 }
83
84 $htmlForm = $this->getFormObject( $user, $this->getContext() );
85 $htmlForm->setSubmitCallback( [ 'Preferences', 'tryUISubmit' ] );
86
87 $prefTabs = [];
88 foreach ( $htmlForm->getPreferenceSections() as $key ) {
89 $prefTabs[] = [
90 'name' => $key,
91 'label' => $htmlForm->getLegend( $key ),
92 ];
93 }
94 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
95
96 // TODO: Render fake tabs here to avoid FOUC.
97 // $out->addHTML( $fakeTabs );
98
99 $htmlForm->show();
100 }
101
102 /**
103 * Get the preferences form to use.
104 * @param User $user The user.
105 * @param IContextSource $context The context.
106 * @return PreferencesForm|HTMLForm
107 */
108 protected function getFormObject( $user, IContextSource $context ) {
109 return Preferences::getFormObject( $user, $context );
110 }
111
112 protected function showResetForm() {
113 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
114 throw new PermissionsError( 'editmyoptions' );
115 }
116
117 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
118
119 $context = new DerivativeContext( $this->getContext() );
120 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
121 $htmlForm = HTMLForm::factory( 'ooui', [], $context, 'prefs-restore' );
122
123 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
124 $htmlForm->setSubmitDestructive();
125 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
126 $htmlForm->suppressReset();
127
128 $htmlForm->show();
129 }
130
131 public function submitReset( $formData ) {
132 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
133 throw new PermissionsError( 'editmyoptions' );
134 }
135
136 $user = $this->getUser()->getInstanceForUpdate();
137 $user->resetOptions( 'all', $this->getContext() );
138 $user->saveSettings();
139
140 // Set session data for the success message
141 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
142
143 $url = $this->getPageTitle()->getFullUrlForRedirect();
144 $this->getOutput()->redirect( $url );
145
146 return true;
147 }
148
149 protected function getGroupName() {
150 return 'users';
151 }
152 }