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