X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FHTMLForm.php;h=5c0d7dbf6ce3c1f52056d482fe6390b6eedde87b;hb=957850f0da357337c24291a29d5ea0f08b5050a9;hp=d1a5438ff5baed310cd32e812014e5b88731f898;hpb=ca0e1a0ff0bee1d600eb6083144b5a43b7c28095;p=lhc%2Fweb%2Fwiklou.git
diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php
index d1a5438ff5..5c0d7dbf6c 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;
+class HTMLForm extends ContextSource {
# 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',
@@ -73,7 +81,10 @@ class HTMLForm {
);
protected $mMessagePrefix;
+
+ /** @var HTMLFormField[] */
protected $mFlatFields;
+
protected $mFieldTree;
protected $mShowReset = false;
public $mFieldData;
@@ -84,6 +95,8 @@ class HTMLForm {
protected $mPre = '';
protected $mHeader = '';
protected $mFooter = '';
+ protected $mSectionHeaders = array();
+ protected $mSectionFooters = array();
protected $mPost = '';
protected $mId;
@@ -91,6 +104,7 @@ class HTMLForm {
protected $mSubmitName;
protected $mSubmitText;
protected $mSubmitTooltip;
+
protected $mTitle;
protected $mMethod = 'post';
@@ -99,14 +113,35 @@ class HTMLForm {
protected $mButtons = array();
protected $mWrapperLegend = false;
+
+ /**
+ * If true, sections that contain both fields and subsections will
+ * render their subsections before their fields.
+ *
+ * Subclasses may set this to false to render subsections after fields
+ * instead.
+ */
+ protected $mSubSectionBeforeFields = true;
/**
* Build a new HTMLForm from an array of field attributes
* @param $descriptor Array of Field constructs, as described above
+ * @param $context IContextSource available since 1.18, will become compulsory in 1.18.
+ * Obviates the need to call $form->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, /*IContextSource*/ $context = null, $messagePrefix = '' ) {
+ if( $context instanceof IContextSource ){
+ $this->setContext( $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 +152,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 +184,32 @@ 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() { wfDeprecated( __METHOD__, '1.18' ); }
/**
* Initialise a new Object for the field
- * @param $descriptor input Descriptor, as described above
+ * @param $fieldname string
+ * @param $descriptor string 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 +220,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;
@@ -261,7 +288,7 @@ class HTMLForm {
$data = $this->filterDataForSubmit( $this->mFieldData );
- $res = call_user_func( $callback, $data );
+ $res = call_user_func( $callback, $data, $this );
return $res;
}
@@ -291,7 +318,16 @@ class HTMLForm {
* Set the introductory message, overwriting any existing message.
* @param $msg String complete text of message to display
*/
- function setIntro( $msg ) { $this->mPre = $msg; }
+ function setIntro( $msg ) {
+ $this->setPreText( $msg );
+ }
+
+ /**
+ * Set the introductory message, overwriting any existing message.
+ * @since 1.19
+ * @param $msg String complete text of message to display
+ */
+ function setPreText( $msg ) { $this->mPre = $msg; }
/**
* Add introductory text.
@@ -302,14 +338,62 @@ class HTMLForm {
/**
* Add header text, inside the form.
* @param $msg String complete text of message to display
+ * @param $section The section to add the header to
+ */
+ 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;
+ }
+ }
+
+ /**
+ * Set header text, inside the form.
+ * @since 1.19
+ * @param $msg String complete text of message to display
+ * @param $section The section to add the header to
*/
- function addHeaderText( $msg ) { $this->mHeader .= $msg; }
+ function setHeaderText( $msg, $section = null ) {
+ if ( is_null( $section ) ) {
+ $this->mHeader = $msg;
+ } else {
+ $this->mSectionHeaders[$section] = $msg;
+ }
+ }
/**
* Add footer text, inside the form.
* @param $msg String complete text of message to display
+ * @param $section string The section to add the footer text to
*/
- 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;
+ }
+ }
+
+ /**
+ * Set footer text, inside the form.
+ * @since 1.19
+ * @param $msg String complete text of message to display
+ * @param $section string The section to add the footer text to
+ */
+ function setFooterText( $msg, $section = null ) {
+ if ( is_null( $section ) ) {
+ $this->mFooter = $msg;
+ } else {
+ $this->mSectionFooters[$section] = $msg;
+ }
+ }
/**
* Add text to the end of the display.
@@ -317,9 +401,15 @@ class HTMLForm {
*/
function addPostText( $msg ) { $this->mPost .= $msg; }
+ /**
+ * Set text at the end of the display.
+ * @param $msg String complete text of message to display
+ */
+ function setPostText( $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
*/
@@ -338,7 +428,18 @@ class HTMLForm {
* @param $submitResult Mixed output from HTMLForm::trySubmit()
*/
function displayForm( $submitResult ) {
- global $wgOut;
+ $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
+ }
+
+ /**
+ * Returns the raw HTML generated by the form
+ * @param $submitResult Mixed output from HTMLForm::trySubmit()
+ * @return string
+ */
+ function getHTML( $submitResult ) {
+ # For good measure (it is the default)
+ $this->getOutput()->preventClickjacking();
+ $this->getOutput()->addModules( 'mediawiki.htmlform' );
$html = ''
. $this->getErrors( $submitResult )
@@ -351,11 +452,7 @@ class HTMLForm {
$html = $this->wrapForm( $html );
- $wgOut->addHTML( ''
- . $this->mPre
- . $html
- . $this->mPost
- );
+ return '' . $this->mPre . $html . $this->mPost;
}
/**
@@ -392,11 +489,17 @@ class HTMLForm {
* @return String HTML.
*/
function getHiddenFields() {
- global $wgUser;
+ global $wgUsePathInfo;
$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()->getEditToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
+ $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
+ }
+
+ if ( !$wgUsePathInfo && $this->getMethod() == 'get' ) {
+ $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
+ }
foreach ( $this->mHiddenFields as $data ) {
list( $value, $attribs ) = $data;
@@ -423,8 +526,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';
@@ -464,6 +566,7 @@ class HTMLForm {
/**
* Get the whole body of the form.
+ * @return String
*/
function getBody() {
return $this->displaySection( $this->mFieldTree );
@@ -471,13 +574,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 +600,7 @@ class HTMLForm {
* @param $errors Array of message keys/values
* @return String HTML, a
list of errors
*/
- static function formatErrors( $errors ) {
+ public static function formatErrors( $errors ) {
$errorstr = '';
foreach ( $errors as $error ) {
@@ -507,7 +613,7 @@ class HTMLForm {
$errorstr .= Html::rawElement(
'li',
- null,
+ array(),
wfMsgExt( $msg, array( 'parseinline' ), $error )
);
}
@@ -525,6 +631,15 @@ class HTMLForm {
$this->mSubmitText = $t;
}
+ /**
+ * Set the text for the submit button to a message
+ * @since 1.19
+ * @param $msg String message key
+ */
+ public function setSubmitTextMsg( $msg ) {
+ return $this->setSubmitText( $this->msg( $msg )->escaped() );
+ }
+
/**
* Get the text for the submit button, either customised or a default.
* @return unknown_type
@@ -545,7 +660,8 @@ class HTMLForm {
/**
* Set the id for the submit button.
- * @param $t String. FIXME: Integrity is *not* validated
+ * @param $t String.
+ * @todo FIXME: Integrity of $t is *not* validated
*/
function setSubmitID( $t ) {
$this->mSubmitID = $t;
@@ -562,6 +678,16 @@ class HTMLForm {
*/
public function setWrapperLegend( $legend ) { $this->mWrapperLegend = $legend; }
+ /**
+ * Prompt the whole form to be wrapped in a