MediaWiki  master
Xml.php
Go to the documentation of this file.
1 <?php
26 class Xml {
39  public static function element( $element, $attribs = null, $contents = '',
40  $allowShortTag = true
41  ) {
42  $out = '<' . $element;
43  if ( !is_null( $attribs ) ) {
44  $out .= self::expandAttributes( $attribs );
45  }
46  if ( is_null( $contents ) ) {
47  $out .= '>';
48  } else {
49  if ( $allowShortTag && $contents === '' ) {
50  $out .= ' />';
51  } else {
52  $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
53  }
54  }
55  return $out;
56  }
57 
67  public static function expandAttributes( $attribs ) {
68  $out = '';
69  if ( is_null( $attribs ) ) {
70  return null;
71  } elseif ( is_array( $attribs ) ) {
72  foreach ( $attribs as $name => $val ) {
73  $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
74  }
75  return $out;
76  } else {
77  throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
78  }
79  }
80 
91  public static function elementClean( $element, $attribs = [], $contents = '' ) {
93  if ( $attribs ) {
94  $attribs = array_map( [ 'UtfNormal\Validator', 'cleanUp' ], $attribs );
95  }
96  if ( $contents ) {
97  $contents = $wgContLang->normalize( $contents );
98  }
99  return self::element( $element, $attribs, $contents );
100  }
101 
109  public static function openElement( $element, $attribs = null ) {
110  return '<' . $element . self::expandAttributes( $attribs ) . '>';
111  }
112 
118  public static function closeElement( $element ) {
119  return "</$element>";
120  }
121 
131  public static function tags( $element, $attribs = null, $contents ) {
132  return self::openElement( $element, $attribs ) . $contents . "</$element>";
133  }
134 
144  public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
145  global $wgLang;
146  $options = [];
147  $data = new XmlSelect( 'month', $id, $selected );
148  if ( is_null( $selected ) ) {
149  $selected = '';
150  }
151  if ( !is_null( $allmonths ) ) {
152  $options[wfMessage( 'monthsall' )->text()] = $allmonths;
153  }
154  for ( $i = 1; $i < 13; $i++ ) {
155  $options[$wgLang->getMonthName( $i )] = $i;
156  }
157  $data->addOptions( $options );
158  $data->setAttribute( 'class', 'mw-month-selector' );
159  return $data->getHTML();
160  }
161 
168  public static function dateMenu( $year, $month ) {
169  # Offset overrides year/month selection
170  if ( $month && $month !== -1 ) {
171  $encMonth = intval( $month );
172  } else {
173  $encMonth = '';
174  }
175  if ( $year ) {
176  $encYear = intval( $year );
177  } elseif ( $encMonth ) {
178  $timestamp = MWTimestamp::getInstance();
179  $thisMonth = intval( $timestamp->format( 'n' ) );
180  $thisYear = intval( $timestamp->format( 'Y' ) );
181  if ( intval( $encMonth ) > $thisMonth ) {
182  $thisYear--;
183  }
184  $encYear = $thisYear;
185  } else {
186  $encYear = '';
187  }
188  $inputAttribs = [ 'id' => 'year', 'maxlength' => 4, 'size' => 7 ];
189  return self::label( wfMessage( 'year' )->text(), 'year' ) . ' ' .
190  Html::input( 'year', $encYear, 'number', $inputAttribs ) . ' ' .
191  self::label( wfMessage( 'month' )->text(), 'month' ) . ' ' .
192  self::monthSelector( $encMonth, -1 );
193  }
194 
205  public static function languageSelector( $selected, $customisedOnly = true,
206  $inLanguage = null, $overrideAttrs = [], Message $msg = null
207  ) {
209 
210  $include = $customisedOnly ? 'mwfile' : 'mw';
211  $languages = Language::fetchLanguageNames( $inLanguage, $include );
212 
213  // Make sure the site language is in the list;
214  // a custom language code might not have a defined name...
215  if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
217  // Sort the array again
218  ksort( $languages );
219  }
220 
226  $selected = isset( $languages[$selected] ) ? $selected : $wgLanguageCode;
227  $options = "\n";
228  foreach ( $languages as $code => $name ) {
229  $options .= self::option( "$code - $name", $code, $code == $selected ) . "\n";
230  }
231 
232  $attrs = [ 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ];
233  $attrs = array_merge( $attrs, $overrideAttrs );
234 
235  if ( $msg === null ) {
236  $msg = wfMessage( 'yourlanguage' );
237  }
238  return [
239  self::label( $msg->text(), $attrs['id'] ),
240  self::tags( 'select', $attrs, $options )
241  ];
242  }
243 
251  public static function span( $text, $class, $attribs = [] ) {
252  return self::element( 'span', [ 'class' => $class ] + $attribs, $text );
253  }
254 
263  public static function wrapClass( $text, $class, $tag = 'span', $attribs = [] ) {
264  return self::tags( $tag, [ 'class' => $class ] + $attribs, $text );
265  }
266 
275  public static function input( $name, $size = false, $value = false, $attribs = [] ) {
276  $attributes = [ 'name' => $name ];
277 
278  if ( $size ) {
279  $attributes['size'] = $size;
280  }
281 
282  if ( $value !== false ) { // maybe 0
283  $attributes['value'] = $value;
284  }
285 
286  return self::element( 'input',
287  Html::getTextInputAttributes( $attributes + $attribs ) );
288  }
289 
298  public static function password( $name, $size = false, $value = false,
299  $attribs = []
300  ) {
301  return self::input( $name, $size, $value,
302  array_merge( $attribs, [ 'type' => 'password' ] ) );
303  }
304 
313  public static function attrib( $name, $present = true ) {
314  return $present ? [ $name => $name ] : [];
315  }
316 
324  public static function check( $name, $checked = false, $attribs = [] ) {
325  return self::element( 'input', array_merge(
326  [
327  'name' => $name,
328  'type' => 'checkbox',
329  'value' => 1 ],
330  self::attrib( 'checked', $checked ),
331  $attribs ) );
332  }
333 
342  public static function radio( $name, $value, $checked = false, $attribs = [] ) {
343  return self::element( 'input', [
344  'name' => $name,
345  'type' => 'radio',
346  'value' => $value ] + self::attrib( 'checked', $checked ) + $attribs );
347  }
348 
359  public static function label( $label, $id, $attribs = [] ) {
360  $a = [ 'for' => $id ];
361 
362  foreach ( [ 'class', 'title' ] as $attr ) {
363  if ( isset( $attribs[$attr] ) ) {
364  $a[$attr] = $attribs[$attr];
365  }
366  }
367 
368  return self::element( 'label', $a, $label );
369  }
370 
381  public static function inputLabel( $label, $name, $id, $size = false,
382  $value = false, $attribs = []
383  ) {
384  list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
385  return $label . '&#160;' . $input;
386  }
387 
400  public static function inputLabelSep( $label, $name, $id, $size = false,
401  $value = false, $attribs = []
402  ) {
403  return [
404  self::label( $label, $id, $attribs ),
405  self::input( $name, $size, $value, [ 'id' => $id ] + $attribs )
406  ];
407  }
408 
420  public static function checkLabel( $label, $name, $id, $checked = false, $attribs = [] ) {
422  $chkLabel = self::check( $name, $checked, [ 'id' => $id ] + $attribs ) .
423  '&#160;' .
424  self::label( $label, $id, $attribs );
425 
426  if ( $wgUseMediaWikiUIEverywhere ) {
427  $chkLabel = self::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
428  $chkLabel . self::closeElement( 'div' );
429  }
430  return $chkLabel;
431  }
432 
445  public static function radioLabel( $label, $name, $value, $id,
446  $checked = false, $attribs = []
447  ) {
448  return self::radio( $name, $value, $checked, [ 'id' => $id ] + $attribs ) .
449  '&#160;' .
450  self::label( $label, $id, $attribs );
451  }
452 
460  public static function submitButton( $value, $attribs = [] ) {
462  $baseAttrs = [
463  'type' => 'submit',
464  'value' => $value,
465  ];
466  // Done conditionally for time being as it is possible
467  // some submit forms
468  // might need to be mw-ui-destructive (e.g. delete a page)
469  if ( $wgUseMediaWikiUIEverywhere ) {
470  $baseAttrs['class'] = 'mw-ui-button mw-ui-progressive';
471  }
472  // Any custom attributes will take precendence of anything in baseAttrs e.g. override the class
473  $attribs = $attribs + $baseAttrs;
474  return Html::element( 'input', $attribs );
475  }
476 
485  public static function option( $text, $value = null, $selected = false,
486  $attribs = [] ) {
487  if ( !is_null( $value ) ) {
488  $attribs['value'] = $value;
489  }
490  if ( $selected ) {
491  $attribs['selected'] = 'selected';
492  }
493  return Html::element( 'option', $attribs, $text );
494  }
495 
509  public static function listDropDown( $name = '', $list = '', $other = '',
510  $selected = '', $class = '', $tabindex = null
511  ) {
512  $options = self::listDropDownOptions( $list, [ 'other' => $other ] );
513 
514  $xmlSelect = new XmlSelect( $name, $name, $selected );
515  $xmlSelect->addOptions( $options );
516 
517  if ( $class ) {
518  $xmlSelect->setAttribute( 'class', $class );
519  }
520  if ( $tabindex ) {
521  $xmlSelect->setAttribute( 'tabindex', $tabindex );
522  }
523 
524  return $xmlSelect->getHTML();
525  }
526 
540  public static function listDropDownOptions( $list, $params = [] ) {
541  $options = [];
542 
543  if ( isset( $params['other'] ) ) {
544  $options[ $params['other'] ] = 'other';
545  }
546 
547  $optgroup = false;
548  foreach ( explode( "\n", $list ) as $option ) {
549  $value = trim( $option );
550  if ( $value == '' ) {
551  continue;
552  } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
553  # A new group is starting...
554  $value = trim( substr( $value, 1 ) );
555  $optgroup = $value;
556  } elseif ( substr( $value, 0, 2 ) == '**' ) {
557  # groupmember
558  $opt = trim( substr( $value, 2 ) );
559  if ( $optgroup === false ) {
560  $options[$opt] = $opt;
561  } else {
562  $options[$optgroup][$opt] = $opt;
563  }
564  } else {
565  # groupless reason list
566  $optgroup = false;
567  $options[$option] = $option;
568  }
569  }
570 
571  return $options;
572  }
573 
582  public static function listDropDownOptionsOoui( $options ) {
583  $optionsOoui = [];
584 
585  foreach ( $options as $text => $value ) {
586  if ( is_array( $value ) ) {
587  $optionsOoui[] = [ 'optgroup' => (string)$text ];
588  foreach ( $value as $text2 => $value2 ) {
589  $optionsOoui[] = [ 'data' => (string)$value2, 'label' => (string)$text2 ];
590  }
591  } else {
592  $optionsOoui[] = [ 'data' => (string)$value, 'label' => (string)$text ];
593  }
594  }
595 
596  return $optionsOoui;
597  }
598 
610  public static function fieldset( $legend = false, $content = false, $attribs = [] ) {
611  $s = self::openElement( 'fieldset', $attribs ) . "\n";
612 
613  if ( $legend ) {
614  $s .= self::element( 'legend', null, $legend ) . "\n";
615  }
616 
617  if ( $content !== false ) {
618  $s .= $content . "\n";
619  $s .= self::closeElement( 'fieldset' ) . "\n";
620  }
621 
622  return $s;
623  }
624 
636  public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = [] ) {
637  return self::element( 'textarea',
639  [
640  'name' => $name,
641  'id' => $name,
642  'cols' => $cols,
643  'rows' => $rows
644  ] + $attribs
645  ),
646  $content, false );
647  }
648 
660  public static function encodeJsVar( $value, $pretty = false ) {
661  if ( $value instanceof XmlJsCode ) {
662  return $value->value;
663  }
664  return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
665  }
666 
678  public static function encodeJsCall( $name, $args, $pretty = false ) {
679  foreach ( $args as &$arg ) {
680  $arg = self::encodeJsVar( $arg, $pretty );
681  if ( $arg === false ) {
682  return false;
683  }
684  }
685 
686  return "$name(" . ( $pretty
687  ? ( ' ' . implode( ', ', $args ) . ' ' )
688  : implode( ',', $args )
689  ) . ");";
690  }
691 
703  private static function isWellFormed( $text ) {
704  $parser = xml_parser_create( "UTF-8" );
705 
706  # case folding violates XML standard, turn it off
707  xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
708 
709  if ( !xml_parse( $parser, $text, true ) ) {
710  // $err = xml_error_string( xml_get_error_code( $parser ) );
711  // $position = xml_get_current_byte_index( $parser );
712  // $fragment = $this->extractFragment( $html, $position );
713  // $this->mXmlError = "$err at byte $position:\n$fragment";
714  xml_parser_free( $parser );
715  return false;
716  }
717 
718  xml_parser_free( $parser );
719 
720  return true;
721  }
722 
731  public static function isWellFormedXmlFragment( $text ) {
732  $html =
734  '<html>' .
735  $text .
736  '</html>';
737 
738  return self::isWellFormed( $html );
739  }
740 
748  public static function escapeTagsOnly( $in ) {
749  return str_replace(
750  [ '"', '>', '<' ],
751  [ '&quot;', '&gt;', '&lt;' ],
752  $in );
753  }
754 
766  public static function buildForm( $fields, $submitLabel = null, $submitAttribs = [] ) {
767  $form = '';
768  $form .= "<table><tbody>";
769 
770  foreach ( $fields as $labelmsg => $input ) {
771  $id = "mw-$labelmsg";
772  $form .= self::openElement( 'tr', [ 'id' => $id ] );
773 
774  // TODO use a <label> here for accessibility purposes - will need
775  // to either not use a table to build the form, or find the ID of
776  // the input somehow.
777 
778  $form .= self::tags( 'td', [ 'class' => 'mw-label' ], wfMessage( $labelmsg )->parse() );
779  $form .= self::openElement( 'td', [ 'class' => 'mw-input' ] )
780  . $input . self::closeElement( 'td' );
781  $form .= self::closeElement( 'tr' );
782  }
783 
784  if ( $submitLabel ) {
785  $form .= self::openElement( 'tr' );
786  $form .= self::tags( 'td', [], '' );
787  $form .= self::openElement( 'td', [ 'class' => 'mw-submit' ] )
788  . self::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs )
789  . self::closeElement( 'td' );
790  $form .= self::closeElement( 'tr' );
791  }
792 
793  $form .= "</tbody></table>";
794 
795  return $form;
796  }
797 
805  public static function buildTable( $rows, $attribs = [], $headers = null ) {
806  $s = self::openElement( 'table', $attribs );
807 
808  if ( is_array( $headers ) ) {
809  $s .= self::openElement( 'thead', $attribs );
810 
811  foreach ( $headers as $id => $header ) {
812  $attribs = [];
813 
814  if ( is_string( $id ) ) {
815  $attribs['id'] = $id;
816  }
817 
818  $s .= self::element( 'th', $attribs, $header );
819  }
820  $s .= self::closeElement( 'thead' );
821  }
822 
823  foreach ( $rows as $id => $row ) {
824  $attribs = [];
825 
826  if ( is_string( $id ) ) {
827  $attribs['id'] = $id;
828  }
829 
830  $s .= self::buildTableRow( $attribs, $row );
831  }
832 
833  $s .= self::closeElement( 'table' );
834 
835  return $s;
836  }
837 
844  public static function buildTableRow( $attribs, $cells ) {
845  $s = self::openElement( 'tr', $attribs );
846 
847  foreach ( $cells as $id => $cell ) {
848  $attribs = [];
849 
850  if ( is_string( $id ) ) {
851  $attribs['id'] = $id;
852  }
853 
854  $s .= self::element( 'td', $attribs, $cell );
855  }
856 
857  $s .= self::closeElement( 'tr' );
858 
859  return $s;
860  }
861 }
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g.with the RejectParserCacheValue hook) because MediaWiki won't do it for you.&$defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2636
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:2019
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:790
if(is_array($mode)) switch($mode) $input
static element($element, $attribs=null, $contents= '', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
static radioLabel($label, $name, $value, $id, $checked=false, $attribs=[])
Convenience function to build an HTML radio button with a label.
Definition: Xml.php:445
static isWellFormedXmlFragment($text)
Check if a string is a well-formed XML fragment.
Definition: Xml.php:731
static expandAttributes($attribs)
Given an array of ('attributename' => 'value'), it generates the code to set the XML attributes : att...
Definition: Xml.php:67
Class for generating HTML " element.
Definition: Html.php:661
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:790
static encodeJsVar($value, $pretty=false)
Encode a variable of arbitrary type to JavaScript.
Definition: Xml.php:660
static textarea($name, $content, $cols=40, $rows=5, $attribs=[])
Shortcut for creating textareas.
Definition: Xml.php:636
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
static buildTableRow($attribs, $cells)
Build a row for a table.
Definition: Xml.php:844
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
static checkLabel($label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition: Xml.php:420
static attrib($name, $present=true)
Internal function for use in checkboxes and radio buttons and such.
Definition: Xml.php:313
static buildTable($rows, $attribs=[], $headers=null)
Build a table of data.
Definition: Xml.php:805
static encodeJsCall($name, $args, $pretty=false)
Create a call to a JavaScript function.
Definition: Xml.php:678
static isWellFormed($text)
Check if a string is well-formed XML.
Definition: Xml.php:703
static listDropDown($name= '', $list= '', $other= '', $selected= '', $class= '', $tabindex=null)
Build a drop-down box from a textual list.
Definition: Xml.php:509
static escapeTagsOnly($in)
Replace " > and < with their respective HTML entities ( ", >, <)
Definition: Xml.php:748
static element($element, $attribs=[], $contents= '')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
static buildForm($fields, $submitLabel=null, $submitAttribs=[])
Generate a form (without the opening form element).
Definition: Xml.php:766
static listDropDownOptions($list, $params=[])
Build options for a drop-down box from a textual list.
Definition: Xml.php:540
static password($name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML password input field.
Definition: Xml.php:298
static encodeAttribute($text)
Encode an attribute value for HTML output.
Definition: Sanitizer.php:1129