MediaWiki  REL1_23
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         $this->requireLogin();
00065 
00066         parent::execute( $par );
00067 
00068         $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'Preferences' ) );
00069     }
00070 
00071     public function onSuccess() {
00072         $this->getOutput()->wrapWikiMsg(
00073             "<div class='successbox'>\n$1\n</div>",
00074             'resettokens-done'
00075         );
00076     }
00077 
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 
00117     protected function alterForm( HTMLForm $form ) {
00118         if ( $this->getTokensList() ) {
00119             $form->setSubmitTextMsg( 'resettokens-resetbutton' );
00120         } else {
00121             $form->suppressDefaultSubmit();
00122         }
00123     }
00124 
00125     public function onSubmit( array $formData ) {
00126         if ( $formData['tokens'] ) {
00127             $user = $this->getUser();
00128             foreach ( $formData['tokens'] as $tokenPref ) {
00129                 $user->resetTokenFromOption( $tokenPref );
00130             }
00131             $user->saveSettings();
00132 
00133             return true;
00134         }
00135 
00136         return false;
00137     }
00138 
00139     protected function getGroupName() {
00140         return 'users';
00141     }
00142 
00143     public function isListed() {
00144         return (bool)$this->getTokensList();
00145     }
00146 }