X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FHTMLForm.php;h=ec038c90f7b27d94479a028b6446a28635a13a48;hb=0a49fd69b0f1ba627ac82e4a91ed1f825e4235ce;hp=0fddfe46171244d1af6aef20523810e8cdce3834;hpb=c3889287f2a53563289757f4f9aadb04bbe77064;p=lhc%2Fweb%2Fwiklou.git
diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php
index 0fddfe4617..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,7 +101,10 @@ 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();
@@ -116,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;
@@ -152,68 +173,77 @@ 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;
}
/**
- * 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.
+ * Prepare form for submission
*/
- function show() {
+ 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" );
}
- self::addJS();
-
# 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();
+ $result = $this->tryAuthorizedSubmit();
if ( $result === true || ( $result instanceof Status && $result->isGood() ) ){
return $result;
}
- # Display form.
$this->displayForm( $result );
return false;
}
@@ -288,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.
@@ -304,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
*/
@@ -323,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 )
@@ -336,7 +386,7 @@ class HTMLForm {
$html = $this->wrapForm( $html );
- $wgOut->addHTML( ''
+ $this->getOutput()->addHTML( ''
. $this->mPre
. $html
. $this->mPost
@@ -361,7 +411,7 @@ class HTMLForm {
# Attributes
$attribs = array(
'action' => $this->getTitle()->getFullURL(),
- 'method' => 'post',
+ 'method' => $this->mMethod,
'class' => 'visualClear',
'enctype' => $encType,
);
@@ -377,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;
@@ -408,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';
@@ -456,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 {
@@ -479,7 +531,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 ) {
@@ -492,7 +544,7 @@ class HTMLForm {
$errorstr .= Html::rawElement(
'li',
- null,
+ array(),
wfMsgExt( $msg, array( 'parseinline' ), $error )
);
}
@@ -528,10 +580,10 @@ class HTMLForm {
$this->mSubmitTooltip = $name;
}
-
/**
* 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;
@@ -571,7 +623,51 @@ class HTMLForm {
* @return Title
*/
function getTitle() {
- return $this->mTitle;
+ return $this->mTitle === false
+ ? $this->getContext()->title
+ : $this->mTitle;
+ }
+
+ /**
+ * @return RequestContext
+ */
+ public function getContext(){
+ return $this->mContext instanceof RequestContext
+ ? $this->mContext
+ : RequestContext::getMain();
+ }
+
+ /**
+ * @return OutputPage
+ */
+ public function getOutput(){
+ return $this->getContext()->output;
+ }
+
+ /**
+ * @return WebRequest
+ */
+ public function getRequest(){
+ return $this->getContext()->request;
+ }
+
+ /**
+ * @return User
+ */
+ public function getUser(){
+ return $this->getContext()->user;
+ }
+
+ /**
+ * Set the method used to submit the form
+ * @param $method String
+ */
+ public function setMethod( $method='post' ){
+ $this->mMethod = $method;
+ }
+
+ public function getMethod(){
+ return $this->mMethod;
}
/**
@@ -594,7 +690,13 @@ class HTMLForm {
$hasLeftColumn = true;
} elseif ( is_array( $value ) ) {
$section = $this->displaySection( $value, $key );
- $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
+ $legend = $this->getLegend( $key );
+ if ( isset( $this->mSectionHeaders[$key] ) ) {
+ $section = $this->mSectionHeaders[$key] . $section;
+ }
+ if ( isset( $this->mSectionFooters[$key] ) ) {
+ $section .= $this->mSectionFooters[$key];
+ }
$subsectionHtml .= Xml::fieldset( $legend, $section ) . "\n";
}
}
@@ -623,8 +725,6 @@ class HTMLForm {
* Construct the form fields from the Descriptor array
*/
function loadData() {
- global $wgRequest;
-
$fieldData = array();
foreach ( $this->mFlatFields as $fieldname => $field ) {
@@ -633,7 +733,7 @@ class HTMLForm {
} elseif ( !empty( $field->mParams['disabled'] ) ) {
$fieldData[$fieldname] = $field->getDefault();
} else {
- $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
+ $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() );
}
}
@@ -665,6 +765,16 @@ class HTMLForm {
function filterDataForSubmit( $data ) {
return $data;
}
+
+ /**
+ * Get a string to go in the