4 * Module of static functions for generating XML
9 * Format an XML element with given attributes and, optionally, text content.
10 * Element and attribute names are assumed to be ready for literal inclusion.
11 * Strings are assumed to not contain XML-illegal characters; special
12 * characters (<, >, &) are escaped but illegals are not touched.
14 * @param $element String:
15 * @param $attribs Array: Name=>value pairs. Values will be escaped.
16 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
19 public static function element( $element, $attribs = null, $contents = '') {
20 $out = '<' . $element;
21 if( !is_null( $attribs ) ) {
22 $out .= self
::expandAttributes( $attribs );
24 if( is_null( $contents ) ) {
27 if( $contents === '' ) {
30 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
37 * Given an array of ('attributename' => 'value'), it generates the code
38 * to set the XML attributes : attributename="value".
39 * The values are passed to Sanitizer::encodeAttribute.
40 * Return null if no attributes given.
41 * @param $attribs Array of attributes for an XML element
43 private static function expandAttributes( $attribs ) {
45 if( is_array( $attribs ) ) {
46 foreach( $attribs as $name => $val )
47 $out .= " {$name}=\"" . Sanitizer
::encodeAttribute( $val ) . '"';
49 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__
);
55 * Format an XML element as with self::element(), but run text through the
56 * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
59 * @param $element String:
60 * @param $attribs Array: Name=>value pairs. Values will be escaped.
61 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
64 public static function elementClean( $element, $attribs = array(), $contents = '') {
66 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
69 wfProfileIn( __METHOD__
. '-norm' );
70 $contents = UtfNormal
::cleanUp( $contents );
71 wfProfileOut( __METHOD__
. '-norm' );
73 return self
::element( $element, $attribs, $contents );
76 /** This open an XML element */
77 public static function openElement( $element, $attribs = null ) {
78 return '<' . $element . self
::expandAttributes( $attribs ) . '>';
82 public static function closeElement( $element ) { return "</$element>"; }
85 * Same as <link>element</link>, but does not escape contents. Handy when the
86 * content you have is already valid xml.
88 public static function tags( $element, $attribs = null, $contents ) {
89 return self
::openElement( $element, $attribs ) . $contents . "</$element>";
93 * Build a drop-down box for selecting a namespace
95 * @param mixed $selected Namespace which should be pre-selected
96 * @param mixed $all Value of an item denoting all namespaces, or null to omit
97 * @param bool $hidden Include hidden namespaces? [WTF? --RC]
100 public static function namespaceSelector( $selected = '', $all = null, $hidden = false ) {
102 $namespaces = $wgContLang->getFormattedNamespaces();
105 if( !is_null( $all ) )
106 $namespaces = array( $all => wfMsg( 'namespacesall' ) ) +
$namespaces;
107 foreach( $namespaces as $index => $name ) {
108 if( $index < NS_MAIN
)
111 $name = wfMsg( 'blanknamespace' );
112 $options[] = self
::option( $name, $index, $index === $selected );
115 return Xml
::openElement( 'select', array( 'id' => 'namespace', 'name' => 'namespace',
116 'class' => 'namespaceselector' ) )
118 . implode( "\n", $options )
120 . Xml
::closeElement( 'select' );
124 * Create a date selector
126 * @param $selected Mixed: the month which should be selected, default ''
127 * @param $allmonths String: value of a special item denoting all month. Null to not include (default)
128 * @param string $id Element identifier
129 * @return String: Html string containing the month selector
131 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
134 if( is_null( $selected ) )
136 if( !is_null( $allmonths ) )
137 $options[] = self
::option( wfMsg( 'monthsall' ), $allmonths, $selected === $allmonths );
138 for( $i = 1; $i < 13; $i++
)
139 $options[] = self
::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
140 return self
::openElement( 'select', array( 'id' => $id, 'name' => 'month' ) )
141 . implode( "\n", $options )
142 . self
::closeElement( 'select' );
147 * @param $language The language code of the selected language
148 * @param $customisedOnly If true only languages which have some content are listed
149 * @return array of label and select
151 public static function languageSelector( $selected, $customisedOnly = true ) {
152 global $wgContLanguageCode;
154 * Make sure the site language is in the list; a custom language code
155 * might not have a defined name...
157 $languages = Language
::getLanguageNames( $customisedOnly );
158 if( !array_key_exists( $wgContLanguageCode, $languages ) ) {
159 $languages[$wgContLanguageCode] = $wgContLanguageCode;
164 * If a bogus value is set, default to the content language.
165 * Otherwise, no default is selected and the user ends up
166 * with an Afrikaans interface since it's first in the list.
168 $selected = isset( $languages[$selected] ) ?
$selected : $wgContLanguageCode;
170 foreach( $languages as $code => $name ) {
171 $options .= Xml
::option( "$code - $name", $code, ($code == $selected) ) . "\n";
175 Xml
::label( wfMsg('yourlanguage'), 'wpUserLanguage' ),
177 array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ),
184 public static function span( $text, $class, $attribs=array() ) {
185 return self
::element( 'span', array( 'class' => $class ) +
$attribs, $text );
189 * Convenience function to build an HTML text input field
190 * @return string HTML
192 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
193 return self
::element( 'input', array(
196 'value' => $value ) +
$attribs );
200 * Convenience function to build an HTML password input field
201 * @return string HTML
203 public static function password( $name, $size=false, $value=false, $attribs=array() ) {
204 return self
::input( $name, $size, $value, array_merge($attribs, array('type' => 'password')));
208 * Internal function for use in checkboxes and radio buttons and such.
211 public static function attrib( $name, $present = true ) {
212 return $present ?
array( $name => $name ) : array();
216 * Convenience function to build an HTML checkbox
217 * @return string HTML
219 public static function check( $name, $checked=false, $attribs=array() ) {
220 return self
::element( 'input', array_merge(
223 'type' => 'checkbox',
225 self
::attrib( 'checked', $checked ),
230 * Convenience function to build an HTML radio button
231 * @return string HTML
233 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
234 return self
::element( 'input', array(
237 'value' => $value ) + self
::attrib( 'checked', $checked ) +
$attribs );
241 * Convenience function to build an HTML form label
242 * @return string HTML
244 public static function label( $label, $id ) {
245 return self
::element( 'label', array( 'for' => $id ), $label );
249 * Convenience function to build an HTML text input field with a label
250 * @return string HTML
252 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
253 return Xml
::label( $label, $id ) .
255 self
::input( $name, $size, $value, array( 'id' => $id ) +
$attribs );
259 * Convenience function to build an HTML checkbox with a label
260 * @return string HTML
262 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
263 return self
::check( $name, $checked, array( 'id' => $id ) +
$attribs ) .
265 self
::label( $label, $id );
269 * Convenience function to build an HTML radio button with a label
270 * @return string HTML
272 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
273 return self
::radio( $name, $value, $checked, array( 'id' => $id ) +
$attribs ) .
275 self
::label( $label, $id );
279 * Convenience function to build an HTML submit button
280 * @param $value String: label text for the button
281 * @param $attribs Array: optional custom attributes
282 * @return string HTML
284 public static function submitButton( $value, $attribs=array() ) {
285 return self
::element( 'input', array( 'type' => 'submit', 'value' => $value ) +
$attribs );
289 * Convenience function to build an HTML hidden form field.
290 * @todo Document $name parameter.
292 * @param $value String: label text for the button
293 * @param $attribs Array: optional custom attributes
294 * @return string HTML
296 public static function hidden( $name, $value, $attribs=array() ) {
297 return self
::element( 'input', array(
300 'value' => $value ) +
$attribs );
304 * Convenience function to build an HTML drop-down list item.
305 * @param $text String: text for this item
306 * @param $value String: form submission value; if empty, use text
307 * @param $selected boolean: if true, will be the default selected item
308 * @param $attribs array: optional additional HTML attributes
309 * @return string HTML
311 public static function option( $text, $value=null, $selected=false,
313 if( !is_null( $value ) ) {
314 $attribs['value'] = $value;
317 $attribs['selected'] = 'selected';
319 return self
::element( 'option', $attribs, $text );
323 * Returns an escaped string suitable for inclusion in a string literal
324 * for JavaScript source code.
325 * Illegal control characters are assumed not to be present.
327 * @param string $string
330 public static function escapeJsString( $string ) {
331 // See ECMA 262 section 7.8.4 for string literal format
339 # To avoid closing the element or CDATA section
343 # To avoid any complaints about bad entity refs
346 # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
347 # Encode certain Unicode formatting chars so affected
348 # versions of Gecko don't misinterpret our strings;
349 # this is a common problem with Farsi text.
350 "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
351 "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
353 return strtr( $string, $pairs );
357 * Encode a variable of unknown type to JavaScript.
358 * Arrays are converted to JS arrays, objects are converted to JS associative
359 * arrays (objects). So cast your PHP associative arrays to objects before
360 * passing them to here.
362 public static function encodeJsVar( $value ) {
363 if ( is_bool( $value ) ) {
364 $s = $value ?
'true' : 'false';
365 } elseif ( is_null( $value ) ) {
367 } elseif ( is_int( $value ) ) {
369 } elseif ( is_array( $value ) ) {
371 foreach ( $value as $elt ) {
375 $s .= self
::encodeJsVar( $elt );
378 } elseif ( is_object( $value ) ) {
380 foreach ( (array)$value as $name => $elt ) {
384 $s .= '"' . self
::escapeJsString( $name ) . '": ' .
385 self
::encodeJsVar( $elt );
389 $s = '"' . self
::escapeJsString( $value ) . '"';
396 * Check if a string is well-formed XML.
397 * Must include the surrounding tag.
399 * @param $text String: string to test.
402 * @todo Error position reporting return
404 public static function isWellFormed( $text ) {
405 $parser = xml_parser_create( "UTF-8" );
407 # case folding violates XML standard, turn it off
408 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
410 if( !xml_parse( $parser, $text, true ) ) {
411 //$err = xml_error_string( xml_get_error_code( $parser ) );
412 //$position = xml_get_current_byte_index( $parser );
413 //$fragment = $this->extractFragment( $html, $position );
414 //$this->mXmlError = "$err at byte $position:\n$fragment";
415 xml_parser_free( $parser );
418 xml_parser_free( $parser );
423 * Check if a string is a well-formed XML fragment.
424 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
425 * and can use HTML named entities.
427 * @param $text String:
430 public static function isWellFormedXmlFragment( $text ) {
432 Sanitizer
::hackDocType() .
436 return Xml
::isWellFormed( $html );
440 * Replace " > and < with their respective HTML entities ( ",
443 * @param $in String: text that might contain HTML tags.
444 * @return string Escaped string
446 public static function escapeTagsOnly( $in ) {
448 array( '"', '>', '<' ),
449 array( '"', '>', '<' ),