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 ) {
44 if( is_null( $attribs ) ) {
48 foreach( $attribs as $name => $val ) {
49 $out .= ' ' . $name . '="' . Sanitizer
::encodeAttribute( $val ) . '"';
56 * Format an XML element as with self::element(), but run text through the
57 * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
60 * @param $element String:
61 * @param $attribs Array: Name=>value pairs. Values will be escaped.
62 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
65 public static function elementClean( $element, $attribs = array(), $contents = '') {
67 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
70 wfProfileIn( __METHOD__
. '-norm' );
71 $contents = UtfNormal
::cleanUp( $contents );
72 wfProfileOut( __METHOD__
. '-norm' );
74 return self
::element( $element, $attribs, $contents );
77 /** This open an XML element */
78 public static function openElement( $element, $attribs = null ) {
79 return '<' . $element . self
::expandAttributes( $attribs ) . '>';
83 public static function closeElement( $element ) { return "</$element>"; }
86 * Same as <link>element</link>, but does not escape contents. Handy when the
87 * content you have is already valid xml.
89 public static function tags( $element, $attribs = null, $contents ) {
90 return self
::openElement( $element, $attribs ) . $contents . "</$element>";
94 * Create a namespace selector
96 * @param $selected Mixed: the namespace which should be selected, default ''
97 * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default)
98 * @param $includehidden Bool: include hidden namespaces?
99 * @return String: Html string containing the namespace selector
101 public static function namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
103 if( is_null( $selected ) )
105 $s = "\n<select id='namespace' name='namespace' class='namespaceselector'>\n";
106 $arr = $wgContLang->getFormattedNamespaces();
107 if( !is_null($allnamespaces) ) {
108 $arr = array($allnamespaces => wfMsg('namespacesall')) +
$arr;
110 foreach ($arr as $index => $name) {
111 if ($index < NS_MAIN
) continue;
113 $name = $index !== 0 ?
$name : wfMsg('blanknamespace');
115 if ($index === $selected) {
116 $s .= "\t" . self
::element("option",
117 array("value" => $index, "selected" => "selected"),
120 $s .= "\t" . self
::element("option", array("value" => $index), $name) . "\n";
128 * Create a date selector
130 * @param $selected Mixed: the month which should be selected, default ''
131 * @param $allmonths String: value of a special item denoting all month. Null to not include (default)
132 * @return String: Html string containing the month selector
134 public static function monthSelector($selected = '', $allmonths = null) {
135 if( is_null( $selected ) )
137 $s = "\n<select id='month' name='month' class='monthselector'>\n";
138 $arr = Language
::$mMonthMsgs;
140 if( !is_null($allmonths) ) {
141 $arr = array($allmonths => 'monthsall') +
$arr;
143 foreach ($arr as $index => $name) {
144 $message = wfMsgHtml($name);
145 $index++
; // Let January be 1
147 if ($index === $selected) {
148 $s .= "\t" . self
::element("option",
149 array("value" => $index, "selected" => "selected"), $message) . "\n";
151 $s .= "\t" . self
::element("option", array("value" => $index), $message) . "\n";
160 * @param $language The language code of the selected language
161 * @param $customisedOnly If true only languages which have some content are listed
162 * @return array of label and select
164 public static function languageSelector( $selected, $customisedOnly = true ) {
165 global $wgContLanguageCode;
167 * Make sure the site language is in the list; a custom language code
168 * might not have a defined name...
170 $languages = Language
::getLanguageNames( $customisedOnly );
171 if( !array_key_exists( $wgContLanguageCode, $languages ) ) {
172 $languages[$wgContLanguageCode] = $wgContLanguageCode;
177 * If a bogus value is set, default to the content language.
178 * Otherwise, no default is selected and the user ends up
179 * with an Afrikaans interface since it's first in the list.
181 $selected = isset( $languages[$selected] ) ?
$selected : $wgContLanguageCode;
183 foreach( $languages as $code => $name ) {
184 $options .= Xml
::option( "$code - $name", $code, ($code == $selected) ) . "\n";
188 Xml
::label( wfMsg('yourlanguage'), 'wpUserLanguage' ),
190 array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ),
197 public static function span( $text, $class, $attribs=array() ) {
198 return self
::element( 'span', array( 'class' => $class ) +
$attribs, $text );
202 * Convenience function to build an HTML text input field
203 * @return string HTML
205 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
206 return self
::element( 'input', array(
209 'value' => $value ) +
$attribs );
213 * Convenience function to build an HTML password input field
214 * @return string HTML
216 public static function password( $name, $size=false, $value=false, $attribs=array() ) {
217 return self
::input( $name, $size, $value, array_merge($attribs, array('type' => 'password')));
221 * Internal function for use in checkboxes and radio buttons and such.
224 public static function attrib( $name, $present = true ) {
225 return $present ?
array( $name => $name ) : array();
229 * Convenience function to build an HTML checkbox
230 * @return string HTML
232 public static function check( $name, $checked=false, $attribs=array() ) {
233 return self
::element( 'input', array_merge(
236 'type' => 'checkbox',
238 self
::attrib( 'checked', $checked ),
243 * Convenience function to build an HTML radio button
244 * @return string HTML
246 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
247 return self
::element( 'input', array(
250 'value' => $value ) + self
::attrib( 'checked', $checked ) +
$attribs );
254 * Convenience function to build an HTML form label
255 * @return string HTML
257 public static function label( $label, $id ) {
258 return self
::element( 'label', array( 'for' => $id ), $label );
262 * Convenience function to build an HTML text input field with a label
263 * @return string HTML
265 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
266 return Xml
::label( $label, $id ) .
268 self
::input( $name, $size, $value, array( 'id' => $id ) +
$attribs );
272 * Convenience function to build an HTML checkbox with a label
273 * @return string HTML
275 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
276 return self
::check( $name, $checked, array( 'id' => $id ) +
$attribs ) .
278 self
::label( $label, $id );
282 * Convenience function to build an HTML radio button with a label
283 * @return string HTML
285 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
286 return self
::radio( $name, $value, $checked, array( 'id' => $id ) +
$attribs ) .
288 self
::label( $label, $id );
292 * Convenience function to build an HTML submit button
293 * @param $value String: label text for the button
294 * @param $attribs Array: optional custom attributes
295 * @return string HTML
297 public static function submitButton( $value, $attribs=array() ) {
298 return self
::element( 'input', array( 'type' => 'submit', 'value' => $value ) +
$attribs );
302 * Convenience function to build an HTML hidden form field.
303 * @todo Document $name parameter.
305 * @param $value String: label text for the button
306 * @param $attribs Array: optional custom attributes
307 * @return string HTML
309 public static function hidden( $name, $value, $attribs=array() ) {
310 return self
::element( 'input', array(
313 'value' => $value ) +
$attribs );
317 * Convenience function to build an HTML drop-down list item.
318 * @param $text String: text for this item
319 * @param $value String: form submission value; if empty, use text
320 * @param $selected boolean: if true, will be the default selected item
321 * @param $attribs array: optional additional HTML attributes
322 * @return string HTML
324 public static function option( $text, $value=null, $selected=false,
326 if( !is_null( $value ) ) {
327 $attribs['value'] = $value;
330 $attribs['selected'] = 'selected';
332 return self
::element( 'option', $attribs, $text );
336 * Returns an escaped string suitable for inclusion in a string literal
337 * for JavaScript source code.
338 * Illegal control characters are assumed not to be present.
340 * @param string $string
343 public static function escapeJsString( $string ) {
344 // See ECMA 262 section 7.8.4 for string literal format
352 # To avoid closing the element or CDATA section
356 # To avoid any complaints about bad entity refs
359 return strtr( $string, $pairs );
363 * Encode a variable of unknown type to JavaScript.
364 * Doesn't support hashtables just yet.
366 public static function encodeJsVar( $value ) {
367 if ( is_bool( $value ) ) {
368 $s = $value ?
'true' : 'false';
369 } elseif ( is_null( $value ) ) {
371 } elseif ( is_int( $value ) ) {
373 } elseif ( is_array( $value ) ) {
375 foreach ( $value as $name => $elt ) {
379 $s .= self
::encodeJsVar( $elt );
383 $s = '"' . self
::escapeJsString( $value ) . '"';
390 * Check if a string is well-formed XML.
391 * Must include the surrounding tag.
393 * @param $text String: string to test.
396 * @todo Error position reporting return
398 public static function isWellFormed( $text ) {
399 $parser = xml_parser_create( "UTF-8" );
401 # case folding violates XML standard, turn it off
402 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
404 if( !xml_parse( $parser, $text, true ) ) {
405 //$err = xml_error_string( xml_get_error_code( $parser ) );
406 //$position = xml_get_current_byte_index( $parser );
407 //$fragment = $this->extractFragment( $html, $position );
408 //$this->mXmlError = "$err at byte $position:\n$fragment";
409 xml_parser_free( $parser );
412 xml_parser_free( $parser );
417 * Check if a string is a well-formed XML fragment.
418 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
419 * and can use HTML named entities.
421 * @param $text String:
424 public static function isWellFormedXmlFragment( $text ) {
426 Sanitizer
::hackDocType() .
430 return Xml
::isWellFormed( $html );
434 * Replace " > and < with their respective HTML entities ( ",
437 * @param $in String: text that might contain HTML tags.
438 * @return string Escaped string
440 public static function escapeTagsOnly( $in ) {
442 array( '"', '>', '<' ),
443 array( '"', '>', '<' ),