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