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