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 ) {
137 if( is_null( $selected ) )
139 if( !is_null( $allmonths ) )
140 $options[] = self
::option( wfMsg( 'monthsall' ), $allmonths, $selected === $allmonths );
141 for( $i = 1; $i < 13; $i++
)
142 $options[] = self
::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
143 return self
::openElement( 'select', array( 'id' => 'month', 'name' => 'month' ) )
144 . implode( "\n", $options )
145 . self
::closeElement( 'select' );
150 * @param $language The language code of the selected language
151 * @param $customisedOnly If true only languages which have some content are listed
152 * @return array of label and select
154 public static function languageSelector( $selected, $customisedOnly = true ) {
155 global $wgContLanguageCode;
157 * Make sure the site language is in the list; a custom language code
158 * might not have a defined name...
160 $languages = Language
::getLanguageNames( $customisedOnly );
161 if( !array_key_exists( $wgContLanguageCode, $languages ) ) {
162 $languages[$wgContLanguageCode] = $wgContLanguageCode;
167 * If a bogus value is set, default to the content language.
168 * Otherwise, no default is selected and the user ends up
169 * with an Afrikaans interface since it's first in the list.
171 $selected = isset( $languages[$selected] ) ?
$selected : $wgContLanguageCode;
173 foreach( $languages as $code => $name ) {
174 $options .= Xml
::option( "$code - $name", $code, ($code == $selected) ) . "\n";
178 Xml
::label( wfMsg('yourlanguage'), 'wpUserLanguage' ),
180 array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ),
187 public static function span( $text, $class, $attribs=array() ) {
188 return self
::element( 'span', array( 'class' => $class ) +
$attribs, $text );
192 * Convenience function to build an HTML text input field
193 * @return string HTML
195 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
196 return self
::element( 'input', array(
199 'value' => $value ) +
$attribs );
203 * Convenience function to build an HTML password input field
204 * @return string HTML
206 public static function password( $name, $size=false, $value=false, $attribs=array() ) {
207 return self
::input( $name, $size, $value, array_merge($attribs, array('type' => 'password')));
211 * Internal function for use in checkboxes and radio buttons and such.
214 public static function attrib( $name, $present = true ) {
215 return $present ?
array( $name => $name ) : array();
219 * Convenience function to build an HTML checkbox
220 * @return string HTML
222 public static function check( $name, $checked=false, $attribs=array() ) {
223 return self
::element( 'input', array_merge(
226 'type' => 'checkbox',
228 self
::attrib( 'checked', $checked ),
233 * Convenience function to build an HTML radio button
234 * @return string HTML
236 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
237 return self
::element( 'input', array(
240 'value' => $value ) + self
::attrib( 'checked', $checked ) +
$attribs );
244 * Convenience function to build an HTML form label
245 * @return string HTML
247 public static function label( $label, $id ) {
248 return self
::element( 'label', array( 'for' => $id ), $label );
252 * Convenience function to build an HTML text input field with a label
253 * @return string HTML
255 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
256 return Xml
::label( $label, $id ) .
258 self
::input( $name, $size, $value, array( 'id' => $id ) +
$attribs );
262 * Convenience function to build an HTML checkbox with a label
263 * @return string HTML
265 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
266 return self
::check( $name, $checked, array( 'id' => $id ) +
$attribs ) .
268 self
::label( $label, $id );
272 * Convenience function to build an HTML radio button with a label
273 * @return string HTML
275 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
276 return self
::radio( $name, $value, $checked, array( 'id' => $id ) +
$attribs ) .
278 self
::label( $label, $id );
282 * Convenience function to build an HTML submit button
283 * @param $value String: label text for the button
284 * @param $attribs Array: optional custom attributes
285 * @return string HTML
287 public static function submitButton( $value, $attribs=array() ) {
288 return self
::element( 'input', array( 'type' => 'submit', 'value' => $value ) +
$attribs );
292 * Convenience function to build an HTML hidden form field.
293 * @todo Document $name parameter.
295 * @param $value String: label text for the button
296 * @param $attribs Array: optional custom attributes
297 * @return string HTML
299 public static function hidden( $name, $value, $attribs=array() ) {
300 return self
::element( 'input', array(
303 'value' => $value ) +
$attribs );
307 * Convenience function to build an HTML drop-down list item.
308 * @param $text String: text for this item
309 * @param $value String: form submission value; if empty, use text
310 * @param $selected boolean: if true, will be the default selected item
311 * @param $attribs array: optional additional HTML attributes
312 * @return string HTML
314 public static function option( $text, $value=null, $selected=false,
316 if( !is_null( $value ) ) {
317 $attribs['value'] = $value;
320 $attribs['selected'] = 'selected';
322 return self
::element( 'option', $attribs, $text );
326 * Returns an escaped string suitable for inclusion in a string literal
327 * for JavaScript source code.
328 * Illegal control characters are assumed not to be present.
330 * @param string $string
333 public static function escapeJsString( $string ) {
334 // See ECMA 262 section 7.8.4 for string literal format
342 # To avoid closing the element or CDATA section
346 # To avoid any complaints about bad entity refs
349 return strtr( $string, $pairs );
353 * Encode a variable of unknown type to JavaScript.
354 * Doesn't support hashtables just yet.
356 public static function encodeJsVar( $value ) {
357 if ( is_bool( $value ) ) {
358 $s = $value ?
'true' : 'false';
359 } elseif ( is_null( $value ) ) {
361 } elseif ( is_int( $value ) ) {
363 } elseif ( is_array( $value ) ) {
365 foreach ( $value as $name => $elt ) {
369 $s .= self
::encodeJsVar( $elt );
373 $s = '"' . self
::escapeJsString( $value ) . '"';
380 * Check if a string is well-formed XML.
381 * Must include the surrounding tag.
383 * @param $text String: string to test.
386 * @todo Error position reporting return
388 public static function isWellFormed( $text ) {
389 $parser = xml_parser_create( "UTF-8" );
391 # case folding violates XML standard, turn it off
392 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
394 if( !xml_parse( $parser, $text, true ) ) {
395 //$err = xml_error_string( xml_get_error_code( $parser ) );
396 //$position = xml_get_current_byte_index( $parser );
397 //$fragment = $this->extractFragment( $html, $position );
398 //$this->mXmlError = "$err at byte $position:\n$fragment";
399 xml_parser_free( $parser );
402 xml_parser_free( $parser );
407 * Check if a string is a well-formed XML fragment.
408 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
409 * and can use HTML named entities.
411 * @param $text String:
414 public static function isWellFormedXmlFragment( $text ) {
416 Sanitizer
::hackDocType() .
420 return Xml
::isWellFormed( $html );
424 * Replace " > and < with their respective HTML entities ( ",
427 * @param $in String: text that might contain HTML tags.
428 * @return string Escaped string
430 public static function escapeTagsOnly( $in ) {
432 array( '"', '>', '<' ),
433 array( '"', '>', '<' ),