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