protected $mSubmitName;
protected $mSubmitText;
protected $mSubmitTooltip;
+
+ protected $mContext; // <! IContextSource
protected $mTitle;
protected $mMethod = 'post';
/**
* 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->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();
/**
* 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( $fieldname, $descriptor ) {
} elseif ( isset( $descriptor['type'] ) ) {
$class = self::$typeMappings[$descriptor['type']];
$descriptor['class'] = $class;
+ } else {
+ $class = null;
}
if ( !$class ) {
*/
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" );
}
* @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;
/**
* 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 ) {
+ function addHeaderText( $msg, $section = null ) {
if ( is_null( $section ) ) {
- $this->mHeader .= $msg;
+ $this->mHeader .= $msg;
} else {
if ( !isset( $this->mSectionHeaders[$section] ) ) {
$this->mSectionHeaders[$section] = '';
/**
* 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, $section = null ) {
+ function addFooterText( $msg, $section = null ) {
if ( is_null( $section ) ) {
- $this->mFooter .= $msg;
+ $this->mFooter .= $msg;
} else {
if ( !isset( $this->mSectionFooters[$section] ) ) {
$this->mSectionFooters[$section] = '';
}
- $this->mSectionFooters[$section] .= $msg;
+ $this->mSectionFooters[$section] .= $msg;
}
}
* @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 )
$html = $this->wrapForm( $html );
- $wgOut->addHTML( ''
+ $this->getOutput()->addHTML( ''
. $this->mPre
. $html
. $this->mPost
* @return String HTML.
*/
function getHiddenFields() {
- global $wgUser;
+ global $wgUsePathInfo;
$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";
+ }
+
+ if ( !$wgUsePathInfo && $this->getMethod() == 'get' ) {
$html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
}
}
if ( isset( $this->mSubmitTooltip ) ) {
- global $wgUser;
- $attribs += $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mSubmitTooltip );
+ $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
}
$attribs['class'] = 'mw-htmlform-submit';
/**
* 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;
if ( $errors->isOK() ) {
$errorstr = '';
} else {
- $errorstr = $wgOut->parse( $errors->getWikiText() );
+ $errorstr = $this->getOutput()->parse( $errors->getWikiText() );
}
} elseif ( is_array( $errors ) ) {
$errorstr = $this->formatErrors( $errors );
* @param $errors Array of message keys/values
* @return String HTML, a <ul> list of errors
*/
- static function formatErrors( $errors ) {
+ public static function formatErrors( $errors ) {
$errorstr = '';
foreach ( $errors as $error ) {
$errorstr .= Html::rawElement(
'li',
- null,
+ array(),
wfMsgExt( $msg, array( 'parseinline' ), $error )
);
}
/**
* 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;
* @return Title
*/
function getTitle() {
- return $this->mTitle;
+ return $this->mTitle === false
+ ? $this->getContext()->getTitle()
+ : $this->mTitle;
+ }
+
+ /**
+ * @return IContextSource
+ */
+ public function getContext(){
+ return $this->mContext instanceof IContextSource
+ ? $this->mContext
+ : RequestContext::getMain();
+ }
+
+ /**
+ * @return OutputPage
+ */
+ public function getOutput(){
+ return $this->getContext()->getOutput();
+ }
+
+ /**
+ * @return WebRequest
+ */
+ public function getRequest(){
+ return $this->getContext()->getRequest();
+ }
+
+ /**
+ * @return User
+ */
+ public function getUser(){
+ return $this->getContext()->getUser();
}
/**
/**
* TODO: Document
- * @param $fields
+ * @param $fields array of fields (either arrays or objects)
+ * @param $sectionName string ID attribute of the <table> tag for this section, ignored if empty
+ * @param $fieldsetIDPrefix string ID prefix for the <fieldset> tag of each subsection, ignored if empty
*/
- function displaySection( $fields, $sectionName = '' ) {
+ function displaySection( $fields, $sectionName = '', $fieldsetIDPrefix = '' ) {
$tableHtml = '';
$subsectionHtml = '';
$hasLeftColumn = false;
: $value->getDefault();
$tableHtml .= $value->getTableRow( $v );
- if ( $value->getLabel() != ' ' )
+ if ( $value->getLabel() != ' ' ) {
$hasLeftColumn = true;
+ }
} elseif ( is_array( $value ) ) {
$section = $this->displaySection( $value, $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";
+ $attributes = array();
+ if ( $fieldsetIDPrefix ) {
+ $attributes['id'] = Sanitizer::escapeId( "$fieldsetIDPrefix$key" );
+ }
+ $subsectionHtml .= Xml::fieldset( $legend, $section, $attributes ) . "\n";
}
}
* Construct the form fields from the Descriptor array
*/
function loadData() {
- global $wgRequest;
-
$fieldData = array();
foreach ( $this->mFlatFields as $fieldname => $field ) {
} elseif ( !empty( $field->mParams['disabled'] ) ) {
$fieldData[$fieldname] = $field->getDefault();
} else {
- $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
+ $fieldData[$fieldname] = $field->loadDataFromRequest( $this->getRequest() );
}
}
protected $mID;
protected $mClass = '';
protected $mDefault;
+
+ /**
+ * @var HTMLForm
+ */
public $mParent;
/**
/**
* Initialise the object
- * @param $params Associative Array. See HTMLForm doc for syntax.
+ * @param $params array Associative Array. See HTMLForm doc for syntax.
*/
function __construct( $params ) {
$this->mParams = $params;
*/
function getTableRow( $value ) {
# Check for invalid data.
- global $wgRequest;
$errors = $this->validate( $value, $this->mParent->mFieldData );
$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 {
$helptext = null;
if ( isset( $this->mParams['help-message'] ) ) {
- $msg = $this->mParams['help-message'];
- $helptext = wfMsgExt( $msg, 'parseinline' );
- if ( wfEmptyMsg( $msg ) ) {
- # Never mind
- $helptext = null;
+ $msg = wfMessage( $this->mParams['help-message'] );
+ if ( $msg->exists() ) {
+ $helptext = $msg->parse();
}
} elseif ( isset( $this->mParams['help-messages'] ) ) {
# help-message can be passed a message key (string) or an array containing
# a message key and additional parameters. This makes it impossible to pass
# an array of message key
- foreach( $this->mParams['help-messages'] as $msg ) {
- $candidate = wfMsgExt( $msg, 'parseinline' );
- if( wfEmptyMsg( $msg ) ) {
- $candidate = null;
+ foreach( $this->mParams['help-messages'] as $name ) {
+ $msg = wfMessage( $name );
+ if( $msg->exists() ) {
+ $helptext .= $msg->parse(); // append message
}
- $helptext .= $candidate; // append message
- }
+ }
} elseif ( isset( $this->mParams['help'] ) ) {
$helptext = $this->mParams['help'];
}
if ( empty( $this->mParams['tooltip'] ) ) {
return array();
}
-
- global $wgUser;
-
- return $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mParams['tooltip'] );
+ return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
}
/**
return ' ';
}
+ /**
+ * @param $request WebRequest
+ * @return String
+ */
function loadDataFromRequest( $request ) {
$invert = false;
if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
$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 ){
+ foreach( $this->mParams['options'] as &$opt ){
if( is_int( $opt ) ){
$opt = strval( $opt );
}
function __construct( $params ) {
if ( !in_array( 'other', $params['options'], true ) ) {
- $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
+ $msg = isset( $params['other'] ) ? $params['other'] : wfMsg( 'htmlform-selectorother-other' );
+ $params['options'][$msg] = 'other';
}
parent::__construct( $params );
return "$select<br />\n$textbox";
}
+ /**
+ * @param $request WebRequest
+ * @return String
+ */
function loadDataFromRequest( $request ) {
if ( $request->getCheck( $this->mName ) ) {
$val = $request->getText( $this->mName );
return $html;
}
+ /**
+ * @param $request WebRequest
+ * @return String
+ */
function loadDataFromRequest( $request ) {
if ( $this->mParent->getMethod() == 'post' ) {
if( $request->wasPosted() ){
# field, is it because the user has not yet submitted the form, or that they
# have submitted it with all the options unchecked? We will have to assume the
# latter, which basically means that you can't specify 'positive' defaults
- # for GET forms. FIXME...
+ # for GET forms.
+ # @todo FIXME...
return $request->getArray( $this->mName, array() );
}
}
* Plus a text field underneath for an additional reason. The 'value' of the field is
* ""<select>: <extra reason>"", or "<extra reason>" if nothing has been selected in the
* select dropdown.
- * FIXME: If made 'required', only the text field should be compulsory.
+ * @todo FIXME: If made 'required', only the text field should be compulsory.
*/
class HTMLSelectAndOtherField extends HTMLSelectField {
function __construct( $params ) {
if ( array_key_exists( 'other', $params ) ) {
} elseif( array_key_exists( 'other-message', $params ) ){
- $params['other'] = wfMsg( $params['other-message'] );
+ $params['other'] = wfMessage( $params['other-message'] )->escaped();
} else {
- $params['other'] = wfMsg( 'htmlform-selectorother-other' );
+ $params['other'] = null;
}
if ( array_key_exists( 'options', $params ) ) {
# Options array already specified
} elseif( array_key_exists( 'options-message', $params ) ){
# Generate options array from a system message
- $params['options'] = self::parseMessage( wfMsg( $params['options-message'], $params['other'] ) );
+ $params['options'] = self::parseMessage(
+ wfMessage( $params['options-message'] )->inContentLanguage()->escaped(),
+ $params['other']
+ );
} else {
# Sulk
throw new MWException( 'HTMLSelectAndOtherField called without any options' );
*/
public static function parseMessage( $string, $otherName=null ) {
if( $otherName === null ){
- $otherName = wfMsg( 'htmlform-selectorother-other' );
+ $otherName = wfMessage( 'htmlform-selectorother-other' )->escaped();
}
$optgroup = false;
} 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];
}
}
}
$msg->inContentLanguage();
-
-
+
+
return '<tr><td></td><td class="mw-input">'
. '<div class="mw-editTools">'
. $msg->parseAsBlock()