Live hack: Skip some work on empty category/link sets
[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 string $element
10 * @param array $attribs Name=>value pairs. Values will be escaped.
11 * @param string $contents 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 string $element
39 * @param array $attribs Name=>value pairs. Values will be escaped.
40 * @param string $contents 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 mixed $selected The namespace which should be selected, default ''
61 * @param string $allnamespaces Value of a special item denoting all namespaces. Null to not include (default)
62 * @param bool $includehidden Include hidden namespaces?
63 * @return 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 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 string $value Label text for the button
184 * @param array $attribs 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 * @param string $value Label text for the button
194 * @param array $attribs optional custom attributes
195 * @return string HTML
196 */
197 function wfHidden( $name, $value, $attribs=array() ) {
198 return wfElement( 'input', array(
199 'name' => $name,
200 'type' => 'hidden',
201 'value' => $value ) + $attribs );
202 }
203
204 /**
205 * Returns an escaped string suitable for inclusion in a string literal
206 * for JavaScript source code.
207 * Illegal control characters are assumed not to be present.
208 *
209 * @param string $string
210 * @return string
211 */
212 function wfEscapeJsString( $string ) {
213 // See ECMA 262 section 7.8.4 for string literal format
214 $pairs = array(
215 "\\" => "\\\\",
216 "\"" => "\\\"",
217 '\'' => '\\\'',
218 "\n" => "\\n",
219 "\r" => "\\r",
220
221 # To avoid closing the element or CDATA section
222 "<" => "\\x3c",
223 ">" => "\\x3e",
224 );
225 return strtr( $string, $pairs );
226 }
227
228 /**
229 * Check if a string is well-formed XML.
230 * Must include the surrounding tag.
231 *
232 * @param string $text
233 * @return bool
234 *
235 * @todo Error position reporting return
236 */
237 function wfIsWellFormedXml( $text ) {
238 $parser = xml_parser_create( "UTF-8" );
239
240 # case folding violates XML standard, turn it off
241 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
242
243 if( !xml_parse( $parser, $text, true ) ) {
244 $err = xml_error_string( xml_get_error_code( $parser ) );
245 $position = xml_get_current_byte_index( $parser );
246 //$fragment = $this->extractFragment( $html, $position );
247 //$this->mXmlError = "$err at byte $position:\n$fragment";
248 xml_parser_free( $parser );
249 return false;
250 }
251 xml_parser_free( $parser );
252 return true;
253 }
254
255 /**
256 * Check if a string is a well-formed XML fragment.
257 * Wraps fragment in an <html> bit and doctype, so it can be a fragment
258 * and can use HTML named entities.
259 *
260 * @param string $text
261 * @return bool
262 */
263 function wfIsWellFormedXmlFragment( $text ) {
264 $html =
265 Sanitizer::hackDocType() .
266 '<html>' .
267 $text .
268 '</html>';
269 return wfIsWellFormedXml( $html );
270 }
271
272
273 ?>