MediaWiki  1.32.0
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  public 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 += [
70  'title' => Linker::titleAttrib( $this->mSubmitTooltip ),
71  'accessKey' => Linker::accesskey( $this->mSubmitTooltip ),
72  ];
73  }
74 
75  $attribs['classes'] = [ 'mw-htmlform-submit' ];
76  $attribs['type'] = 'submit';
77  $attribs['label'] = $this->getSubmitText();
78  $attribs['value'] = $this->getSubmitText();
79  $attribs['flags'] = $this->mSubmitFlags;
80  $attribs['useInputTag'] = $isBadIE;
81 
82  $buttons .= new OOUI\ButtonInputWidget( $attribs );
83  }
84 
85  if ( $this->mShowReset ) {
86  $buttons .= new OOUI\ButtonInputWidget( [
87  'type' => 'reset',
88  'label' => $this->msg( 'htmlform-reset' )->text(),
89  'useInputTag' => $isBadIE,
90  ] );
91  }
92 
93  if ( $this->mShowCancel ) {
94  $target = $this->mCancelTarget ?: Title::newMainPage();
95  if ( $target instanceof Title ) {
96  $target = $target->getLocalURL();
97  }
98  $buttons .= new OOUI\ButtonWidget( [
99  'label' => $this->msg( 'cancel' )->text(),
100  'href' => $target,
101  ] );
102  }
103 
104  foreach ( $this->mButtons as $button ) {
105  $attrs = [];
106 
107  if ( $button['attribs'] ) {
108  $attrs += $button['attribs'];
109  }
110 
111  if ( isset( $button['id'] ) ) {
112  $attrs['id'] = $button['id'];
113  }
114 
115  if ( $isBadIE ) {
116  $label = $button['value'];
117  } elseif ( isset( $button['label-message'] ) ) {
118  $label = new OOUI\HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
119  } elseif ( isset( $button['label'] ) ) {
120  $label = $button['label'];
121  } elseif ( isset( $button['label-raw'] ) ) {
122  $label = new OOUI\HtmlSnippet( $button['label-raw'] );
123  } else {
124  $label = $button['value'];
125  }
126 
127  $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
128 
129  $buttons .= new OOUI\ButtonInputWidget( [
130  'type' => 'submit',
131  'name' => $button['name'],
132  'value' => $button['value'],
133  'label' => $label,
134  'flags' => $button['flags'],
135  'framed' => $button['framed'],
136  'useInputTag' => $isBadIE,
137  ] + $attrs );
138  }
139 
140  if ( !$buttons ) {
141  return '';
142  }
143 
144  return Html::rawElement( 'div',
145  [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
146  }
147 
148  protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
149  // to get a user visible effect, wrap the fieldset into a framed panel layout
150  $layout = new OOUI\PanelLayout( [
151  'expanded' => false,
152  'padded' => true,
153  'framed' => true,
154  'infusable' => false,
155  ] );
156 
157  $layout->appendContent(
158  new OOUI\FieldsetLayout( [
159  'label' => $legend,
160  'infusable' => false,
161  'items' => [
162  new OOUI\Widget( [
163  'content' => new OOUI\HtmlSnippet( $section )
164  ] ),
165  ],
166  ] + $attributes )
167  );
168  return $layout;
169  }
170 
178  protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
179  if ( !$fieldsHtml ) {
180  // Do not generate any wrappers for empty sections. Sections may be empty if they only have
181  // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
182  return '';
183  }
184 
185  $html = implode( '', $fieldsHtml );
186 
187  if ( $sectionName ) {
189  'div',
190  [ 'id' => Sanitizer::escapeIdForAttribute( $sectionName ) ],
191  $html
192  );
193  }
194  return $html;
195  }
196 
202  public function getErrorsOrWarnings( $elements, $elementsType ) {
203  if ( $elements === '' ) {
204  return '';
205  }
206 
207  if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
208  throw new DomainException( $elementsType . ' is not a valid type.' );
209  }
210  $errors = [];
211  if ( $elements instanceof Status ) {
212  if ( !$elements->isGood() ) {
213  $errors = $elements->getErrorsByType( $elementsType );
214  foreach ( $errors as &$error ) {
215  // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ]
216  // Output: [ 'foo', 'a', 'b', 'c' ]
217  $error = array_merge( [ $error['message'] ], $error['params'] );
218  }
219  }
220  } elseif ( $elementsType === 'error' ) {
221  if ( is_array( $elements ) ) {
222  $errors = $elements;
223  } elseif ( is_string( $elements ) ) {
224  $errors = [ $elements ];
225  }
226  }
227 
228  foreach ( $errors as &$error ) {
229  $error = $this->getMessage( $error )->parse();
230  $error = new OOUI\HtmlSnippet( $error );
231  }
232 
233  // Used in getBody()
234  if ( $elementsType === 'error' ) {
235  $this->oouiErrors = $errors;
236  } else {
237  $this->oouiWarnings = $errors;
238  }
239  return '';
240  }
241 
242  public function getHeaderText( $section = null ) {
243  if ( is_null( $section ) ) {
244  // We handle $this->mHeader elsewhere, in getBody()
245  return '';
246  } else {
247  return parent::getHeaderText( $section );
248  }
249  }
250 
251  public function getBody() {
252  $html = parent::getBody();
253  if ( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) {
254  $classes = [ 'mw-htmlform-ooui-header' ];
255  if ( $this->oouiErrors ) {
256  $classes[] = 'mw-htmlform-ooui-header-errors';
257  }
258  if ( $this->oouiWarnings ) {
259  $classes[] = 'mw-htmlform-ooui-header-warnings';
260  }
261  // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
262  if ( $this->mHeader ) {
263  $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
264  } else {
265  $element = new OOUI\Widget( [] );
266  }
267  $html = new OOUI\FieldLayout(
268  $element,
269  [
270  'align' => 'top',
271  'errors' => $this->oouiErrors,
272  'notices' => $this->oouiWarnings,
273  'classes' => $classes,
274  ]
275  ) . $html;
276  }
277  return $html;
278  }
279 
280  public function wrapForm( $html ) {
281  if ( is_string( $this->mWrapperLegend ) ) {
282  $content = new OOUI\FieldsetLayout( [
283  'label' => $this->mWrapperLegend,
284  'items' => [
285  new OOUI\Widget( [
286  'content' => new OOUI\HtmlSnippet( $html )
287  ] ),
288  ],
289  ] );
290  } else {
291  $content = new OOUI\HtmlSnippet( $html );
292  }
293 
294  $form = new OOUI\FormLayout( $this->getFormAttributes() + [
295  'classes' => [ 'mw-htmlform', 'mw-htmlform-ooui' ],
296  'content' => $content,
297  ] );
298 
299  // Include a wrapper for style, if requested.
300  $form = new OOUI\PanelLayout( [
301  'classes' => [ 'mw-htmlform-ooui-wrapper' ],
302  'expanded' => false,
303  'padded' => $this->mWrapperLegend !== false,
304  'framed' => $this->mWrapperLegend !== false,
305  'content' => $form,
306  ] );
307 
308  return $form;
309  }
310 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:33
ContextSource\msg
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
Title\newMainPage
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:597
HTMLForm\$mSubmitID
$mSubmitID
Definition: HTMLForm.php:207
OOUIHTMLForm\formatSection
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
Definition: OOUIHTMLForm.php:178
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:71
OOUIHTMLForm\getButtons
getButtons()
Get the submit and (potentially) reset buttons.
Definition: OOUIHTMLForm.php:51
OOUIHTMLForm\__construct
__construct( $descriptor, $context=null, $messagePrefix='')
Build a new HTMLForm from an array of field attributes.
Definition: OOUIHTMLForm.php:31
HTMLForm\$mSubmitName
$mSubmitName
Definition: HTMLForm.php:208
php
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
OOUIHTMLForm\getErrorsOrWarnings
getErrorsOrWarnings( $elements, $elementsType)
Definition: OOUIHTMLForm.php:202
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
OOUIHTMLForm
Compact stacked vertical format for forms, implemented using OOUI widgets.
Definition: OOUIHTMLForm.php:27
HTMLForm\$mSubmitFlags
$mSubmitFlags
Definition: HTMLForm.php:190
$html
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:2036
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
$attribs
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:2036
OOUIHTMLForm\wrapForm
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
Definition: OOUIHTMLForm.php:280
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
OOUIHTMLForm\loadInputFromParameters
static loadInputFromParameters( $fieldname, $descriptor, HTMLForm $parent=null)
Initialise a new Object for the field.
Definition: OOUIHTMLForm.php:43
HTMLForm\getFormAttributes
getFormAttributes()
Get HTML attributes for the <form> tag.
Definition: HTMLForm.php:1058
OOUIHTMLForm\$displayFormat
string $displayFormat
Symbolic display format name.
Definition: OOUIHTMLForm.php:41
Linker\titleAttrib
static titleAttrib( $name, $options=null, array $msgParams=[])
Given the id of an interface element, constructs the appropriate title attribute from the system mess...
Definition: Linker.php:1967
OOUIHTMLForm\$oouiErrors
$oouiErrors
Definition: OOUIHTMLForm.php:28
text
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
Definition: All_system_messages.txt:1267
$parent
$parent
Definition: pageupdater.txt:71
Title
Represents a title within MediaWiki.
Definition: Title.php:39
HTMLForm\getSubmitText
getSubmitText()
Get the text for the submit button, either customised or a default.
Definition: HTMLForm.php:1389
HTMLForm\getMessage
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLForm.php:1906
$section
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:3090
as
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
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
OOUIHTMLForm\$oouiWarnings
$oouiWarnings
Definition: OOUIHTMLForm.php:29
true
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:2036
$content
$content
Definition: pageupdater.txt:72
OOUIHTMLForm\getBody
getBody()
Get the whole body of the form.
Definition: OOUIHTMLForm.php:251
Linker\accesskey
static accesskey( $name)
Given the id of an interface element, constructs the appropriate accesskey attribute from the system ...
Definition: Linker.php:2015
OOUIHTMLForm\getHeaderText
getHeaderText( $section=null)
Get header text.
Definition: OOUIHTMLForm.php:242
OOUIHTMLForm\wrapFieldSetSection
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into an user-visible fieldset.
Definition: OOUIHTMLForm.php:148
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition: HTMLForm.php:136