Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| SpecialResetTokens | |
0.00% |
0 / 58 |
|
0.00% |
0 / 12 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| doesWrites | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| requiresUnblock | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTokensList | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| execute | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| onSuccess | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getFormFields | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 | |||
| alterForm | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getDisplayFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onSubmit | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isListed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Specials; |
| 8 | |
| 9 | use MediaWiki\Html\Html; |
| 10 | use MediaWiki\HTMLForm\HTMLForm; |
| 11 | use MediaWiki\MainConfigNames; |
| 12 | use MediaWiki\SpecialPage\FormSpecialPage; |
| 13 | use MediaWiki\SpecialPage\SpecialPage; |
| 14 | |
| 15 | /** |
| 16 | * Let users reset tokens like the watchlist token. |
| 17 | * |
| 18 | * @ingroup SpecialPage |
| 19 | * @ingroup Auth |
| 20 | * @deprecated since 1.26 |
| 21 | */ |
| 22 | class SpecialResetTokens extends FormSpecialPage { |
| 23 | /** @var array|null */ |
| 24 | private $tokensList; |
| 25 | |
| 26 | public function __construct() { |
| 27 | parent::__construct( 'ResetTokens' ); |
| 28 | } |
| 29 | |
| 30 | /** @inheritDoc */ |
| 31 | public function doesWrites() { |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | /** @inheritDoc */ |
| 36 | public function requiresUnblock() { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns the token information list for this page after running |
| 42 | * the hook and filtering out disabled preferences. |
| 43 | * |
| 44 | * @return array |
| 45 | */ |
| 46 | protected function getTokensList() { |
| 47 | if ( !$this->tokensList ) { |
| 48 | $tokens = [ |
| 49 | [ 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ], |
| 50 | ]; |
| 51 | $this->getHookRunner()->onSpecialResetTokensTokens( $tokens ); |
| 52 | |
| 53 | $hiddenPrefs = $this->getConfig()->get( MainConfigNames::HiddenPrefs ); |
| 54 | $tokens = array_filter( $tokens, static function ( $tok ) use ( $hiddenPrefs ) { |
| 55 | return !in_array( $tok['preference'], $hiddenPrefs ); |
| 56 | } ); |
| 57 | |
| 58 | $this->tokensList = $tokens; |
| 59 | } |
| 60 | |
| 61 | return $this->tokensList; |
| 62 | } |
| 63 | |
| 64 | /** @inheritDoc */ |
| 65 | public function execute( $par ) { |
| 66 | // This is a preferences page, so no user JS for y'all. |
| 67 | $this->getOutput()->disallowUserJs(); |
| 68 | $this->requireNamedUser(); |
| 69 | |
| 70 | parent::execute( $par ); |
| 71 | |
| 72 | $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) ); |
| 73 | } |
| 74 | |
| 75 | public function onSuccess() { |
| 76 | $this->getOutput()->wrapWikiMsg( |
| 77 | Html::successBox( '$1' ), |
| 78 | 'resettokens-done' |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Display appropriate message if there's nothing to do. |
| 84 | * The submit button is also suppressed in this case (see alterForm()). |
| 85 | * @return array |
| 86 | */ |
| 87 | protected function getFormFields() { |
| 88 | $user = $this->getUser(); |
| 89 | $tokens = $this->getTokensList(); |
| 90 | |
| 91 | if ( $tokens ) { |
| 92 | $tokensForForm = []; |
| 93 | foreach ( $tokens as $tok ) { |
| 94 | $label = $this->msg( 'resettokens-token-label' ) |
| 95 | ->rawParams( $this->msg( $tok['label-message'] )->parse() ) |
| 96 | ->params( $user->getTokenFromOption( $tok['preference'] ) ) |
| 97 | ->escaped(); |
| 98 | $tokensForForm[$label] = $tok['preference']; |
| 99 | } |
| 100 | |
| 101 | $desc = [ |
| 102 | 'label-message' => 'resettokens-tokens', |
| 103 | 'type' => 'multiselect', |
| 104 | 'options' => $tokensForForm, |
| 105 | ]; |
| 106 | } else { |
| 107 | $desc = [ |
| 108 | 'label-message' => 'resettokens-no-tokens', |
| 109 | 'type' => 'info', |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | return [ |
| 114 | 'tokens' => $desc, |
| 115 | ]; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Suppress the submit button if there's nothing to do; |
| 120 | * provide additional message on it otherwise. |
| 121 | */ |
| 122 | protected function alterForm( HTMLForm $form ) { |
| 123 | $form->setSubmitDestructive(); |
| 124 | if ( $this->getTokensList() ) { |
| 125 | $form->setSubmitTextMsg( 'resettokens-resetbutton' ); |
| 126 | } else { |
| 127 | $form->suppressDefaultSubmit(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** @inheritDoc */ |
| 132 | protected function getDisplayFormat() { |
| 133 | return 'ooui'; |
| 134 | } |
| 135 | |
| 136 | /** @inheritDoc */ |
| 137 | public function onSubmit( array $formData ) { |
| 138 | if ( $formData['tokens'] ) { |
| 139 | $user = $this->getUser(); |
| 140 | foreach ( $formData['tokens'] as $tokenPref ) { |
| 141 | $user->resetTokenFromOption( $tokenPref ); |
| 142 | } |
| 143 | $user->saveSettings(); |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | /** @inheritDoc */ |
| 152 | protected function getGroupName() { |
| 153 | return 'login'; |
| 154 | } |
| 155 | |
| 156 | /** @inheritDoc */ |
| 157 | public function isListed() { |
| 158 | return (bool)$this->getTokensList(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Retain the old class name for backwards compatibility. |
| 164 | * @deprecated since 1.41 |
| 165 | */ |
| 166 | class_alias( SpecialResetTokens::class, 'SpecialResetTokens' ); |