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