MediaWiki master
Xml.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Xml;
10
13use UtfNormal\Validator;
14
18class Xml {
35 public static function element( $element, $attribs = null, $contents = '',
36 $allowShortTag = true
37 ) {
38 $out = '<' . $element;
39 if ( $attribs !== null ) {
40 $out .= self::expandAttributes( $attribs );
41 }
42 if ( $contents === null ) {
43 $out .= '>';
44 } elseif ( $allowShortTag && $contents === '' ) {
45 $out .= ' />';
46 } else {
47 // @phan-suppress-next-line PhanTypeMismatchArgumentNullableInternal $contents is non-nullable
48 $out .= '>' . htmlspecialchars( $contents, ENT_NOQUOTES ) . "</$element>";
49 }
50 return $out;
51 }
52
61 public static function expandAttributes( ?array $attribs ) {
62 if ( $attribs === null ) {
63 return null;
64 }
65 $out = '';
66 foreach ( $attribs as $name => $val ) {
67 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
68 }
69 return $out;
70 }
71
83 public static function elementClean( $element, $attribs = [], $contents = '' ) {
84 if ( $attribs ) {
85 $attribs = array_map( Validator::cleanUp( ... ), $attribs );
86 }
87 if ( $contents ) {
88 $contents =
89 MediaWikiServices::getInstance()->getContentLanguage()->normalize( $contents );
90 }
91 return self::element( $element, $attribs, $contents );
92 }
93
101 public static function openElement( $element, $attribs = null ) {
102 return '<' . $element . self::expandAttributes( $attribs ) . '>';
103 }
104
110 public static function closeElement( $element ) {
111 return "</$element>";
112 }
113
127 public static function tags( $element, $attribs, $contents ) {
128 return self::openElement( $element, $attribs ) . $contents . "</$element>";
129 }
130
142 private static function isWellFormed( $text ) {
143 $parser = xml_parser_create( "UTF-8" );
144
145 # case folding violates XML standard, turn it off
146 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
147
148 if ( !xml_parse( $parser, $text, true ) ) {
149 return false;
150 }
151 return true;
152 }
153
162 public static function isWellFormedXmlFragment( $text ) {
163 $html =
164 Sanitizer::hackDocType() .
165 '<html>' .
166 $text .
167 '</html>';
168
169 return self::isWellFormed( $html );
170 }
171
179 public static function escapeTagsOnly( $in ) {
180 return str_replace(
181 [ '"', '>', '<' ],
182 [ '&quot;', '&gt;', '&lt;' ],
183 $in );
184 }
185
186}
188class_alias( Xml::class, 'Xml' );
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:34
Module of static functions for generating XML.
Definition Xml.php:18
static escapeTagsOnly( $in)
Replace " > and < with their respective HTML entities ( ", >, <)
Definition Xml.php:179
static elementClean( $element, $attribs=[], $contents='')
Format an XML element as with self::element(), but run text through the content language's normalize(...
Definition Xml.php:83
static isWellFormedXmlFragment( $text)
Check if a string is a well-formed XML fragment.
Definition Xml.php:162
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition Xml.php:35
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition Xml.php:127
static expandAttributes(?array $attribs)
Given an array of ('attributename' => 'value'), it generates the code to set the XML attributes : att...
Definition Xml.php:61
static openElement( $element, $attribs=null)
This opens an XML element.
Definition Xml.php:101
static closeElement( $element)
Shortcut to close an XML element.
Definition Xml.php:110