* Added redirect to section feature. Use it wisely.
[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 $contents = UtfNormal::cleanUp( $contents );
54 }
55 return self::element( $element, $attribs, $contents );
56 }
57
58 // Shortcuts
59 public static function openElement( $element, $attribs = null ) { return self::element( $element, $attribs, null ); }
60 public static function closeElement( $element ) { return "</$element>"; }
61
62 /**
63 * Create a namespace selector
64 *
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
69 */
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 );
79 }
80 }
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;
85 }
86 foreach ($arr as $index => $name) {
87 if ($index < NS_MAIN) continue;
88
89 $name = $index !== 0 ? $name : wfMsg('blanknamespace');
90
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";
97 }
98 }
99 $s .= "</select>\n";
100 return $s;
101 }
102
103 public static function span( $text, $class, $attribs=array() ) {
104 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
105 }
106
107 /**
108 * Convenience function to build an HTML text input field
109 * @return string HTML
110 */
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 );
116 }
117
118 /**
119 * Internal function for use in checkboxes and radio buttons and such.
120 * @return array
121 */
122 public static function attrib( $name, $present = true ) {
123 return $present ? array( $name => $name ) : array();
124 }
125
126 /**
127 * Convenience function to build an HTML checkbox
128 * @return string HTML
129 */
130 public static function check( $name, $checked=false, $attribs=array() ) {
131 return self::element( 'input', array(
132 'name' => $name,
133 'type' => 'checkbox',
134 'value' => 1 ) + self::attrib( 'checked', $checked ) + $attribs );
135 }
136
137 /**
138 * Convenience function to build an HTML radio button
139 * @return string HTML
140 */
141 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
142 return self::element( 'input', array(
143 'name' => $name,
144 'type' => 'radio',
145 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
146 }
147
148 /**
149 * Convenience function to build an HTML form label
150 * @return string HTML
151 */
152 public static function label( $label, $id ) {
153 return self::element( 'label', array( 'for' => $id ), $label );
154 }
155
156 /**
157 * Convenience function to build an HTML text input field with a label
158 * @return string HTML
159 */
160 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
161 return Xml::label( $label, $id ) .
162 '&nbsp;' .
163 self::input( $name, $size, $value, array( 'id' => $id ) + $attribs );
164 }
165
166 /**
167 * Convenience function to build an HTML checkbox with a label
168 * @return string HTML
169 */
170 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
171 return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
172 '&nbsp;' .
173 self::label( $label, $id );
174 }
175
176 /**
177 * Convenience function to build an HTML radio button with a label
178 * @return string HTML
179 */
180 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
181 return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
182 '&nbsp;' .
183 self::label( $label, $id );
184 }
185
186 /**
187 * Convenience function to build an HTML submit button
188 * @param $value String: label text for the button
189 * @param $attribs Array: optional custom attributes
190 * @return string HTML
191 */
192 public static function submitButton( $value, $attribs=array() ) {
193 return self::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
194 }
195
196 /**
197 * Convenience function to build an HTML hidden form field.
198 * @todo Document $name parameter.
199 * @param $name FIXME
200 * @param $value String: label text for the button
201 * @param $attribs Array: optional custom attributes
202 * @return string HTML
203 */
204 public static function hidden( $name, $value, $attribs=array() ) {
205 return self::element( 'input', array(
206 'name' => $name,
207 'type' => 'hidden',
208 'value' => $value ) + $attribs );
209 }
210
211 /**
212 * Convenience function to build an HTML drop-down list item.
213 * @param $text String: text for this item
214 * @param $value String: form submission value; if empty, use text
215 * @param $selected boolean: if true, will be the default selected item
216 * @param $attribs array: optional additional HTML attributes
217 * @return string HTML
218 */
219 public static function option( $text, $value=null, $selected=false,
220 $attribs=array() ) {
221 if( !is_null( $value ) ) {
222 $attribs['value'] = $value;
223 }
224 if( $selected ) {
225 $attribs['selected'] = 'selected';
226 }
227 return self::element( 'option', $attribs, $text );
228 }
229
230 /**
231 * Returns an escaped string suitable for inclusion in a string literal
232 * for JavaScript source code.
233 * Illegal control characters are assumed not to be present.
234 *
235 * @param string $string
236 * @return string
237 */
238 public static function escapeJsString( $string ) {
239 // See ECMA 262 section 7.8.4 for string literal format
240 $pairs = array(
241 "\\" => "\\\\",
242 "\"" => "\\\"",
243 '\'' => '\\\'',
244 "\n" => "\\n",
245 "\r" => "\\r",
246
247 # To avoid closing the element or CDATA section
248 "<" => "\\x3c",
249 ">" => "\\x3e",
250
251 # To avoid any complaints about bad entity refs
252 "&" => "\\x26",
253 );
254 return strtr( $string, $pairs );
255 }
256
257 /**
258 * Encode a variable of unknown type to JavaScript.
259 * Doesn't support hashtables just yet.
260 */
261 public static function encodeJsVar( $value ) {
262 if ( is_bool( $value ) ) {
263 $s = $value ? 'true' : 'false';
264 } elseif ( is_null( $value ) ) {
265 $s = 'null';
266 } elseif ( is_int( $value ) ) {
267 $s = $value;
268 } elseif ( is_array( $value ) ) {
269 $s = '[';
270 foreach ( $value as $name => $elt ) {
271 if ( $s != '[' ) {
272 $s .= ', ';
273 }
274 $s .= self::encodeJsVar( $elt );
275 }
276 $s .= ']';
277 } else {
278 $s = '"' . self::escapeJsString( $value ) . '"';
279 }
280 return $s;
281 }
282
283
284 /**
285 * Check if a string is well-formed XML.
286 * Must include the surrounding tag.
287 *
288 * @param $text String: string to test.
289 * @return bool
290 *
291 * @todo Error position reporting return
292 */
293 public static function isWellFormed( $text ) {
294 $parser = xml_parser_create( "UTF-8" );
295
296 # case folding violates XML standard, turn it off
297 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
298
299 if( !xml_parse( $parser, $text, true ) ) {
300 //$err = xml_error_string( xml_get_error_code( $parser ) );
301 //$position = xml_get_current_byte_index( $parser );
302 //$fragment = $this->extractFragment( $html, $position );
303 //$this->mXmlError = "$err at byte $position:\n$fragment";
304 xml_parser_free( $parser );
305 return false;
306 }
307 xml_parser_free( $parser );
308 return true;
309 }
310
311 /**
312 * Check if a string is a well-formed XML fragment.
313 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
314 * and can use HTML named entities.
315 *
316 * @param $text String:
317 * @return bool
318 */
319 public static function isWellFormedXmlFragment( $text ) {
320 $html =
321 Sanitizer::hackDocType() .
322 '<html>' .
323 $text .
324 '</html>';
325 return Xml::isWellFormed( $html );
326 }
327
328 /**
329 * Escape html tags
330 * Basically replacing " > and < with HTML entities ( &quot;, &gt;, &lt;)
331 *
332 * @param $in String: text that might contain HTML tags.
333 * @return string Escaped string
334 */
335 function escapeTagsOnly( $in ) {
336 return str_replace(
337 array( '"', '>', '<' ),
338 array( '&quot;', '&gt;', '&lt;' ),
339 $in );
340 }
341 }
342 ?>