Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / htmlform / OOUIHTMLForm.php
blob0b227278a329885c33dbddc063546e4af51aac1e
1 <?php
3 /**
4 * HTML form generation and submission handling, OOUI style.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
24 /**
25 * Compact stacked vertical format for forms, implemented using OOUI widgets.
27 class OOUIHTMLForm extends HTMLForm {
28 private $oouiErrors;
30 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
31 parent::__construct( $descriptor, $context, $messagePrefix );
32 $this->getOutput()->enableOOUI();
33 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
36 /**
37 * Symbolic display format name.
38 * @var string
40 protected $displayFormat = 'ooui';
42 public static function loadInputFromParameters( $fieldname, $descriptor,
43 HTMLForm $parent = null
44 ) {
45 $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
46 $field->setShowEmptyLabel( false );
47 return $field;
50 function getButtons() {
51 $buttons = '';
53 // IE<8 has bugs with <button>, so we'll need to avoid them.
54 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
56 if ( $this->mShowSubmit ) {
57 $attribs = [ 'infusable' => true ];
59 if ( isset( $this->mSubmitID ) ) {
60 $attribs['id'] = $this->mSubmitID;
63 if ( isset( $this->mSubmitName ) ) {
64 $attribs['name'] = $this->mSubmitName;
67 if ( isset( $this->mSubmitTooltip ) ) {
68 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
71 $attribs['classes'] = [ 'mw-htmlform-submit' ];
72 $attribs['type'] = 'submit';
73 $attribs['label'] = $this->getSubmitText();
74 $attribs['value'] = $this->getSubmitText();
75 $attribs['flags'] = $this->mSubmitFlags;
76 $attribs['useInputTag'] = $isBadIE;
78 $buttons .= new OOUI\ButtonInputWidget( $attribs );
81 if ( $this->mShowReset ) {
82 $buttons .= new OOUI\ButtonInputWidget( [
83 'type' => 'reset',
84 'label' => $this->msg( 'htmlform-reset' )->text(),
85 'useInputTag' => $isBadIE,
86 ] );
89 if ( $this->mShowCancel ) {
90 $target = $this->mCancelTarget ?: Title::newMainPage();
91 if ( $target instanceof Title ) {
92 $target = $target->getLocalURL();
94 $buttons .= new OOUI\ButtonWidget( [
95 'label' => $this->msg( 'cancel' )->text(),
96 'href' => $target,
97 ] );
100 foreach ( $this->mButtons as $button ) {
101 $attrs = [];
103 if ( $button['attribs'] ) {
104 $attrs += $button['attribs'];
107 if ( isset( $button['id'] ) ) {
108 $attrs['id'] = $button['id'];
111 if ( $isBadIE ) {
112 $label = $button['value'];
113 } elseif ( isset( $button['label-message'] ) ) {
114 $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
115 } elseif ( isset( $button['label'] ) ) {
116 $label = $button['label'];
117 } elseif ( isset( $button['label-raw'] ) ) {
118 $label = new OOUI\HtmlSnippet( $button['label-raw'] );
119 } else {
120 $label = $button['value'];
123 $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
125 $buttons .= new OOUI\ButtonInputWidget( [
126 'type' => 'submit',
127 'name' => $button['name'],
128 'value' => $button['value'],
129 'label' => $label,
130 'flags' => $button['flags'],
131 'framed' => $button['framed'],
132 'useInputTag' => $isBadIE,
133 ] + $attrs );
136 if ( !$buttons ) {
137 return '';
140 return Html::rawElement( 'div',
141 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
144 protected function wrapFieldSetSection( $legend, $section, $attributes ) {
145 // to get a user visible effect, wrap the fieldset into a framed panel layout
146 $layout = new OOUI\PanelLayout( [
147 'expanded' => false,
148 'padded' => true,
149 'framed' => true,
150 'infusable' => false,
151 ] );
153 $layout->appendContent(
154 new OOUI\FieldsetLayout( [
155 'label' => $legend,
156 'infusable' => false,
157 'items' => [
158 new OOUI\Widget( [
159 'content' => new OOUI\HtmlSnippet( $section )
160 ] ),
162 ] + $attributes )
164 return $layout;
168 * Put a form section together from the individual fields' HTML, merging it and wrapping.
169 * @param OOUI\FieldLayout[] $fieldsHtml
170 * @param string $sectionName
171 * @param bool $anyFieldHasLabel Unused
172 * @return string HTML
174 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
175 $config = [
176 'items' => $fieldsHtml,
178 if ( $sectionName ) {
179 $config['id'] = Sanitizer::escapeId( $sectionName );
181 if ( is_string( $this->mWrapperLegend ) ) {
182 $config['label'] = $this->mWrapperLegend;
184 return new OOUI\FieldsetLayout( $config );
188 * @param string|array|Status $err
189 * @return string
191 function getErrors( $err ) {
192 if ( !$err ) {
193 $errors = [];
194 } elseif ( $err instanceof Status ) {
195 if ( $err->isOK() ) {
196 $errors = [];
197 } else {
198 $errors = $err->getErrorsByType( 'error' );
199 foreach ( $errors as &$error ) {
200 // Input: array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) )
201 // Output: array( 'foo', 'a', 'b', 'c' )
202 $error = array_merge( [ $error['message'] ], $error['params'] );
205 } else {
206 $errors = $err;
207 if ( !is_array( $errors ) ) {
208 $errors = [ $errors ];
212 foreach ( $errors as &$error ) {
213 $error = $this->getMessage( $error )->parse();
214 $error = new OOUI\HtmlSnippet( $error );
217 // Used in getBody()
218 $this->oouiErrors = $errors;
219 return '';
222 function getHeaderText( $section = null ) {
223 if ( is_null( $section ) ) {
224 // We handle $this->mHeader elsewhere, in getBody()
225 return '';
226 } else {
227 return parent::getHeaderText( $section );
231 function getBody() {
232 $fieldset = parent::getBody();
233 // FIXME This only works for forms with no subsections
234 if ( $fieldset instanceof OOUI\FieldsetLayout ) {
235 $classes = [ 'mw-htmlform-ooui-header' ];
236 if ( $this->oouiErrors ) {
237 $classes[] = 'mw-htmlform-ooui-header-errors';
239 if ( $this->mHeader || $this->oouiErrors ) {
240 // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
241 if ( $this->mHeader ) {
242 $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
243 } else {
244 $element = new OOUI\Widget( [] );
246 $fieldset->addItems( [
247 new OOUI\FieldLayout(
248 $element,
250 'align' => 'top',
251 'errors' => $this->oouiErrors,
252 'classes' => $classes,
255 ], 0 );
258 return $fieldset;
261 function wrapForm( $html ) {
262 $form = new OOUI\FormLayout( $this->getFormAttributes() + [
263 'classes' => [ 'mw-htmlform-ooui' ],
264 'content' => new OOUI\HtmlSnippet( $html ),
265 ] );
267 // Include a wrapper for style, if requested.
268 $form = new OOUI\PanelLayout( [
269 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
270 'expanded' => false,
271 'padded' => $this->mWrapperLegend !== false,
272 'framed' => $this->mWrapperLegend !== false,
273 'content' => $form,
274 ] );
276 return $form;