Merge "docs: Fix typo"
[mediawiki.git] / includes / specials / SpecialResetTokens.php
blob0a772bb2f9431bdabd8d865a05664313cfb2f27f
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Specials;
23 use MediaWiki\Html\Html;
24 use MediaWiki\HTMLForm\HTMLForm;
25 use MediaWiki\MainConfigNames;
26 use MediaWiki\SpecialPage\FormSpecialPage;
27 use MediaWiki\SpecialPage\SpecialPage;
29 /**
30 * Let users reset tokens like the watchlist token.
32 * @ingroup SpecialPage
33 * @ingroup Auth
34 * @deprecated since 1.26
36 class SpecialResetTokens extends FormSpecialPage {
37 /** @var array|null */
38 private $tokensList;
40 public function __construct() {
41 parent::__construct( 'ResetTokens' );
44 public function doesWrites() {
45 return true;
48 public function requiresUnblock() {
49 return false;
52 /**
53 * Returns the token information list for this page after running
54 * the hook and filtering out disabled preferences.
56 * @return array
58 protected function getTokensList() {
59 if ( !$this->tokensList ) {
60 $tokens = [
61 [ 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ],
63 $this->getHookRunner()->onSpecialResetTokensTokens( $tokens );
65 $hiddenPrefs = $this->getConfig()->get( MainConfigNames::HiddenPrefs );
66 $tokens = array_filter( $tokens, static function ( $tok ) use ( $hiddenPrefs ) {
67 return !in_array( $tok['preference'], $hiddenPrefs );
68 } );
70 $this->tokensList = $tokens;
73 return $this->tokensList;
76 public function execute( $par ) {
77 // This is a preferences page, so no user JS for y'all.
78 $this->getOutput()->disallowUserJs();
79 $this->requireNamedUser();
81 parent::execute( $par );
83 $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
86 public function onSuccess() {
87 $this->getOutput()->wrapWikiMsg(
88 Html::successBox( '$1' ),
89 'resettokens-done'
93 /**
94 * Display appropriate message if there's nothing to do.
95 * The submit button is also suppressed in this case (see alterForm()).
96 * @return array
98 protected function getFormFields() {
99 $user = $this->getUser();
100 $tokens = $this->getTokensList();
102 if ( $tokens ) {
103 $tokensForForm = [];
104 foreach ( $tokens as $tok ) {
105 $label = $this->msg( 'resettokens-token-label' )
106 ->rawParams( $this->msg( $tok['label-message'] )->parse() )
107 ->params( $user->getTokenFromOption( $tok['preference'] ) )
108 ->escaped();
109 $tokensForForm[$label] = $tok['preference'];
112 $desc = [
113 'label-message' => 'resettokens-tokens',
114 'type' => 'multiselect',
115 'options' => $tokensForForm,
117 } else {
118 $desc = [
119 'label-message' => 'resettokens-no-tokens',
120 'type' => 'info',
124 return [
125 'tokens' => $desc,
130 * Suppress the submit button if there's nothing to do;
131 * provide additional message on it otherwise.
133 protected function alterForm( HTMLForm $form ) {
134 $form->setSubmitDestructive();
135 if ( $this->getTokensList() ) {
136 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
137 } else {
138 $form->suppressDefaultSubmit();
142 protected function getDisplayFormat() {
143 return 'ooui';
146 public function onSubmit( array $formData ) {
147 if ( $formData['tokens'] ) {
148 $user = $this->getUser();
149 foreach ( $formData['tokens'] as $tokenPref ) {
150 $user->resetTokenFromOption( $tokenPref );
152 $user->saveSettings();
154 return true;
157 return false;
160 protected function getGroupName() {
161 return 'login';
164 public function isListed() {
165 return (bool)$this->getTokensList();
170 * Retain the old class name for backwards compatibility.
171 * @deprecated since 1.41
173 class_alias( SpecialResetTokens::class, 'SpecialResetTokens' );