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 foreach( $attribs as $name => $val ) {
23 $out .= ' ' . $name . '="' . Sanitizer
::encodeAttribute( $val ) . '"';
26 if( is_null( $contents ) ) {
29 if( $contents === '' ) {
32 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
39 * Format an XML element as with self::element(), but run text through the
40 * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
43 * @param $element String:
44 * @param $attribs Array: Name=>value pairs. Values will be escaped.
45 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
48 public static function elementClean( $element, $attribs = array(), $contents = '') {
50 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
53 wfProfileIn( __METHOD__
. '-norm' );
54 $contents = UtfNormal
::cleanUp( $contents );
55 wfProfileOut( __METHOD__
. '-norm' );
57 return self
::element( $element, $attribs, $contents );
61 public static function openElement( $element, $attribs = null ) { return self
::element( $element, $attribs, null ); }
62 public static function closeElement( $element ) { return "</$element>"; }
65 * Same as <link>element</link>, but does not escape contents. Handy when the
66 * content you have is already valid xml.
68 public static function tags( $element, $attribs = null, $contents ) {
69 return self
::element( $element, $attribs, null ) . $contents . "</$element>";
73 * Create a namespace selector
75 * @param $selected Mixed: the namespace which should be selected, default ''
76 * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default)
77 * @param $includehidden Bool: include hidden namespaces?
78 * @return String: Html string containing the namespace selector
80 public static function namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
82 if( $selected !== '' ) {
83 if( is_null( $selected ) ) {
84 // No namespace selected; let exact match work without hitting Main
87 // Let input be numeric strings without breaking the empty match.
88 $selected = intval( $selected );
91 $s = "\n<select id='namespace' name='namespace' class='namespaceselector'>\n";
92 $arr = $wgContLang->getFormattedNamespaces();
93 if( !is_null($allnamespaces) ) {
94 $arr = array($allnamespaces => wfMsg('namespacesall')) +
$arr;
96 foreach ($arr as $index => $name) {
97 if ($index < NS_MAIN
) continue;
99 $name = $index !== 0 ?
$name : wfMsg('blanknamespace');
101 if ($index === $selected) {
102 $s .= "\t" . self
::element("option",
103 array("value" => $index, "selected" => "selected"),
106 $s .= "\t" . self
::element("option", array("value" => $index), $name) . "\n";
115 * @param $language The language code of the selected language
116 * @param $customisedOnly If true only languages which have some content are listed
117 * @return array of label and select
119 public static function languageSelector( $selected, $customisedOnly = true ) {
120 global $wgContLanguageCode;
122 * Make sure the site language is in the list; a custom language code
123 * might not have a defined name...
125 $languages = Language
::getLanguageNames( $customisedOnly );
126 if( !array_key_exists( $wgContLanguageCode, $languages ) ) {
127 $languages[$wgContLanguageCode] = $wgContLanguageCode;
132 * If a bogus value is set, default to the content language.
133 * Otherwise, no default is selected and the user ends up
134 * with an Afrikaans interface since it's first in the list.
136 $selected = isset( $languages[$selected] ) ?
$selected : $wgContLanguageCode;
138 foreach( $languages as $code => $name ) {
139 $options .= Xml
::option( "$code - $name", $code, ($code == $selected) ) . "\n";
143 Xml
::label( wfMsg('yourlanguage'), 'wpUserLanguage' ),
145 array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ),
152 public static function span( $text, $class, $attribs=array() ) {
153 return self
::element( 'span', array( 'class' => $class ) +
$attribs, $text );
157 * Convenience function to build an HTML text input field
158 * @return string HTML
160 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
161 return self
::element( 'input', array(
164 'value' => $value ) +
$attribs );
168 * Internal function for use in checkboxes and radio buttons and such.
171 public static function attrib( $name, $present = true ) {
172 return $present ?
array( $name => $name ) : array();
176 * Convenience function to build an HTML checkbox
177 * @return string HTML
179 public static function check( $name, $checked=false, $attribs=array() ) {
180 return self
::element( 'input', array_merge(
183 'type' => 'checkbox',
185 self
::attrib( 'checked', $checked ),
190 * Convenience function to build an HTML radio button
191 * @return string HTML
193 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
194 return self
::element( 'input', array(
197 'value' => $value ) + self
::attrib( 'checked', $checked ) +
$attribs );
201 * Convenience function to build an HTML form label
202 * @return string HTML
204 public static function label( $label, $id ) {
205 return self
::element( 'label', array( 'for' => $id ), $label );
209 * Convenience function to build an HTML text input field with a label
210 * @return string HTML
212 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
213 return Xml
::label( $label, $id ) .
215 self
::input( $name, $size, $value, array( 'id' => $id ) +
$attribs );
219 * Convenience function to build an HTML checkbox with a label
220 * @return string HTML
222 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
223 return self
::check( $name, $checked, array( 'id' => $id ) +
$attribs ) .
225 self
::label( $label, $id );
229 * Convenience function to build an HTML radio button with a label
230 * @return string HTML
232 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
233 return self
::radio( $name, $value, $checked, array( 'id' => $id ) +
$attribs ) .
235 self
::label( $label, $id );
239 * Convenience function to build an HTML submit button
240 * @param $value String: label text for the button
241 * @param $attribs Array: optional custom attributes
242 * @return string HTML
244 public static function submitButton( $value, $attribs=array() ) {
245 return self
::element( 'input', array( 'type' => 'submit', 'value' => $value ) +
$attribs );
249 * Convenience function to build an HTML hidden form field.
250 * @todo Document $name parameter.
252 * @param $value String: label text for the button
253 * @param $attribs Array: optional custom attributes
254 * @return string HTML
256 public static function hidden( $name, $value, $attribs=array() ) {
257 return self
::element( 'input', array(
260 'value' => $value ) +
$attribs );
264 * Convenience function to build an HTML drop-down list item.
265 * @param $text String: text for this item
266 * @param $value String: form submission value; if empty, use text
267 * @param $selected boolean: if true, will be the default selected item
268 * @param $attribs array: optional additional HTML attributes
269 * @return string HTML
271 public static function option( $text, $value=null, $selected=false,
273 if( !is_null( $value ) ) {
274 $attribs['value'] = $value;
277 $attribs['selected'] = 'selected';
279 return self
::element( 'option', $attribs, $text );
283 * Returns an escaped string suitable for inclusion in a string literal
284 * for JavaScript source code.
285 * Illegal control characters are assumed not to be present.
287 * @param string $string
290 public static function escapeJsString( $string ) {
291 // See ECMA 262 section 7.8.4 for string literal format
299 # To avoid closing the element or CDATA section
303 # To avoid any complaints about bad entity refs
306 return strtr( $string, $pairs );
310 * Encode a variable of unknown type to JavaScript.
311 * Doesn't support hashtables just yet.
313 public static function encodeJsVar( $value ) {
314 if ( is_bool( $value ) ) {
315 $s = $value ?
'true' : 'false';
316 } elseif ( is_null( $value ) ) {
318 } elseif ( is_int( $value ) ) {
320 } elseif ( is_array( $value ) ) {
322 foreach ( $value as $name => $elt ) {
326 $s .= self
::encodeJsVar( $elt );
330 $s = '"' . self
::escapeJsString( $value ) . '"';
337 * Check if a string is well-formed XML.
338 * Must include the surrounding tag.
340 * @param $text String: string to test.
343 * @todo Error position reporting return
345 public static function isWellFormed( $text ) {
346 $parser = xml_parser_create( "UTF-8" );
348 # case folding violates XML standard, turn it off
349 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
351 if( !xml_parse( $parser, $text, true ) ) {
352 //$err = xml_error_string( xml_get_error_code( $parser ) );
353 //$position = xml_get_current_byte_index( $parser );
354 //$fragment = $this->extractFragment( $html, $position );
355 //$this->mXmlError = "$err at byte $position:\n$fragment";
356 xml_parser_free( $parser );
359 xml_parser_free( $parser );
364 * Check if a string is a well-formed XML fragment.
365 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
366 * and can use HTML named entities.
368 * @param $text String:
371 public static function isWellFormedXmlFragment( $text ) {
373 Sanitizer
::hackDocType() .
377 return Xml
::isWellFormed( $html );
381 * Replace " > and < with their respective HTML entities ( ",
384 * @param $in String: text that might contain HTML tags.
385 * @return string Escaped string
387 public static function escapeTagsOnly( $in ) {
389 array( '"', '>', '<' ),
390 array( '"', '>', '<' ),