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
30 class SpecialResetTokens
extends FormSpecialPage
{
33 public function __construct() {
34 parent
::__construct( 'ResetTokens' );
38 * Returns the token information list for this page after running
39 * the hook and filtering out disabled preferences.
43 protected function getTokensList() {
44 if ( !isset( $this->tokensList
) ) {
46 array( 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ),
48 Hooks
::run( 'SpecialResetTokensTokens', array( &$tokens ) );
50 $hiddenPrefs = $this->getConfig()->get( 'HiddenPrefs' );
51 $tokens = array_filter( $tokens, function ( $tok ) use ( $hiddenPrefs ) {
52 return !in_array( $tok['preference'], $hiddenPrefs );
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>",
79 * Display appropriate message if there's nothing to do.
80 * The submit button is also suppressed in this case (see alterForm()).
83 protected function getFormFields() {
84 $user = $this->getUser();
85 $tokens = $this->getTokensList();
88 $tokensForForm = array();
89 foreach ( $tokens as $tok ) {
90 $label = $this->msg( 'resettokens-token-label' )
91 ->rawParams( $this->msg( $tok['label-message'] )->parse() )
92 ->params( $user->getTokenFromOption( $tok['preference'] ) )
94 $tokensForForm[$label] = $tok['preference'];
98 'label-message' => 'resettokens-tokens',
99 'type' => 'multiselect',
100 'options' => $tokensForForm,
104 'label-message' => 'resettokens-no-tokens',
115 * Suppress the submit button if there's nothing to do;
116 * provide additional message on it otherwise.
117 * @param HTMLForm $form
119 protected function alterForm( HTMLForm
$form ) {
120 if ( $this->getTokensList() ) {
121 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
123 $form->suppressDefaultSubmit();
127 protected function getDisplayFormat() {
131 public function onSubmit( array $formData ) {
132 if ( $formData['tokens'] ) {
133 $user = $this->getUser();
134 foreach ( $formData['tokens'] as $tokenPref ) {
135 $user->resetTokenFromOption( $tokenPref );
137 $user->saveSettings();
145 protected function getGroupName() {
149 public function isListed() {
150 return (bool)$this->getTokensList();