Merge "mw.Feedback: If the message is posted remotely, link the title correctly"
[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' );
56 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
57
58 $session = $this->getRequest()->getSession();
59 if ( $session->get( 'specialPreferencesSaveSuccess' ) ) {
60 // Remove session data for the success message
61 $session->remove( 'specialPreferencesSaveSuccess' );
62 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
63
64 $out->addHTML(
65 Html::rawElement(
66 'div',
67 [
68 'class' => 'mw-preferences-messagebox mw-notify-success successbox',
69 'id' => 'mw-preferences-success',
70 'data-mw-autohide' => 'false',
71 ],
72 Html::element( 'p', [], $this->msg( 'savedprefs' )->text() )
73 )
74 );
75 }
76
77 $this->addHelpLink( 'Help:Preferences' );
78
79 // Load the user from the master to reduce CAS errors on double post (T95839)
80 if ( $this->getRequest()->wasPosted() ) {
81 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
82 } else {
83 $user = $this->getUser();
84 }
85
86 $htmlForm = $this->getFormObject( $user, $this->getContext() );
87 $sectionTitles = $htmlForm->getPreferenceSections();
88
89 $prefTabs = '';
90 foreach ( $sectionTitles as $key ) {
91 $prefTabs .= Html::rawElement( 'li',
92 [
93 'role' => 'presentation',
94 'class' => ( $key === 'personal' ) ? 'selected' : null
95 ],
96 Html::rawElement( 'a',
97 [
98 'id' => 'preftab-' . $key,
99 'role' => 'tab',
100 'href' => '#mw-prefsection-' . $key,
101 'aria-controls' => 'mw-prefsection-' . $key,
102 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
103 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
104 ],
105 $htmlForm->getLegend( $key )
106 )
107 );
108 }
109
110 $out->addHTML(
111 Html::rawElement( 'ul',
112 [
113 'id' => 'preftoc',
114 'role' => 'tablist'
115 ],
116 $prefTabs )
117 );
118 $htmlForm->show();
119 }
120
121 /**
122 * Get the preferences form to use.
123 * @param User $user The user.
124 * @param IContextSource $context The context.
125 * @return PreferencesForm|HTMLForm
126 */
127 protected function getFormObject( $user, IContextSource $context ) {
128 $preferencesFactory = MediaWikiServices::getInstance()->getPreferencesFactory();
129 $form = $preferencesFactory->getForm( $user, $context );
130 return $form;
131 }
132
133 protected function showResetForm() {
134 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
135 throw new PermissionsError( 'editmyoptions' );
136 }
137
138 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
139
140 $context = new DerivativeContext( $this->getContext() );
141 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
142 $htmlForm = new HTMLForm( [], $context, 'prefs-restore' );
143
144 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
145 $htmlForm->setSubmitDestructive();
146 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
147 $htmlForm->suppressReset();
148
149 $htmlForm->show();
150 }
151
152 public function submitReset( $formData ) {
153 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
154 throw new PermissionsError( 'editmyoptions' );
155 }
156
157 $user = $this->getUser()->getInstanceForUpdate();
158 $user->resetOptions( 'all', $this->getContext() );
159 $user->saveSettings();
160
161 // Set session data for the success message
162 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
163
164 $url = $this->getPageTitle()->getFullUrlForRedirect();
165 $this->getOutput()->redirect( $url );
166
167 return true;
168 }
169
170 protected function getGroupName() {
171 return 'users';
172 }
173 }