+ static function formatErrors( $errors ) {
+ $errorstr = '';
+ foreach ( $errors as $error ) {
+ if( is_array( $error ) ) {
+ $msg = array_shift( $error );
+ } else {
+ $msg = $error;
+ $error = array();
+ }
+ $errorstr .= Xml::tags(
+ 'li',
+ null,
+ wfMsgExt( $msg, array( 'parseinline' ), $error )
+ );
+ }
+
+ $errorstr = Xml::tags( 'ul', null, $errorstr );
+
+ return $errorstr;
+ }
+
+ function setSubmitText( $t ) {
+ $this->mSubmitText = $t;
+ }
+
+ function getSubmitText() {
+ return isset( $this->mSubmitText ) ? $this->mSubmitText : wfMsg( 'htmlform-submit' );
+ }
+
+ function setSubmitID( $t ) {
+ $this->mSubmitID = $t;
+ }
+
+ function setMessagePrefix( $p ) {
+ $this->mMessagePrefix = $p;
+ }
+
+ function setTitle( $t ) {
+ $this->mTitle = $t;
+ }
+
+ function getTitle() {
+ return $this->mTitle;
+ }
+
+ function displaySection( $fields ) {
+ $tableHtml = '';
+ $subsectionHtml = '';
+ $hasLeftColumn = false;
+
+ foreach( $fields as $key => $value ) {
+ if ( is_object( $value ) ) {
+ $v = empty( $value->mParams['nodata'] )
+ ? $this->mFieldData[$key]
+ : $value->getDefault();
+ $tableHtml .= $value->getTableRow( $v );
+
+ if( $value->getLabel() != ' ' )
+ $hasLeftColumn = true;
+ } elseif ( is_array( $value ) ) {
+ $section = $this->displaySection( $value );
+ $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
+ $subsectionHtml .= Xml::fieldset( $legend, $section ) . "\n";
+ }
+ }
+
+ $classes = array();
+ if( !$hasLeftColumn ) // Avoid strange spacing when no labels exist
+ $classes[] = 'mw-htmlform-nolabel';
+ $classes = implode( ' ', $classes );
+
+ $tableHtml = "<table class='$classes'><tbody>\n$tableHtml\n</tbody></table>\n";
+
+ return $subsectionHtml . "\n" . $tableHtml;
+ }
+
+ function loadData() {
+ global $wgRequest;
+
+ $fieldData = array();
+
+ foreach( $this->mFlatFields as $fieldname => $field ) {
+ if ( !empty( $field->mParams['nodata'] ) ) continue;
+ if ( !empty( $field->mParams['disabled'] ) ) {
+ $fieldData[$fieldname] = $field->getDefault();
+ } else {
+ $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
+ }
+ }
+
+ // Filter data.
+ foreach( $fieldData as $name => &$value ) {
+ $field = $this->mFlatFields[$name];
+ $value = $field->filter( $value, $this->mFlatFields );
+ }
+
+ $this->mFieldData = $fieldData;
+ }
+
+ function importData( $fieldData ) {
+ // Filter data.
+ foreach( $fieldData as $name => &$value ) {
+ $field = $this->mFlatFields[$name];
+ $value = $field->filter( $value, $this->mFlatFields );
+ }
+
+ foreach( $this->mFlatFields as $fieldname => $field ) {
+ if ( !isset( $fieldData[$fieldname] ) )
+ $fieldData[$fieldname] = $field->getDefault();
+ }
+
+ $this->mFieldData = $fieldData;
+ }
+
+ function suppressReset( $suppressReset = true ) {
+ $this->mShowReset = !$suppressReset;
+ }
+
+ function filterDataForSubmit( $data ) {
+ return $data;
+ }
+}
+
+abstract class HTMLFormField {
+ abstract function getInputHTML( $value );
+
+ function validate( $value, $alldata ) {
+ if ( isset( $this->mValidationCallback ) ) {
+ return call_user_func( $this->mValidationCallback, $value, $alldata );
+ }
+
+ return true;
+ }
+
+ function filter( $value, $alldata ) {
+ if( isset( $this->mFilterCallback ) ) {
+ $value = call_user_func( $this->mFilterCallback, $value, $alldata );
+ }
+
+ return $value;
+ }
+
+ function loadDataFromRequest( $request ) {
+ if( $request->getCheck( $this->mName ) ) {
+ return $request->getText( $this->mName );
+ } else {
+ return $this->getDefault();
+ }
+ }
+
+ function __construct( $params ) {
+ $this->mParams = $params;
+
+ if( isset( $params['label-message'] ) ) {
+ $msgInfo = $params['label-message'];
+
+ if ( is_array( $msgInfo ) ) {
+ $msg = array_shift( $msgInfo );
+ } else {
+ $msg = $msgInfo;
+ $msgInfo = array();
+ }
+
+ $this->mLabel = wfMsgExt( $msg, 'parseinline', $msgInfo );
+ } elseif ( isset( $params['label'] ) ) {
+ $this->mLabel = $params['label'];
+ }
+
+ if ( isset( $params['name'] ) ) {
+ $name = $params['name'];
+ $validName = Sanitizer::escapeId( $name );
+ if( $name != $validName ) {
+ throw new MWException("Invalid name '$name' passed to " . __METHOD__ );
+ }
+ $this->mName = 'wp'.$name;
+ $this->mID = 'mw-input-'.$name;
+ }
+
+ if ( isset( $params['default'] ) ) {
+ $this->mDefault = $params['default'];
+ }
+
+ if ( isset( $params['id'] ) ) {
+ $id = $params['id'];
+ $validId = Sanitizer::escapeId( $id );
+ if( $id != $validId ) {
+ throw new MWException("Invalid id '$id' passed to " . __METHOD__ );
+ }
+ $this->mID = $id;
+ }
+
+ if ( isset( $params['validation-callback'] ) ) {
+ $this->mValidationCallback = $params['validation-callback'];
+ }
+
+ if ( isset( $params['filter-callback'] ) ) {
+ $this->mFilterCallback = $params['filter-callback'];
+ }
+ }
+
+ function getTableRow( $value ) {
+ // Check for invalid data.
+ global $wgRequest;
+
+ $errors = $this->validate( $value, $this->mParent->mFieldData );
+ if ( $errors === true || !$wgRequest->wasPosted() ) {
+ $errors = '';
+ } else {
+ $errors = Xml::tags( 'span', array( 'class' => 'error' ), $errors );
+ }
+
+ $html = '';
+
+ $html .= Xml::tags( 'td', array( 'class' => 'mw-label' ),
+ Xml::tags( 'label', array( 'for' => $this->mID ), $this->getLabel() )
+ );
+ $html .= Xml::tags( 'td', array( 'class' => 'mw-input' ),
+ $this->getInputHTML( $value ) ."\n$errors" );
+
+ $fieldType = get_class( $this );
+
+ $html = Xml::tags( 'tr', array( 'class' => "mw-htmlform-field-$fieldType" ),
+ $html ) . "\n";
+
+ $helptext = null;
+ if ( isset( $this->mParams['help-message'] ) ) {
+ $msg = $this->mParams['help-message'];
+ $helptext = wfMsgExt( $msg, 'parseinline' );
+ if ( wfEmptyMsg( $msg, $helptext ) ) {
+ # Never mind
+ $helptext = null;