| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class SpecialPreferences extends SpecialPage { |
| 42 |
|
| 43 |
private PreferencesFactory $preferencesFactory; |
| 44 |
private UserOptionsManager $userOptionsManager; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct( |
| 51 |
?PreferencesFactory $preferencesFactory = null, |
| 52 |
?UserOptionsManager $userOptionsManager = null |
| 53 |
) { |
| 54 |
parent::__construct( 'Preferences' ); |
| 55 |
|
| 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(); |
| 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 |
|
| 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 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 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 |
|
| 165 |
HTMLForm::factory( 'ooui', $desc, $this->getContext(), 'prefs-restore' ) |
| 166 |
->setTitle( $this->getPageTitle( 'reset' ) ) |
| 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 |
|
| 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 |
|
| 200 |
|
| 201 |
|
| 202 |
class_alias( SpecialPreferences::class, 'SpecialPreferences' ); |