Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialGlobalPreferences
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 6
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
 getFormObject
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 showResetForm
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 addHelpLink
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 submitReset
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace GlobalPreferences;
4
5use ErrorPageError;
6use MediaWiki\Context\DerivativeContext;
7use MediaWiki\Context\IContextSource;
8use MediaWiki\HTMLForm\HTMLForm;
9use MediaWiki\Permissions\PermissionManager;
10use MediaWiki\Specials\SpecialPreferences;
11use MediaWiki\User\User;
12use PermissionsError;
13use PreferencesFormOOUI;
14use UserNotLoggedIn;
15
16class SpecialGlobalPreferences extends SpecialPreferences {
17
18    private PermissionManager $permissionManager;
19    private GlobalPreferencesFactory $preferencesFactory;
20
21    public function __construct(
22        PermissionManager $permissionManager,
23        GlobalPreferencesFactory $preferencesFactory
24    ) {
25        parent::__construct();
26        $this->mName = 'GlobalPreferences';
27        $this->permissionManager = $permissionManager;
28        $this->preferencesFactory = $preferencesFactory;
29    }
30
31    /**
32     * Execute the special page.
33     * @param null|string $par The subpage name, if any.
34     * @throws ErrorPageError
35     * @throws UserNotLoggedIn
36     */
37    public function execute( $par ) {
38        // Call the parent
39        parent::execute( $par );
40
41        // Remove subpages other than 'reset', including trailing slash.
42        if ( $par !== null && $par !== 'reset' ) {
43            $this->getOutput()->redirect( rtrim( $this->getPageTitle()->getCanonicalURL(), '/' ) );
44            return;
45        }
46        // Dirty override to check user can set global prefs.
47        if ( !$this->getUser()->isNamed() ) {
48            // @todo use our own error messages here
49            $this->setHeaders();
50            throw new UserNotLoggedIn();
51        }
52        if ( !$this->preferencesFactory->isUserGlobalized( $this->getUser() ) ) {
53            $this->setHeaders();
54            throw new ErrorPageError( 'globalprefs-error-header', 'globalprefs-notglobal' );
55        }
56
57        // Add link back to (local) Preferences.
58        if ( $par === null ) {
59            $link = $this->getLinkRenderer()->makeKnownLink(
60                static::getSafeTitleFor( 'Preferences' ),
61                $this->msg( 'mypreferences' )->text()
62            );
63            // Same left-arrow as used in Skin::subPageSubtitle().
64            $this->getOutput()->addSubtitle( "&lt; $link" );
65        }
66
67        // Add module styles and scripts separately
68        // so non-JS users get the styles quicker and to avoid a FOUC.
69        $this->getOutput()->addModuleStyles( 'ext.GlobalPreferences.global-nojs' );
70        $this->getOutput()->addModules( 'ext.GlobalPreferences.global' );
71    }
72
73    /**
74     * Get the preferences form to use.
75     * @param User $user
76     * @param IContextSource $context
77     * @return PreferencesFormOOUI|HTMLForm
78     */
79    protected function getFormObject( $user, IContextSource $context ) {
80        $form = $this->preferencesFactory->getForm( $user, $context, GlobalPreferencesFormOOUI::class );
81        return $form;
82    }
83
84    /**
85     * Display the preferences-reset confirmation page.
86     * This is identical to parent::showResetForm except with the message names changed.
87     * @throws PermissionsError
88     */
89    protected function showResetForm() {
90        if ( !$this->permissionManager->userHasRight( $this->getUser(), 'editmyoptions' ) ) {
91            throw new PermissionsError( 'editmyoptions' );
92        }
93
94        $this->getOutput()->addWikiMsg( 'globalprefs-reset-intro' );
95
96        $context = new DerivativeContext( $this->getContext() );
97        // Reset subpage
98        $context->setTitle( $this->getPageTitle( 'reset' ) );
99        $htmlForm = HTMLForm::factory( 'ooui', [], $context, 'globalprefs-restore' );
100
101        $htmlForm->setSubmitTextMsg( 'globalprefs-restoreprefs' );
102        $htmlForm->setSubmitDestructive();
103        $htmlForm->setSubmitCallback( [ $this, 'submitReset' ] );
104
105        $htmlForm->show();
106    }
107
108    /**
109     * Adds help link with an icon via page indicators.
110     * @param string $to Ignored.
111     * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
112     */
113    public function addHelpLink( $to, $overrideBaseUrl = false ) {
114        parent::addHelpLink( 'Help:Extension:GlobalPreferences', $overrideBaseUrl );
115    }
116
117    /**
118     * Handle reset submission (subpage '/reset').
119     * @param string[] $formData The submitted data (not used).
120     * @return bool
121     * @throws PermissionsError
122     */
123    public function submitReset( $formData ) {
124        if ( !$this->permissionManager->userHasRight( $this->getUser(), 'editmyoptions' ) ) {
125            throw new PermissionsError( 'editmyoptions' );
126        }
127
128        $this->preferencesFactory->resetGlobalUserSettings( $this->getUser() );
129
130        $url = $this->getPageTitle()->getFullURL( 'success' );
131
132        $this->getOutput()->redirect( $url );
133
134        return true;
135    }
136}