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