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
21 * @ingroup SpecialPage
25 * Let users reset tokens like the watchlist token.
27 * @ingroup SpecialPage
29 class SpecialResetTokens
extends FormSpecialPage
{
32 public function __construct() {
33 parent
::__construct( 'ResetTokens' );
37 * Returns the token information list for this page after running
38 * the hook and filtering out disabled preferences.
42 protected function getTokensList() {
43 if ( !isset( $this->tokensList
) ) {
45 array( 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ),
47 Hooks
::run( 'SpecialResetTokensTokens', array( &$tokens ) );
49 $hiddenPrefs = $this->getConfig()->get( 'HiddenPrefs' );
50 $tokens = array_filter( $tokens, function ( $tok ) use ( $hiddenPrefs ) {
51 return !in_array( $tok['preference'], $hiddenPrefs );
54 $this->tokensList
= $tokens;
57 return $this->tokensList
;
60 public function execute( $par ) {
61 // This is a preferences page, so no user JS for y'all.
62 $this->getOutput()->disallowUserJs();
63 $this->requireLogin();
65 parent
::execute( $par );
67 $this->getOutput()->addReturnTo( SpecialPage
::getTitleFor( 'Preferences' ) );
70 public function onSuccess() {
71 $this->getOutput()->wrapWikiMsg(
72 "<div class='successbox'>\n$1\n</div>",
78 * Display appropriate message if there's nothing to do.
79 * The submit button is also suppressed in this case (see alterForm()).
82 protected function getFormFields() {
83 $user = $this->getUser();
84 $tokens = $this->getTokensList();
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'] ) )
93 $tokensForForm[$label] = $tok['preference'];
97 'label-message' => 'resettokens-tokens',
98 'type' => 'multiselect',
99 'options' => $tokensForForm,
103 'label-message' => 'resettokens-no-tokens',
114 * Suppress the submit button if there's nothing to do;
115 * provide additional message on it otherwise.
116 * @param HTMLForm $form
118 protected function alterForm( HTMLForm
$form ) {
119 if ( $this->getTokensList() ) {
120 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
122 $form->suppressDefaultSubmit();
126 public function onSubmit( array $formData ) {
127 if ( $formData['tokens'] ) {
128 $user = $this->getUser();
129 foreach ( $formData['tokens'] as $tokenPref ) {
130 $user->resetTokenFromOption( $tokenPref );
132 $user->saveSettings();
140 protected function getGroupName() {
144 public function isListed() {
145 return (bool)$this->getTokensList();