Index: trunk/phase3/includes/Xml.php |
— | — | @@ -1,282 +1,282 @@ |
2 | | -<?php
|
3 | | -
|
4 | | -/**
|
5 | | - * Module of static functions for generating XML
|
6 | | - */
|
7 | | -
|
8 | | -class Xml {
|
9 | | - /**
|
10 | | - * Format an XML element with given attributes and, optionally, text content.
|
11 | | - * Element and attribute names are assumed to be ready for literal inclusion.
|
12 | | - * Strings are assumed to not contain XML-illegal characters; special
|
13 | | - * characters (<, >, &) are escaped but illegals are not touched.
|
14 | | - *
|
15 | | - * @param $element String:
|
16 | | - * @param $attribs Array: Name=>value pairs. Values will be escaped.
|
17 | | - * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
|
18 | | - * @return string
|
19 | | - */
|
20 | | - public static function element( $element, $attribs = null, $contents = '') {
|
21 | | - $out = '<' . $element;
|
22 | | - if( !is_null( $attribs ) ) {
|
23 | | - foreach( $attribs as $name => $val ) {
|
24 | | - $out .= ' ' . $name . '="' . Sanitizer::encodeAttribute( $val ) . '"';
|
25 | | - }
|
26 | | - }
|
27 | | - if( is_null( $contents ) ) {
|
28 | | - $out .= '>';
|
29 | | - } else {
|
30 | | - if( $contents === '' ) {
|
31 | | - $out .= ' />';
|
32 | | - } else {
|
33 | | - $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
|
34 | | - }
|
35 | | - }
|
36 | | - return $out;
|
37 | | - }
|
38 | | -
|
39 | | - /**
|
40 | | - * Format an XML element as with self::element(), but run text through the
|
41 | | - * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
|
42 | | - * is passed.
|
43 | | - *
|
44 | | - * @param $element String:
|
45 | | - * @param $attribs Array: Name=>value pairs. Values will be escaped.
|
46 | | - * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
|
47 | | - * @return string
|
48 | | - */
|
49 | | - public static function elementClean( $element, $attribs = array(), $contents = '') {
|
50 | | - if( $attribs ) {
|
51 | | - $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
|
52 | | - }
|
53 | | - if( $contents ) {
|
54 | | - $contents = UtfNormal::cleanUp( $contents );
|
55 | | - }
|
56 | | - return self::element( $element, $attribs, $contents );
|
57 | | - }
|
58 | | -
|
59 | | - // Shortcuts
|
60 | | - public static function openElement( $element, $attribs = null ) { return self::element( $element, $attribs, null ); }
|
61 | | - public static function closeElement( $element ) { return "</$element>"; }
|
62 | | -
|
63 | | - /**
|
64 | | - * Create a namespace selector
|
65 | | - *
|
66 | | - * @param $selected Mixed: the namespace which should be selected, default ''
|
67 | | - * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default)
|
68 | | - * @param $includehidden Bool: include hidden namespaces?
|
69 | | - * @return String: Html string containing the namespace selector
|
70 | | - */
|
71 | | - public static function &namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
|
72 | | - global $wgContLang;
|
73 | | - if( $selected !== '' ) {
|
74 | | - if( is_null( $selected ) ) {
|
75 | | - // No namespace selected; let exact match work without hitting Main
|
76 | | - $selected = '';
|
77 | | - } else {
|
78 | | - // Let input be numeric strings without breaking the empty match.
|
79 | | - $selected = intval( $selected );
|
80 | | - }
|
81 | | - }
|
82 | | - $s = "\n<select id='namespace' name='namespace' class='namespaceselector'>\n";
|
83 | | - $arr = $wgContLang->getFormattedNamespaces();
|
84 | | - if( !is_null($allnamespaces) ) {
|
85 | | - $arr = array($allnamespaces => wfMsg('namespacesall')) + $arr;
|
86 | | - }
|
87 | | - foreach ($arr as $index => $name) {
|
88 | | - if ($index < NS_MAIN) continue;
|
89 | | -
|
90 | | - $name = $index !== 0 ? $name : wfMsg('blanknamespace');
|
91 | | -
|
92 | | - if ($index === $selected) {
|
93 | | - $s .= "\t" . self::element("option",
|
94 | | - array("value" => $index, "selected" => "selected"),
|
95 | | - $name) . "\n";
|
96 | | - } else {
|
97 | | - $s .= "\t" . self::element("option", array("value" => $index), $name) . "\n";
|
98 | | - }
|
99 | | - }
|
100 | | - $s .= "</select>\n";
|
101 | | - return $s;
|
102 | | - }
|
103 | | -
|
104 | | - public static function span( $text, $class, $attribs=array() ) {
|
105 | | - return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
|
106 | | - }
|
107 | | -
|
108 | | - /**
|
109 | | - * Convenience function to build an HTML text input field
|
110 | | - * @return string HTML
|
111 | | - */
|
112 | | - public static function input( $name, $size=false, $value=false, $attribs=array() ) {
|
113 | | - return self::element( 'input', array(
|
114 | | - 'name' => $name,
|
115 | | - 'size' => $size,
|
116 | | - 'value' => $value ) + $attribs );
|
117 | | - }
|
118 | | -
|
119 | | - /**
|
120 | | - * Internal function for use in checkboxes and radio buttons and such.
|
121 | | - * @return array
|
122 | | - */
|
123 | | - public static function attrib( $name, $present = true ) {
|
124 | | - return $present ? array( $name => $name ) : array();
|
125 | | - }
|
126 | | -
|
127 | | - /**
|
128 | | - * Convenience function to build an HTML checkbox
|
129 | | - * @return string HTML
|
130 | | - */
|
131 | | - public static function check( $name, $checked=false, $attribs=array() ) {
|
132 | | - return self::element( 'input', array(
|
133 | | - 'name' => $name,
|
134 | | - 'type' => 'checkbox',
|
135 | | - 'value' => 1 ) + self::attrib( 'checked', $checked ) + $attribs );
|
136 | | - }
|
137 | | -
|
138 | | - /**
|
139 | | - * Convenience function to build an HTML radio button
|
140 | | - * @return string HTML
|
141 | | - */
|
142 | | - public static function radio( $name, $value, $checked=false, $attribs=array() ) {
|
143 | | - return self::element( 'input', array(
|
144 | | - 'name' => $name,
|
145 | | - 'type' => 'radio',
|
146 | | - 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
|
147 | | - }
|
148 | | -
|
149 | | - /**
|
150 | | - * Convenience function to build an HTML form label
|
151 | | - * @return string HTML
|
152 | | - */
|
153 | | - public static function label( $label, $id ) {
|
154 | | - return self::element( 'label', array( 'for' => $id ), $label );
|
155 | | - }
|
156 | | -
|
157 | | - /**
|
158 | | - * Convenience function to build an HTML text input field with a label
|
159 | | - * @return string HTML
|
160 | | - */
|
161 | | - public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
|
162 | | - return Xml::label( $label, $id ) .
|
163 | | - ' ' .
|
164 | | - self::input( $name, $size, $value, array( 'id' => $id ) + $attribs );
|
165 | | - }
|
166 | | -
|
167 | | - /**
|
168 | | - * Convenience function to build an HTML checkbox with a label
|
169 | | - * @return string HTML
|
170 | | - */
|
171 | | - public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
|
172 | | - return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
|
173 | | - ' ' .
|
174 | | - self::label( $label, $id );
|
175 | | - }
|
176 | | -
|
177 | | - /**
|
178 | | - * Convenience function to build an HTML radio button with a label
|
179 | | - * @return string HTML
|
180 | | - */
|
181 | | - public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
|
182 | | - return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
|
183 | | - ' ' .
|
184 | | - self::label( $label, $id );
|
185 | | - }
|
186 | | -
|
187 | | - /**
|
188 | | - * Convenience function to build an HTML submit button
|
189 | | - * @param $value String: label text for the button
|
190 | | - * @param $attribs Array: optional custom attributes
|
191 | | - * @return string HTML
|
192 | | - */
|
193 | | - public static function submitButton( $value, $attribs=array() ) {
|
194 | | - return self::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
|
195 | | - }
|
196 | | -
|
197 | | - /**
|
198 | | - * Convenience function to build an HTML hidden form field.
|
199 | | - * @todo Document $name parameter.
|
200 | | - * @param $name FIXME
|
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 hidden( $name, $value, $attribs=array() ) {
|
206 | | - return self::element( 'input', array(
|
207 | | - 'name' => $name,
|
208 | | - 'type' => 'hidden',
|
209 | | - 'value' => $value ) + $attribs );
|
210 | | - }
|
211 | | -
|
212 | | - /**
|
213 | | - * Returns an escaped string suitable for inclusion in a string literal
|
214 | | - * for JavaScript source code.
|
215 | | - * Illegal control characters are assumed not to be present.
|
216 | | - *
|
217 | | - * @param string $string
|
218 | | - * @return string
|
219 | | - */
|
220 | | - public static function escapeJsString( $string ) {
|
221 | | - // See ECMA 262 section 7.8.4 for string literal format
|
222 | | - $pairs = array(
|
223 | | - "\\" => "\\\\",
|
224 | | - "\"" => "\\\"",
|
225 | | - '\'' => '\\\'',
|
226 | | - "\n" => "\\n",
|
227 | | - "\r" => "\\r",
|
228 | | -
|
229 | | - # To avoid closing the element or CDATA section
|
230 | | - "<" => "\\x3c",
|
231 | | - ">" => "\\x3e",
|
232 | | -
|
233 | | - # To avoid any complaints about bad entity refs
|
234 | | - "&" => "\\x26",
|
235 | | - );
|
236 | | - return strtr( $string, $pairs );
|
237 | | - }
|
238 | | -
|
239 | | - /**
|
240 | | - * Check if a string is well-formed XML.
|
241 | | - * Must include the surrounding tag.
|
242 | | - *
|
243 | | - * @param $text String: string to test.
|
244 | | - * @return bool
|
245 | | - *
|
246 | | - * @todo Error position reporting return
|
247 | | - */
|
248 | | - public static function isWellFormed( $text ) {
|
249 | | - $parser = xml_parser_create( "UTF-8" );
|
250 | | -
|
251 | | - # case folding violates XML standard, turn it off
|
252 | | - xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
|
253 | | -
|
254 | | - if( !xml_parse( $parser, $text, true ) ) {
|
255 | | - $err = xml_error_string( xml_get_error_code( $parser ) );
|
256 | | - $position = xml_get_current_byte_index( $parser );
|
257 | | - //$fragment = $this->extractFragment( $html, $position );
|
258 | | - //$this->mXmlError = "$err at byte $position:\n$fragment";
|
259 | | - xml_parser_free( $parser );
|
260 | | - return false;
|
261 | | - }
|
262 | | - xml_parser_free( $parser );
|
263 | | - return true;
|
264 | | - }
|
265 | | -
|
266 | | - /**
|
267 | | - * Check if a string is a well-formed XML fragment.
|
268 | | - * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
|
269 | | - * and can use HTML named entities.
|
270 | | - *
|
271 | | - * @param $text String:
|
272 | | - * @return bool
|
273 | | - */
|
274 | | - public static function isWellFormedXmlFragment( $text ) {
|
275 | | - $html =
|
276 | | - Sanitizer::hackDocType() .
|
277 | | - '<html>' .
|
278 | | - $text .
|
279 | | - '</html>';
|
280 | | - return Xml::isWellFormed( $html );
|
281 | | - }
|
282 | | -}
|
283 | | -?>
|
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Module of static functions for generating XML |
| 6 | + */ |
| 7 | + |
| 8 | +class Xml { |
| 9 | + /** |
| 10 | + * Format an XML element with given attributes and, optionally, text content. |
| 11 | + * Element and attribute names are assumed to be ready for literal inclusion. |
| 12 | + * Strings are assumed to not contain XML-illegal characters; special |
| 13 | + * characters (<, >, &) are escaped but illegals are not touched. |
| 14 | + * |
| 15 | + * @param $element String: |
| 16 | + * @param $attribs Array: Name=>value pairs. Values will be escaped. |
| 17 | + * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default) |
| 18 | + * @return string |
| 19 | + */ |
| 20 | + public static function element( $element, $attribs = null, $contents = '') { |
| 21 | + $out = '<' . $element; |
| 22 | + if( !is_null( $attribs ) ) { |
| 23 | + foreach( $attribs as $name => $val ) { |
| 24 | + $out .= ' ' . $name . '="' . Sanitizer::encodeAttribute( $val ) . '"'; |
| 25 | + } |
| 26 | + } |
| 27 | + if( is_null( $contents ) ) { |
| 28 | + $out .= '>'; |
| 29 | + } else { |
| 30 | + if( $contents === '' ) { |
| 31 | + $out .= ' />'; |
| 32 | + } else { |
| 33 | + $out .= '>' . htmlspecialchars( $contents ) . "</$element>"; |
| 34 | + } |
| 35 | + } |
| 36 | + return $out; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Format an XML element as with self::element(), but run text through the |
| 41 | + * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8 |
| 42 | + * is passed. |
| 43 | + * |
| 44 | + * @param $element String: |
| 45 | + * @param $attribs Array: Name=>value pairs. Values will be escaped. |
| 46 | + * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default) |
| 47 | + * @return string |
| 48 | + */ |
| 49 | + public static function elementClean( $element, $attribs = array(), $contents = '') { |
| 50 | + if( $attribs ) { |
| 51 | + $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs ); |
| 52 | + } |
| 53 | + if( $contents ) { |
| 54 | + $contents = UtfNormal::cleanUp( $contents ); |
| 55 | + } |
| 56 | + return self::element( $element, $attribs, $contents ); |
| 57 | + } |
| 58 | + |
| 59 | + // Shortcuts |
| 60 | + public static function openElement( $element, $attribs = null ) { return self::element( $element, $attribs, null ); } |
| 61 | + public static function closeElement( $element ) { return "</$element>"; } |
| 62 | + |
| 63 | + /** |
| 64 | + * Create a namespace selector |
| 65 | + * |
| 66 | + * @param $selected Mixed: the namespace which should be selected, default '' |
| 67 | + * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default) |
| 68 | + * @param $includehidden Bool: include hidden namespaces? |
| 69 | + * @return String: Html string containing the namespace selector |
| 70 | + */ |
| 71 | + public static function &namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) { |
| 72 | + global $wgContLang; |
| 73 | + if( $selected !== '' ) { |
| 74 | + if( is_null( $selected ) ) { |
| 75 | + // No namespace selected; let exact match work without hitting Main |
| 76 | + $selected = ''; |
| 77 | + } else { |
| 78 | + // Let input be numeric strings without breaking the empty match. |
| 79 | + $selected = intval( $selected ); |
| 80 | + } |
| 81 | + } |
| 82 | + $s = "\n<select id='namespace' name='namespace' class='namespaceselector'>\n"; |
| 83 | + $arr = $wgContLang->getFormattedNamespaces(); |
| 84 | + if( !is_null($allnamespaces) ) { |
| 85 | + $arr = array($allnamespaces => wfMsg('namespacesall')) + $arr; |
| 86 | + } |
| 87 | + foreach ($arr as $index => $name) { |
| 88 | + if ($index < NS_MAIN) continue; |
| 89 | + |
| 90 | + $name = $index !== 0 ? $name : wfMsg('blanknamespace'); |
| 91 | + |
| 92 | + if ($index === $selected) { |
| 93 | + $s .= "\t" . self::element("option", |
| 94 | + array("value" => $index, "selected" => "selected"), |
| 95 | + $name) . "\n"; |
| 96 | + } else { |
| 97 | + $s .= "\t" . self::element("option", array("value" => $index), $name) . "\n"; |
| 98 | + } |
| 99 | + } |
| 100 | + $s .= "</select>\n"; |
| 101 | + return $s; |
| 102 | + } |
| 103 | + |
| 104 | + public static function span( $text, $class, $attribs=array() ) { |
| 105 | + return self::element( 'span', array( 'class' => $class ) + $attribs, $text ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Convenience function to build an HTML text input field |
| 110 | + * @return string HTML |
| 111 | + */ |
| 112 | + public static function input( $name, $size=false, $value=false, $attribs=array() ) { |
| 113 | + return self::element( 'input', array( |
| 114 | + 'name' => $name, |
| 115 | + 'size' => $size, |
| 116 | + 'value' => $value ) + $attribs ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Internal function for use in checkboxes and radio buttons and such. |
| 121 | + * @return array |
| 122 | + */ |
| 123 | + public static function attrib( $name, $present = true ) { |
| 124 | + return $present ? array( $name => $name ) : array(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Convenience function to build an HTML checkbox |
| 129 | + * @return string HTML |
| 130 | + */ |
| 131 | + public static function check( $name, $checked=false, $attribs=array() ) { |
| 132 | + return self::element( 'input', array( |
| 133 | + 'name' => $name, |
| 134 | + 'type' => 'checkbox', |
| 135 | + 'value' => 1 ) + self::attrib( 'checked', $checked ) + $attribs ); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Convenience function to build an HTML radio button |
| 140 | + * @return string HTML |
| 141 | + */ |
| 142 | + public static function radio( $name, $value, $checked=false, $attribs=array() ) { |
| 143 | + return self::element( 'input', array( |
| 144 | + 'name' => $name, |
| 145 | + 'type' => 'radio', |
| 146 | + 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs ); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Convenience function to build an HTML form label |
| 151 | + * @return string HTML |
| 152 | + */ |
| 153 | + public static function label( $label, $id ) { |
| 154 | + return self::element( 'label', array( 'for' => $id ), $label ); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Convenience function to build an HTML text input field with a label |
| 159 | + * @return string HTML |
| 160 | + */ |
| 161 | + public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) { |
| 162 | + return Xml::label( $label, $id ) . |
| 163 | + ' ' . |
| 164 | + self::input( $name, $size, $value, array( 'id' => $id ) + $attribs ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Convenience function to build an HTML checkbox with a label |
| 169 | + * @return string HTML |
| 170 | + */ |
| 171 | + public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) { |
| 172 | + return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) . |
| 173 | + ' ' . |
| 174 | + self::label( $label, $id ); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Convenience function to build an HTML radio button with a label |
| 179 | + * @return string HTML |
| 180 | + */ |
| 181 | + public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) { |
| 182 | + return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) . |
| 183 | + ' ' . |
| 184 | + self::label( $label, $id ); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Convenience function to build an HTML submit button |
| 189 | + * @param $value String: label text for the button |
| 190 | + * @param $attribs Array: optional custom attributes |
| 191 | + * @return string HTML |
| 192 | + */ |
| 193 | + public static function submitButton( $value, $attribs=array() ) { |
| 194 | + return self::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs ); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Convenience function to build an HTML hidden form field. |
| 199 | + * @todo Document $name parameter. |
| 200 | + * @param $name FIXME |
| 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 hidden( $name, $value, $attribs=array() ) { |
| 206 | + return self::element( 'input', array( |
| 207 | + 'name' => $name, |
| 208 | + 'type' => 'hidden', |
| 209 | + 'value' => $value ) + $attribs ); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Returns an escaped string suitable for inclusion in a string literal |
| 214 | + * for JavaScript source code. |
| 215 | + * Illegal control characters are assumed not to be present. |
| 216 | + * |
| 217 | + * @param string $string |
| 218 | + * @return string |
| 219 | + */ |
| 220 | + public static function escapeJsString( $string ) { |
| 221 | + // See ECMA 262 section 7.8.4 for string literal format |
| 222 | + $pairs = array( |
| 223 | + "\\" => "\\\\", |
| 224 | + "\"" => "\\\"", |
| 225 | + '\'' => '\\\'', |
| 226 | + "\n" => "\\n", |
| 227 | + "\r" => "\\r", |
| 228 | + |
| 229 | + # To avoid closing the element or CDATA section |
| 230 | + "<" => "\\x3c", |
| 231 | + ">" => "\\x3e", |
| 232 | + |
| 233 | + # To avoid any complaints about bad entity refs |
| 234 | + "&" => "\\x26", |
| 235 | + ); |
| 236 | + return strtr( $string, $pairs ); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Check if a string is well-formed XML. |
| 241 | + * Must include the surrounding tag. |
| 242 | + * |
| 243 | + * @param $text String: string to test. |
| 244 | + * @return bool |
| 245 | + * |
| 246 | + * @todo Error position reporting return |
| 247 | + */ |
| 248 | + public static function isWellFormed( $text ) { |
| 249 | + $parser = xml_parser_create( "UTF-8" ); |
| 250 | + |
| 251 | + # case folding violates XML standard, turn it off |
| 252 | + xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false ); |
| 253 | + |
| 254 | + if( !xml_parse( $parser, $text, true ) ) { |
| 255 | + $err = xml_error_string( xml_get_error_code( $parser ) ); |
| 256 | + $position = xml_get_current_byte_index( $parser ); |
| 257 | + //$fragment = $this->extractFragment( $html, $position ); |
| 258 | + //$this->mXmlError = "$err at byte $position:\n$fragment"; |
| 259 | + xml_parser_free( $parser ); |
| 260 | + return false; |
| 261 | + } |
| 262 | + xml_parser_free( $parser ); |
| 263 | + return true; |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * Check if a string is a well-formed XML fragment. |
| 268 | + * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment |
| 269 | + * and can use HTML named entities. |
| 270 | + * |
| 271 | + * @param $text String: |
| 272 | + * @return bool |
| 273 | + */ |
| 274 | + public static function isWellFormedXmlFragment( $text ) { |
| 275 | + $html = |
| 276 | + Sanitizer::hackDocType() . |
| 277 | + '<html>' . |
| 278 | + $text . |
| 279 | + '</html>'; |
| 280 | + return Xml::isWellFormed( $html ); |
| 281 | + } |
| 282 | +} |
| 283 | +?> |
Property changes on: trunk/phase3/includes/Xml.php |
___________________________________________________________________ |
Added: svn:eol |
284 | 284 | + native |
Property changes on: trunk/phase3/includes/SearchPostgres.php |
___________________________________________________________________ |
Added: svn:eol |
285 | 285 | + native |
Property changes on: trunk/phase3/includes/AutoLoader.php |
___________________________________________________________________ |
Added: svn:eol |
286 | 286 | + native |
Index: trunk/phase3/includes/ImageFunctions.php |
— | — | @@ -1,223 +1,223 @@ |
2 | | -<?php
|
3 | | -
|
4 | | -/**
|
5 | | - * Returns the image directory of an image
|
6 | | - * The result is an absolute path.
|
7 | | - *
|
8 | | - * This function is called from thumb.php before Setup.php is included
|
9 | | - *
|
10 | | - * @param $fname String: file name of the image file.
|
11 | | - * @public
|
12 | | - */
|
13 | | -function wfImageDir( $fname ) {
|
14 | | - global $wgUploadDirectory, $wgHashedUploadDirectory;
|
15 | | -
|
16 | | - if (!$wgHashedUploadDirectory) { return $wgUploadDirectory; }
|
17 | | -
|
18 | | - $hash = md5( $fname );
|
19 | | - $dest = $wgUploadDirectory . '/' . $hash{0} . '/' . substr( $hash, 0, 2 );
|
20 | | -
|
21 | | - return $dest;
|
22 | | -}
|
23 | | -
|
24 | | -/**
|
25 | | - * Returns the image directory of an image's thubnail
|
26 | | - * The result is an absolute path.
|
27 | | - *
|
28 | | - * This function is called from thumb.php before Setup.php is included
|
29 | | - *
|
30 | | - * @param $fname String: file name of the original image file
|
31 | | - * @param $shared Boolean: (optional) use the shared upload directory (default: 'false').
|
32 | | - * @public
|
33 | | - */
|
34 | | -function wfImageThumbDir( $fname, $shared = false ) {
|
35 | | - $base = wfImageArchiveDir( $fname, 'thumb', $shared );
|
36 | | - if ( Image::isHashed( $shared ) ) {
|
37 | | - $dir = "$base/$fname";
|
38 | | - } else {
|
39 | | - $dir = $base;
|
40 | | - }
|
41 | | -
|
42 | | - return $dir;
|
43 | | -}
|
44 | | -
|
45 | | -/**
|
46 | | - * Old thumbnail directory, kept for conversion
|
47 | | - */
|
48 | | -function wfDeprecatedThumbDir( $thumbName , $subdir='thumb', $shared=false) {
|
49 | | - return wfImageArchiveDir( $thumbName, $subdir, $shared );
|
50 | | -}
|
51 | | -
|
52 | | -/**
|
53 | | - * Returns the image directory of an image's old version
|
54 | | - * The result is an absolute path.
|
55 | | - *
|
56 | | - * This function is called from thumb.php before Setup.php is included
|
57 | | - *
|
58 | | - * @param $fname String: file name of the thumbnail file, including file size prefix.
|
59 | | - * @param $subdir String: subdirectory of the image upload directory that should be used for storing the old version. Default is 'archive'.
|
60 | | - * @param $shared Boolean use the shared upload directory (only relevant for other functions which call this one). Default is 'false'.
|
61 | | - * @public
|
62 | | - */
|
63 | | -function wfImageArchiveDir( $fname , $subdir='archive', $shared=false ) {
|
64 | | - global $wgUploadDirectory, $wgHashedUploadDirectory;
|
65 | | - global $wgSharedUploadDirectory, $wgHashedSharedUploadDirectory;
|
66 | | - $dir = $shared ? $wgSharedUploadDirectory : $wgUploadDirectory;
|
67 | | - $hashdir = $shared ? $wgHashedSharedUploadDirectory : $wgHashedUploadDirectory;
|
68 | | - if (!$hashdir) { return $dir.'/'.$subdir; }
|
69 | | - $hash = md5( $fname );
|
70 | | -
|
71 | | - return $dir.'/'.$subdir.'/'.$hash[0].'/'.substr( $hash, 0, 2 );
|
72 | | -}
|
73 | | -
|
74 | | -
|
75 | | -/*
|
76 | | - * Return the hash path component of an image path (URL or filesystem),
|
77 | | - * e.g. "/3/3c/", or just "/" if hashing is not used.
|
78 | | - *
|
79 | | - * @param $dbkey The filesystem / database name of the file
|
80 | | - * @param $fromSharedDirectory Use the shared file repository? It may
|
81 | | - * use different hash settings from the local one.
|
82 | | - */
|
83 | | -function wfGetHashPath ( $dbkey, $fromSharedDirectory = false ) {
|
84 | | - if( Image::isHashed( $fromSharedDirectory ) ) {
|
85 | | - $hash = md5($dbkey);
|
86 | | - return '/' . $hash{0} . '/' . substr( $hash, 0, 2 ) . '/';
|
87 | | - } else {
|
88 | | - return '/';
|
89 | | - }
|
90 | | -}
|
91 | | -
|
92 | | -/**
|
93 | | - * Returns the image URL of an image's old version
|
94 | | - *
|
95 | | - * @param $name String: file name of the image file
|
96 | | - * @param $subdir String: (optional) subdirectory of the image upload directory that is used by the old version. Default is 'archive'
|
97 | | - * @public
|
98 | | - */
|
99 | | -function wfImageArchiveUrl( $name, $subdir='archive' ) {
|
100 | | - global $wgUploadPath, $wgHashedUploadDirectory;
|
101 | | -
|
102 | | - if ($wgHashedUploadDirectory) {
|
103 | | - $hash = md5( substr( $name, 15) );
|
104 | | - $url = $wgUploadPath.'/'.$subdir.'/' . $hash{0} . '/' .
|
105 | | - substr( $hash, 0, 2 ) . '/'.$name;
|
106 | | - } else {
|
107 | | - $url = $wgUploadPath.'/'.$subdir.'/'.$name;
|
108 | | - }
|
109 | | - return wfUrlencode($url);
|
110 | | -}
|
111 | | -
|
112 | | -/**
|
113 | | - * Return a rounded pixel equivalent for a labeled CSS/SVG length.
|
114 | | - * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers
|
115 | | - *
|
116 | | - * @param $length String: CSS/SVG length.
|
117 | | - * @return Integer: length in pixels
|
118 | | - */
|
119 | | -function wfScaleSVGUnit( $length ) {
|
120 | | - static $unitLength = array(
|
121 | | - 'px' => 1.0,
|
122 | | - 'pt' => 1.25,
|
123 | | - 'pc' => 15.0,
|
124 | | - 'mm' => 3.543307,
|
125 | | - 'cm' => 35.43307,
|
126 | | - 'in' => 90.0,
|
127 | | - '' => 1.0, // "User units" pixels by default
|
128 | | - '%' => 2.0, // Fake it!
|
129 | | - );
|
130 | | - if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
|
131 | | - $length = floatval( $matches[1] );
|
132 | | - $unit = $matches[2];
|
133 | | - return round( $length * $unitLength[$unit] );
|
134 | | - } else {
|
135 | | - // Assume pixels
|
136 | | - return round( floatval( $length ) );
|
137 | | - }
|
138 | | -}
|
139 | | -
|
140 | | -/**
|
141 | | - * Compatible with PHP getimagesize()
|
142 | | - * @todo support gzipped SVGZ
|
143 | | - * @todo check XML more carefully
|
144 | | - * @todo sensible defaults
|
145 | | - *
|
146 | | - * @param $filename String: full name of the file (passed to php fopen()).
|
147 | | - * @return array
|
148 | | - */
|
149 | | -function wfGetSVGsize( $filename ) {
|
150 | | - $width = 256;
|
151 | | - $height = 256;
|
152 | | -
|
153 | | - // Read a chunk of the file
|
154 | | - $f = fopen( $filename, "rt" );
|
155 | | - if( !$f ) return false;
|
156 | | - $chunk = fread( $f, 4096 );
|
157 | | - fclose( $f );
|
158 | | -
|
159 | | - // Uber-crappy hack! Run through a real XML parser.
|
160 | | - if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) {
|
161 | | - return false;
|
162 | | - }
|
163 | | - $tag = $matches[1];
|
164 | | - if( preg_match( '/\bwidth\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
|
165 | | - $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
|
166 | | - }
|
167 | | - if( preg_match( '/\bheight\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) {
|
168 | | - $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) );
|
169 | | - }
|
170 | | -
|
171 | | - return array( $width, $height, 'SVG',
|
172 | | - "width=\"$width\" height=\"$height\"" );
|
173 | | -}
|
174 | | -
|
175 | | -/**
|
176 | | - * Determine if an image exists on the 'bad image list'.
|
177 | | - *
|
178 | | - * @param $name String: the image name to check
|
179 | | - * @return bool
|
180 | | - */
|
181 | | -function wfIsBadImage( $name ) {
|
182 | | - static $titleList = false;
|
183 | | - wfProfileIn( __METHOD__ );
|
184 | | - $bad = false;
|
185 | | - if( wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
|
186 | | - if( !$titleList ) {
|
187 | | - # Build the list now
|
188 | | - $titleList = array();
|
189 | | - $lines = explode( "\n", wfMsgForContent( 'bad_image_list' ) );
|
190 | | - foreach( $lines as $line ) {
|
191 | | - if( preg_match( '/^\*\s*\[\[:?(.*?)\]\]/i', $line, $matches ) ) {
|
192 | | - $title = Title::newFromText( $matches[1] );
|
193 | | - if( is_object( $title ) && $title->getNamespace() == NS_IMAGE )
|
194 | | - $titleList[ $title->getDBkey() ] = true;
|
195 | | - }
|
196 | | - }
|
197 | | - }
|
198 | | - wfProfileOut( __METHOD__ );
|
199 | | - return array_key_exists( $name, $titleList );
|
200 | | - } else {
|
201 | | - wfProfileOut( __METHOD__ );
|
202 | | - return $bad;
|
203 | | - }
|
204 | | -}
|
205 | | -
|
206 | | -/**
|
207 | | - * Calculate the largest thumbnail width for a given original file size
|
208 | | - * such that the thumbnail's height is at most $maxHeight.
|
209 | | - * @param $boxWidth Integer Width of the thumbnail box.
|
210 | | - * @param $boxHeight Integer Height of the thumbnail box.
|
211 | | - * @param $maxHeight Integer Maximum height expected for the thumbnail.
|
212 | | - * @return Integer.
|
213 | | - */
|
214 | | -function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
|
215 | | - $idealWidth = $boxWidth * $maxHeight / $boxHeight;
|
216 | | - $roundedUp = ceil( $idealWidth );
|
217 | | - if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight )
|
218 | | - return floor( $idealWidth );
|
219 | | - else
|
220 | | - return $roundedUp;
|
221 | | -}
|
222 | | -
|
223 | | -
|
224 | | -?>
|
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Returns the image directory of an image |
| 6 | + * The result is an absolute path. |
| 7 | + * |
| 8 | + * This function is called from thumb.php before Setup.php is included |
| 9 | + * |
| 10 | + * @param $fname String: file name of the image file. |
| 11 | + * @public |
| 12 | + */ |
| 13 | +function wfImageDir( $fname ) { |
| 14 | + global $wgUploadDirectory, $wgHashedUploadDirectory; |
| 15 | + |
| 16 | + if (!$wgHashedUploadDirectory) { return $wgUploadDirectory; } |
| 17 | + |
| 18 | + $hash = md5( $fname ); |
| 19 | + $dest = $wgUploadDirectory . '/' . $hash{0} . '/' . substr( $hash, 0, 2 ); |
| 20 | + |
| 21 | + return $dest; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Returns the image directory of an image's thubnail |
| 26 | + * The result is an absolute path. |
| 27 | + * |
| 28 | + * This function is called from thumb.php before Setup.php is included |
| 29 | + * |
| 30 | + * @param $fname String: file name of the original image file |
| 31 | + * @param $shared Boolean: (optional) use the shared upload directory (default: 'false'). |
| 32 | + * @public |
| 33 | + */ |
| 34 | +function wfImageThumbDir( $fname, $shared = false ) { |
| 35 | + $base = wfImageArchiveDir( $fname, 'thumb', $shared ); |
| 36 | + if ( Image::isHashed( $shared ) ) { |
| 37 | + $dir = "$base/$fname"; |
| 38 | + } else { |
| 39 | + $dir = $base; |
| 40 | + } |
| 41 | + |
| 42 | + return $dir; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Old thumbnail directory, kept for conversion |
| 47 | + */ |
| 48 | +function wfDeprecatedThumbDir( $thumbName , $subdir='thumb', $shared=false) { |
| 49 | + return wfImageArchiveDir( $thumbName, $subdir, $shared ); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Returns the image directory of an image's old version |
| 54 | + * The result is an absolute path. |
| 55 | + * |
| 56 | + * This function is called from thumb.php before Setup.php is included |
| 57 | + * |
| 58 | + * @param $fname String: file name of the thumbnail file, including file size prefix. |
| 59 | + * @param $subdir String: subdirectory of the image upload directory that should be used for storing the old version. Default is 'archive'. |
| 60 | + * @param $shared Boolean use the shared upload directory (only relevant for other functions which call this one). Default is 'false'. |
| 61 | + * @public |
| 62 | + */ |
| 63 | +function wfImageArchiveDir( $fname , $subdir='archive', $shared=false ) { |
| 64 | + global $wgUploadDirectory, $wgHashedUploadDirectory; |
| 65 | + global $wgSharedUploadDirectory, $wgHashedSharedUploadDirectory; |
| 66 | + $dir = $shared ? $wgSharedUploadDirectory : $wgUploadDirectory; |
| 67 | + $hashdir = $shared ? $wgHashedSharedUploadDirectory : $wgHashedUploadDirectory; |
| 68 | + if (!$hashdir) { return $dir.'/'.$subdir; } |
| 69 | + $hash = md5( $fname ); |
| 70 | + |
| 71 | + return $dir.'/'.$subdir.'/'.$hash[0].'/'.substr( $hash, 0, 2 ); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +/* |
| 76 | + * Return the hash path component of an image path (URL or filesystem), |
| 77 | + * e.g. "/3/3c/", or just "/" if hashing is not used. |
| 78 | + * |
| 79 | + * @param $dbkey The filesystem / database name of the file |
| 80 | + * @param $fromSharedDirectory Use the shared file repository? It may |
| 81 | + * use different hash settings from the local one. |
| 82 | + */ |
| 83 | +function wfGetHashPath ( $dbkey, $fromSharedDirectory = false ) { |
| 84 | + if( Image::isHashed( $fromSharedDirectory ) ) { |
| 85 | + $hash = md5($dbkey); |
| 86 | + return '/' . $hash{0} . '/' . substr( $hash, 0, 2 ) . '/'; |
| 87 | + } else { |
| 88 | + return '/'; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Returns the image URL of an image's old version |
| 94 | + * |
| 95 | + * @param $name String: file name of the image file |
| 96 | + * @param $subdir String: (optional) subdirectory of the image upload directory that is used by the old version. Default is 'archive' |
| 97 | + * @public |
| 98 | + */ |
| 99 | +function wfImageArchiveUrl( $name, $subdir='archive' ) { |
| 100 | + global $wgUploadPath, $wgHashedUploadDirectory; |
| 101 | + |
| 102 | + if ($wgHashedUploadDirectory) { |
| 103 | + $hash = md5( substr( $name, 15) ); |
| 104 | + $url = $wgUploadPath.'/'.$subdir.'/' . $hash{0} . '/' . |
| 105 | + substr( $hash, 0, 2 ) . '/'.$name; |
| 106 | + } else { |
| 107 | + $url = $wgUploadPath.'/'.$subdir.'/'.$name; |
| 108 | + } |
| 109 | + return wfUrlencode($url); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Return a rounded pixel equivalent for a labeled CSS/SVG length. |
| 114 | + * http://www.w3.org/TR/SVG11/coords.html#UnitIdentifiers |
| 115 | + * |
| 116 | + * @param $length String: CSS/SVG length. |
| 117 | + * @return Integer: length in pixels |
| 118 | + */ |
| 119 | +function wfScaleSVGUnit( $length ) { |
| 120 | + static $unitLength = array( |
| 121 | + 'px' => 1.0, |
| 122 | + 'pt' => 1.25, |
| 123 | + 'pc' => 15.0, |
| 124 | + 'mm' => 3.543307, |
| 125 | + 'cm' => 35.43307, |
| 126 | + 'in' => 90.0, |
| 127 | + '' => 1.0, // "User units" pixels by default |
| 128 | + '%' => 2.0, // Fake it! |
| 129 | + ); |
| 130 | + if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) { |
| 131 | + $length = floatval( $matches[1] ); |
| 132 | + $unit = $matches[2]; |
| 133 | + return round( $length * $unitLength[$unit] ); |
| 134 | + } else { |
| 135 | + // Assume pixels |
| 136 | + return round( floatval( $length ) ); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Compatible with PHP getimagesize() |
| 142 | + * @todo support gzipped SVGZ |
| 143 | + * @todo check XML more carefully |
| 144 | + * @todo sensible defaults |
| 145 | + * |
| 146 | + * @param $filename String: full name of the file (passed to php fopen()). |
| 147 | + * @return array |
| 148 | + */ |
| 149 | +function wfGetSVGsize( $filename ) { |
| 150 | + $width = 256; |
| 151 | + $height = 256; |
| 152 | + |
| 153 | + // Read a chunk of the file |
| 154 | + $f = fopen( $filename, "rt" ); |
| 155 | + if( !$f ) return false; |
| 156 | + $chunk = fread( $f, 4096 ); |
| 157 | + fclose( $f ); |
| 158 | + |
| 159 | + // Uber-crappy hack! Run through a real XML parser. |
| 160 | + if( !preg_match( '/<svg\s*([^>]*)\s*>/s', $chunk, $matches ) ) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + $tag = $matches[1]; |
| 164 | + if( preg_match( '/\bwidth\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) { |
| 165 | + $width = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) ); |
| 166 | + } |
| 167 | + if( preg_match( '/\bheight\s*=\s*("[^"]+"|\'[^\']+\')/s', $tag, $matches ) ) { |
| 168 | + $height = wfScaleSVGUnit( trim( substr( $matches[1], 1, -1 ) ) ); |
| 169 | + } |
| 170 | + |
| 171 | + return array( $width, $height, 'SVG', |
| 172 | + "width=\"$width\" height=\"$height\"" ); |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Determine if an image exists on the 'bad image list'. |
| 177 | + * |
| 178 | + * @param $name String: the image name to check |
| 179 | + * @return bool |
| 180 | + */ |
| 181 | +function wfIsBadImage( $name ) { |
| 182 | + static $titleList = false; |
| 183 | + wfProfileIn( __METHOD__ ); |
| 184 | + $bad = false; |
| 185 | + if( wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) { |
| 186 | + if( !$titleList ) { |
| 187 | + # Build the list now |
| 188 | + $titleList = array(); |
| 189 | + $lines = explode( "\n", wfMsgForContent( 'bad_image_list' ) ); |
| 190 | + foreach( $lines as $line ) { |
| 191 | + if( preg_match( '/^\*\s*\[\[:?(.*?)\]\]/i', $line, $matches ) ) { |
| 192 | + $title = Title::newFromText( $matches[1] ); |
| 193 | + if( is_object( $title ) && $title->getNamespace() == NS_IMAGE ) |
| 194 | + $titleList[ $title->getDBkey() ] = true; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + wfProfileOut( __METHOD__ ); |
| 199 | + return array_key_exists( $name, $titleList ); |
| 200 | + } else { |
| 201 | + wfProfileOut( __METHOD__ ); |
| 202 | + return $bad; |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +/** |
| 207 | + * Calculate the largest thumbnail width for a given original file size |
| 208 | + * such that the thumbnail's height is at most $maxHeight. |
| 209 | + * @param $boxWidth Integer Width of the thumbnail box. |
| 210 | + * @param $boxHeight Integer Height of the thumbnail box. |
| 211 | + * @param $maxHeight Integer Maximum height expected for the thumbnail. |
| 212 | + * @return Integer. |
| 213 | + */ |
| 214 | +function wfFitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) { |
| 215 | + $idealWidth = $boxWidth * $maxHeight / $boxHeight; |
| 216 | + $roundedUp = ceil( $idealWidth ); |
| 217 | + if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) |
| 218 | + return floor( $idealWidth ); |
| 219 | + else |
| 220 | + return $roundedUp; |
| 221 | +} |
| 222 | + |
| 223 | + |
| 224 | +?> |
Property changes on: trunk/phase3/includes/ImageFunctions.php |
___________________________________________________________________ |
Added: svn:eol |
225 | 225 | + native |
Property changes on: trunk/phase3/includes/WebResponse.php |
___________________________________________________________________ |
Added: svn:eol |
226 | 226 | + native |
Property changes on: trunk/phase3/includes/AjaxResponse.php |
___________________________________________________________________ |
Added: svn:eol-style |
227 | 227 | + native |
Property changes on: trunk/phase3/includes/IP.php |
___________________________________________________________________ |
Added: svn:eol |
228 | 228 | + native |