MediaWiki  REL1_24
FormSpecialPage.php
Go to the documentation of this file.
00001 <?php
00031 abstract class FormSpecialPage extends SpecialPage {
00036     protected $par = null;
00037 
00042     abstract protected function getFormFields();
00043 
00048     protected function preText() {
00049         return '';
00050     }
00051 
00056     protected function postText() {
00057         return '';
00058     }
00059 
00064     protected function alterForm( HTMLForm $form ) {
00065     }
00066 
00073     protected function getMessagePrefix() {
00074         return strtolower( $this->getName() );
00075     }
00076 
00081     protected function getForm() {
00082         $this->fields = $this->getFormFields();
00083 
00084         $form = new HTMLForm( $this->fields, $this->getContext(), $this->getMessagePrefix() );
00085         $form->setSubmitCallback( array( $this, 'onSubmit' ) );
00086         // If the form is a compact vertical form, then don't output this ugly
00087         // fieldset surrounding it.
00088         // XXX Special pages can setDisplayFormat to 'vform' in alterForm(), but that
00089         // is called after this.
00090         if ( !$form->isVForm() ) {
00091             $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
00092         }
00093 
00094         $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
00095         if ( !$headerMsg->isDisabled() ) {
00096             $form->addHeaderText( $headerMsg->parseAsBlock() );
00097         }
00098 
00099         // Retain query parameters (uselang etc)
00100         $params = array_diff_key(
00101             $this->getRequest()->getQueryValues(), array( 'title' => null ) );
00102         $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
00103 
00104         $form->addPreText( $this->preText() );
00105         $form->addPostText( $this->postText() );
00106         $this->alterForm( $form );
00107 
00108         // Give hooks a chance to alter the form, adding extra fields or text etc
00109         wfRunHooks( 'SpecialPageBeforeFormDisplay', array( $this->getName(), &$form ) );
00110 
00111         return $form;
00112     }
00113 
00120     abstract public function onSubmit( array $data /* $form = null */ );
00121 
00127     public function onSuccess() {
00128     }
00129 
00135     public function execute( $par ) {
00136         $this->setParameter( $par );
00137         $this->setHeaders();
00138 
00139         // This will throw exceptions if there's a problem
00140         $this->checkExecutePermissions( $this->getUser() );
00141 
00142         $form = $this->getForm();
00143         if ( $form->show() ) {
00144             $this->onSuccess();
00145         }
00146     }
00147 
00152     protected function setParameter( $par ) {
00153         $this->par = $par;
00154     }
00155 
00163     protected function checkExecutePermissions( User $user ) {
00164         $this->checkPermissions();
00165 
00166         if ( $this->requiresUnblock() && $user->isBlocked() ) {
00167             $block = $user->getBlock();
00168             throw new UserBlockedError( $block );
00169         }
00170 
00171         if ( $this->requiresWrite() ) {
00172             $this->checkReadOnly();
00173         }
00174 
00175         return true;
00176     }
00177 
00182     public function requiresWrite() {
00183         return true;
00184     }
00185 
00190     public function requiresUnblock() {
00191         return true;
00192     }
00193 }