protected $mSubmitName;
protected $mSubmitText;
protected $mSubmitTooltip;
+
+ protected $mContext; // <! RequestContext
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 RequestContext available since 1.18, will become compulsory in 1.19.
+ * 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, /*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();
} 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;
* @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;
-
$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 ( isset( $this->mSubmitTooltip ) ) {
- global $wgUser;
- $attribs += $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mSubmitTooltip );
+ $attribs += Linker::tooltipAndAccessKeyAttribs( $this->mSubmitTooltip );
}
$attribs['class'] = 'mw-htmlform-submit';
*/
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 );
$errorstr .= Html::rawElement(
'li',
- null,
+ array(),
wfMsgExt( $msg, array( 'parseinline' ), $error )
);
}
* @return Title
*/
function getTitle() {
- return $this->mTitle;
+ return $this->mTitle === false
+ ? $this->getContext()->title
+ : $this->mTitle;
+ }
+
+ public function getContext(){
+ return $this->mContext instanceof RequestContext
+ ? $this->mContext
+ : RequestContext::getMain();
+ }
+
+ public function getOutput(){
+ return $this->getContext()->output;
+ }
+
+ public function getRequest(){
+ return $this->getContext()->request;
+ }
+
+ public function getUser(){
+ return $this->getContext()->user;
}
/**
* 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() );
}
}
*/
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 {
if ( empty( $this->mParams['tooltip'] ) ) {
return array();
}
-
- global $wgUser;
-
- return $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mParams['tooltip'] );
+ return Linker::tooltipAndAccessKeyAttribs( $this->mParams['tooltip'] );
}
/**
$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 ){
if( is_int( $opt ) ){
} 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];
}