MediaWiki master
SpecialResetTokens.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
28
37 private $tokensList;
38
39 public function __construct() {
40 parent::__construct( 'ResetTokens' );
41 }
42
43 public function doesWrites() {
44 return true;
45 }
46
47 public function requiresUnblock() {
48 return false;
49 }
50
57 protected function getTokensList() {
58 if ( !isset( $this->tokensList ) ) {
59 $tokens = [
60 [ 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ],
61 ];
62 $this->getHookRunner()->onSpecialResetTokensTokens( $tokens );
63
64 $hiddenPrefs = $this->getConfig()->get( MainConfigNames::HiddenPrefs );
65 $tokens = array_filter( $tokens, static function ( $tok ) use ( $hiddenPrefs ) {
66 return !in_array( $tok['preference'], $hiddenPrefs );
67 } );
68
69 $this->tokensList = $tokens;
70 }
71
72 return $this->tokensList;
73 }
74
75 public function execute( $par ) {
76 // This is a preferences page, so no user JS for y'all.
77 $this->getOutput()->disallowUserJs();
78 $this->requireNamedUser();
79
80 parent::execute( $par );
81
82 $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
83 }
84
85 public function onSuccess() {
86 $this->getOutput()->wrapWikiMsg(
87 Html::successBox( '$1' ),
88 'resettokens-done'
89 );
90 }
91
97 protected function getFormFields() {
98 $user = $this->getUser();
99 $tokens = $this->getTokensList();
100
101 if ( $tokens ) {
102 $tokensForForm = [];
103 foreach ( $tokens as $tok ) {
104 $label = $this->msg( 'resettokens-token-label' )
105 ->rawParams( $this->msg( $tok['label-message'] )->parse() )
106 ->params( $user->getTokenFromOption( $tok['preference'] ) )
107 ->escaped();
108 $tokensForForm[$label] = $tok['preference'];
109 }
110
111 $desc = [
112 'label-message' => 'resettokens-tokens',
113 'type' => 'multiselect',
114 'options' => $tokensForForm,
115 ];
116 } else {
117 $desc = [
118 'label-message' => 'resettokens-no-tokens',
119 'type' => 'info',
120 ];
121 }
122
123 return [
124 'tokens' => $desc,
125 ];
126 }
127
133 protected function alterForm( HTMLForm $form ) {
134 $form->setSubmitDestructive();
135 if ( $this->getTokensList() ) {
136 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
137 } else {
138 $form->suppressDefaultSubmit();
139 }
140 }
141
142 protected function getDisplayFormat() {
143 return 'ooui';
144 }
145
146 public function onSubmit( array $formData ) {
147 if ( $formData['tokens'] ) {
148 $user = $this->getUser();
149 foreach ( $formData['tokens'] as $tokenPref ) {
150 $user->resetTokenFromOption( $tokenPref );
151 }
152 $user->saveSettings();
153
154 return true;
155 }
156
157 return false;
158 }
159
160 protected function getGroupName() {
161 return 'login';
162 }
163
164 public function isListed() {
165 return (bool)$this->getTokensList();
166 }
167}
168
173class_alias( SpecialResetTokens::class, 'SpecialResetTokens' );
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
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 sub-page 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.
getConfig()
Shortcut to get main config object.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
requireNamedUser( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in or is a temporary user, throws UserNotLoggedIn.
getOutput()
Get the OutputPage being used for this instance.
Let users reset tokens like the watchlist token.
doesWrites()
Indicates whether this special page may perform database writes.
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...