MediaWiki master
SpecialResetTokens.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
28
38 private $tokensList;
39
40 public function __construct() {
41 parent::__construct( 'ResetTokens' );
42 }
43
44 public function doesWrites() {
45 return true;
46 }
47
48 public function requiresUnblock() {
49 return false;
50 }
51
58 protected function getTokensList() {
59 if ( !isset( $this->tokensList ) ) {
60 $tokens = [
61 [ 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ],
62 ];
63 $this->getHookRunner()->onSpecialResetTokensTokens( $tokens );
64
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 } );
69
70 $this->tokensList = $tokens;
71 }
72
73 return $this->tokensList;
74 }
75
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();
80
81 parent::execute( $par );
82
83 $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
84 }
85
86 public function onSuccess() {
87 $this->getOutput()->wrapWikiMsg(
88 Html::successBox( '$1' ),
89 'resettokens-done'
90 );
91 }
92
98 protected function getFormFields() {
99 $user = $this->getUser();
100 $tokens = $this->getTokensList();
101
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'];
110 }
111
112 $desc = [
113 'label-message' => 'resettokens-tokens',
114 'type' => 'multiselect',
115 'options' => $tokensForForm,
116 ];
117 } else {
118 $desc = [
119 'label-message' => 'resettokens-no-tokens',
120 'type' => 'info',
121 ];
122 }
123
124 return [
125 'tokens' => $desc,
126 ];
127 }
128
134 protected function alterForm( HTMLForm $form ) {
135 $form->setSubmitDestructive();
136 if ( $this->getTokensList() ) {
137 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
138 } else {
139 $form->suppressDefaultSubmit();
140 }
141 }
142
143 protected function getDisplayFormat() {
144 return 'ooui';
145 }
146
147 public function onSubmit( array $formData ) {
148 if ( $formData['tokens'] ) {
149 $user = $this->getUser();
150 foreach ( $formData['tokens'] as $tokenPref ) {
151 $user->resetTokenFromOption( $tokenPref );
152 }
153 $user->saveSettings();
154
155 return true;
156 }
157
158 return false;
159 }
160
161 protected function getGroupName() {
162 return 'login';
163 }
164
165 public function isListed() {
166 return (bool)$this->getTokensList();
167 }
168}
169
174class_alias( SpecialResetTokens::class, 'SpecialResetTokens' );
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
suppressDefaultSubmit( $suppressSubmit=true)
Stop a default submit button being shown for this form.
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setSubmitDestructive()
Identify that the submit button in the form has a destructive action.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
A class containing constants representing the names of configuration variables.
const HiddenPrefs
Name constant for the HiddenPrefs setting, for use with Config::get()
Special page which uses an HTMLForm to handle processing.
string null $par
The subpage of the special page.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getUser()
Shortcut to get the User executing this instance.
requireNamedUser( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin', bool $alwaysRedirectToLoginPage=false)
If the user is not logged in or is a temporary user, throws UserNotLoggedIn.
getConfig()
Shortcut to get main config object.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
Let users reset tokens like the watchlist token.
doesWrites()
Indicates whether POST requests to this special page require write access to the wiki.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
requiresUnblock()
Whether this action cannot be executed by a blocked user, default to requiresPost()
alterForm(HTMLForm $form)
Suppress the submit button if there's nothing to do; provide additional message on it otherwise.
getTokensList()
Returns the token information list for this page after running the hook and filtering out disabled pr...
getFormFields()
Display appropriate message if there's nothing to do.
getDisplayFormat()
Get display format for the form.
execute( $par)
Basic SpecialPage workflow: get a form, send it to the user; get some data back,.
isListed()
Whether this special page is listed in Special:SpecialPages.
onSubmit(array $formData)
Process the form on submission.
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...