revert the 21393, which was revert of 21389, which was revert of 20291.
[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 foreach( $attribs as $name => $val ) {
23 $out .= ' ' . $name . '="' . Sanitizer::encodeAttribute( $val ) . '"';
24 }
25 }
26 if( is_null( $contents ) ) {
27 $out .= '>';
28 } else {
29 if( $contents === '' ) {
30 $out .= ' />';
31 } else {
32 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
33 }
34 }
35 return $out;
36 }
37
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.
42 *
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
47 */
48 public static function elementClean( $element, $attribs = array(), $contents = '') {
49 if( $attribs ) {
50 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
51 }
52 if( $contents ) {
53 wfProfileIn( __METHOD__ . '-norm' );
54 $contents = UtfNormal::cleanUp( $contents );
55 wfProfileOut( __METHOD__ . '-norm' );
56 }
57 return self::element( $element, $attribs, $contents );
58 }
59
60 // Shortcuts
61 public static function openElement( $element, $attribs = null ) { return self::element( $element, $attribs, null ); }
62 public static function closeElement( $element ) { return "</$element>"; }
63
64 /**
65 * Same as <link>element</link>, but does not escape contents. Handy when the
66 * content you have is already valid xml.
67 */
68 public static function tags( $element, $attribs = null, $contents ) {
69 return self::element( $element, $attribs, null ) . $contents . "</$element>";
70 }
71
72 /**
73 * Create a namespace selector
74 *
75 * @param $selected Mixed: the namespace which should be selected, default ''
76 * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default)
77 * @param $includehidden Bool: include hidden namespaces?
78 * @return String: Html string containing the namespace selector
79 */
80 public static function namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
81 global $wgContLang;
82 if( $selected !== '' ) {
83 if( is_null( $selected ) ) {
84 // No namespace selected; let exact match work without hitting Main
85 $selected = '';
86 } else {
87 // Let input be numeric strings without breaking the empty match.
88 $selected = intval( $selected );
89 }
90 }
91 $s = "\n<select id='namespace' name='namespace' class='namespaceselector'>\n";
92 $arr = $wgContLang->getFormattedNamespaces();
93 if( !is_null($allnamespaces) ) {
94 $arr = array($allnamespaces => wfMsg('namespacesall')) + $arr;
95 }
96 foreach ($arr as $index => $name) {
97 if ($index < NS_MAIN) continue;
98
99 $name = $index !== 0 ? $name : wfMsg('blanknamespace');
100
101 if ($index === $selected) {
102 $s .= "\t" . self::element("option",
103 array("value" => $index, "selected" => "selected"),
104 $name) . "\n";
105 } else {
106 $s .= "\t" . self::element("option", array("value" => $index), $name) . "\n";
107 }
108 }
109 $s .= "</select>\n";
110 return $s;
111 }
112
113 public static function span( $text, $class, $attribs=array() ) {
114 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
115 }
116
117 /**
118 * Convenience function to build an HTML text input field
119 * @return string HTML
120 */
121 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
122 return self::element( 'input', array(
123 'name' => $name,
124 'size' => $size,
125 'value' => $value ) + $attribs );
126 }
127
128 /**
129 * Internal function for use in checkboxes and radio buttons and such.
130 * @return array
131 */
132 public static function attrib( $name, $present = true ) {
133 return $present ? array( $name => $name ) : array();
134 }
135
136 /**
137 * Convenience function to build an HTML checkbox
138 * @return string HTML
139 */
140 public static function check( $name, $checked=false, $attribs=array() ) {
141 return self::element( 'input', array_merge(
142 array(
143 'name' => $name,
144 'type' => 'checkbox',
145 'value' => 1 ),
146 self::attrib( 'checked', $checked ),
147 $attribs ) );
148 }
149
150 /**
151 * Convenience function to build an HTML radio button
152 * @return string HTML
153 */
154 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
155 return self::element( 'input', array(
156 'name' => $name,
157 'type' => 'radio',
158 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
159 }
160
161 /**
162 * Convenience function to build an HTML form label
163 * @return string HTML
164 */
165 public static function label( $label, $id ) {
166 return self::element( 'label', array( 'for' => $id ), $label );
167 }
168
169 /**
170 * Convenience function to build an HTML text input field with a label
171 * @return string HTML
172 */
173 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
174 return Xml::label( $label, $id ) .
175 '&nbsp;' .
176 self::input( $name, $size, $value, array( 'id' => $id ) + $attribs );
177 }
178
179 /**
180 * Convenience function to build an HTML checkbox with a label
181 * @return string HTML
182 */
183 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
184 return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
185 '&nbsp;' .
186 self::label( $label, $id );
187 }
188
189 /**
190 * Convenience function to build an HTML radio button with a label
191 * @return string HTML
192 */
193 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
194 return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
195 '&nbsp;' .
196 self::label( $label, $id );
197 }
198
199 /**
200 * Convenience function to build an HTML submit button
201 * @param $value String: label text for the button
202 * @param $attribs Array: optional custom attributes
203 * @return string HTML
204 */
205 public static function submitButton( $value, $attribs=array() ) {
206 return self::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
207 }
208
209 /**
210 * Convenience function to build an HTML hidden form field.
211 * @todo Document $name parameter.
212 * @param $name FIXME
213 * @param $value String: label text for the button
214 * @param $attribs Array: optional custom attributes
215 * @return string HTML
216 */
217 public static function hidden( $name, $value, $attribs=array() ) {
218 return self::element( 'input', array(
219 'name' => $name,
220 'type' => 'hidden',
221 'value' => $value ) + $attribs );
222 }
223
224 /**
225 * Convenience function to build an HTML drop-down list item.
226 * @param $text String: text for this item
227 * @param $value String: form submission value; if empty, use text
228 * @param $selected boolean: if true, will be the default selected item
229 * @param $attribs array: optional additional HTML attributes
230 * @return string HTML
231 */
232 public static function option( $text, $value=null, $selected=false,
233 $attribs=array() ) {
234 if( !is_null( $value ) ) {
235 $attribs['value'] = $value;
236 }
237 if( $selected ) {
238 $attribs['selected'] = 'selected';
239 }
240 return self::element( 'option', $attribs, $text );
241 }
242
243 /**
244 * Returns an escaped string suitable for inclusion in a string literal
245 * for JavaScript source code.
246 * Illegal control characters are assumed not to be present.
247 *
248 * @param string $string
249 * @return string
250 */
251 public static function escapeJsString( $string ) {
252 // See ECMA 262 section 7.8.4 for string literal format
253 $pairs = array(
254 "\\" => "\\\\",
255 "\"" => "\\\"",
256 '\'' => '\\\'',
257 "\n" => "\\n",
258 "\r" => "\\r",
259
260 # To avoid closing the element or CDATA section
261 "<" => "\\x3c",
262 ">" => "\\x3e",
263
264 # To avoid any complaints about bad entity refs
265 "&" => "\\x26",
266 );
267 return strtr( $string, $pairs );
268 }
269
270 /**
271 * Encode a variable of unknown type to JavaScript.
272 * Doesn't support hashtables just yet.
273 */
274 public static function encodeJsVar( $value ) {
275 if ( is_bool( $value ) ) {
276 $s = $value ? 'true' : 'false';
277 } elseif ( is_null( $value ) ) {
278 $s = 'null';
279 } elseif ( is_int( $value ) ) {
280 $s = $value;
281 } elseif ( is_array( $value ) ) {
282 $s = '[';
283 foreach ( $value as $name => $elt ) {
284 if ( $s != '[' ) {
285 $s .= ', ';
286 }
287 $s .= self::encodeJsVar( $elt );
288 }
289 $s .= ']';
290 } else {
291 $s = '"' . self::escapeJsString( $value ) . '"';
292 }
293 return $s;
294 }
295
296
297 /**
298 * Check if a string is well-formed XML.
299 * Must include the surrounding tag.
300 *
301 * @param $text String: string to test.
302 * @return bool
303 *
304 * @todo Error position reporting return
305 */
306 public static function isWellFormed( $text ) {
307 $parser = xml_parser_create( "UTF-8" );
308
309 # case folding violates XML standard, turn it off
310 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
311
312 if( !xml_parse( $parser, $text, true ) ) {
313 //$err = xml_error_string( xml_get_error_code( $parser ) );
314 //$position = xml_get_current_byte_index( $parser );
315 //$fragment = $this->extractFragment( $html, $position );
316 //$this->mXmlError = "$err at byte $position:\n$fragment";
317 xml_parser_free( $parser );
318 return false;
319 }
320 xml_parser_free( $parser );
321 return true;
322 }
323
324 /**
325 * Check if a string is a well-formed XML fragment.
326 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
327 * and can use HTML named entities.
328 *
329 * @param $text String:
330 * @return bool
331 */
332 public static function isWellFormedXmlFragment( $text ) {
333 $html =
334 Sanitizer::hackDocType() .
335 '<html>' .
336 $text .
337 '</html>';
338 return Xml::isWellFormed( $html );
339 }
340
341 /**
342 * Replace " > and < with their respective HTML entities ( &quot;,
343 * &gt;, &lt;)
344 *
345 * @param $in String: text that might contain HTML tags.
346 * @return string Escaped string
347 */
348 public static function escapeTagsOnly( $in ) {
349 return str_replace(
350 array( '"', '>', '<' ),
351 array( '&quot;', '&gt;', '&lt;' ),
352 $in );
353 }
354 }
355 ?>