MediaWiki master
SpecialResetTokens.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
31
39 private $tokensList;
40
41 public function __construct() {
42 parent::__construct( 'ResetTokens' );
43 }
44
45 public function doesWrites() {
46 return true;
47 }
48
49 public function requiresUnblock() {
50 return false;
51 }
52
59 protected function getTokensList() {
60 if ( !isset( $this->tokensList ) ) {
61 $tokens = [
62 [ 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ],
63 ];
64 $this->getHookRunner()->onSpecialResetTokensTokens( $tokens );
65
66 $hiddenPrefs = $this->getConfig()->get( MainConfigNames::HiddenPrefs );
67 $tokens = array_filter( $tokens, static function ( $tok ) use ( $hiddenPrefs ) {
68 return !in_array( $tok['preference'], $hiddenPrefs );
69 } );
70
71 $this->tokensList = $tokens;
72 }
73
74 return $this->tokensList;
75 }
76
77 public function execute( $par ) {
78 // This is a preferences page, so no user JS for y'all.
79 $this->getOutput()->disallowUserJs();
80 $this->requireNamedUser();
81
82 parent::execute( $par );
83
84 $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
85 }
86
87 public function onSuccess() {
88 $this->getOutput()->wrapWikiMsg(
89 Html::successBox( '$1' ),
90 'resettokens-done'
91 );
92 }
93
99 protected function getFormFields() {
100 $user = $this->getUser();
101 $tokens = $this->getTokensList();
102
103 if ( $tokens ) {
104 $tokensForForm = [];
105 foreach ( $tokens as $tok ) {
106 $label = $this->msg( 'resettokens-token-label' )
107 ->rawParams( $this->msg( $tok['label-message'] )->parse() )
108 ->params( $user->getTokenFromOption( $tok['preference'] ) )
109 ->escaped();
110 $tokensForForm[$label] = $tok['preference'];
111 }
112
113 $desc = [
114 'label-message' => 'resettokens-tokens',
115 'type' => 'multiselect',
116 'options' => $tokensForForm,
117 ];
118 } else {
119 $desc = [
120 'label-message' => 'resettokens-no-tokens',
121 'type' => 'info',
122 ];
123 }
124
125 return [
126 'tokens' => $desc,
127 ];
128 }
129
135 protected function alterForm( HTMLForm $form ) {
136 $form->setSubmitDestructive();
137 if ( $this->getTokensList() ) {
138 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
139 } else {
140 $form->suppressDefaultSubmit();
141 }
142 }
143
144 protected function getDisplayFormat() {
145 return 'ooui';
146 }
147
148 public function onSubmit( array $formData ) {
149 if ( $formData['tokens'] ) {
150 $user = $this->getUser();
151 foreach ( $formData['tokens'] as $tokenPref ) {
152 $user->resetTokenFromOption( $tokenPref );
153 }
154 $user->saveSettings();
155
156 return true;
157 }
158
159 return false;
160 }
161
162 protected function getGroupName() {
163 return 'login';
164 }
165
166 public function isListed() {
167 return (bool)$this->getTokensList();
168 }
169}
170
175class_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...
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...