X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FHTMLForm.php;h=ec038c90f7b27d94479a028b6446a28635a13a48;hb=0a49fd69b0f1ba627ac82e4a91ed1f825e4235ce;hp=50683a4b196a6c6d9cf7d989a18ee868710955b6;hpb=e5a74e5ff74b7ab1642035290e55359e45d8361a;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index 50683a4b19..ec038c90f7 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -1,56 +1,64 @@ $info, * where $info is an Associative Array with any of the following: - * - * 'class' -- the subclass of HTMLFormField that will be used - * to create the object. *NOT* the CSS class! - * 'type' -- roughly translates into the type attribute. + * if 'class' is not specified, this is used as a map + * through HTMLForm::$typeMappings to get the class name. + * 'default' -- default value when the form is displayed + * 'id' -- HTML id attribute + * 'cssclass' -- CSS class + * 'options' -- varies according to the specific object. + * 'label-message' -- message key for a message to use as the label. + * can be an array of msg key and then parameters to + * the message. + * 'label' -- alternatively, a raw text message. Overridden by + * label-message + * '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 + * 'filter-callback -- a function name to give you the chance to + * massage the inputted value before it's processed. + * @see HTMLForm::filter() + * '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( 'text' => 'HTMLTextField', + 'textarea' => 'HTMLTextAreaField', 'select' => 'HTMLSelectField', 'radio' => 'HTMLRadioField', 'multiselect' => 'HTMLMultiSelectField', @@ -60,65 +68,91 @@ class HTMLForm { 'float' => 'HTMLFloatField', 'info' => 'HTMLInfoField', 'selectorother' => 'HTMLSelectOrOtherField', + 'selectandother' => 'HTMLSelectAndOtherField', 'submit' => 'HTMLSubmitField', 'hidden' => 'HTMLHiddenField', - + 'edittools' => 'HTMLEditTools', + # HTMLTextField will output the correct type="" attribute automagically. - # There are about four zillion other HTML 5 input types, like url, but + # There are about four zillion other HTML5 input types, like url, but # we don't use those at the moment, so no point in adding all of them. 'email' => 'HTMLTextField', 'password' => 'HTMLTextField', ); - + protected $mMessagePrefix; protected $mFlatFields; protected $mFieldTree; protected $mShowReset = false; public $mFieldData; - + protected $mSubmitCallback; protected $mValidationErrorMessage; - + protected $mPre = ''; protected $mHeader = ''; + protected $mFooter = ''; + protected $mSectionHeaders = array(); + protected $mSectionFooters = array(); protected $mPost = ''; - + protected $mId; + protected $mSubmitID; + 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(); $this->mFlatFields = array(); - foreach( $descriptor as $fieldname => $info ) { - $section = ''; - if ( isset( $info['section'] ) ) - $section = $info['section']; + foreach ( $descriptor as $fieldname => $info ) { + $section = isset( $info['section'] ) + ? $info['section'] + : ''; - $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; - if( $section ) { + if ( $section ) { $sectionParts = explode( '/', $section ); - while( count( $sectionParts ) ) { + while ( count( $sectionParts ) ) { $newName = array_shift( $sectionParts ); if ( !isset( $setSection[$newName] ) ) { @@ -137,65 +171,79 @@ class HTMLForm { } /** - * Add the HTMLForm-specific JavaScript, if it hasn't been + * 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->addScriptClass( '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 ) { + if ( !$class ) { throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) ); } + $descriptor['fieldname'] = $fieldname; + $obj = new $class( $descriptor ); return $obj; } /** - * The here's-one-I-made-earlier option: do the submission if - * posted, or display the form with or without funky valiation - * errors - * @return Bool whether submission was successful. + * Prepare form for submission */ - function show() { - $html = ''; - - self::addJS(); + function prepareForm() { + # Check if we have the info we need + if ( !$this->mTitle instanceof Title && $this->mTitle !== false ) { + throw new MWException( "You must call setTitle() on an HTMLForm" ); + } # Load data from the request. $this->loadData(); + } - # Try a submission - global $wgUser, $wgRequest; - $editToken = $wgRequest->getVal( 'wpEditToken' ); + /** + * Try submitting, with edit token check first + * @return Status|boolean + */ + function tryAuthorizedSubmit() { + $editToken = $this->getRequest()->getVal( 'wpEditToken' ); $result = false; - if ( $wgUser->matchEditToken( $editToken ) ) + if ( $this->getMethod() != 'post' || $this->getUser()->matchEditToken( $editToken ) ) { $result = $this->trySubmit(); + } + return $result; + } + + /** + * The here's-one-I-made-earlier option: do the submission if + * posted, or display the form with or without funky valiation + * errors + * @return Bool or Status whether submission was successful. + */ + function show() { + $this->prepareForm(); - if( $result === true ) + $result = $this->tryAuthorizedSubmit(); + if ( $result === true || ( $result instanceof Status && $result->isGood() ) ){ return $result; + } - # Display form. $this->displayForm( $result ); return false; } @@ -204,19 +252,20 @@ class HTMLForm { * Validate all the fields, and call the submision callback * function if everything is kosher. * @return Mixed Bool true == Successful submission, Bool false - * == No submission attempted, anything else == Error to - * display. + * == No submission attempted, anything else == Error to + * display. */ function trySubmit() { # Check for validation - foreach( $this->mFlatFields as $fieldname => $field ) { - if ( !empty( $field->mParams['nodata'] ) ) + foreach ( $this->mFlatFields as $fieldname => $field ) { + if ( !empty( $field->mParams['nodata'] ) ) { continue; - if ( $field->validate( + } + if ( $field->validate( $this->mFieldData[$fieldname], - $this->mFieldData ) - !== true ) - { + $this->mFieldData ) + !== true + ) { return isset( $this->mValidationErrorMessage ) ? $this->mValidationErrorMessage : array( 'htmlform-invalid-input' ); @@ -236,23 +285,23 @@ class HTMLForm { * Set a callback to a function to do something with the form * once it's been successfully validated. * @param $cb String function name. The function will be passed - * the output from HTMLForm::filterDataForSubmit, and must - * return Bool true on success, Bool false if no submission - * was attempted, or String HTML output to display on error. + * the output from HTMLForm::filterDataForSubmit, and must + * return Bool true on success, Bool false if no submission + * was attempted, or String HTML output to display on error. */ function setSubmitCallback( $cb ) { $this->mSubmitCallback = $cb; } /** - * Set a message to display on a validation error. + * Set a message to display on a validation error. * @param $msg Mixed String or Array of valid inputs to wfMsgExt() - * (so each entry can be either a String or Array) + * (so each entry can be either a String or Array) */ function setValidationErrorMessage( $msg ) { $this->mValidationErrorMessage = $msg; } - + /** * Set the introductory message, overwriting any existing message. * @param $msg String complete text of message to display @@ -264,54 +313,80 @@ class HTMLForm { * @param $msg String complete text of message to display */ function addPreText( $msg ) { $this->mPre .= $msg; } - + /** * 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, $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. * @param $msg String complete text of message to display */ function addPostText( $msg ) { $this->mPost .= $msg; } - + /** * 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 */ - public function addHiddenField( $name, $value ){ - $this->mHiddenFields[ $name ] = $value; + public function addHiddenField( $name, $value, $attribs = array() ) { + $attribs += array( 'name' => $name ); + $this->mHiddenFields[] = array( $value, $attribs ); } - - public function addButton( $name, $value, $id=null ){ - $this->mButtons[] = compact( 'name', 'value', 'id' ); + + public function addButton( $name, $value, $id = null, $attribs = null ) { + $this->mButtons[] = compact( 'name', 'value', 'id', 'attribs' ); } /** - * Display the form (sending to wgOut), with an appropriate error + * Display the form (sending to wgOut), with an appropriate error * message or stack of messages, and any validation errors, etc. * @param $submitResult Mixed output from HTMLForm::trySubmit() */ function displayForm( $submitResult ) { - global $wgOut; - - if ( $submitResult !== false ) { - $this->displayErrors( $submitResult ); - } + # For good measure (it is the default) + $this->getOutput()->preventClickjacking(); + $this->getOutput()->addModules( 'mediawiki.htmlform' ); $html = '' + . $this->getErrors( $submitResult ) . $this->mHeader . $this->getBody() . $this->getHiddenFields() . $this->getButtons() + . $this->mFooter ; $html = $this->wrapForm( $html ); - $wgOut->addHTML( '' + $this->getOutput()->addHTML( '' . $this->mPre . $html . $this->mPost @@ -324,21 +399,27 @@ class HTMLForm { * @return String wrapped HTML. */ function wrapForm( $html ) { - + # Include a
wrapper for style, if requested. - if( $this->mWrapperLegend !== false ){ + if ( $this->mWrapperLegend !== false ) { $html = Xml::fieldset( $this->mWrapperLegend, $html ); } - - return Html::rawElement( - 'form', - array( - 'action' => $this->getTitle()->getFullURL(), - 'method' => 'post', - 'class' => 'visualClear', - ), - $html + # Use multipart/form-data + $encType = $this->mUseMultipart + ? 'multipart/form-data' + : 'application/x-www-form-urlencoded'; + # Attributes + $attribs = array( + 'action' => $this->getTitle()->getFullURL(), + 'method' => $this->mMethod, + 'class' => 'visualClear', + 'enctype' => $encType, ); + if ( !empty( $this->mId ) ) { + $attribs['id'] = $this->mId; + } + + return Html::rawElement( 'form', $attribs, $html ); } /** @@ -346,14 +427,15 @@ class HTMLForm { * @return String HTML. */ function getHiddenFields() { - global $wgUser; $html = ''; + if( $this->getMethod() == 'post' ){ + $html .= Html::hidden( 'wpEditToken', $this->getUser()->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n"; + $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n"; + } - $html .= Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n"; - $html .= Html::hidden( 'title', $this->getTitle() ) . "\n"; - - foreach( $this->mHiddenFields as $name => $value ){ - $html .= Html::hidden( $name, $value ) . "\n"; + foreach ( $this->mHiddenFields as $data ) { + list( $value, $attribs ) = $data; + $html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n"; } return $html; @@ -365,17 +447,25 @@ class HTMLForm { */ function getButtons() { $html = ''; - $attribs = array(); - if ( isset( $this->mSubmitID ) ) + if ( isset( $this->mSubmitID ) ) { $attribs['id'] = $this->mSubmitID; + } + + if ( isset( $this->mSubmitName ) ) { + $attribs['name'] = $this->mSubmitName; + } + + if ( isset( $this->mSubmitTooltip ) ) { + $attribs += Linker::tooltipAndAccessKeyAttribs( $this->mSubmitTooltip ); + } $attribs['class'] = 'mw-htmlform-submit'; $html .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n"; - if( $this->mShowReset ) { + if ( $this->mShowReset ) { $html .= Html::element( 'input', array( @@ -384,18 +474,25 @@ class HTMLForm { ) ) . "\n"; } - - foreach( $this->mButtons as $button ){ + + foreach ( $this->mButtons as $button ) { $attrs = array( 'type' => 'submit', 'name' => $button['name'], 'value' => $button['value'] ); - if( isset( $button['id'] ) ) + + if ( $button['attribs'] ) { + $attrs += $button['attribs']; + } + + if ( isset( $button['id'] ) ) { $attrs['id'] = $button['id']; + } + $html .= Html::element( 'input', $attrs ); } - + return $html; } @@ -408,19 +505,25 @@ 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 displayErrors( $errors ) { - if ( is_array( $errors ) ) { + function getErrors( $errors ) { + if ( $errors instanceof Status ) { + if ( $errors->isOK() ) { + $errorstr = ''; + } else { + $errorstr = $this->getOutput()->parse( $errors->getWikiText() ); + } + } elseif ( is_array( $errors ) ) { $errorstr = $this->formatErrors( $errors ); } else { $errorstr = $errors; } - - $errorstr = Html::rawElement( 'div', array( 'class' => 'error' ), $errorstr ); - global $wgOut; - $wgOut->addHTML( $errorstr ); + return $errorstr + ? Html::rawElement( 'div', array( 'class' => 'error' ), $errorstr ) + : ''; } /** @@ -428,18 +531,20 @@ class HTMLForm { * @param $errors Array of message keys/values * @return String HTML, a