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 ) . '"';
53 * Format an XML element as with self::element(), but run text through the
54 * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
57 * @param $element String:
58 * @param $attribs Array: Name=>value pairs. Values will be escaped.
59 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
62 public static function elementClean( $element, $attribs = array(), $contents = '') {
64 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
67 wfProfileIn( __METHOD__
. '-norm' );
68 $contents = UtfNormal
::cleanUp( $contents );
69 wfProfileOut( __METHOD__
. '-norm' );
71 return self
::element( $element, $attribs, $contents );
74 /** This open an XML element */
75 public static function openElement( $element, $attribs = null ) {
76 return '<' . $element . self
::expandAttributes( $attribs ) . '>';
80 public static function closeElement( $element ) { return "</$element>"; }
83 * Same as <link>element</link>, but does not escape contents. Handy when the
84 * content you have is already valid xml.
86 public static function tags( $element, $attribs = null, $contents ) {
87 return self
::openElement( $element, $attribs ) . $contents . "</$element>";
91 * Build a drop-down box for selecting a namespace
93 * @param mixed $selected Namespace which should be pre-selected
94 * @param mixed $all Value of an item denoting all namespaces, or null to omit
95 * @param bool $hidden Include hidden namespaces? [WTF? --RC]
98 public static function namespaceSelector( $selected = '', $all = null, $hidden = false ) {
100 $namespaces = $wgContLang->getFormattedNamespaces();
103 if( !is_null( $all ) )
104 $namespaces = array( $all => wfMsg( 'namespacesall' ) ) +
$namespaces;
105 foreach( $namespaces as $index => $name ) {
106 if( $index < NS_MAIN
)
109 $name = wfMsg( 'blanknamespace' );
110 $options[] = self
::option( $name, $index, $index === $selected );
113 return Xml
::openElement( 'select', array( 'id' => 'namespace', 'name' => 'namespace',
114 'class' => 'namespaceselector' ) )
116 . implode( "\n", $options )
118 . Xml
::closeElement( 'select' );
122 * Create a date selector
124 * @param $selected Mixed: the month which should be selected, default ''
125 * @param $allmonths String: value of a special item denoting all month. Null to not include (default)
126 * @param string $id Element identifier
127 * @return String: Html string containing the month selector
129 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
132 if( is_null( $selected ) )
134 if( !is_null( $allmonths ) )
135 $options[] = self
::option( wfMsg( 'monthsall' ), $allmonths, $selected === $allmonths );
136 for( $i = 1; $i < 13; $i++
)
137 $options[] = self
::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
138 return self
::openElement( 'select', array( 'id' => $id, 'name' => 'month' ) )
139 . implode( "\n", $options )
140 . self
::closeElement( 'select' );
145 * @param $language The language code of the selected language
146 * @param $customisedOnly If true only languages which have some content are listed
147 * @return array of label and select
149 public static function languageSelector( $selected, $customisedOnly = true ) {
150 global $wgContLanguageCode;
152 * Make sure the site language is in the list; a custom language code
153 * might not have a defined name...
155 $languages = Language
::getLanguageNames( $customisedOnly );
156 if( !array_key_exists( $wgContLanguageCode, $languages ) ) {
157 $languages[$wgContLanguageCode] = $wgContLanguageCode;
162 * If a bogus value is set, default to the content language.
163 * Otherwise, no default is selected and the user ends up
164 * with an Afrikaans interface since it's first in the list.
166 $selected = isset( $languages[$selected] ) ?
$selected : $wgContLanguageCode;
168 foreach( $languages as $code => $name ) {
169 $options .= Xml
::option( "$code - $name", $code, ($code == $selected) ) . "\n";
173 Xml
::label( wfMsg('yourlanguage'), 'wpUserLanguage' ),
175 array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ),
182 public static function span( $text, $class, $attribs=array() ) {
183 return self
::element( 'span', array( 'class' => $class ) +
$attribs, $text );
187 * Convenience function to build an HTML text input field
188 * @return string HTML
190 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
191 return self
::element( 'input', array(
194 'value' => $value ) +
$attribs );
198 * Convenience function to build an HTML password input field
199 * @return string HTML
201 public static function password( $name, $size=false, $value=false, $attribs=array() ) {
202 return self
::input( $name, $size, $value, array_merge($attribs, array('type' => 'password')));
206 * Internal function for use in checkboxes and radio buttons and such.
209 public static function attrib( $name, $present = true ) {
210 return $present ?
array( $name => $name ) : array();
214 * Convenience function to build an HTML checkbox
215 * @return string HTML
217 public static function check( $name, $checked=false, $attribs=array() ) {
218 return self
::element( 'input', array_merge(
221 'type' => 'checkbox',
223 self
::attrib( 'checked', $checked ),
228 * Convenience function to build an HTML radio button
229 * @return string HTML
231 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
232 return self
::element( 'input', array(
235 'value' => $value ) + self
::attrib( 'checked', $checked ) +
$attribs );
239 * Convenience function to build an HTML form label
240 * @return string HTML
242 public static function label( $label, $id ) {
243 return self
::element( 'label', array( 'for' => $id ), $label );
247 * Convenience function to build an HTML text input field with a label
248 * @return string HTML
250 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
251 return Xml
::label( $label, $id ) .
253 self
::input( $name, $size, $value, array( 'id' => $id ) +
$attribs );
257 * Convenience function to build an HTML checkbox with a label
258 * @return string HTML
260 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
261 return self
::check( $name, $checked, array( 'id' => $id ) +
$attribs ) .
263 self
::label( $label, $id );
267 * Convenience function to build an HTML radio button with a label
268 * @return string HTML
270 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
271 return self
::radio( $name, $value, $checked, array( 'id' => $id ) +
$attribs ) .
273 self
::label( $label, $id );
277 * Convenience function to build an HTML submit button
278 * @param $value String: label text for the button
279 * @param $attribs Array: optional custom attributes
280 * @return string HTML
282 public static function submitButton( $value, $attribs=array() ) {
283 return self
::element( 'input', array( 'type' => 'submit', 'value' => $value ) +
$attribs );
287 * Convenience function to build an HTML hidden form field.
288 * @todo Document $name parameter.
290 * @param $value String: label text for the button
291 * @param $attribs Array: optional custom attributes
292 * @return string HTML
294 public static function hidden( $name, $value, $attribs=array() ) {
295 return self
::element( 'input', array(
298 'value' => $value ) +
$attribs );
302 * Convenience function to build an HTML drop-down list item.
303 * @param $text String: text for this item
304 * @param $value String: form submission value; if empty, use text
305 * @param $selected boolean: if true, will be the default selected item
306 * @param $attribs array: optional additional HTML attributes
307 * @return string HTML
309 public static function option( $text, $value=null, $selected=false,
311 if( !is_null( $value ) ) {
312 $attribs['value'] = $value;
315 $attribs['selected'] = 'selected';
317 return self
::element( 'option', $attribs, $text );
321 * Returns an escaped string suitable for inclusion in a string literal
322 * for JavaScript source code.
323 * Illegal control characters are assumed not to be present.
325 * @param string $string
328 public static function escapeJsString( $string ) {
329 // See ECMA 262 section 7.8.4 for string literal format
337 # To avoid closing the element or CDATA section
341 # To avoid any complaints about bad entity refs
344 # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
345 # Encode certain Unicode formatting chars so affected
346 # versions of Gecko don't misinterpret our strings;
347 # this is a common problem with Farsi text.
348 "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
349 "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
351 return strtr( $string, $pairs );
355 * Encode a variable of unknown type to JavaScript.
356 * Arrays are converted to JS arrays, objects are converted to JS associative
357 * arrays (objects). So cast your PHP associative arrays to objects before
358 * passing them to here.
360 public static function encodeJsVar( $value ) {
361 if ( is_bool( $value ) ) {
362 $s = $value ?
'true' : 'false';
363 } elseif ( is_null( $value ) ) {
365 } elseif ( is_int( $value ) ) {
367 } elseif ( is_array( $value ) ) {
369 foreach ( $value as $elt ) {
373 $s .= self
::encodeJsVar( $elt );
376 } elseif ( is_object( $value ) ) {
378 foreach ( (array)$value as $name => $elt ) {
382 $s .= '"' . self
::escapeJsString( $name ) . '": ' .
383 self
::encodeJsVar( $elt );
387 $s = '"' . self
::escapeJsString( $value ) . '"';
394 * Check if a string is well-formed XML.
395 * Must include the surrounding tag.
397 * @param $text String: string to test.
400 * @todo Error position reporting return
402 public static function isWellFormed( $text ) {
403 $parser = xml_parser_create( "UTF-8" );
405 # case folding violates XML standard, turn it off
406 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
408 if( !xml_parse( $parser, $text, true ) ) {
409 //$err = xml_error_string( xml_get_error_code( $parser ) );
410 //$position = xml_get_current_byte_index( $parser );
411 //$fragment = $this->extractFragment( $html, $position );
412 //$this->mXmlError = "$err at byte $position:\n$fragment";
413 xml_parser_free( $parser );
416 xml_parser_free( $parser );
421 * Check if a string is a well-formed XML fragment.
422 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
423 * and can use HTML named entities.
425 * @param $text String:
428 public static function isWellFormedXmlFragment( $text ) {
430 Sanitizer
::hackDocType() .
434 return Xml
::isWellFormed( $html );
438 * Replace " > and < with their respective HTML entities ( ",
441 * @param $in String: text that might contain HTML tags.
442 * @return string Escaped string
444 public static function escapeTagsOnly( $in ) {
446 array( '"', '>', '<' ),
447 array( '"', '>', '<' ),