MediaWiki  1.28.1
OOUIHTMLForm.php
Go to the documentation of this file.
1 <?php
2 
27 class OOUIHTMLForm extends HTMLForm {
28  private $oouiErrors;
29  private $oouiWarnings;
30 
31  public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
32  parent::__construct( $descriptor, $context, $messagePrefix );
33  $this->getOutput()->enableOOUI();
34  $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
35  }
36 
41  protected $displayFormat = 'ooui';
42 
43  public static function loadInputFromParameters( $fieldname, $descriptor,
44  HTMLForm $parent = null
45  ) {
46  $field = parent::loadInputFromParameters( $fieldname, $descriptor, $parent );
47  $field->setShowEmptyLabel( false );
48  return $field;
49  }
50 
51  function getButtons() {
52  $buttons = '';
53 
54  // IE<8 has bugs with <button>, so we'll need to avoid them.
55  $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
56 
57  if ( $this->mShowSubmit ) {
58  $attribs = [ 'infusable' => true ];
59 
60  if ( isset( $this->mSubmitID ) ) {
61  $attribs['id'] = $this->mSubmitID;
62  }
63 
64  if ( isset( $this->mSubmitName ) ) {
65  $attribs['name'] = $this->mSubmitName;
66  }
67 
68  if ( isset( $this->mSubmitTooltip ) ) {
69  $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
70  }
71 
72  $attribs['classes'] = [ 'mw-htmlform-submit' ];
73  $attribs['type'] = 'submit';
74  $attribs['label'] = $this->getSubmitText();
75  $attribs['value'] = $this->getSubmitText();
76  $attribs['flags'] = $this->mSubmitFlags;
77  $attribs['useInputTag'] = $isBadIE;
78 
79  $buttons .= new OOUI\ButtonInputWidget( $attribs );
80  }
81 
82  if ( $this->mShowReset ) {
83  $buttons .= new OOUI\ButtonInputWidget( [
84  'type' => 'reset',
85  'label' => $this->msg( 'htmlform-reset' )->text(),
86  'useInputTag' => $isBadIE,
87  ] );
88  }
89 
90  if ( $this->mShowCancel ) {
91  $target = $this->mCancelTarget ?: Title::newMainPage();
92  if ( $target instanceof Title ) {
93  $target = $target->getLocalURL();
94  }
95  $buttons .= new OOUI\ButtonWidget( [
96  'label' => $this->msg( 'cancel' )->text(),
97  'href' => $target,
98  ] );
99  }
100 
101  foreach ( $this->mButtons as $button ) {
102  $attrs = [];
103 
104  if ( $button['attribs'] ) {
105  $attrs += $button['attribs'];
106  }
107 
108  if ( isset( $button['id'] ) ) {
109  $attrs['id'] = $button['id'];
110  }
111 
112  if ( $isBadIE ) {
113  $label = $button['value'];
114  } elseif ( isset( $button['label-message'] ) ) {
115  $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
116  } elseif ( isset( $button['label'] ) ) {
117  $label = $button['label'];
118  } elseif ( isset( $button['label-raw'] ) ) {
119  $label = new OOUI\HtmlSnippet( $button['label-raw'] );
120  } else {
121  $label = $button['value'];
122  }
123 
124  $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
125 
126  $buttons .= new OOUI\ButtonInputWidget( [
127  'type' => 'submit',
128  'name' => $button['name'],
129  'value' => $button['value'],
130  'label' => $label,
131  'flags' => $button['flags'],
132  'framed' => $button['framed'],
133  'useInputTag' => $isBadIE,
134  ] + $attrs );
135  }
136 
137  if ( !$buttons ) {
138  return '';
139  }
140 
141  return Html::rawElement( 'div',
142  [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
143  }
144 
145  protected function wrapFieldSetSection( $legend, $section, $attributes ) {
146  // to get a user visible effect, wrap the fieldset into a framed panel layout
147  $layout = new OOUI\PanelLayout( [
148  'expanded' => false,
149  'padded' => true,
150  'framed' => true,
151  'infusable' => false,
152  ] );
153 
154  $layout->appendContent(
155  new OOUI\FieldsetLayout( [
156  'label' => $legend,
157  'infusable' => false,
158  'items' => [
159  new OOUI\Widget( [
160  'content' => new OOUI\HtmlSnippet( $section )
161  ] ),
162  ],
163  ] + $attributes )
164  );
165  return $layout;
166  }
167 
175  protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
176  $config = [
177  'items' => $fieldsHtml,
178  ];
179  if ( $sectionName ) {
180  $config['id'] = Sanitizer::escapeId( $sectionName );
181  }
182  if ( is_string( $this->mWrapperLegend ) ) {
183  $config['label'] = $this->mWrapperLegend;
184  }
185  return new OOUI\FieldsetLayout( $config );
186  }
187 
193  function getErrorsOrWarnings( $elements, $elementsType ) {
194  if ( !in_array( $elementsType, [ 'error', 'warning' ] ) ) {
195  throw new DomainException( $elementsType . ' is not a valid type.' );
196  }
197  if ( !$elements ) {
198  $errors = [];
199  } elseif ( $elements instanceof Status ) {
200  if ( $elements->isGood() ) {
201  $errors = [];
202  } else {
203  $errors = $elements->getErrorsByType( $elementsType );
204  foreach ( $errors as &$error ) {
205  // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ]
206  // Output: [ 'foo', 'a', 'b', 'c' ]
207  $error = array_merge( [ $error['message'] ], $error['params'] );
208  }
209  }
210  } elseif ( $elementsType === 'errors' ) {
211  $errors = $elements;
212  if ( !is_array( $errors ) ) {
213  $errors = [ $errors ];
214  }
215  } else {
216  $errors = [];
217  }
218 
219  foreach ( $errors as &$error ) {
220  $error = $this->getMessage( $error )->parse();
221  $error = new OOUI\HtmlSnippet( $error );
222  }
223 
224  // Used in getBody()
225  if ( $elementsType === 'error' ) {
226  $this->oouiErrors = $errors;
227  } else {
228  $this->oouiWarnings = $errors;
229  }
230  return '';
231  }
232 
233  function getHeaderText( $section = null ) {
234  if ( is_null( $section ) ) {
235  // We handle $this->mHeader elsewhere, in getBody()
236  return '';
237  } else {
238  return parent::getHeaderText( $section );
239  }
240  }
241 
242  function getBody() {
243  $fieldset = parent::getBody();
244  // FIXME This only works for forms with no subsections
245  if ( $fieldset instanceof OOUI\FieldsetLayout ) {
246  $classes = [ 'mw-htmlform-ooui-header' ];
247  if ( $this->oouiErrors ) {
248  $classes[] = 'mw-htmlform-ooui-header-errors';
249  }
250  if ( $this->oouiWarnings ) {
251  $classes[] = 'mw-htmlform-ooui-header-warnings';
252  }
253  if ( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) {
254  // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
255  if ( $this->mHeader ) {
256  $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
257  } else {
258  $element = new OOUI\Widget( [] );
259  }
260  $fieldset->addItems( [
261  new OOUI\FieldLayout(
262  $element,
263  [
264  'align' => 'top',
265  'errors' => $this->oouiErrors,
266  'notices' => $this->oouiWarnings,
267  'classes' => $classes,
268  ]
269  )
270  ], 0 );
271  }
272  }
273  return $fieldset;
274  }
275 
276  function wrapForm( $html ) {
277  $form = new OOUI\FormLayout( $this->getFormAttributes() + [
278  'classes' => [ 'mw-htmlform-ooui' ],
279  'content' => new OOUI\HtmlSnippet( $html ),
280  ] );
281 
282  // Include a wrapper for style, if requested.
283  $form = new OOUI\PanelLayout( [
284  'classes' => [ 'mw-htmlform-ooui-wrapper' ],
285  'expanded' => false,
286  'padded' => $this->mWrapperLegend !== false,
287  'framed' => $this->mWrapperLegend !== false,
288  'content' => $form,
289  ] );
290 
291  return $form;
292  }
293 }
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1936
the array() calling protocol came about after MediaWiki 1.4rc1.
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:556
$mWrapperLegend
Definition: HTMLForm.php:224
__construct($descriptor, $context=null, $messagePrefix= '')
static rawElement($element, $attribs=[], $contents= '')
Returns an HTML element in a string.
Definition: Html.php:209
getMessage($value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a name + parameters array) into a Message.
Definition: HTMLForm.php:1869
string $displayFormat
Symbolic display format name.
getHeaderText($section=null)
IContextSource $context
static tooltipAndAccesskeyAttribs($name, array $msgParams=[])
Returns the attributes for the tooltip and access key.
Definition: Linker.php:2182
getRequest()
Get the WebRequest object.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1936
msg()
Get a Message object with context set Parameters are the same as wfMessage()
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
wrapFieldSetSection($legend, $section, $attributes)
getFormAttributes()
Get HTML attributes for the
tag.
Definition: HTMLForm.php:1037
getErrorsOrWarnings($elements, $elementsType)
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition: hooks.txt:1936
Object handling generic submission, CSRF protection, layout and other logic for UI forms...
Definition: HTMLForm.php:128
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
getSubmitText()
Get the text for the submit button, either customised or a default.
Definition: HTMLForm.php:1361
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition: hooks.txt:2889
static escapeId($id, $options=[])
Given a value, escape it so that it can be used in an id attribute and return it. ...
Definition: Sanitizer.php:1170
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static loadInputFromParameters($fieldname, $descriptor, HTMLForm $parent=null)
Compact stacked vertical format for forms, implemented using OOUI widgets.
getOutput()
Get the OutputPage object.