Allow programmatic input in Command
[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 $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 $sectionTitles = $htmlForm->getPreferenceSections();
87
88 $prefTabs = '';
89 foreach ( $sectionTitles as $key ) {
90 $prefTabs .= Html::rawElement( 'li',
91 [
92 'role' => 'presentation',
93 'class' => ( $key === 'personal' ) ? 'selected' : null
94 ],
95 Html::rawElement( 'a',
96 [
97 'id' => 'preftab-' . $key,
98 'role' => 'tab',
99 'href' => '#mw-prefsection-' . $key,
100 'aria-controls' => 'mw-prefsection-' . $key,
101 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
102 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
103 ],
104 $htmlForm->getLegend( $key )
105 )
106 );
107 }
108
109 $out->addHTML(
110 Html::rawElement( 'ul',
111 [
112 'id' => 'preftoc',
113 'role' => 'tablist'
114 ],
115 $prefTabs )
116 );
117 $htmlForm->show();
118 }
119
120 /**
121 * Get the preferences form to use.
122 * @param User $user The user.
123 * @param IContextSource $context The context.
124 * @return PreferencesForm|HTMLForm
125 */
126 protected function getFormObject( $user, IContextSource $context ) {
127 return Preferences::getFormObject( $user, $context );
128 }
129
130 protected function showResetForm() {
131 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
132 throw new PermissionsError( 'editmyoptions' );
133 }
134
135 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
136
137 $context = new DerivativeContext( $this->getContext() );
138 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
139 $htmlForm = new HTMLForm( [], $context, 'prefs-restore' );
140
141 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
142 $htmlForm->setSubmitDestructive();
143 $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
144 $htmlForm->suppressReset();
145
146 $htmlForm->show();
147 }
148
149 public function submitReset( $formData ) {
150 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
151 throw new PermissionsError( 'editmyoptions' );
152 }
153
154 $user = $this->getUser()->getInstanceForUpdate();
155 $user->resetOptions( 'all', $this->getContext() );
156 $user->saveSettings();
157
158 // Set session data for the success message
159 $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 );
160
161 $url = $this->getPageTitle()->getFullUrlForRedirect();
162 $this->getOutput()->redirect( $url );
163
164 return true;
165 }
166
167 protected function getGroupName() {
168 return 'users';
169 }
170 }