X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FHTMLForm.php;h=ec038c90f7b27d94479a028b6446a28635a13a48;hb=0a49fd69b0f1ba627ac82e4a91ed1f825e4235ce;hp=d1a5438ff5baed310cd32e812014e5b88731f898;hpb=ca0e1a0ff0bee1d600eb6083144b5a43b7c28095;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index d1a5438ff5..ec038c90f7 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -33,6 +33,10 @@ * 'help-message' -- message key for a message to use as a help text. * can be an array of msg key and then parameters to * the message. + * Overwrites 'help-messages'. + * 'help-messages' -- array of message key. As above, each item can + * be an array of msg key and then parameters. + * Overwrites 'help-message'. * 'required' -- passed through to the object, indicating that it * is a required field. * 'size' -- the length of text fields @@ -42,11 +46,14 @@ * 'validation-callback' -- a function name to give you the chance * to impose extra validation on the field input. * @see HTMLForm::validate() + * 'name' -- By default, the 'name' attribute of the input field + * is "wp{$fieldname}". If you want a different name + * (eg one without the "wp" prefix), specify it here and + * it will be used without modification. * * TODO: Document 'section' / 'subsection' stuff */ class HTMLForm { - static $jsAdded = false; # A mapping of 'type' inputs onto standard HTMLFormField subclasses static $typeMappings = array( @@ -61,6 +68,7 @@ class HTMLForm { 'float' => 'HTMLFloatField', 'info' => 'HTMLInfoField', 'selectorother' => 'HTMLSelectOrOtherField', + 'selectandother' => 'HTMLSelectAndOtherField', 'submit' => 'HTMLSubmitField', 'hidden' => 'HTMLHiddenField', 'edittools' => 'HTMLEditTools', @@ -84,6 +92,8 @@ class HTMLForm { protected $mPre = ''; protected $mHeader = ''; protected $mFooter = ''; + protected $mSectionHeaders = array(); + protected $mSectionFooters = array(); protected $mPost = ''; protected $mId; @@ -91,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(); @@ -117,15 +141,11 @@ class HTMLForm { ? $info['section'] : ''; - $info['name'] = isset( $info['name'] ) - ? $info['name'] - : $fieldname; - if ( isset( $info['type'] ) && $info['type'] == 'file' ) { $this->mUseMultipart = true; } - $field = self::loadInputFromParameters( $info ); + $field = self::loadInputFromParameters( $fieldname, $info ); $field->mParent = $this; $setSection =& $loadedDescriptor; @@ -153,32 +173,31 @@ class HTMLForm { /** * Add the HTMLForm-specific JavaScript, if it hasn't been * done already. + * @deprecated since 1.18 load modules with ResourceLoader instead */ - static function addJS() { - if ( self::$jsAdded ) return; - - global $wgOut; - - $wgOut->addModules( 'mediawiki.legacy.htmlform' ); - } + static function addJS() { } /** * Initialise a new Object for the field * @param $descriptor input Descriptor, as described above * @return HTMLFormField subclass */ - static function loadInputFromParameters( $descriptor ) { + static function loadInputFromParameters( $fieldname, $descriptor ) { if ( isset( $descriptor['class'] ) ) { $class = $descriptor['class']; } elseif ( isset( $descriptor['type'] ) ) { $class = self::$typeMappings[$descriptor['type']]; $descriptor['class'] = $class; + } else { + $class = null; } if ( !$class ) { throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) ); } + $descriptor['fieldname'] = $fieldname; + $obj = new $class( $descriptor ); return $obj; @@ -189,27 +208,23 @@ 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" ); } - // FIXME shouldn't this be closer to displayForm() ? - self::addJS(); - # Load data from the request. $this->loadData(); } /** * Try submitting, with edit token check first - * @return Status|boolean + * @return Status|boolean */ function tryAuthorizedSubmit() { - global $wgUser, $wgRequest; - $editToken = $wgRequest->getVal( 'wpEditToken' ); + $editToken = $this->getRequest()->getVal( 'wpEditToken' ); $result = false; - if ( $wgUser->matchEditToken( $editToken ) ) { + if ( $this->getMethod() != 'post' || $this->getUser()->matchEditToken( $editToken ) ) { $result = $this->trySubmit(); } return $result; @@ -303,13 +318,31 @@ class HTMLForm { * Add header text, inside the form. * @param $msg String complete text of message to display */ - function addHeaderText( $msg ) { $this->mHeader .= $msg; } + function addHeaderText( $msg, $section = null ) { + if ( is_null( $section ) ) { + $this->mHeader .= $msg; + } else { + if ( !isset( $this->mSectionHeaders[$section] ) ) { + $this->mSectionHeaders[$section] = ''; + } + $this->mSectionHeaders[$section] .= $msg; + } + } /** * Add footer text, inside the form. * @param $msg String complete text of message to display */ - function addFooterText( $msg ) { $this->mFooter .= $msg; } + function addFooterText( $msg, $section = null ) { + if ( is_null( $section ) ) { + $this->mFooter .= $msg; + } else { + if ( !isset( $this->mSectionFooters[$section] ) ) { + $this->mSectionFooters[$section] = ''; + } + $this->mSectionFooters[$section] .= $msg; + } + } /** * Add text to the end of the display. @@ -319,7 +352,7 @@ class HTMLForm { /** * Add a hidden field to the output - * @param $name String field name + * @param $name String field name. This will be used exactly as entered * @param $value String field value * @param $attribs Array */ @@ -338,7 +371,9 @@ class HTMLForm { * @param $submitResult Mixed output from HTMLForm::trySubmit() */ function displayForm( $submitResult ) { - global $wgOut; + # For good measure (it is the default) + $this->getOutput()->preventClickjacking(); + $this->getOutput()->addModules( 'mediawiki.htmlform' ); $html = '' . $this->getErrors( $submitResult ) @@ -351,7 +386,7 @@ class HTMLForm { $html = $this->wrapForm( $html ); - $wgOut->addHTML( '' + $this->getOutput()->addHTML( '' . $this->mPre . $html . $this->mPost @@ -392,11 +427,11 @@ class HTMLForm { * @return String HTML. */ function getHiddenFields() { - global $wgUser; - $html = ''; - $html .= Html::hidden( 'wpEditToken', $wgUser->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n"; - $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n"; + if( $this->getMethod() == 'post' ){ + $html .= Html::hidden( 'wpEditToken', $this->getUser()->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n"; + $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n"; + } foreach ( $this->mHiddenFields as $data ) { list( $value, $attribs ) = $data; @@ -423,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'; @@ -471,13 +505,16 @@ class HTMLForm { /** * Format and display an error message stack. - * @param $errors Mixed String or Array of message keys + * @param $errors String|Array|Status * @return String */ function getErrors( $errors ) { if ( $errors instanceof Status ) { - global $wgOut; - $errorstr = $wgOut->parse( $errors->getWikiText() ); + if ( $errors->isOK() ) { + $errorstr = ''; + } else { + $errorstr = $this->getOutput()->parse( $errors->getWikiText() ); + } } elseif ( is_array( $errors ) ) { $errorstr = $this->formatErrors( $errors ); } else { @@ -494,7 +531,7 @@ class HTMLForm { * @param $errors Array of message keys/values * @return String HTML, a