Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / htmlform / fields / HTMLButtonField.php
bloba19bd5a1e455b84cfb4e9a0e9227091985e05ad0
1 <?php
3 /**
4 * Adds a generic button inline to the form. Does not do anything, you must add
5 * click handling code in JavaScript. Use a HTMLSubmitField if you merely
6 * wish to add a submit button to a form.
8 * Additional recognized configuration parameters include:
9 * - flags: OOUI flags for the button, see OOUI\FlaggedElement
10 * - buttonlabel-message: Message to use for the button display text, instead
11 * of the value from 'default'. Overrides 'buttonlabel' and 'buttonlabel-raw'.
12 * - buttonlabel: Text to display for the button display text, instead
13 * of the value from 'default'. Overrides 'buttonlabel-raw'.
14 * - buttonlabel-raw: HTMLto display for the button display text, instead
15 * of the value from 'default'.
16 * - formnovalidate: Set to true if clicking this button should suppress
17 * client-side form validation. Used in HTMLFormFieldCloner for add/remove
18 * buttons.
20 * Note that the buttonlabel parameters are not supported on IE6 and IE7 due to
21 * bugs in those browsers. If detected, they will be served buttons using the
22 * value of 'default' as the button label.
24 * @since 1.22
26 class HTMLButtonField extends HTMLFormField {
27 protected $buttonType = 'button';
28 protected $buttonLabel = null;
30 /** @var array $mFlags Flags to add to OOUI Button widget */
31 protected $mFlags = [];
33 protected $mFormnovalidate = false;
35 public function __construct( $info ) {
36 $info['nodata'] = true;
37 if ( isset( $info['flags'] ) ) {
38 $this->mFlags = $info['flags'];
41 if ( isset( $info['formnovalidate'] ) ) {
42 $this->mFormnovalidate = $info['formnovalidate'];
45 # Generate the label from a message, if possible
46 if ( isset( $info['buttonlabel-message'] ) ) {
47 $this->buttonLabel = $this->getMessage( $info['buttonlabel-message'] )->parse();
48 } elseif ( isset( $info['buttonlabel'] ) ) {
49 if ( $info['buttonlabel'] === '&#160;' ) {
50 // Apparently some things set &nbsp directly and in an odd format
51 $this->buttonLabel = '&#160;';
52 } else {
53 $this->buttonLabel = htmlspecialchars( $info['buttonlabel'] );
55 } elseif ( isset( $info['buttonlabel-raw'] ) ) {
56 $this->buttonLabel = $info['buttonlabel-raw'];
59 $this->setShowEmptyLabel( false );
61 parent::__construct( $info );
64 public function getInputHTML( $value ) {
65 $flags = '';
66 $prefix = 'mw-htmlform-';
67 if ( $this->mParent instanceof VFormHTMLForm ||
68 $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' )
69 ) {
70 $prefix = 'mw-ui-';
71 // add mw-ui-button separately, so the descriptor doesn't need to set it
72 $flags .= ' ' . $prefix . 'button';
74 foreach ( $this->mFlags as $flag ) {
75 $flags .= ' ' . $prefix . $flag;
77 $attr = [
78 'class' => 'mw-htmlform-submit ' . $this->mClass . $flags,
79 'id' => $this->mID,
80 'type' => $this->buttonType,
81 'name' => $this->mName,
82 'value' => $this->getDefault(),
83 'formnovalidate' => $this->mFormnovalidate,
84 ] + $this->getAttributes( [ 'disabled', 'tabindex' ] );
86 if ( $this->isBadIE() ) {
87 return Html::element( 'input', $attr );
88 } else {
89 return Html::rawElement( 'button', $attr,
90 $this->buttonLabel ?: htmlspecialchars( $this->getDefault() ) );
94 /**
95 * Get the OOUI widget for this field.
96 * @param string $value
97 * @return OOUI\ButtonInputWidget
99 public function getInputOOUI( $value ) {
100 return new OOUI\ButtonInputWidget( [
101 'name' => $this->mName,
102 'value' => $this->getDefault(),
103 'label' => !$this->isBadIE() && $this->buttonLabel
104 ? new OOUI\HtmlSnippet( $this->buttonLabel )
105 : $this->getDefault(),
106 'type' => $this->buttonType,
107 'classes' => [ 'mw-htmlform-submit', $this->mClass ],
108 'id' => $this->mID,
109 'flags' => $this->mFlags,
110 'useInputTag' => $this->isBadIE(),
111 ] + OOUI\Element::configFromHtmlAttributes(
112 $this->getAttributes( [ 'disabled', 'tabindex' ] )
113 ) );
116 protected function needsLabel() {
117 return false;
121 * Button cannot be invalid
123 * @param string $value
124 * @param array $alldata
126 * @return bool|string|Message
128 public function validate( $value, $alldata ) {
129 return true;
133 * IE<8 has bugs with <button>, so we'll need to avoid them.
134 * @return bool Whether the request is from a bad version of IE
136 private function isBadIE() {
137 $request = $this->mParent
138 ? $this->mParent->getRequest()
139 : RequestContext::getMain()->getRequest();
140 return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );