Updates for Indonesian (id) localisation. New translations (block-log-flags...) and...
[mediawiki.git] / includes / Xml.php
blob67dda7fea9708b043bb4237129448a89df053d81
1 <?php
3 /**
4 * Module of static functions for generating XML
5 */
7 class Xml {
8 /**
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)
17 * @return string
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 ) ) {
27 $out .= '>';
28 } else {
29 if( $contents === '' ) {
30 $out .= ' />';
31 } else {
32 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
35 return $out;
38 /**
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
41 * is passed.
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)
46 * @return string
48 public static function elementClean( $element, $attribs = array(), $contents = '') {
49 if( $attribs ) {
50 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
52 if( $contents ) {
53 $contents = UtfNormal::cleanUp( $contents );
55 return self::element( $element, $attribs, $contents );
58 // Shortcuts
59 public static function openElement( $element, $attribs = null ) { return self::element( $element, $attribs, null ); }
60 public static function closeElement( $element ) { return "</$element>"; }
62 /**
63 * Create a namespace selector
65 * @param $selected Mixed: the namespace which should be selected, default ''
66 * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default)
67 * @param $includehidden Bool: include hidden namespaces?
68 * @return String: Html string containing the namespace selector
70 public static function &namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
71 global $wgContLang;
72 if( $selected !== '' ) {
73 if( is_null( $selected ) ) {
74 // No namespace selected; let exact match work without hitting Main
75 $selected = '';
76 } else {
77 // Let input be numeric strings without breaking the empty match.
78 $selected = intval( $selected );
81 $s = "\n<select id='namespace' name='namespace' class='namespaceselector'>\n";
82 $arr = $wgContLang->getFormattedNamespaces();
83 if( !is_null($allnamespaces) ) {
84 $arr = array($allnamespaces => wfMsg('namespacesall')) + $arr;
86 foreach ($arr as $index => $name) {
87 if ($index < NS_MAIN) continue;
89 $name = $index !== 0 ? $name : wfMsg('blanknamespace');
91 if ($index === $selected) {
92 $s .= "\t" . self::element("option",
93 array("value" => $index, "selected" => "selected"),
94 $name) . "\n";
95 } else {
96 $s .= "\t" . self::element("option", array("value" => $index), $name) . "\n";
99 $s .= "</select>\n";
100 return $s;
103 public static function span( $text, $class, $attribs=array() ) {
104 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
108 * Convenience function to build an HTML text input field
109 * @return string HTML
111 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
112 return self::element( 'input', array(
113 'name' => $name,
114 'size' => $size,
115 'value' => $value ) + $attribs );
119 * Internal function for use in checkboxes and radio buttons and such.
120 * @return array
122 public static function attrib( $name, $present = true ) {
123 return $present ? array( $name => $name ) : array();
127 * Convenience function to build an HTML checkbox
128 * @return string HTML
130 public static function check( $name, $checked=false, $attribs=array() ) {
131 return self::element( 'input', array_merge(
132 array(
133 'name' => $name,
134 'type' => 'checkbox',
135 'value' => 1 ),
136 self::attrib( 'checked', $checked ),
137 $attribs ) );
141 * Convenience function to build an HTML radio button
142 * @return string HTML
144 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
145 return self::element( 'input', array(
146 'name' => $name,
147 'type' => 'radio',
148 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
152 * Convenience function to build an HTML form label
153 * @return string HTML
155 public static function label( $label, $id ) {
156 return self::element( 'label', array( 'for' => $id ), $label );
160 * Convenience function to build an HTML text input field with a label
161 * @return string HTML
163 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
164 return Xml::label( $label, $id ) .
165 '&nbsp;' .
166 self::input( $name, $size, $value, array( 'id' => $id ) + $attribs );
170 * Convenience function to build an HTML checkbox with a label
171 * @return string HTML
173 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
174 return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
175 '&nbsp;' .
176 self::label( $label, $id );
180 * Convenience function to build an HTML radio button with a label
181 * @return string HTML
183 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
184 return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
185 '&nbsp;' .
186 self::label( $label, $id );
190 * Convenience function to build an HTML submit button
191 * @param $value String: label text for the button
192 * @param $attribs Array: optional custom attributes
193 * @return string HTML
195 public static function submitButton( $value, $attribs=array() ) {
196 return self::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
200 * Convenience function to build an HTML hidden form field.
201 * @todo Document $name parameter.
202 * @param $name FIXME
203 * @param $value String: label text for the button
204 * @param $attribs Array: optional custom attributes
205 * @return string HTML
207 public static function hidden( $name, $value, $attribs=array() ) {
208 return self::element( 'input', array(
209 'name' => $name,
210 'type' => 'hidden',
211 'value' => $value ) + $attribs );
215 * Convenience function to build an HTML drop-down list item.
216 * @param $text String: text for this item
217 * @param $value String: form submission value; if empty, use text
218 * @param $selected boolean: if true, will be the default selected item
219 * @param $attribs array: optional additional HTML attributes
220 * @return string HTML
222 public static function option( $text, $value=null, $selected=false,
223 $attribs=array() ) {
224 if( !is_null( $value ) ) {
225 $attribs['value'] = $value;
227 if( $selected ) {
228 $attribs['selected'] = 'selected';
230 return self::element( 'option', $attribs, $text );
234 * Returns an escaped string suitable for inclusion in a string literal
235 * for JavaScript source code.
236 * Illegal control characters are assumed not to be present.
238 * @param string $string
239 * @return string
241 public static function escapeJsString( $string ) {
242 // See ECMA 262 section 7.8.4 for string literal format
243 $pairs = array(
244 "\\" => "\\\\",
245 "\"" => "\\\"",
246 '\'' => '\\\'',
247 "\n" => "\\n",
248 "\r" => "\\r",
250 # To avoid closing the element or CDATA section
251 "<" => "\\x3c",
252 ">" => "\\x3e",
254 # To avoid any complaints about bad entity refs
255 "&" => "\\x26",
257 return strtr( $string, $pairs );
261 * Encode a variable of unknown type to JavaScript.
262 * Doesn't support hashtables just yet.
264 public static function encodeJsVar( $value ) {
265 if ( is_bool( $value ) ) {
266 $s = $value ? 'true' : 'false';
267 } elseif ( is_null( $value ) ) {
268 $s = 'null';
269 } elseif ( is_int( $value ) ) {
270 $s = $value;
271 } elseif ( is_array( $value ) ) {
272 $s = '[';
273 foreach ( $value as $name => $elt ) {
274 if ( $s != '[' ) {
275 $s .= ', ';
277 $s .= self::encodeJsVar( $elt );
279 $s .= ']';
280 } else {
281 $s = '"' . self::escapeJsString( $value ) . '"';
283 return $s;
288 * Check if a string is well-formed XML.
289 * Must include the surrounding tag.
291 * @param $text String: string to test.
292 * @return bool
294 * @todo Error position reporting return
296 public static function isWellFormed( $text ) {
297 $parser = xml_parser_create( "UTF-8" );
299 # case folding violates XML standard, turn it off
300 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
302 if( !xml_parse( $parser, $text, true ) ) {
303 //$err = xml_error_string( xml_get_error_code( $parser ) );
304 //$position = xml_get_current_byte_index( $parser );
305 //$fragment = $this->extractFragment( $html, $position );
306 //$this->mXmlError = "$err at byte $position:\n$fragment";
307 xml_parser_free( $parser );
308 return false;
310 xml_parser_free( $parser );
311 return true;
315 * Check if a string is a well-formed XML fragment.
316 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
317 * and can use HTML named entities.
319 * @param $text String:
320 * @return bool
322 public static function isWellFormedXmlFragment( $text ) {
323 $html =
324 Sanitizer::hackDocType() .
325 '<html>' .
326 $text .
327 '</html>';
328 return Xml::isWellFormed( $html );
332 * Replace " > and < with their respective HTML entities ( &quot;,
333 * &gt;, &lt;)
335 * @param $in String: text that might contain HTML tags.
336 * @return string Escaped string
338 public static function escapeTagsOnly( $in ) {
339 return str_replace(
340 array( '"', '>', '<' ),
341 array( '&quot;', '&gt;', '&lt;' ),
342 $in );