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