Merge "Fix MultiHttpClient to return correct errors"
[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 if ( !isset( $this->tokensList ) ) {
44 $tokens = array(
45 array( 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ),
46 );
47 Hooks::run( 'SpecialResetTokensTokens', array( &$tokens ) );
48
49 $hiddenPrefs = $this->getConfig()->get( 'HiddenPrefs' );
50 $tokens = array_filter( $tokens, function ( $tok ) use ( $hiddenPrefs ) {
51 return !in_array( $tok['preference'], $hiddenPrefs );
52 } );
53
54 $this->tokensList = $tokens;
55 }
56
57 return $this->tokensList;
58 }
59
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();
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 * @return array
81 */
82 protected function getFormFields() {
83 $user = $this->getUser();
84 $tokens = $this->getTokensList();
85
86 if ( $tokens ) {
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'] ) )
92 ->escaped();
93 $tokensForForm[$label] = $tok['preference'];
94 }
95
96 $desc = array(
97 'label-message' => 'resettokens-tokens',
98 'type' => 'multiselect',
99 'options' => $tokensForForm,
100 );
101 } else {
102 $desc = array(
103 'label-message' => 'resettokens-no-tokens',
104 'type' => 'info',
105 );
106 }
107
108 return array(
109 'tokens' => $desc,
110 );
111 }
112
113 /**
114 * Suppress the submit button if there's nothing to do;
115 * provide additional message on it otherwise.
116 * @param HTMLForm $form
117 */
118 protected function alterForm( HTMLForm $form ) {
119 if ( $this->getTokensList() ) {
120 $form->setSubmitTextMsg( 'resettokens-resetbutton' );
121 } else {
122 $form->suppressDefaultSubmit();
123 }
124 }
125
126 protected function getDisplayFormat() {
127 return 'ooui';
128 }
129
130 public function onSubmit( array $formData ) {
131 if ( $formData['tokens'] ) {
132 $user = $this->getUser();
133 foreach ( $formData['tokens'] as $tokenPref ) {
134 $user->resetTokenFromOption( $tokenPref );
135 }
136 $user->saveSettings();
137
138 return true;
139 }
140
141 return false;
142 }
143
144 protected function getGroupName() {
145 return 'users';
146 }
147
148 public function isListed() {
149 return (bool)$this->getTokensList();
150 }
151 }