Merge "Http::getProxy() method to get proxy configuration"
[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' );
54 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
55
56 $request = $this->getRequest();
57 if ( $request->getSessionData( 'specialPreferencesSaveSuccess' ) ) {
58 // Remove session data for the success message
59 $request->setSessionData( 'specialPreferencesSaveSuccess', null );
60
61 $out->wrapWikiMsg(
62 Html::rawElement(
63 'div',
64 [
65 'class' => 'mw-preferences-messagebox successbox',
66 'id' => 'mw-preferences-success'
67 ],
68 Html::element( 'p', [], '$1' )
69 ),
70 'savedprefs'
71 );
72 }
73
74 $this->addHelpLink( 'Help:Preferences' );
75
76 // Load the user from the master to reduce CAS errors on double post (T95839)
77 $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser();
78
79 $htmlForm = Preferences::getFormObject( $user, $this->getContext() );
80 $htmlForm->setSubmitCallback( [ 'Preferences', 'tryUISubmit' ] );
81 $sectionTitles = $htmlForm->getPreferenceSections();
82
83 $prefTabs = '';
84 foreach ( $sectionTitles as $key ) {
85 $prefTabs .= Html::rawElement( 'li',
86 [
87 'role' => 'presentation',
88 'class' => ( $key === 'personal' ) ? 'selected' : null
89 ],
90 Html::rawElement( 'a',
91 [
92 'id' => 'preftab-' . $key,
93 'role' => 'tab',
94 'href' => '#mw-prefsection-' . $key,
95 'aria-controls' => 'mw-prefsection-' . $key,
96 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
97 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
98 ],
99 $htmlForm->getLegend( $key )
100 )
101 );
102 }
103
104 $out->addHTML(
105 Html::rawElement( 'ul',
106 [
107 'id' => 'preftoc',
108 'role' => 'tablist'
109 ],
110 $prefTabs )
111 );
112 $htmlForm->show();
113 }
114
115 private function showResetForm() {
116 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
117 throw new PermissionsError( 'editmyoptions' );
118 }
119
120 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
121
122 $context = new DerivativeContext( $this->getContext() );
123 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
124 $htmlForm = new HTMLForm( [], $context, 'prefs-restore' );
125
126 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
127 $htmlForm->setSubmitDestructive();
128 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
129 $htmlForm->suppressReset();
130
131 $htmlForm->show();
132 }
133
134 public function submitReset( $formData ) {
135 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
136 throw new PermissionsError( 'editmyoptions' );
137 }
138
139 $user = $this->getUser()->getInstanceForUpdate();
140 $user->resetOptions( 'all', $this->getContext() );
141 $user->saveSettings();
142
143 // Set session data for the success message
144 $this->getRequest()->setSessionData( 'specialPreferencesSaveSuccess', 1 );
145
146 $url = $this->getPageTitle()->getFullURL();
147 $this->getOutput()->redirect( $url );
148
149 return true;
150 }
151
152 protected function getGroupName() {
153 return 'users';
154 }
155 }