Merge "Special:Preferences: Drop non-OOUI legacy form version"
[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 function __construct() {
33 parent::__construct( 'Preferences' );
34 }
35
36 /**
37 *
38 * @since 1.32
39 * @deprecated since 1.32
40 * @return bool
41 */
42 public static function isOouiEnabled() {
43 // wfDeprecated( __METHOD__, '1.31' );
44 return true;
45 }
46
47 public function doesWrites() {
48 return true;
49 }
50
51 public function execute( $par ) {
52 $this->setHeaders();
53 $this->outputHeader();
54 $out = $this->getOutput();
55 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
56
57 $this->requireLogin( 'prefsnologintext2' );
58 $this->checkReadOnly();
59
60 if ( $par == 'reset' ) {
61 $this->showResetForm();
62
63 return;
64 }
65
66 $out->addModules( 'mediawiki.special.preferences.ooui' );
67 $out->addModuleStyles( 'mediawiki.special.preferences.styles.ooui' );
68 $out->addModuleStyles( 'oojs-ui-widgets.styles' );
69
70 $session = $this->getRequest()->getSession();
71 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
72 // Remove session data for the success message
73 $session->remove( 'specialPreferencesSaveSuccess' );
74 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
75
76 $out->addHTML(
77 Html::rawElement(
78 'div',
79 [
80 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
81 'id' => 'mw-preferences-success',
82 'data-mw-autohide' => 'false',
83 ],
84 Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
85 )
86 );
87 }
88
89 $this->addHelpLink( 'Help:Preferences' );
90
91 // Load the user from the master to reduce CAS errors on double post (T95839)
92 if ( $this->getRequest()->wasPosted() ) {
93 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
94 } else {
95 $user = $this->getUser();
96 }
97
98 $htmlForm = $this->getFormObject( $user, $this->getContext() );
99 $sectionTitles = $htmlForm->getPreferenceSections();
100
101 $prefTabs = [];
102 foreach ( $sectionTitles as $key ) {
103 $prefTabs[] = [
104 'name' => $key,
105 'label' => $htmlForm->getLegend( $key ),
106 ];
107 }
108 $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs );
109
110 $htmlForm->show();
111 }
112
113 /**
114 * Get the preferences form to use.
115 * @param User $user The user.
116 * @param IContextSource $context The context.
117 * @return PreferencesFormLegacy|HTMLForm
118 */
119 protected function getFormObject( $user, IContextSource $context ) {
120 $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
121 $form = $preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class );
122 return $form;
123 }
124
125 protected function showResetForm() {
126 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
127 throw new PermissionsError( 'editmyoptions' );
128 }
129
130 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
131
132 $context = new DerivativeContext( $this->getContext() );
133 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
134 $htmlForm = HTMLForm::factory( 'ooui', [], $context, 'prefs-restore' );
135
136 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
137 $htmlForm->setSubmitDestructive();
138 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
139 $htmlForm->suppressReset();
140
141 $htmlForm->show();
142 }
143
144 public function submitReset( $formData ) {
145 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
146 throw new PermissionsError( 'editmyoptions' );
147 }
148
149 $user = $this->getUser()->getInstanceForUpdate();
150 $user->resetOptions( 'all', $this->getContext() );
151 $user->saveSettings();
152
153 // Set session data for the success message
154 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
155
156 $url = $this->getPageTitle()->getFullUrlForRedirect();
157 $this->getOutput()->redirect( $url );
158
159 return true;
160 }
161
162 protected function getGroupName() {
163 return 'users';
164 }
165 }