Throw an exception for invalid data in expandAttributes() so we get a backtrace and...
[lhc/web/wiklou.git] / includes / Xml.php
1 <?php
2
3 /**
4 * Module of static functions for generating XML
5 */
6
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.
13 *
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
18 */
19 public static function element( $element, $attribs = null, $contents = '') {
20 $out = '<' . $element;
21 if( !is_null( $attribs ) ) {
22 $out .= self::expandAttributes( $attribs );
23 }
24 if( is_null( $contents ) ) {
25 $out .= '>';
26 } else {
27 if( $contents === '' ) {
28 $out .= ' />';
29 } else {
30 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
31 }
32 }
33 return $out;
34 }
35
36 /**
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
42 */
43 private static function expandAttributes( $attribs ) {
44 $out = '';
45 if( is_array( $attribs ) ) {
46 foreach( $attribs as $name => $val )
47 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
48 } else {
49 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
50 }
51 return $out;
52 }
53
54 /**
55 * Format an XML element as with self::element(), but run text through the
56 * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
57 * is passed.
58 *
59 * @param $element String:
60 * @param $attribs Array: Name=>value pairs. Values will be escaped.
61 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
62 * @return string
63 */
64 public static function elementClean( $element, $attribs = array(), $contents = '') {
65 if( $attribs ) {
66 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
67 }
68 if( $contents ) {
69 wfProfileIn( __METHOD__ . '-norm' );
70 $contents = UtfNormal::cleanUp( $contents );
71 wfProfileOut( __METHOD__ . '-norm' );
72 }
73 return self::element( $element, $attribs, $contents );
74 }
75
76 /** This open an XML element */
77 public static function openElement( $element, $attribs = null ) {
78 return '<' . $element . self::expandAttributes( $attribs ) . '>';
79 }
80
81 // Shortcut
82 public static function closeElement( $element ) { return "</$element>"; }
83
84 /**
85 * Same as <link>element</link>, but does not escape contents. Handy when the
86 * content you have is already valid xml.
87 */
88 public static function tags( $element, $attribs = null, $contents ) {
89 return self::openElement( $element, $attribs ) . $contents . "</$element>";
90 }
91
92 /**
93 * Build a drop-down box for selecting a namespace
94 *
95 * @param mixed $selected Namespace which should be pre-selected
96 * @param mixed $all Value of an item denoting all namespaces, or null to omit
97 * @param bool $hidden Include hidden namespaces? [WTF? --RC]
98 * @return string
99 */
100 public static function namespaceSelector( $selected = '', $all = null, $hidden = false ) {
101 global $wgContLang;
102 $namespaces = $wgContLang->getFormattedNamespaces();
103 $options = array();
104
105 if( !is_null( $all ) )
106 $namespaces = array( $all => wfMsg( 'namespacesall' ) ) + $namespaces;
107 foreach( $namespaces as $index => $name ) {
108 if( $index < NS_MAIN )
109 continue;
110 if( $index === 0 )
111 $name = wfMsg( 'blanknamespace' );
112 $options[] = self::option( $name, $index, $index === $selected );
113 }
114
115 return Xml::openElement( 'select', array( 'id' => 'namespace', 'name' => 'namespace',
116 'class' => 'namespaceselector' ) )
117 . "\n"
118 . implode( "\n", $options )
119 . "\n"
120 . Xml::closeElement( 'select' );
121 }
122
123 /**
124 * Create a date selector
125 *
126 * @param $selected Mixed: the month which should be selected, default ''
127 * @param $allmonths String: value of a special item denoting all month. Null to not include (default)
128 * @param string $id Element identifier
129 * @return String: Html string containing the month selector
130 */
131 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
132 global $wgLang;
133 $options = array();
134 if( is_null( $selected ) )
135 $selected = '';
136 if( !is_null( $allmonths ) )
137 $options[] = self::option( wfMsg( 'monthsall' ), $allmonths, $selected === $allmonths );
138 for( $i = 1; $i < 13; $i++ )
139 $options[] = self::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
140 return self::openElement( 'select', array( 'id' => $id, 'name' => 'month' ) )
141 . implode( "\n", $options )
142 . self::closeElement( 'select' );
143 }
144
145 /**
146 *
147 * @param $language The language code of the selected language
148 * @param $customisedOnly If true only languages which have some content are listed
149 * @return array of label and select
150 */
151 public static function languageSelector( $selected, $customisedOnly = true ) {
152 global $wgContLanguageCode;
153 /**
154 * Make sure the site language is in the list; a custom language code
155 * might not have a defined name...
156 */
157 $languages = Language::getLanguageNames( $customisedOnly );
158 if( !array_key_exists( $wgContLanguageCode, $languages ) ) {
159 $languages[$wgContLanguageCode] = $wgContLanguageCode;
160 }
161 ksort( $languages );
162
163 /**
164 * If a bogus value is set, default to the content language.
165 * Otherwise, no default is selected and the user ends up
166 * with an Afrikaans interface since it's first in the list.
167 */
168 $selected = isset( $languages[$selected] ) ? $selected : $wgContLanguageCode;
169 $options = "\n";
170 foreach( $languages as $code => $name ) {
171 $options .= Xml::option( "$code - $name", $code, ($code == $selected) ) . "\n";
172 }
173
174 return array(
175 Xml::label( wfMsg('yourlanguage'), 'wpUserLanguage' ),
176 Xml::tags( 'select',
177 array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ),
178 $options
179 )
180 );
181
182 }
183
184 public static function span( $text, $class, $attribs=array() ) {
185 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
186 }
187
188 /**
189 * Convenience function to build an HTML text input field
190 * @return string HTML
191 */
192 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
193 return self::element( 'input', array(
194 'name' => $name,
195 'size' => $size,
196 'value' => $value ) + $attribs );
197 }
198
199 /**
200 * Convenience function to build an HTML password input field
201 * @return string HTML
202 */
203 public static function password( $name, $size=false, $value=false, $attribs=array() ) {
204 return self::input( $name, $size, $value, array_merge($attribs, array('type' => 'password')));
205 }
206
207 /**
208 * Internal function for use in checkboxes and radio buttons and such.
209 * @return array
210 */
211 public static function attrib( $name, $present = true ) {
212 return $present ? array( $name => $name ) : array();
213 }
214
215 /**
216 * Convenience function to build an HTML checkbox
217 * @return string HTML
218 */
219 public static function check( $name, $checked=false, $attribs=array() ) {
220 return self::element( 'input', array_merge(
221 array(
222 'name' => $name,
223 'type' => 'checkbox',
224 'value' => 1 ),
225 self::attrib( 'checked', $checked ),
226 $attribs ) );
227 }
228
229 /**
230 * Convenience function to build an HTML radio button
231 * @return string HTML
232 */
233 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
234 return self::element( 'input', array(
235 'name' => $name,
236 'type' => 'radio',
237 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
238 }
239
240 /**
241 * Convenience function to build an HTML form label
242 * @return string HTML
243 */
244 public static function label( $label, $id ) {
245 return self::element( 'label', array( 'for' => $id ), $label );
246 }
247
248 /**
249 * Convenience function to build an HTML text input field with a label
250 * @return string HTML
251 */
252 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
253 return Xml::label( $label, $id ) .
254 '&nbsp;' .
255 self::input( $name, $size, $value, array( 'id' => $id ) + $attribs );
256 }
257
258 /**
259 * Convenience function to build an HTML checkbox with a label
260 * @return string HTML
261 */
262 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
263 return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
264 '&nbsp;' .
265 self::label( $label, $id );
266 }
267
268 /**
269 * Convenience function to build an HTML radio button with a label
270 * @return string HTML
271 */
272 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
273 return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
274 '&nbsp;' .
275 self::label( $label, $id );
276 }
277
278 /**
279 * Convenience function to build an HTML submit button
280 * @param $value String: label text for the button
281 * @param $attribs Array: optional custom attributes
282 * @return string HTML
283 */
284 public static function submitButton( $value, $attribs=array() ) {
285 return self::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
286 }
287
288 /**
289 * Convenience function to build an HTML hidden form field.
290 * @todo Document $name parameter.
291 * @param $name FIXME
292 * @param $value String: label text for the button
293 * @param $attribs Array: optional custom attributes
294 * @return string HTML
295 */
296 public static function hidden( $name, $value, $attribs=array() ) {
297 return self::element( 'input', array(
298 'name' => $name,
299 'type' => 'hidden',
300 'value' => $value ) + $attribs );
301 }
302
303 /**
304 * Convenience function to build an HTML drop-down list item.
305 * @param $text String: text for this item
306 * @param $value String: form submission value; if empty, use text
307 * @param $selected boolean: if true, will be the default selected item
308 * @param $attribs array: optional additional HTML attributes
309 * @return string HTML
310 */
311 public static function option( $text, $value=null, $selected=false,
312 $attribs=array() ) {
313 if( !is_null( $value ) ) {
314 $attribs['value'] = $value;
315 }
316 if( $selected ) {
317 $attribs['selected'] = 'selected';
318 }
319 return self::element( 'option', $attribs, $text );
320 }
321
322 /**
323 * Returns an escaped string suitable for inclusion in a string literal
324 * for JavaScript source code.
325 * Illegal control characters are assumed not to be present.
326 *
327 * @param string $string
328 * @return string
329 */
330 public static function escapeJsString( $string ) {
331 // See ECMA 262 section 7.8.4 for string literal format
332 $pairs = array(
333 "\\" => "\\\\",
334 "\"" => "\\\"",
335 '\'' => '\\\'',
336 "\n" => "\\n",
337 "\r" => "\\r",
338
339 # To avoid closing the element or CDATA section
340 "<" => "\\x3c",
341 ">" => "\\x3e",
342
343 # To avoid any complaints about bad entity refs
344 "&" => "\\x26",
345
346 # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
347 # Encode certain Unicode formatting chars so affected
348 # versions of Gecko don't misinterpret our strings;
349 # this is a common problem with Farsi text.
350 "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
351 "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
352 );
353 return strtr( $string, $pairs );
354 }
355
356 /**
357 * Encode a variable of unknown type to JavaScript.
358 * Arrays are converted to JS arrays, objects are converted to JS associative
359 * arrays (objects). So cast your PHP associative arrays to objects before
360 * passing them to here.
361 */
362 public static function encodeJsVar( $value ) {
363 if ( is_bool( $value ) ) {
364 $s = $value ? 'true' : 'false';
365 } elseif ( is_null( $value ) ) {
366 $s = 'null';
367 } elseif ( is_int( $value ) ) {
368 $s = $value;
369 } elseif ( is_array( $value ) ) {
370 $s = '[';
371 foreach ( $value as $elt ) {
372 if ( $s != '[' ) {
373 $s .= ', ';
374 }
375 $s .= self::encodeJsVar( $elt );
376 }
377 $s .= ']';
378 } elseif ( is_object( $value ) ) {
379 $s = '{';
380 foreach ( (array)$value as $name => $elt ) {
381 if ( $s != '{' ) {
382 $s .= ', ';
383 }
384 $s .= '"' . self::escapeJsString( $name ) . '": ' .
385 self::encodeJsVar( $elt );
386 }
387 $s .= '}';
388 } else {
389 $s = '"' . self::escapeJsString( $value ) . '"';
390 }
391 return $s;
392 }
393
394
395 /**
396 * Check if a string is well-formed XML.
397 * Must include the surrounding tag.
398 *
399 * @param $text String: string to test.
400 * @return bool
401 *
402 * @todo Error position reporting return
403 */
404 public static function isWellFormed( $text ) {
405 $parser = xml_parser_create( "UTF-8" );
406
407 # case folding violates XML standard, turn it off
408 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
409
410 if( !xml_parse( $parser, $text, true ) ) {
411 //$err = xml_error_string( xml_get_error_code( $parser ) );
412 //$position = xml_get_current_byte_index( $parser );
413 //$fragment = $this->extractFragment( $html, $position );
414 //$this->mXmlError = "$err at byte $position:\n$fragment";
415 xml_parser_free( $parser );
416 return false;
417 }
418 xml_parser_free( $parser );
419 return true;
420 }
421
422 /**
423 * Check if a string is a well-formed XML fragment.
424 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
425 * and can use HTML named entities.
426 *
427 * @param $text String:
428 * @return bool
429 */
430 public static function isWellFormedXmlFragment( $text ) {
431 $html =
432 Sanitizer::hackDocType() .
433 '<html>' .
434 $text .
435 '</html>';
436 return Xml::isWellFormed( $html );
437 }
438
439 /**
440 * Replace " > and < with their respective HTML entities ( &quot;,
441 * &gt;, &lt;)
442 *
443 * @param $in String: text that might contain HTML tags.
444 * @return string Escaped string
445 */
446 public static function escapeTagsOnly( $in ) {
447 return str_replace(
448 array( '"', '>', '<' ),
449 array( '&quot;', '&gt;', '&lt;' ),
450 $in );
451 }
452 }
453