123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862 |
- <?php
- class Xml {
-
- public static function element( $element, $attribs = null, $contents = '',
- $allowShortTag = true
- ) {
- $out = '<' . $element;
- if ( !is_null( $attribs ) ) {
- $out .= self::expandAttributes( $attribs );
- }
- if ( is_null( $contents ) ) {
- $out .= '>';
- } else {
- if ( $allowShortTag && $contents === '' ) {
- $out .= ' />';
- } else {
- $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
- }
- }
- return $out;
- }
-
- public static function expandAttributes( $attribs ) {
- $out = '';
- if ( is_null( $attribs ) ) {
- return null;
- } elseif ( is_array( $attribs ) ) {
- foreach ( $attribs as $name => $val ) {
- $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
- }
- return $out;
- } else {
- throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
- }
- }
-
- public static function elementClean( $element, $attribs = [], $contents = '' ) {
- global $wgContLang;
- if ( $attribs ) {
- $attribs = array_map( [ 'UtfNormal\Validator', 'cleanUp' ], $attribs );
- }
- if ( $contents ) {
- $contents = $wgContLang->normalize( $contents );
- }
- return self::element( $element, $attribs, $contents );
- }
-
- public static function openElement( $element, $attribs = null ) {
- return '<' . $element . self::expandAttributes( $attribs ) . '>';
- }
-
- public static function closeElement( $element ) {
- return "</$element>";
- }
-
- public static function tags( $element, $attribs = null, $contents ) {
- return self::openElement( $element, $attribs ) . $contents . "</$element>";
- }
-
- public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
- global $wgLang;
- $options = [];
- $data = new XmlSelect( 'month', $id, $selected );
- if ( is_null( $selected ) ) {
- $selected = '';
- }
- if ( !is_null( $allmonths ) ) {
- $options[wfMessage( 'monthsall' )->text()] = $allmonths;
- }
- for ( $i = 1; $i < 13; $i++ ) {
- $options[$wgLang->getMonthName( $i )] = $i;
- }
- $data->addOptions( $options );
- $data->setAttribute( 'class', 'mw-month-selector' );
- return $data->getHTML();
- }
-
- public static function dateMenu( $year, $month ) {
-
- if ( $month && $month !== -1 ) {
- $encMonth = intval( $month );
- } else {
- $encMonth = '';
- }
- if ( $year ) {
- $encYear = intval( $year );
- } elseif ( $encMonth ) {
- $timestamp = MWTimestamp::getInstance();
- $thisMonth = intval( $timestamp->format( 'n' ) );
- $thisYear = intval( $timestamp->format( 'Y' ) );
- if ( intval( $encMonth ) > $thisMonth ) {
- $thisYear--;
- }
- $encYear = $thisYear;
- } else {
- $encYear = '';
- }
- $inputAttribs = [ 'id' => 'year', 'maxlength' => 4, 'size' => 7 ];
- return self::label( wfMessage( 'year' )->text(), 'year' ) . ' ' .
- Html::input( 'year', $encYear, 'number', $inputAttribs ) . ' ' .
- self::label( wfMessage( 'month' )->text(), 'month' ) . ' ' .
- self::monthSelector( $encMonth, -1 );
- }
-
- public static function languageSelector( $selected, $customisedOnly = true,
- $inLanguage = null, $overrideAttrs = [], Message $msg = null
- ) {
- global $wgLanguageCode;
- $include = $customisedOnly ? 'mwfile' : 'mw';
- $languages = Language::fetchLanguageNames( $inLanguage, $include );
-
-
- if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
- $languages[$wgLanguageCode] = $wgLanguageCode;
- }
- ksort( $languages );
-
- $selected = isset( $languages[$selected] ) ? $selected : $wgLanguageCode;
- $options = "\n";
- foreach ( $languages as $code => $name ) {
- $options .= self::option( "$code - $name", $code, $code == $selected ) . "\n";
- }
- $attrs = [ 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ];
- $attrs = array_merge( $attrs, $overrideAttrs );
- if ( $msg === null ) {
- $msg = wfMessage( 'yourlanguage' );
- }
- return [
- self::label( $msg->text(), $attrs['id'] ),
- self::tags( 'select', $attrs, $options )
- ];
- }
-
- public static function span( $text, $class, $attribs = [] ) {
- return self::element( 'span', [ 'class' => $class ] + $attribs, $text );
- }
-
- public static function wrapClass( $text, $class, $tag = 'span', $attribs = [] ) {
- return self::tags( $tag, [ 'class' => $class ] + $attribs, $text );
- }
-
- public static function input( $name, $size = false, $value = false, $attribs = [] ) {
- $attributes = [ 'name' => $name ];
- if ( $size ) {
- $attributes['size'] = $size;
- }
- if ( $value !== false ) {
- $attributes['value'] = $value;
- }
- return self::element( 'input',
- Html::getTextInputAttributes( $attributes + $attribs ) );
- }
-
- public static function password( $name, $size = false, $value = false,
- $attribs = []
- ) {
- return self::input( $name, $size, $value,
- array_merge( $attribs, [ 'type' => 'password' ] ) );
- }
-
- public static function attrib( $name, $present = true ) {
- return $present ? [ $name => $name ] : [];
- }
-
- public static function check( $name, $checked = false, $attribs = [] ) {
- return self::element( 'input', array_merge(
- [
- 'name' => $name,
- 'type' => 'checkbox',
- 'value' => 1 ],
- self::attrib( 'checked', $checked ),
- $attribs ) );
- }
-
- public static function radio( $name, $value, $checked = false, $attribs = [] ) {
- return self::element( 'input', [
- 'name' => $name,
- 'type' => 'radio',
- 'value' => $value ] + self::attrib( 'checked', $checked ) + $attribs );
- }
-
- public static function label( $label, $id, $attribs = [] ) {
- $a = [ 'for' => $id ];
- foreach ( [ 'class', 'title' ] as $attr ) {
- if ( isset( $attribs[$attr] ) ) {
- $a[$attr] = $attribs[$attr];
- }
- }
- return self::element( 'label', $a, $label );
- }
-
- public static function inputLabel( $label, $name, $id, $size = false,
- $value = false, $attribs = []
- ) {
- list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
- return $label . ' ' . $input;
- }
-
- public static function inputLabelSep( $label, $name, $id, $size = false,
- $value = false, $attribs = []
- ) {
- return [
- self::label( $label, $id, $attribs ),
- self::input( $name, $size, $value, [ 'id' => $id ] + $attribs )
- ];
- }
-
- public static function checkLabel( $label, $name, $id, $checked = false, $attribs = [] ) {
- global $wgUseMediaWikiUIEverywhere;
- $chkLabel = self::check( $name, $checked, [ 'id' => $id ] + $attribs ) .
- ' ' .
- self::label( $label, $id, $attribs );
- if ( $wgUseMediaWikiUIEverywhere ) {
- $chkLabel = self::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
- $chkLabel . self::closeElement( 'div' );
- }
- return $chkLabel;
- }
-
- public static function radioLabel( $label, $name, $value, $id,
- $checked = false, $attribs = []
- ) {
- return self::radio( $name, $value, $checked, [ 'id' => $id ] + $attribs ) .
- ' ' .
- self::label( $label, $id, $attribs );
- }
-
- public static function submitButton( $value, $attribs = [] ) {
- global $wgUseMediaWikiUIEverywhere;
- $baseAttrs = [
- 'type' => 'submit',
- 'value' => $value,
- ];
-
-
-
- if ( $wgUseMediaWikiUIEverywhere ) {
- $baseAttrs['class'] = 'mw-ui-button mw-ui-progressive';
- }
-
- $attribs = $attribs + $baseAttrs;
- return Html::element( 'input', $attribs );
- }
-
- public static function option( $text, $value = null, $selected = false,
- $attribs = [] ) {
- if ( !is_null( $value ) ) {
- $attribs['value'] = $value;
- }
- if ( $selected ) {
- $attribs['selected'] = 'selected';
- }
- return Html::element( 'option', $attribs, $text );
- }
-
- public static function listDropDown( $name = '', $list = '', $other = '',
- $selected = '', $class = '', $tabindex = null
- ) {
- $options = self::listDropDownOptions( $list, [ 'other' => $other ] );
- $xmlSelect = new XmlSelect( $name, $name, $selected );
- $xmlSelect->addOptions( $options );
- if ( $class ) {
- $xmlSelect->setAttribute( 'class', $class );
- }
- if ( $tabindex ) {
- $xmlSelect->setAttribute( 'tabindex', $tabindex );
- }
- return $xmlSelect->getHTML();
- }
-
- public static function listDropDownOptions( $list, $params = [] ) {
- $options = [];
- if ( isset( $params['other'] ) ) {
- $options[ $params['other'] ] = 'other';
- }
- $optgroup = false;
- foreach ( explode( "\n", $list ) as $option ) {
- $value = trim( $option );
- if ( $value == '' ) {
- continue;
- } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
-
- $value = trim( substr( $value, 1 ) );
- $optgroup = $value;
- } elseif ( substr( $value, 0, 2 ) == '**' ) {
-
- $opt = trim( substr( $value, 2 ) );
- if ( $optgroup === false ) {
- $options[$opt] = $opt;
- } else {
- $options[$optgroup][$opt] = $opt;
- }
- } else {
-
- $optgroup = false;
- $options[$option] = $option;
- }
- }
- return $options;
- }
-
- public static function listDropDownOptionsOoui( $options ) {
- $optionsOoui = [];
- foreach ( $options as $text => $value ) {
- if ( is_array( $value ) ) {
- $optionsOoui[] = [ 'optgroup' => (string)$text ];
- foreach ( $value as $text2 => $value2 ) {
- $optionsOoui[] = [ 'data' => (string)$value2, 'label' => (string)$text2 ];
- }
- } else {
- $optionsOoui[] = [ 'data' => (string)$value, 'label' => (string)$text ];
- }
- }
- return $optionsOoui;
- }
-
- public static function fieldset( $legend = false, $content = false, $attribs = [] ) {
- $s = self::openElement( 'fieldset', $attribs ) . "\n";
- if ( $legend ) {
- $s .= self::element( 'legend', null, $legend ) . "\n";
- }
- if ( $content !== false ) {
- $s .= $content . "\n";
- $s .= self::closeElement( 'fieldset' ) . "\n";
- }
- return $s;
- }
-
- public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = [] ) {
- return self::element( 'textarea',
- Html::getTextInputAttributes(
- [
- 'name' => $name,
- 'id' => $name,
- 'cols' => $cols,
- 'rows' => $rows
- ] + $attribs
- ),
- $content, false );
- }
-
- public static function encodeJsVar( $value, $pretty = false ) {
- if ( $value instanceof XmlJsCode ) {
- return $value->value;
- }
- return FormatJson::encode( $value, $pretty, FormatJson::UTF8_OK );
- }
-
- public static function encodeJsCall( $name, $args, $pretty = false ) {
- foreach ( $args as &$arg ) {
- $arg = self::encodeJsVar( $arg, $pretty );
- if ( $arg === false ) {
- return false;
- }
- }
- return "$name(" . ( $pretty
- ? ( ' ' . implode( ', ', $args ) . ' ' )
- : implode( ',', $args )
- ) . ");";
- }
-
- private static function isWellFormed( $text ) {
- $parser = xml_parser_create( "UTF-8" );
-
- xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
- if ( !xml_parse( $parser, $text, true ) ) {
-
-
-
-
- xml_parser_free( $parser );
- return false;
- }
- xml_parser_free( $parser );
- return true;
- }
-
- public static function isWellFormedXmlFragment( $text ) {
- $html =
- Sanitizer::hackDocType() .
- '<html>' .
- $text .
- '</html>';
- return self::isWellFormed( $html );
- }
-
- public static function escapeTagsOnly( $in ) {
- return str_replace(
- [ '"', '>', '<' ],
- [ '"', '>', '<' ],
- $in );
- }
-
- public static function buildForm( $fields, $submitLabel = null, $submitAttribs = [] ) {
- $form = '';
- $form .= "<table><tbody>";
- foreach ( $fields as $labelmsg => $input ) {
- $id = "mw-$labelmsg";
- $form .= self::openElement( 'tr', [ 'id' => $id ] );
-
-
-
- $form .= self::tags( 'td', [ 'class' => 'mw-label' ], wfMessage( $labelmsg )->parse() );
- $form .= self::openElement( 'td', [ 'class' => 'mw-input' ] )
- . $input . self::closeElement( 'td' );
- $form .= self::closeElement( 'tr' );
- }
- if ( $submitLabel ) {
- $form .= self::openElement( 'tr' );
- $form .= self::tags( 'td', [], '' );
- $form .= self::openElement( 'td', [ 'class' => 'mw-submit' ] )
- . self::submitButton( wfMessage( $submitLabel )->text(), $submitAttribs )
- . self::closeElement( 'td' );
- $form .= self::closeElement( 'tr' );
- }
- $form .= "</tbody></table>";
- return $form;
- }
-
- public static function buildTable( $rows, $attribs = [], $headers = null ) {
- $s = self::openElement( 'table', $attribs );
- if ( is_array( $headers ) ) {
- $s .= self::openElement( 'thead', $attribs );
- foreach ( $headers as $id => $header ) {
- $attribs = [];
- if ( is_string( $id ) ) {
- $attribs['id'] = $id;
- }
- $s .= self::element( 'th', $attribs, $header );
- }
- $s .= self::closeElement( 'thead' );
- }
- foreach ( $rows as $id => $row ) {
- $attribs = [];
- if ( is_string( $id ) ) {
- $attribs['id'] = $id;
- }
- $s .= self::buildTableRow( $attribs, $row );
- }
- $s .= self::closeElement( 'table' );
- return $s;
- }
-
- public static function buildTableRow( $attribs, $cells ) {
- $s = self::openElement( 'tr', $attribs );
- foreach ( $cells as $id => $cell ) {
- $attribs = [];
- if ( is_string( $id ) ) {
- $attribs['id'] = $id;
- }
- $s .= self::element( 'td', $attribs, $cell );
- }
- $s .= self::closeElement( 'tr' );
- return $s;
- }
- }
|