MediaWiki  1.29.2
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 += 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  public function getErrorsOrWarnings( $elements, $elementsType ) {
194  if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
195  throw new DomainException( $elementsType . ' is not a valid type.' );
196  }
197  $errors = [];
198  if ( $elements instanceof Status ) {
199  if ( !$elements->isGood() ) {
200  $errors = $elements->getErrorsByType( $elementsType );
201  foreach ( $errors as &$error ) {
202  // Input: [ 'message' => 'foo', 'errors' => [ 'a', 'b', 'c' ] ]
203  // Output: [ 'foo', 'a', 'b', 'c' ]
204  $error = array_merge( [ $error['message'] ], $error['params'] );
205  }
206  }
207  } elseif ( $elementsType === 'error' ) {
208  if ( is_array( $elements ) ) {
209  $errors = $elements;
210  } elseif ( is_string( $elements ) ) {
211  $errors = [ $elements ];
212  }
213  }
214 
215  foreach ( $errors as &$error ) {
216  $error = $this->getMessage( $error )->parse();
217  $error = new OOUI\HtmlSnippet( $error );
218  }
219 
220  // Used in getBody()
221  if ( $elementsType === 'error' ) {
222  $this->oouiErrors = $errors;
223  } else {
224  $this->oouiWarnings = $errors;
225  }
226  return '';
227  }
228 
229  public function getHeaderText( $section = null ) {
230  if ( is_null( $section ) ) {
231  // We handle $this->mHeader elsewhere, in getBody()
232  return '';
233  } else {
234  return parent::getHeaderText( $section );
235  }
236  }
237 
238  public function getBody() {
239  $fieldset = parent::getBody();
240  // FIXME This only works for forms with no subsections
241  if ( $fieldset instanceof OOUI\FieldsetLayout ) {
242  $classes = [ 'mw-htmlform-ooui-header' ];
243  if ( $this->oouiErrors ) {
244  $classes[] = 'mw-htmlform-ooui-header-errors';
245  }
246  if ( $this->oouiWarnings ) {
247  $classes[] = 'mw-htmlform-ooui-header-warnings';
248  }
249  if ( $this->mHeader || $this->oouiErrors || $this->oouiWarnings ) {
250  // if there's no header, don't create an (empty) LabelWidget, simply use a placeholder
251  if ( $this->mHeader ) {
252  $element = new OOUI\LabelWidget( [ 'label' => new OOUI\HtmlSnippet( $this->mHeader ) ] );
253  } else {
254  $element = new OOUI\Widget( [] );
255  }
256  $fieldset->addItems( [
257  new OOUI\FieldLayout(
258  $element,
259  [
260  'align' => 'top',
261  'errors' => $this->oouiErrors,
262  'notices' => $this->oouiWarnings,
263  'classes' => $classes,
264  ]
265  )
266  ], 0 );
267  }
268  }
269  return $fieldset;
270  }
271 
272  public function wrapForm( $html ) {
273  $form = new OOUI\FormLayout( $this->getFormAttributes() + [
274  'classes' => [ 'mw-htmlform', 'mw-htmlform-ooui' ],
275  'content' => new OOUI\HtmlSnippet( $html ),
276  ] );
277 
278  // Include a wrapper for style, if requested.
279  $form = new OOUI\PanelLayout( [
280  'classes' => [ 'mw-htmlform-ooui-wrapper' ],
281  'expanded' => false,
282  'padded' => $this->mWrapperLegend !== false,
283  'framed' => $this->mWrapperLegend !== false,
284  'content' => $form,
285  ] );
286 
287  return $form;
288  }
289 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:34
ContextSource\msg
msg()
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:187
text
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
Title\newMainPage
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:559
HTMLForm\$mWrapperLegend
$mWrapperLegend
Definition: HTMLForm.php:226
HTMLForm\$mSubmitID
$mSubmitID
Definition: HTMLForm.php:198
OOUIHTMLForm\formatSection
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
Definition: OOUIHTMLForm.php:175
ContextSource\getRequest
getRequest()
Get the WebRequest object.
Definition: ContextSource.php:78
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:199
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:193
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:181
$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:1956
ContextSource\getOutput
getOutput()
Get the OutputPage object.
Definition: ContextSource.php:123
$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:1956
OOUIHTMLForm\wrapForm
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
Definition: OOUIHTMLForm.php:272
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:1044
Linker\tooltipAndAccesskeyAttribs
static tooltipAndAccesskeyAttribs( $name, array $msgParams=[])
Returns the attributes for the tooltip and access key.
Definition: Linker.php:2098
OOUIHTMLForm\$displayFormat
string $displayFormat
Symbolic display format name.
Definition: OOUIHTMLForm.php:41
OOUIHTMLForm\$oouiErrors
$oouiErrors
Definition: OOUIHTMLForm.php:28
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:1372
HTMLForm\getMessage
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
Definition: HTMLForm.php:1880
$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:2929
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:209
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:1956
OOUIHTMLForm\getBody
getBody()
Get the whole body of the form.
Definition: OOUIHTMLForm.php:238
OOUIHTMLForm\wrapFieldSetSection
wrapFieldSetSection( $legend, $section, $attributes)
Wraps the given $section into an user-visible fieldset.
Definition: OOUIHTMLForm.php:145
OOUIHTMLForm\getHeaderText
getHeaderText( $section=null)
Get header text.
Definition: OOUIHTMLForm.php:229
array
the array() calling protocol came about after MediaWiki 1.4rc1.
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition: HTMLForm.php:128