Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
49.44% |
44 / 89 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
SpecialPreferences | |
50.00% |
44 / 88 |
|
28.57% |
2 / 7 |
38.50 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
doesWrites | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
71.70% |
38 / 53 |
|
0.00% |
0 / 1 |
6.82 | |||
getFormObject | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
showResetForm | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
6 | |||
submitReset | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Specials; |
22 | |
23 | use MediaWiki\Context\IContextSource; |
24 | use MediaWiki\Html\Html; |
25 | use MediaWiki\HTMLForm\HTMLForm; |
26 | use MediaWiki\MediaWikiServices; |
27 | use MediaWiki\Preferences\PreferencesFactory; |
28 | use MediaWiki\SpecialPage\SpecialPage; |
29 | use MediaWiki\User\Options\UserOptionsManager; |
30 | use MediaWiki\User\User; |
31 | use OOUI\FieldLayout; |
32 | use OOUI\SearchInputWidget; |
33 | use PermissionsError; |
34 | use PreferencesFormOOUI; |
35 | |
36 | /** |
37 | * A special page that allows users to change their preferences |
38 | * |
39 | * @ingroup SpecialPage |
40 | */ |
41 | class SpecialPreferences extends SpecialPage { |
42 | |
43 | private PreferencesFactory $preferencesFactory; |
44 | private UserOptionsManager $userOptionsManager; |
45 | |
46 | /** |
47 | * @param PreferencesFactory|null $preferencesFactory |
48 | * @param UserOptionsManager|null $userOptionsManager |
49 | */ |
50 | public function __construct( |
51 | ?PreferencesFactory $preferencesFactory = null, |
52 | ?UserOptionsManager $userOptionsManager = null |
53 | ) { |
54 | parent::__construct( 'Preferences' ); |
55 | // This class is extended and therefore falls back to global state - T265924 |
56 | $services = MediaWikiServices::getInstance(); |
57 | $this->preferencesFactory = $preferencesFactory ?? $services->getPreferencesFactory(); |
58 | $this->userOptionsManager = $userOptionsManager ?? $services->getUserOptionsManager(); |
59 | } |
60 | |
61 | public function doesWrites() { |
62 | return true; |
63 | } |
64 | |
65 | public function execute( $par ) { |
66 | $this->setHeaders(); |
67 | $this->outputHeader(); |
68 | $out = $this->getOutput(); |
69 | $out->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc. |
70 | |
71 | $this->requireNamedUser( 'prefsnologintext2' ); |
72 | $this->checkReadOnly(); |
73 | |
74 | if ( $par == 'reset' ) { |
75 | $this->showResetForm(); |
76 | |
77 | return; |
78 | } |
79 | |
80 | $out->addModules( 'mediawiki.special.preferences.ooui' ); |
81 | $out->addModuleStyles( [ |
82 | 'mediawiki.special.preferences.styles.ooui', |
83 | 'oojs-ui-widgets.styles', |
84 | ] ); |
85 | |
86 | $session = $this->getRequest()->getSession(); |
87 | if ( $session->get( 'specialPreferencesSaveSuccess' ) ) { |
88 | // Remove session data for the success message |
89 | $session->remove( 'specialPreferencesSaveSuccess' ); |
90 | $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' ); |
91 | |
92 | $out->addHTML( |
93 | Html::successBox( |
94 | Html::element( |
95 | 'p', |
96 | [], |
97 | $this->msg( 'savedprefs' )->text() |
98 | ), |
99 | 'mw-preferences-messagebox mw-notify-success' |
100 | ) |
101 | ); |
102 | } |
103 | |
104 | $this->addHelpLink( 'Help:Preferences' ); |
105 | |
106 | // Load the user from the primary DB to reduce CAS errors on double post (T95839) |
107 | if ( $this->getRequest()->wasPosted() ) { |
108 | $user = $this->getUser()->getInstanceForUpdate() ?: $this->getUser(); |
109 | } else { |
110 | $user = $this->getUser(); |
111 | } |
112 | |
113 | $htmlForm = $this->getFormObject( $user, $this->getContext() ); |
114 | $sectionTitles = $htmlForm->getPreferenceSections(); |
115 | |
116 | $prefTabs = []; |
117 | foreach ( $sectionTitles as $key ) { |
118 | $prefTabs[] = [ |
119 | 'name' => $key, |
120 | 'label' => $htmlForm->getLegend( $key ), |
121 | ]; |
122 | } |
123 | $out->addJsConfigVars( 'wgPreferencesTabs', $prefTabs ); |
124 | |
125 | $out->addHTML( new FieldLayout( |
126 | new SearchInputWidget( [ |
127 | 'placeholder' => $this->msg( 'searchprefs' )->text(), |
128 | ] ), |
129 | [ |
130 | 'classes' => [ 'mw-prefs-search' ], |
131 | 'label' => $this->msg( 'searchprefs' )->text(), |
132 | 'invisibleLabel' => true, |
133 | 'infusable' => true, |
134 | ] |
135 | ) ); |
136 | $htmlForm->show(); |
137 | } |
138 | |
139 | /** |
140 | * Get the preferences form to use. |
141 | * @param User $user |
142 | * @param IContextSource $context |
143 | * @return PreferencesFormOOUI|HTMLForm |
144 | */ |
145 | protected function getFormObject( $user, IContextSource $context ) { |
146 | $form = $this->preferencesFactory->getForm( $user, $context, PreferencesFormOOUI::class ); |
147 | return $form; |
148 | } |
149 | |
150 | protected function showResetForm() { |
151 | if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) { |
152 | throw new PermissionsError( 'editmyoptions' ); |
153 | } |
154 | |
155 | $this->getOutput()->addWikiMsg( 'prefs-reset-intro' ); |
156 | |
157 | $desc = [ |
158 | 'confirm' => [ |
159 | 'type' => 'check', |
160 | 'label-message' => 'prefs-reset-confirm', |
161 | 'required' => true, |
162 | ], |
163 | ]; |
164 | // TODO: disable the submit button if the checkbox is not checked |
165 | HTMLForm::factory( 'ooui', $desc, $this->getContext(), 'prefs-restore' ) |
166 | ->setTitle( $this->getPageTitle( 'reset' ) ) // Reset subpage |
167 | ->setSubmitTextMsg( 'restoreprefs' ) |
168 | ->setSubmitDestructive() |
169 | ->setSubmitCallback( [ $this, 'submitReset' ] ) |
170 | ->showCancel() |
171 | ->setCancelTarget( $this->getPageTitle() ) |
172 | ->show(); |
173 | } |
174 | |
175 | public function submitReset( $formData ) { |
176 | if ( !$this->getAuthority()->isAllowed( 'editmyoptions' ) ) { |
177 | throw new PermissionsError( 'editmyoptions' ); |
178 | } |
179 | |
180 | $user = $this->getUser()->getInstanceForUpdate(); |
181 | $this->userOptionsManager->resetAllOptions( $user ); |
182 | $user->saveSettings(); |
183 | |
184 | // Set session data for the success message |
185 | $this->getRequest()->getSession()->set( 'specialPreferencesSaveSuccess', 1 ); |
186 | |
187 | $url = $this->getPageTitle()->getFullUrlForRedirect(); |
188 | $this->getOutput()->redirect( $url ); |
189 | |
190 | return true; |
191 | } |
192 | |
193 | protected function getGroupName() { |
194 | return 'login'; |
195 | } |
196 | } |
197 | |
198 | /** |
199 | * Retain the old class name for backwards compatibility. |
200 | * @deprecated since 1.41 |
201 | */ |
202 | class_alias( SpecialPreferences::class, 'SpecialPreferences' ); |