Merge "Typo fix"
[lhc/web/wiklou.git] / includes / specials / SpecialResetTokens.php
1 <?php
2 /**
3 * Implements Special:ResetTokens
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Let users reset tokens like the watchlist token.
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialResetTokens extends FormSpecialPage {
30 private $tokensList;
31
32 public function __construct() {
33 parent::__construct( 'ResetTokens' );
34 }
35
36 /**
37 * Returns the token information list for this page after running
38 * the hook and filtering out disabled preferences.
39 *
40 * @return array
41 */
42 protected function getTokensList() {
43 global $wgHiddenPrefs;
44
45 if ( !isset( $this->tokensList ) ) {
46 $tokens = array(
47 array( 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ),
48 );
49 wfRunHooks( 'SpecialResetTokensTokens', array( &$tokens ) );
50
51 $tokens = array_filter( $tokens, function ( $tok ) use ( $wgHiddenPrefs ) {
52 return !in_array( $tok['preference'], $wgHiddenPrefs );
53 } );
54
55 $this->tokensList = $tokens;
56 }
57
58 return $this->tokensList;
59 }
60
61 public function execute( $par ) {
62 // This is a preferences page, so no user JS for y'all.
63 $this->getOutput()->disallowUserJs();
64
65 parent::execute( $par );
66
67 $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
68 }
69
70 public function onSuccess() {
71 $this->getOutput()->wrapWikiMsg(
72 "<div class='successbox'>\n$1\n</div>",
73 'resettokens-done'
74 );
75 }
76
77 /**
78 * Display appropriate message if there's nothing to do.
79 * The submit button is also suppressed in this case (see alterForm()).
80 */
81 protected function getFormFields() {
82 $user = $this->getUser();
83 $tokens = $this->getTokensList();
84
85 if ( $tokens ) {
86 $tokensForForm = array();
87 foreach ( $tokens as $tok ) {
88 $label = $this->msg( 'resettokens-token-label' )
89 ->rawParams( $this->msg( $tok['label-message'] )->escaped() )
90 ->params( $user->getTokenFromOption( $tok['preference'] ) )
91 ->escaped();
92 $tokensForForm[ $label ] = $tok['preference'];
93 }
94
95 $desc = array(
96 'label-message' => 'resettokens-tokens',
97 'type' => 'multiselect',
98 'options' => $tokensForForm,
99 );
100 } else {
101 $desc = array(
102 'label-message' => 'resettokens-no-tokens',
103 'type' => 'info',
104 );
105 }
106
107 return array(
108 'tokens' => $desc,
109 );
110 }
111
112 /**
113 * Suppress the submit button if there's nothing to do;
114 * provide additional message on it otherwise.
115 */
116 protected function alterForm( HTMLForm $form ) {
117 if ( $this->getTokensList() ) {
118 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
119 } else {
120 $form->suppressDefaultSubmit();
121 }
122 }
123
124 public function onSubmit( array $formData ) {
125 if ( $formData['tokens'] ) {
126 $user = $this->getUser();
127 foreach ( $formData['tokens'] as $tokenPref ) {
128 $user->resetTokenFromOption( $tokenPref );
129 }
130 $user->saveSettings();
131
132 return true;
133 }
134
135 return false;
136 }
137
138 protected function getGroupName() {
139 return 'users';
140 }
141
142 public function isListed() {
143 return (bool)$this->getTokensList();
144 }
145 }