MediaWiki  master
SpecialResetTokens.php
Go to the documentation of this file.
1 <?php
26 
34  private $tokensList;
35 
36  public function __construct() {
37  parent::__construct( 'ResetTokens' );
38  }
39 
40  public function doesWrites() {
41  return true;
42  }
43 
44  public function requiresUnblock() {
45  return false;
46  }
47 
54  protected function getTokensList() {
55  if ( !isset( $this->tokensList ) ) {
56  $tokens = [
57  [ 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ],
58  ];
59  $this->getHookRunner()->onSpecialResetTokensTokens( $tokens );
60 
61  $hiddenPrefs = $this->getConfig()->get( MainConfigNames::HiddenPrefs );
62  $tokens = array_filter( $tokens, static function ( $tok ) use ( $hiddenPrefs ) {
63  return !in_array( $tok['preference'], $hiddenPrefs );
64  } );
65 
66  $this->tokensList = $tokens;
67  }
68 
69  return $this->tokensList;
70  }
71 
72  public function execute( $par ) {
73  // This is a preferences page, so no user JS for y'all.
74  $this->getOutput()->disallowUserJs();
75  $this->requireNamedUser();
76 
77  parent::execute( $par );
78 
79  $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
80  }
81 
82  public function onSuccess() {
83  $this->getOutput()->wrapWikiMsg(
84  Html::successBox( '$1' ),
85  'resettokens-done'
86  );
87  }
88 
94  protected function getFormFields() {
95  $user = $this->getUser();
96  $tokens = $this->getTokensList();
97 
98  if ( $tokens ) {
99  $tokensForForm = [];
100  foreach ( $tokens as $tok ) {
101  $label = $this->msg( 'resettokens-token-label' )
102  ->rawParams( $this->msg( $tok['label-message'] )->parse() )
103  ->params( $user->getTokenFromOption( $tok['preference'] ) )
104  ->escaped();
105  $tokensForForm[$label] = $tok['preference'];
106  }
107 
108  $desc = [
109  'label-message' => 'resettokens-tokens',
110  'type' => 'multiselect',
111  'options' => $tokensForForm,
112  ];
113  } else {
114  $desc = [
115  'label-message' => 'resettokens-no-tokens',
116  'type' => 'info',
117  ];
118  }
119 
120  return [
121  'tokens' => $desc,
122  ];
123  }
124 
130  protected function alterForm( HTMLForm $form ) {
131  $form->setSubmitDestructive();
132  if ( $this->getTokensList() ) {
133  $form->setSubmitTextMsg( 'resettokens-resetbutton' );
134  } else {
135  $form->suppressDefaultSubmit();
136  }
137  }
138 
139  protected function getDisplayFormat() {
140  return 'ooui';
141  }
142 
143  public function onSubmit( array $formData ) {
144  if ( $formData['tokens'] ) {
145  $user = $this->getUser();
146  foreach ( $formData['tokens'] as $tokenPref ) {
147  $user->resetTokenFromOption( $tokenPref );
148  }
149  $user->saveSettings();
150 
151  return true;
152  }
153 
154  return false;
155  }
156 
157  protected function getGroupName() {
158  return 'login';
159  }
160 
161  public function isListed() {
162  return (bool)$this->getTokensList();
163  }
164 }
Special page which uses an HTMLForm to handle processing.
string null $par
The sub-page of the special page.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:155
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1637
setSubmitDestructive()
Identify that the submit button in the form has a destructive action.
Definition: HTMLForm.php:1623
suppressDefaultSubmit( $suppressSubmit=true)
Stop a default submit button being shown for this form.
Definition: HTMLForm.php:1721
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
A class containing constants representing the names of configuration variables.
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
requireNamedUser( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in or is a temporary user, throws UserNotLoggedIn.
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,...
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
Let users reset tokens like the watchlist token.
requiresUnblock()
Whether this action cannot be executed by a blocked user, default to requiresPost()
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
doesWrites()
Indicates whether this special page may perform database writes.
getDisplayFormat()
Get display format for the form.
onSubmit(array $formData)
Process the form on submission.
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...
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getFormFields()
Display appropriate message if there's nothing to do.
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.