Merge "Add error checking for file argument"
[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 execute( $par ) {
35 $this->setHeaders();
36 $this->outputHeader();
37 $out = $this->getOutput();
38 $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
39
40 $this->requireLogin( 'prefsnologintext2' );
41 $this->checkReadOnly();
42
43 if ( $par == 'reset' ) {
44 $this->showResetForm();
45
46 return;
47 }
48
49 $out->addModules( 'mediawiki.special.preferences' );
50 $out->addModuleStyles( 'mediawiki.special.preferences.styles' );
51
52 if ( $this->getRequest()->getCheck( 'success' ) ) {
53 $out->wrapWikiMsg(
54 Html::rawElement(
55 'div',
56 array(
57 'class' => 'mw-preferences-messagebox successbox',
58 'id' => 'mw-preferences-success'
59 ),
60 Html::element( 'p', array(), '$1' )
61 ),
62 'savedprefs'
63 );
64 }
65
66 $this->addHelpLink( 'Help:Preferences' );
67
68 $htmlForm = Preferences::getFormObject( $this->getUser(), $this->getContext() );
69 $htmlForm->setSubmitCallback( array( 'Preferences', 'tryUISubmit' ) );
70 $sectionTitles = $htmlForm->getPreferenceSections();
71
72 $prefTabs = '';
73 foreach ( $sectionTitles as $key ) {
74 $prefTabs .= Html::rawElement( 'li',
75 array(
76 'role' => 'presentation',
77 'class' => ( $key === 'personal' ) ? 'selected' : null
78 ),
79 Html::rawElement( 'a',
80 array(
81 'id' => 'preftab-' . $key,
82 'role' => 'tab',
83 'href' => '#mw-prefsection-' . $key,
84 'aria-controls' => 'mw-prefsection-' . $key,
85 'aria-selected' => ( $key === 'personal' ) ? 'true' : 'false',
86 'tabIndex' => ( $key === 'personal' ) ? 0 : -1,
87 ),
88 $htmlForm->getLegend( $key )
89 )
90 );
91 }
92
93 $out->addHTML(
94 Html::rawElement( 'ul',
95 array(
96 'id' => 'preftoc',
97 'role' => 'tablist'
98 ),
99 $prefTabs )
100 );
101 $htmlForm->show();
102 }
103
104 private function showResetForm() {
105 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
106 throw new PermissionsError( 'editmyoptions' );
107 }
108
109 $this->getOutput()->addWikiMsg( 'prefs-reset-intro' );
110
111 $context = new DerivativeContext( $this->getContext() );
112 $context->setTitle( $this->getPageTitle( 'reset' ) ); // Reset subpage
113 $htmlForm = new HTMLForm( array(), $context, 'prefs-restore' );
114
115 $htmlForm->setSubmitTextMsg( 'restoreprefs' );
116 $htmlForm->setSubmitDestructive();
117 $htmlForm->setSubmitCallback( array( $this, 'submitReset' ) );
118 $htmlForm->suppressReset();
119
120 $htmlForm->show();
121 }
122
123 public function submitReset( $formData ) {
124 if ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) {
125 throw new PermissionsError( 'editmyoptions' );
126 }
127
128 $user = $this->getUser();
129 $user->resetOptions( 'all', $this->getContext() );
130 $user->saveSettings();
131
132 $url = $this->getPageTitle()->getFullURL( 'success' );
133
134 $this->getOutput()->redirect( $url );
135
136 return true;
137 }
138
139 protected function getGroupName() {
140 return 'users';
141 }
142 }