Merge "Drop zh-tw message "saveprefs""
[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 * @deprecated 1.26
29 */
30 class SpecialResetTokens extends FormSpecialPage {
31 private $tokensList;
32
33 public function __construct() {
34 parent::__construct( 'ResetTokens' );
35 }
36
37 /**
38 * Returns the token information list for this page after running
39 * the hook and filtering out disabled preferences.
40 *
41 * @return array
42 */
43 protected function getTokensList() {
44 if ( !isset( $this->tokensList ) ) {
45 $tokens = array(
46 array( 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ),
47 );
48 Hooks::run( 'SpecialResetTokensTokens', array( &$tokens ) );
49
50 $hiddenPrefs = $this->getConfig()->get( 'HiddenPrefs' );
51 $tokens = array_filter( $tokens, function ( $tok ) use ( $hiddenPrefs ) {
52 return !in_array( $tok['preference'], $hiddenPrefs );
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 $this->requireLogin();
65
66 parent::execute( $par );
67
68 $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
69 }
70
71 public function onSuccess() {
72 $this->getOutput()->wrapWikiMsg(
73 "<div class='successbox'>\n$1\n</div>",
74 'resettokens-done'
75 );
76 }
77
78 /**
79 * Display appropriate message if there's nothing to do.
80 * The submit button is also suppressed in this case (see alterForm()).
81 * @return array
82 */
83 protected function getFormFields() {
84 $user = $this->getUser();
85 $tokens = $this->getTokensList();
86
87 if ( $tokens ) {
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'] ) )
93 ->escaped();
94 $tokensForForm[$label] = $tok['preference'];
95 }
96
97 $desc = array(
98 'label-message' => 'resettokens-tokens',
99 'type' => 'multiselect',
100 'options' => $tokensForForm,
101 );
102 } else {
103 $desc = array(
104 'label-message' => 'resettokens-no-tokens',
105 'type' => 'info',
106 );
107 }
108
109 return array(
110 'tokens' => $desc,
111 );
112 }
113
114 /**
115 * Suppress the submit button if there's nothing to do;
116 * provide additional message on it otherwise.
117 * @param HTMLForm $form
118 */
119 protected function alterForm( HTMLForm $form ) {
120 if ( $this->getTokensList() ) {
121 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
122 } else {
123 $form->suppressDefaultSubmit();
124 }
125 }
126
127 protected function getDisplayFormat() {
128 return 'ooui';
129 }
130
131 public function onSubmit( array $formData ) {
132 if ( $formData['tokens'] ) {
133 $user = $this->getUser();
134 foreach ( $formData['tokens'] as $tokenPref ) {
135 $user->resetTokenFromOption( $tokenPref );
136 }
137 $user->saveSettings();
138
139 return true;
140 }
141
142 return false;
143 }
144
145 protected function getGroupName() {
146 return 'users';
147 }
148
149 public function isListed() {
150 return (bool)$this->getTokensList();
151 }
152 }