MediaWiki  master
OOUIHTMLForm.php
Go to the documentation of this file.
1 <?php
2 
27 class OOUIHTMLForm extends HTMLForm {
28  private $oouiErrors;
29 
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' );
34  }
35 
40  protected $displayFormat = 'ooui';
41 
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;
48  }
49 
50  function getButtons() {
51  $buttons = '';
52 
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' ) );
55 
56  if ( $this->mShowSubmit ) {
57  $attribs = [ 'infusable' => true ];
58 
59  if ( isset( $this->mSubmitID ) ) {
60  $attribs['id'] = $this->mSubmitID;
61  }
62 
63  if ( isset( $this->mSubmitName ) ) {
64  $attribs['name'] = $this->mSubmitName;
65  }
66 
67  if ( isset( $this->mSubmitTooltip ) ) {
68  $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
69  }
70 
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;
77 
78  $buttons .= new OOUI\ButtonInputWidget( $attribs );
79  }
80 
81  if ( $this->mShowReset ) {
82  $buttons .= new OOUI\ButtonInputWidget( [
83  'type' => 'reset',
84  'label' => $this->msg( 'htmlform-reset' )->text(),
85  'useInputTag' => $isBadIE,
86  ] );
87  }
88 
89  if ( $this->mShowCancel ) {
90  $target = $this->mCancelTarget ?: Title::newMainPage();
91  if ( $target instanceof Title ) {
92  $target = $target->getLocalURL();
93  }
94  $buttons .= new OOUI\ButtonWidget( [
95  'label' => $this->msg( 'cancel' )->text(),
96  'href' => $target,
97  ] );
98  }
99 
100  foreach ( $this->mButtons as $button ) {
101  $attrs = [];
102 
103  if ( $button['attribs'] ) {
104  $attrs += $button['attribs'];
105  }
106 
107  if ( isset( $button['id'] ) ) {
108  $attrs['id'] = $button['id'];
109  }
110 
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'];
121  }
122 
123  $attrs['classes'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
124 
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 );
134  }
135 
136  if ( !$buttons ) {
137  return '';
138  }
139 
140  return Html::rawElement( 'div',
141  [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
142  }
143 
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  ] );
152 
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  ] ),
161  ],
162  ] + $attributes )
163  );
164  return $layout;
165  }
166 
174  protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
175  $config = [
176  'items' => $fieldsHtml,
177  ];
178  if ( $sectionName ) {
179  $config['id'] = Sanitizer::escapeId( $sectionName );
180  }
181  if ( is_string( $this->mWrapperLegend ) ) {
182  $config['label'] = $this->mWrapperLegend;
183  }
184  return new OOUI\FieldsetLayout( $config );
185  }
186 
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'] );
203  }
204  }
205  } else {
206  $errors = $err;
207  if ( !is_array( $errors ) ) {
208  $errors = [ $errors ];
209  }
210  }
211 
212  foreach ( $errors as &$error ) {
213  $error = $this->getMessage( $error )->parse();
214  $error = new OOUI\HtmlSnippet( $error );
215  }
216 
217  // Used in getBody()
218  $this->oouiErrors = $errors;
219  return '';
220  }
221 
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 );
228  }
229  }
230 
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';
238  }
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( [] );
245  }
246  $fieldset->addItems( [
247  new OOUI\FieldLayout(
248  $element,
249  [
250  'align' => 'top',
251  'errors' => $this->oouiErrors,
252  'classes' => $classes,
253  ]
254  )
255  ], 0 );
256  }
257  }
258  return $fieldset;
259  }
260 
261  function wrapForm( $html ) {
262  $form = new OOUI\FormLayout( $this->getFormAttributes() + [
263  'classes' => [ 'mw-htmlform-ooui' ],
264  'content' => new OOUI\HtmlSnippet( $html ),
265  ] );
266 
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  ] );
275 
276  return $form;
277  }
278 }
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:1816
the array() calling protocol came about after MediaWiki 1.4rc1.
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:548
$mWrapperLegend
Definition: HTMLForm.php:221
__construct($descriptor, $context=null, $messagePrefix= '')
static rawElement($element, $attribs=[], $contents= '')
Returns an HTML element in a string.
Definition: Html.php:210
Generic operation result class Has warning/error list, boolean status and arbitrary value...
Definition: Status.php:40
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:1824
string $displayFormat
Symbolic display format name.
Represents a title within MediaWiki.
Definition: Title.php:36
getHeaderText($section=null)
IContextSource $context
static tooltipAndAccesskeyAttribs($name, array $msgParams=[])
Returns the attributes for the tooltip and access key.
Definition: Linker.php:2240
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:1816
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 <form> tag.
Definition: HTMLForm.php:1015
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:1316
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:2755
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:1169
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.
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:1816
getOutput()
Get the OutputPage object.