From: Happy-melon Date: Wed, 13 Apr 2011 16:51:22 +0000 (+0000) Subject: Look mum, no globals! Implement a RequestContext for HTMLForm, passed in the constru... X-Git-Tag: 1.31.0-rc.0~30872 X-Git-Url: http://git.cyclocoop.org/fichier?a=commitdiff_plain;h=58caa451fad5206e0c51234051c62ec8fbeffad8;p=lhc%2Fweb%2Fwiklou.git Look mum, no globals! Implement a RequestContext for HTMLForm, passed in the constructor. Currently this is optional with B/C; at a later date it should be made compulsory. Doing this removes the need to call $form->setTitle() on every single use; only when you want to set the title to something other than the page context title. Implemented the new syntax in a sample of forms. Also fix a few minor errors in HTMLForm.php that my IDE complained about. --- diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index d8d83e8f21..7f85bac2be 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -101,6 +101,8 @@ class HTMLForm { protected $mSubmitName; protected $mSubmitText; protected $mSubmitTooltip; + + protected $mContext; // setTitle() * @param $messagePrefix String a prefix to go in front of default messages */ - public function __construct( $descriptor, $messagePrefix = '' ) { - $this->mMessagePrefix = $messagePrefix; + public function __construct( $descriptor, /*RequestContext*/ $context = null, $messagePrefix = '' ) { + if( $context instanceof RequestContext ){ + $this->mContext = $context; + $this->mTitle = false; // We don't need them to set a title + $this->mMessagePrefix = $messagePrefix; + } else { + // B/C since 1.18 + if( is_string( $context ) && $messagePrefix === '' ){ + // it's actually $messagePrefix + $this->mMessagePrefix = $context; + } + } // Expand out into a tree. $loadedDescriptor = array(); @@ -174,6 +188,8 @@ class HTMLForm { } elseif ( isset( $descriptor['type'] ) ) { $class = self::$typeMappings[$descriptor['type']]; $descriptor['class'] = $class; + } else { + $class = null; } if ( !$class ) { @@ -192,7 +208,7 @@ class HTMLForm { */ function prepareForm() { # Check if we have the info we need - if ( ! $this->mTitle ) { + if ( !$this->mTitle instanceof Title && $this->mTitle !== false ) { throw new MWException( "You must call setTitle() on an HTMLForm" ); } @@ -205,11 +221,10 @@ class HTMLForm { * @return Status|boolean */ function tryAuthorizedSubmit() { - global $wgUser, $wgRequest; - $editToken = $wgRequest->getVal( 'wpEditToken' ); + $editToken = $this->getRequest()->getVal( 'wpEditToken' ); $result = false; - if ( $this->getMethod() != 'post' || $wgUser->matchEditToken( $editToken ) ) { + if ( $this->getMethod() != 'post' || $this->getUser()->matchEditToken( $editToken ) ) { $result = $this->trySubmit(); } return $result; @@ -356,11 +371,9 @@ class HTMLForm { * @param $submitResult Mixed output from HTMLForm::trySubmit() */ function displayForm( $submitResult ) { - global $wgOut; - # For good measure (it is the default) - $wgOut->preventClickjacking(); - $wgOut->addModules( 'mediawiki.htmlform' ); + $this->getOutput()->preventClickjacking(); + $this->getOutput()->addModules( 'mediawiki.htmlform' ); $html = '' . $this->getErrors( $submitResult ) @@ -373,7 +386,7 @@ class HTMLForm { $html = $this->wrapForm( $html ); - $wgOut->addHTML( '' + $this->getOutput()->addHTML( '' . $this->mPre . $html . $this->mPost @@ -414,11 +427,9 @@ class HTMLForm { * @return String HTML. */ function getHiddenFields() { - global $wgUser; - $html = ''; if( $this->getMethod() == 'post' ){ - $html .= Html::hidden( 'wpEditToken', $wgUser->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n"; + $html .= Html::hidden( 'wpEditToken', $this->getUser()->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n"; $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n"; } @@ -447,8 +458,7 @@ class HTMLForm { } if ( isset( $this->mSubmitTooltip ) ) { - global $wgUser; - $attribs += $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mSubmitTooltip ); + $attribs += Linker::tooltipAndAccessKeyAttribs( $this->mSubmitTooltip ); } $attribs['class'] = 'mw-htmlform-submit'; @@ -500,11 +510,10 @@ class HTMLForm { */ function getErrors( $errors ) { if ( $errors instanceof Status ) { - global $wgOut; if ( $errors->isOK() ) { $errorstr = ''; } else { - $errorstr = $wgOut->parse( $errors->getWikiText() ); + $errorstr = $this->getOutput()->parse( $errors->getWikiText() ); } } elseif ( is_array( $errors ) ) { $errorstr = $this->formatErrors( $errors ); @@ -535,7 +544,7 @@ class HTMLForm { $errorstr .= Html::rawElement( 'li', - null, + array(), wfMsgExt( $msg, array( 'parseinline' ), $error ) ); } @@ -613,7 +622,27 @@ class HTMLForm { * @return Title */ function getTitle() { - return $this->mTitle; + return $this->mTitle === false + ? $this->getContext()->title + : $this->mTitle; + } + + public function getContext(){ + return $this->mContext instanceof RequestContext + ? $this->mContext + : RequestContext::getMain(); + } + + public function getOutput(){ + return $this->getContext()->output; + } + + public function getRequest(){ + return $this->getContext()->request; + } + + public function getUser(){ + return $this->getContext()->user; } /** @@ -683,8 +712,6 @@ class HTMLForm { * Construct the form fields from the Descriptor array */ function loadData() { - global $wgRequest; - $fieldData = array(); foreach ( $this->mFlatFields as $fieldname => $field ) { @@ -693,7 +720,7 @@ class HTMLForm { } elseif ( !empty( $field->mParams['disabled'] ) ) { $fieldData[$fieldname] = $field->getDefault(); } else { - $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest ); + $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() ); } } @@ -886,7 +913,6 @@ abstract class HTMLFormField { */ function getTableRow( $value ) { # Check for invalid data. - global $wgRequest; $errors = $this->validate( $value, $this->mParent->mFieldData ); @@ -898,7 +924,7 @@ abstract class HTMLFormField { $verticalLabel = true; } - if ( $errors === true || ( !$wgRequest->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) { + if ( $errors === true || ( !$this->mParent->getRequest()->wasPosted() && ( $this->mParent->getMethod() == 'post' ) ) ) { $errors = ''; $errorClass = ''; } else { @@ -995,10 +1021,7 @@ abstract class HTMLFormField { if ( empty( $this->mParams['tooltip'] ) ) { return array(); } - - global $wgUser; - - return $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mParams['tooltip'] ); + return Linker::tooltipAndAccessKeyAttribs( $this->mParams['tooltip'] ); } /** @@ -1301,7 +1324,7 @@ class HTMLSelectField extends HTMLFormField { $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) ); # If one of the options' 'name' is int(0), it is automatically selected. - # because PHP sucks and things int(0) == 'some string'. + # because PHP sucks and thinks int(0) == 'some string'. # Working around this by forcing all of them to strings. foreach( $this->mParams['options'] as $key => &$opt ){ if( is_int( $opt ) ){ @@ -1575,7 +1598,7 @@ class HTMLSelectAndOtherField extends HTMLSelectField { } else { # groupless reason list $optgroup = false; - $parts = array_map( 'trim', explode( '|', $opt, 2 ) ); + $parts = array_map( 'trim', explode( '|', $option, 2 ) ); if( count( $parts ) === 1 ){ $parts[1] = $parts[0]; } diff --git a/includes/specials/SpecialBlock.php b/includes/specials/SpecialBlock.php index a721ab63c7..0f0fdbc417 100644 --- a/includes/specials/SpecialBlock.php +++ b/includes/specials/SpecialBlock.php @@ -95,8 +95,7 @@ class SpecialBlock extends SpecialPage { $fields = self::getFormFields(); $this->maybeAlterFormDefaults( $fields ); - $form = new HTMLForm( $fields ); - $form->setTitle( $this->getTitle() ); + $form = new HTMLForm( $fields, $this->getContext() ); $form->setWrapperLegend( wfMsg( 'blockip-legend' ) ); $form->setSubmitCallback( array( __CLASS__, 'processForm' ) ); diff --git a/includes/specials/SpecialBlockList.php b/includes/specials/SpecialBlockList.php index 64245ba727..00b47b45a8 100644 --- a/includes/specials/SpecialBlockList.php +++ b/includes/specials/SpecialBlockList.php @@ -79,8 +79,7 @@ class SpecialBlockList extends SpecialPage { 'flatlist' => true, ), ); - $form = new HTMLForm( $fields ); - $form->setTitle( $this->getTitle() ); + $form = new HTMLForm( $fields, $this->getContext() ); $form->setMethod( 'get' ); $form->setWrapperLegend( wfMsg( 'ipblocklist-legend' ) ); $form->setSubmitText( wfMsg( 'ipblocklist-submit' ) ); diff --git a/includes/specials/SpecialPreferences.php b/includes/specials/SpecialPreferences.php index a44ce737b9..b269afa9ef 100644 --- a/includes/specials/SpecialPreferences.php +++ b/includes/specials/SpecialPreferences.php @@ -78,7 +78,7 @@ class SpecialPreferences extends SpecialPage { $wgOut->addWikiMsg( 'prefs-reset-intro' ); - $htmlForm = new HTMLForm( array(), 'prefs-restore' ); + $htmlForm = new HTMLForm( array(), $this->getContext(), 'prefs-restore' ); $htmlForm->setSubmitText( wfMsg( 'restoreprefs' ) ); $htmlForm->setTitle( $this->getTitle( 'reset' ) ); diff --git a/includes/specials/SpecialUnblock.php b/includes/specials/SpecialUnblock.php index ab444d2d77..cef5c2ba5d 100644 --- a/includes/specials/SpecialUnblock.php +++ b/includes/specials/SpecialUnblock.php @@ -61,8 +61,7 @@ class SpecialUnblock extends SpecialPage { $wgOut->setPageTitle( wfMsg( 'unblockip' ) ); $wgOut->addModules( 'mediawiki.special' ); - $form = new HTMLForm( $this->getFields() ); - $form->setTitle( $this->getTitle() ); + $form = new HTMLForm( $this->getFields(), $this->getContext() ); $form->setWrapperLegend( wfMsg( 'unblockip' ) ); $form->setSubmitCallback( array( __CLASS__, 'processUnblock' ) ); $form->setSubmitText( wfMsg( 'ipusubmit' ) );