Merge "Move QueryPage and abstract subclasses to specialpage folder"
[mediawiki.git] / includes / specials / SpecialResetTokens.php
blob864485ce5c037f9a5cb51c1fb9facf4a4d7e79cf
1 <?php
2 /**
3 * Implements Special:ResetTokens
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Let users reset tokens like the watchlist token.
27 * @ingroup SpecialPage
29 class SpecialResetTokens extends FormSpecialPage {
30 private $tokensList;
32 public function __construct() {
33 parent::__construct( 'ResetTokens' );
36 /**
37 * Returns the token information list for this page after running
38 * the hook and filtering out disabled preferences.
40 * @return array
42 protected function getTokensList() {
43 global $wgHiddenPrefs;
45 if ( !isset( $this->tokensList ) ) {
46 $tokens = array(
47 array( 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ),
49 wfRunHooks( 'SpecialResetTokensTokens', array( &$tokens ) );
51 $tokens = array_filter( $tokens, function ( $tok ) use ( $wgHiddenPrefs ) {
52 return !in_array( $tok['preference'], $wgHiddenPrefs );
53 } );
55 $this->tokensList = $tokens;
58 return $this->tokensList;
61 public function execute( $par ) {
62 // This is a preferences page, so no user JS for y'all.
63 $this->getOutput()->disallowUserJs();
64 $this->requireLogin();
66 parent::execute( $par );
68 $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
71 public function onSuccess() {
72 $this->getOutput()->wrapWikiMsg(
73 "<div class='successbox'>\n$1\n</div>",
74 'resettokens-done'
78 /**
79 * Display appropriate message if there's nothing to do.
80 * The submit button is also suppressed in this case (see alterForm()).
82 protected function getFormFields() {
83 $user = $this->getUser();
84 $tokens = $this->getTokensList();
86 if ( $tokens ) {
87 $tokensForForm = array();
88 foreach ( $tokens as $tok ) {
89 $label = $this->msg( 'resettokens-token-label' )
90 ->rawParams( $this->msg( $tok['label-message'] )->parse() )
91 ->params( $user->getTokenFromOption( $tok['preference'] ) )
92 ->escaped();
93 $tokensForForm[$label] = $tok['preference'];
96 $desc = array(
97 'label-message' => 'resettokens-tokens',
98 'type' => 'multiselect',
99 'options' => $tokensForForm,
101 } else {
102 $desc = array(
103 'label-message' => 'resettokens-no-tokens',
104 'type' => 'info',
108 return array(
109 'tokens' => $desc,
114 * Suppress the submit button if there's nothing to do;
115 * provide additional message on it otherwise.
117 protected function alterForm( HTMLForm $form ) {
118 if ( $this->getTokensList() ) {
119 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
120 } else {
121 $form->suppressDefaultSubmit();
125 public function onSubmit( array $formData ) {
126 if ( $formData['tokens'] ) {
127 $user = $this->getUser();
128 foreach ( $formData['tokens'] as $tokenPref ) {
129 $user->resetTokenFromOption( $tokenPref );
131 $user->saveSettings();
133 return true;
136 return false;
139 protected function getGroupName() {
140 return 'users';
143 public function isListed() {
144 return (bool)$this->getTokensList();