MediaWiki  master
SpecialResetTokens.php
Go to the documentation of this file.
1 <?php
25 
33  private $tokensList;
34 
35  public function __construct() {
36  parent::__construct( 'ResetTokens' );
37  }
38 
39  public function doesWrites() {
40  return true;
41  }
42 
43  public function requiresUnblock() {
44  return false;
45  }
46 
53  protected function getTokensList() {
54  if ( !isset( $this->tokensList ) ) {
55  $tokens = [
56  [ 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ],
57  ];
58  $this->getHookRunner()->onSpecialResetTokensTokens( $tokens );
59 
60  $hiddenPrefs = $this->getConfig()->get( MainConfigNames::HiddenPrefs );
61  $tokens = array_filter( $tokens, static function ( $tok ) use ( $hiddenPrefs ) {
62  return !in_array( $tok['preference'], $hiddenPrefs );
63  } );
64 
65  $this->tokensList = $tokens;
66  }
67 
68  return $this->tokensList;
69  }
70 
71  public function execute( $par ) {
72  // This is a preferences page, so no user JS for y'all.
73  $this->getOutput()->disallowUserJs();
74  $this->requireNamedUser();
75 
76  parent::execute( $par );
77 
78  $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
79  }
80 
81  public function onSuccess() {
82  $this->getOutput()->wrapWikiMsg(
83  Html::successBox( '$1' ),
84  'resettokens-done'
85  );
86  }
87 
93  protected function getFormFields() {
94  $user = $this->getUser();
95  $tokens = $this->getTokensList();
96 
97  if ( $tokens ) {
98  $tokensForForm = [];
99  foreach ( $tokens as $tok ) {
100  $label = $this->msg( 'resettokens-token-label' )
101  ->rawParams( $this->msg( $tok['label-message'] )->parse() )
102  ->params( $user->getTokenFromOption( $tok['preference'] ) )
103  ->escaped();
104  $tokensForForm[$label] = $tok['preference'];
105  }
106 
107  $desc = [
108  'label-message' => 'resettokens-tokens',
109  'type' => 'multiselect',
110  'options' => $tokensForForm,
111  ];
112  } else {
113  $desc = [
114  'label-message' => 'resettokens-no-tokens',
115  'type' => 'info',
116  ];
117  }
118 
119  return [
120  'tokens' => $desc,
121  ];
122  }
123 
129  protected function alterForm( HTMLForm $form ) {
130  $form->setSubmitDestructive();
131  if ( $this->getTokensList() ) {
132  $form->setSubmitTextMsg( 'resettokens-resetbutton' );
133  } else {
134  $form->suppressDefaultSubmit();
135  }
136  }
137 
138  protected function getDisplayFormat() {
139  return 'ooui';
140  }
141 
142  public function onSubmit( array $formData ) {
143  if ( $formData['tokens'] ) {
144  $user = $this->getUser();
145  foreach ( $formData['tokens'] as $tokenPref ) {
146  $user->resetTokenFromOption( $tokenPref );
147  }
148  $user->saveSettings();
149 
150  return true;
151  }
152 
153  return false;
154  }
155 
156  protected function getGroupName() {
157  return 'users';
158  }
159 
160  public function isListed() {
161  return (bool)$this->getTokensList();
162  }
163 }
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:150
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1613
setSubmitDestructive()
Identify that the submit button in the form has a destructive action.
Definition: HTMLForm.php:1599
suppressDefaultSubmit( $suppressSubmit=true)
Stop a default submit button being shown for this form.
Definition: HTMLForm.php:1697
static successBox( $html, $className='')
Return a success box.
Definition: Html.php:800
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.
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 POST 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.