MediaWiki  REL1_23
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( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
00110 
00111         return $form;
00112     }
00113 
00119     abstract public function onSubmit( array $data );
00120 
00126     public function onSuccess() {
00127     }
00128 
00134     public function execute( $par ) {
00135         $this->setParameter( $par );
00136         $this->setHeaders();
00137 
00138         // This will throw exceptions if there's a problem
00139         $this->checkExecutePermissions( $this->getUser() );
00140 
00141         $form = $this->getForm();
00142         if ( $form->show() ) {
00143             $this->onSuccess();
00144         }
00145     }
00146 
00151     protected function setParameter( $par ) {
00152         $this->par = $par;
00153     }
00154 
00162     protected function checkExecutePermissions( User $user ) {
00163         $this->checkPermissions();
00164 
00165         if ( $this->requiresUnblock() && $user->isBlocked() ) {
00166             $block = $user->getBlock();
00167             throw new UserBlockedError( $block );
00168         }
00169 
00170         if ( $this->requiresWrite() ) {
00171             $this->checkReadOnly();
00172         }
00173 
00174         return true;
00175     }
00176 
00181     public function requiresWrite() {
00182         return true;
00183     }
00184 
00189     public function requiresUnblock() {
00190         return true;
00191     }
00192 }