MediaWiki  REL1_24
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         if ( !isset( $this->tokensList ) ) {
00044             $tokens = array(
00045                 array( 'preference' => 'watchlisttoken', 'label-message' => 'resettokens-watchlist-token' ),
00046             );
00047             wfRunHooks( 'SpecialResetTokensTokens', array( &$tokens ) );
00048 
00049             $hiddenPrefs = $this->getConfig()->get( 'HiddenPrefs' );
00050             $tokens = array_filter( $tokens, function ( $tok ) use ( $hiddenPrefs ) {
00051                 return !in_array( $tok['preference'], $hiddenPrefs );
00052             } );
00053 
00054             $this->tokensList = $tokens;
00055         }
00056 
00057         return $this->tokensList;
00058     }
00059 
00060     public function execute( $par ) {
00061         // This is a preferences page, so no user JS for y'all.
00062         $this->getOutput()->disallowUserJs();
00063         $this->requireLogin();
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 
00082     protected function getFormFields() {
00083         $user = $this->getUser();
00084         $tokens = $this->getTokensList();
00085 
00086         if ( $tokens ) {
00087             $tokensForForm = array();
00088             foreach ( $tokens as $tok ) {
00089                 $label = $this->msg( 'resettokens-token-label' )
00090                     ->rawParams( $this->msg( $tok['label-message'] )->parse() )
00091                     ->params( $user->getTokenFromOption( $tok['preference'] ) )
00092                     ->escaped();
00093                 $tokensForForm[$label] = $tok['preference'];
00094             }
00095 
00096             $desc = array(
00097                 'label-message' => 'resettokens-tokens',
00098                 'type' => 'multiselect',
00099                 'options' => $tokensForForm,
00100             );
00101         } else {
00102             $desc = array(
00103                 'label-message' => 'resettokens-no-tokens',
00104                 'type' => 'info',
00105             );
00106         }
00107 
00108         return array(
00109             'tokens' => $desc,
00110         );
00111     }
00112 
00118     protected function alterForm( HTMLForm $form ) {
00119         if ( $this->getTokensList() ) {
00120             $form->setSubmitTextMsg( 'resettokens-resetbutton' );
00121         } else {
00122             $form->suppressDefaultSubmit();
00123         }
00124     }
00125 
00126     public function onSubmit( array $formData ) {
00127         if ( $formData['tokens'] ) {
00128             $user = $this->getUser();
00129             foreach ( $formData['tokens'] as $tokenPref ) {
00130                 $user->resetTokenFromOption( $tokenPref );
00131             }
00132             $user->saveSettings();
00133 
00134             return true;
00135         }
00136 
00137         return false;
00138     }
00139 
00140     protected function getGroupName() {
00141         return 'users';
00142     }
00143 
00144     public function isListed() {
00145         return (bool)$this->getTokensList();
00146     }
00147 }