Switching from phpdoc to doxygen (use less than 32MB of memory).
[lhc/web/wiklou.git] / includes / XmlFunctions.php
1 <?php
2
3 /**
4 * Format an XML element with given attributes and, optionally, text content.
5 * Element and attribute names are assumed to be ready for literal inclusion.
6 * Strings are assumed to not contain XML-illegal characters; special
7 * characters (<, >, &) are escaped but illegals are not touched.
8 *
9 * @param $element String:
10 * @param $attribs Array: Name=>value pairs. Values will be escaped.
11 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
12 * @return string
13 */
14 function wfElement( $element, $attribs = null, $contents = '') {
15 $out = '<' . $element;
16 if( !is_null( $attribs ) ) {
17 foreach( $attribs as $name => $val ) {
18 $out .= ' ' . $name . '="' . htmlspecialchars( $val ) . '"';
19 }
20 }
21 if( is_null( $contents ) ) {
22 $out .= '>';
23 } else {
24 if( $contents === '' ) {
25 $out .= ' />';
26 } else {
27 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
28 }
29 }
30 return $out;
31 }
32
33 /**
34 * Format an XML element as with wfElement(), but run text through the
35 * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
36 * is passed.
37 *
38 * @param $element String:
39 * @param $attribs Array: Name=>value pairs. Values will be escaped.
40 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
41 * @return string
42 */
43 function wfElementClean( $element, $attribs = array(), $contents = '') {
44 if( $attribs ) {
45 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
46 }
47 if( $contents ) {
48 $contents = UtfNormal::cleanUp( $contents );
49 }
50 return wfElement( $element, $attribs, $contents );
51 }
52
53 // Shortcuts
54 function wfOpenElement( $element, $attribs = null ) { return wfElement( $element, $attribs, null ); }
55 function wfCloseElement( $element ) { return "</$element>"; }
56
57 /**
58 * Create a namespace selector
59 *
60 * @param $selected Mixed: the namespace which should be selected, default ''
61 * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default)
62 * @param $includehidden Bool: include hidden namespaces?
63 * @return String: Html string containing the namespace selector
64 */
65 function &HTMLnamespaceselector($selected = '', $allnamespaces = null, $includehidden=false) {
66 global $wgContLang;
67 if( $selected !== '' ) {
68 if( is_null( $selected ) ) {
69 // No namespace selected; let exact match work without hitting Main
70 $selected = '';
71 } else {
72 // Let input be numeric strings without breaking the empty match.
73 $selected = intval( $selected );
74 }
75 }
76 $s = "<select id='namespace' name='namespace' class='namespaceselector'>\n\t";
77 $arr = $wgContLang->getFormattedNamespaces();
78 if( !is_null($allnamespaces) ) {
79 $arr = array($allnamespaces => wfMsgHtml('namespacesall')) + $arr;
80 }
81 foreach ($arr as $index => $name) {
82 if ($index < NS_MAIN) continue;
83
84 $name = $index !== 0 ? $name : wfMsgHtml('blanknamespace');
85
86 if ($index === $selected) {
87 $s .= wfElement("option",
88 array("value" => $index, "selected" => "selected"),
89 $name);
90 } else {
91 $s .= wfElement("option", array("value" => $index), $name);
92 }
93 }
94 $s .= "\n</select>\n";
95 return $s;
96 }
97
98 function wfSpan( $text, $class, $attribs=array() ) {
99 return wfElement( 'span', array( 'class' => $class ) + $attribs, $text );
100 }
101
102 /**
103 * Convenience function to build an HTML text input field
104 * @return string HTML
105 */
106 function wfInput( $name, $size=false, $value=false, $attribs=array() ) {
107 return wfElement( 'input', array(
108 'name' => $name,
109 'size' => $size,
110 'value' => $value ) + $attribs );
111 }
112
113 /**
114 * Internal function for use in checkboxes and radio buttons and such.
115 * @return array
116 */
117 function wfAttrib( $name, $present = true ) {
118 return $present ? array( $name => $name ) : array();
119 }
120
121 /**
122 * Convenience function to build an HTML checkbox
123 * @return string HTML
124 */
125 function wfCheck( $name, $checked=false, $attribs=array() ) {
126 return wfElement( 'input', array(
127 'name' => $name,
128 'type' => 'checkbox',
129 'value' => 1 ) + wfAttrib( 'checked', $checked ) + $attribs );
130 }
131
132 /**
133 * Convenience function to build an HTML radio button
134 * @return string HTML
135 */
136 function wfRadio( $name, $value, $checked=false, $attribs=array() ) {
137 return wfElement( 'input', array(
138 'name' => $name,
139 'type' => 'radio',
140 'value' => $value ) + wfAttrib( 'checked', $checked ) + $attribs );
141 }
142
143 /**
144 * Convenience function to build an HTML form label
145 * @return string HTML
146 */
147 function wfLabel( $label, $id ) {
148 return wfElement( 'label', array( 'for' => $id ), $label );
149 }
150
151 /**
152 * Convenience function to build an HTML text input field with a label
153 * @return string HTML
154 */
155 function wfInputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
156 return wfLabel( $label, $id ) .
157 '&nbsp;' .
158 wfInput( $name, $size, $value, array( 'id' => $id ) + $attribs );
159 }
160
161 /**
162 * Convenience function to build an HTML checkbox with a label
163 * @return string HTML
164 */
165 function wfCheckLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
166 return wfCheck( $name, $checked, array( 'id' => $id ) + $attribs ) .
167 '&nbsp;' .
168 wfLabel( $label, $id );
169 }
170
171 /**
172 * Convenience function to build an HTML radio button with a label
173 * @return string HTML
174 */
175 function wfRadioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
176 return wfRadio( $name, $checked, $value, array( 'id' => $id ) + $attribs ) .
177 '&nbsp;' .
178 wfLabel( $label, $id );
179 }
180
181 /**
182 * Convenience function to build an HTML submit button
183 * @param $value String: label text for the button
184 * @param $attribs Array: optional custom attributes
185 * @return string HTML
186 */
187 function wfSubmitButton( $value, $attribs=array() ) {
188 return wfElement( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
189 }
190
191 /**
192 * Convenience function to build an HTML hidden form field.
193 * @todo Document $name parameter.
194 * @param $name FIXME
195 * @param $value String: label text for the button
196 * @param $attribs Array: optional custom attributes
197 * @return string HTML
198 */
199 function wfHidden( $name, $value, $attribs=array() ) {
200 return wfElement( 'input', array(
201 'name' => $name,
202 'type' => 'hidden',
203 'value' => $value ) + $attribs );
204 }
205
206 /**
207 * Returns an escaped string suitable for inclusion in a string literal
208 * for JavaScript source code.
209 * Illegal control characters are assumed not to be present.
210 *
211 * @param string $string
212 * @return string
213 */
214 function wfEscapeJsString( $string ) {
215 // See ECMA 262 section 7.8.4 for string literal format
216 $pairs = array(
217 "\\" => "\\\\",
218 "\"" => "\\\"",
219 '\'' => '\\\'',
220 "\n" => "\\n",
221 "\r" => "\\r",
222
223 # To avoid closing the element or CDATA section
224 "<" => "\\x3c",
225 ">" => "\\x3e",
226 );
227 return strtr( $string, $pairs );
228 }
229
230 /**
231 * Check if a string is well-formed XML.
232 * Must include the surrounding tag.
233 *
234 * @param $text String: string to test.
235 * @return bool
236 *
237 * @todo Error position reporting return
238 */
239 function wfIsWellFormedXml( $text ) {
240 $parser = xml_parser_create( "UTF-8" );
241
242 # case folding violates XML standard, turn it off
243 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
244
245 if( !xml_parse( $parser, $text, true ) ) {
246 $err = xml_error_string( xml_get_error_code( $parser ) );
247 $position = xml_get_current_byte_index( $parser );
248 //$fragment = $this->extractFragment( $html, $position );
249 //$this->mXmlError = "$err at byte $position:\n$fragment";
250 xml_parser_free( $parser );
251 return false;
252 }
253 xml_parser_free( $parser );
254 return true;
255 }
256
257 /**
258 * Check if a string is a well-formed XML fragment.
259 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
260 * and can use HTML named entities.
261 *
262 * @param $text String:
263 * @return bool
264 */
265 function wfIsWellFormedXmlFragment( $text ) {
266 $html =
267 Sanitizer::hackDocType() .
268 '<html>' .
269 $text .
270 '</html>';
271 return wfIsWellFormedXml( $html );
272 }
273
274
275 ?>