MediaWiki  REL1_22
SpecialResetTokens.php
Go to the documentation of this file.
00001 <?php
00029 class SpecialResetTokens extends FormSpecialPage {
00030     private $tokensList;
00031 
00032     public function __construct() {
00033         parent::__construct( 'ResetTokens' );
00034     }
00035 
00042     protected function getTokensList() {
00043         global $wgHiddenPrefs;
00044 
00045         if ( !isset( $this->tokensList ) ) {
00046             $tokens = array(
00047                 array( 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ),
00048             );
00049             wfRunHooks( 'SpecialResetTokensTokens', array( &$tokens ) );
00050 
00051             $tokens = array_filter( $tokens, function ( $tok ) use ( $wgHiddenPrefs ) {
00052                 return !in_array( $tok['preference'], $wgHiddenPrefs );
00053             } );
00054 
00055             $this->tokensList = $tokens;
00056         }
00057 
00058         return $this->tokensList;
00059     }
00060 
00061     public function execute( $par ) {
00062         // This is a preferences page, so no user JS for y'all.
00063         $this->getOutput()->disallowUserJs();
00064 
00065         parent::execute( $par );
00066 
00067         $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
00068     }
00069 
00070     public function onSuccess() {
00071         $this->getOutput()->wrapWikiMsg(
00072             "<div class='successbox'>\n$1\n</div>",
00073             'resettokens-done'
00074         );
00075     }
00076 
00081     protected function getFormFields() {
00082         $user = $this->getUser();
00083         $tokens = $this->getTokensList();
00084 
00085         if ( $tokens ) {
00086             $tokensForForm = array();
00087             foreach ( $tokens as $tok ) {
00088                 $label = $this->msg( 'resettokens-token-label' )
00089                     ->rawParams( $this->msg( $tok['label-message'] )->parse() )
00090                     ->params( $user->getTokenFromOption( $tok['preference'] ) )
00091                     ->escaped();
00092                 $tokensForForm[ $label ] = $tok['preference'];
00093             }
00094 
00095             $desc = array(
00096                 'label-message' => 'resettokens-tokens',
00097                 'type' => 'multiselect',
00098                 'options' => $tokensForForm,
00099             );
00100         } else {
00101             $desc = array(
00102                 'label-message' => 'resettokens-no-tokens',
00103                 'type' => 'info',
00104             );
00105         }
00106 
00107         return array(
00108             'tokens' => $desc,
00109         );
00110     }
00111 
00116     protected function alterForm( HTMLForm $form ) {
00117         if ( $this->getTokensList() ) {
00118             $form->setSubmitTextMsg( 'resettokens-resetbutton' );
00119         } else {
00120             $form->suppressDefaultSubmit();
00121         }
00122     }
00123 
00124     public function onSubmit( array $formData ) {
00125         if ( $formData['tokens'] ) {
00126             $user = $this->getUser();
00127             foreach ( $formData['tokens'] as $tokenPref ) {
00128                 $user->resetTokenFromOption( $tokenPref );
00129             }
00130             $user->saveSettings();
00131 
00132             return true;
00133         }
00134 
00135         return false;
00136     }
00137 
00138     protected function getGroupName() {
00139         return 'users';
00140     }
00141 
00142     public function isListed() {
00143         return (bool)$this->getTokensList();
00144     }
00145 }