Index: trunk/extensions/Math/Math.php |
— | — | @@ -2,21 +2,41 @@ |
3 | 3 | /** |
4 | 4 | * MediaWiki math extension |
5 | 5 | * |
6 | | - * (c) 2002-2011 various MediaWiki contributors |
7 | | - * GPLv2 license; info in main package. |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + * @version 1.0 |
| 9 | + * @author Tomasz Wegrzanowski |
| 10 | + * @author Brion Vibber |
| 11 | + * @copyright © 2002-2011 various MediaWiki contributors |
| 12 | + * @license GPLv2 license; info in main package. |
| 13 | + * @link http://www.mediawiki.org/wiki/Extension:Math Documentation |
| 14 | + * @see https://bugzilla.wikimedia.org/show_bug.cgi?id=14202 |
8 | 15 | */ |
9 | 16 | |
| 17 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 18 | + die( "This is not a valid entry point to MediaWiki.\n" ); |
| 19 | +} |
| 20 | + |
| 21 | +// Extension credits that will show up on Special:Version |
| 22 | +$wgExtensionCredits['parserhook'][] = array( |
| 23 | + 'name' => 'Math', |
| 24 | + 'version' => '1.0', |
| 25 | + 'author' => array( 'Tomasz Wegrzanowski', 'Brion Vibber', '...' ), |
| 26 | + 'description' => 'Render mathematical formulas between <code><math></code> ... <code></math></code> tags', |
| 27 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:Math', |
| 28 | +); |
| 29 | + |
10 | 30 | /** For back-compat */ |
11 | 31 | $wgUseTeX = true; |
12 | 32 | |
13 | 33 | /** Location of the texvc binary */ |
14 | 34 | $wgTexvc = dirname( __FILE__ ) . '/math/texvc'; |
15 | 35 | /** |
16 | | - * Texvc background color |
17 | | - * use LaTeX color format as used in \special function |
18 | | - * for transparent background use value 'Transparent' for alpha transparency or |
19 | | - * 'transparent' for binary transparency. |
20 | | - */ |
| 36 | + * Texvc background color |
| 37 | + * use LaTeX color format as used in \special function |
| 38 | + * for transparent background use value 'Transparent' for alpha transparency or |
| 39 | + * 'transparent' for binary transparency. |
| 40 | + */ |
21 | 41 | $wgTexvcBackgroundColor = 'transparent'; |
22 | 42 | |
23 | 43 | /** |
— | — | @@ -32,14 +52,13 @@ |
33 | 53 | */ |
34 | 54 | $wgMathCheckFiles = true; |
35 | 55 | |
36 | | - |
37 | 56 | /** |
38 | 57 | * The URL path of the math directory. Defaults to "{$wgUploadPath}/math". |
39 | 58 | * |
40 | 59 | * See http://www.mediawiki.org/wiki/Manual:Enable_TeX for details about how to |
41 | 60 | * set up mathematical formula display. |
42 | 61 | */ |
43 | | -$wgMathPath = false; |
| 62 | +$wgMathPath = false; |
44 | 63 | |
45 | 64 | /** |
46 | 65 | * The filesystem path of the math directory. |
— | — | @@ -48,7 +67,7 @@ |
49 | 68 | * See http://www.mediawiki.org/wiki/Manual:Enable_TeX for details about how to |
50 | 69 | * set up mathematical formula display. |
51 | 70 | */ |
52 | | -$wgMathDirectory = false; |
| 71 | +$wgMathDirectory = false; |
53 | 72 | |
54 | 73 | |
55 | 74 | ////////// end of config settings. |
— | — | @@ -58,7 +77,10 @@ |
59 | 78 | $wgHooks['ParserFirstCallInit'][] = 'MathHooks::onParserFirstCallInit'; |
60 | 79 | $wgHooks['GetPreferences'][] = 'MathHooks::onGetPreferences'; |
61 | 80 | |
62 | | -$wgAutoloadClasses['MathHooks'] = dirname( __FILE__ ) . '/Math.hooks.php'; |
63 | | -$wgAutoloadClasses['MathRenderer'] = dirname( __FILE__ ) . '/Math.body.php'; |
| 81 | +$dir = dirname( __FILE__ ) . '/'; |
| 82 | +$wgAutoloadClasses['MathHooks'] = $dir . 'Math.hooks.php'; |
| 83 | +$wgAutoloadClasses['MathRenderer'] = $dir . 'Math.body.php'; |
64 | 84 | |
65 | | -$wgParserTestFiles[] = dirname( __FILE__ ) . "/mathParserTests.txt"; |
| 85 | +$wgExtensionMessagesFiles['Math'] = $dir . 'Math.i18n.php'; |
| 86 | + |
| 87 | +$wgParserTestFiles[] = $dir . 'mathParserTests.txt'; |
\ No newline at end of file |
Index: trunk/extensions/Math/Math.hooks.php |
— | — | @@ -7,30 +7,55 @@ |
8 | 8 | */ |
9 | 9 | |
10 | 10 | class MathHooks { |
| 11 | + /** |
| 12 | + * Set up $wgMathPath and $wgMathDirectory globals if they're not already |
| 13 | + * set. |
| 14 | + */ |
11 | 15 | static function setup() { |
12 | 16 | global $wgMathPath, $wgMathDirectory; |
13 | 17 | global $wgUploadPath, $wgUploadDirectory; |
14 | | - if ( $wgMathPath === false ) $wgMathPath = "{$wgUploadPath}/math"; |
15 | | - if ( $wgMathDirectory === false ) $wgMathDirectory = "{$wgUploadDirectory}/math"; |
| 18 | + if ( $wgMathPath === false ) { |
| 19 | + $wgMathPath = "{$wgUploadPath}/math"; |
| 20 | + } |
| 21 | + if ( $wgMathDirectory === false ) { |
| 22 | + $wgMathDirectory = "{$wgUploadDirectory}/math"; |
| 23 | + } |
16 | 24 | } |
17 | 25 | |
18 | | - static function onParserFirstCallInit($parser) |
19 | | - { |
| 26 | + /** |
| 27 | + * Register the <math> tag with the Parser. |
| 28 | + * |
| 29 | + * @param $parser Object: instance of Parser |
| 30 | + * @return Boolean: true |
| 31 | + */ |
| 32 | + static function onParserFirstCallInit( $parser ) { |
20 | 33 | $parser->setHook( 'math', array( 'MathHooks', 'mathTagHook' ) ); |
21 | 34 | return true; |
22 | 35 | } |
23 | 36 | |
24 | 37 | /** |
25 | | - * @param $content |
26 | | - * @param $attributes |
| 38 | + * Callback function for the <math> parser hook. |
| 39 | + * |
| 40 | + * @param $content |
| 41 | + * @param $attributes |
27 | 42 | * @param $parser Parser |
28 | 43 | * @return |
29 | 44 | */ |
30 | 45 | static function mathTagHook( $content, $attributes, $parser ) { |
31 | 46 | global $wgContLang; |
32 | | - return $wgContLang->armourMath( MathRenderer::renderMath( $content, $attributes, $parser->getOptions() ) ); |
| 47 | + $renderedMath = MathRenderer::renderMath( |
| 48 | + $content, $attributes, $parser->getOptions() |
| 49 | + ); |
| 50 | + return $wgContLang->armourMath( $renderedMath ); |
33 | 51 | } |
34 | 52 | |
| 53 | + /** |
| 54 | + * Add the new math rendering options to Special:Preferences. |
| 55 | + * |
| 56 | + * @param $user Object: current User object |
| 57 | + * @param $defaultPreferences Object: Preferences object |
| 58 | + * @return Boolean: true |
| 59 | + */ |
35 | 60 | static function onGetPreferences( $user, &$defaultPreferences ) { |
36 | 61 | global $wgLang; |
37 | 62 | $defaultPreferences['math'] = array( |
Index: trunk/extensions/Math/Math.body.php |
— | — | @@ -5,7 +5,7 @@ |
6 | 6 | * (c) 2002-2011 Tomasz Wegrzanowski, Brion Vibber, and other MediaWiki contributors |
7 | 7 | * GPLv2 license; info in main package. |
8 | 8 | * |
9 | | - * Contain everything related to <math> </math> parsing |
| 9 | + * Contains everything related to <math> </math> parsing |
10 | 10 | * @file |
11 | 11 | * @ingroup Parser |
12 | 12 | */ |
— | — | @@ -27,7 +27,7 @@ |
28 | 28 | var $mathml = ''; |
29 | 29 | var $conservativeness = 0; |
30 | 30 | |
31 | | - function __construct( $tex, $params=array() ) { |
| 31 | + function __construct( $tex, $params = array() ) { |
32 | 32 | $this->tex = $tex; |
33 | 33 | $this->params = $params; |
34 | 34 | } |
— | — | @@ -80,46 +80,46 @@ |
81 | 81 | $contents = wfShellExec( $cmd ); |
82 | 82 | wfDebug( "TeX output:\n $contents\n---\n" ); |
83 | 83 | |
84 | | - if (strlen($contents) == 0) { |
| 84 | + if ( strlen( $contents ) == 0 ) { |
85 | 85 | return $this->_error( 'math_unknown_error' ); |
86 | 86 | } |
87 | 87 | |
88 | | - $retval = substr ($contents, 0, 1); |
| 88 | + $retval = substr( $contents, 0, 1 ); |
89 | 89 | $errmsg = ''; |
90 | | - if (($retval == 'C') || ($retval == 'M') || ($retval == 'L')) { |
91 | | - if ($retval == 'C') { |
| 90 | + if ( ( $retval == 'C' ) || ( $retval == 'M' ) || ( $retval == 'L' ) ) { |
| 91 | + if ( $retval == 'C' ) { |
92 | 92 | $this->conservativeness = 2; |
93 | | - } else if ($retval == 'M') { |
| 93 | + } elseif ( $retval == 'M' ) { |
94 | 94 | $this->conservativeness = 1; |
95 | 95 | } else { |
96 | 96 | $this->conservativeness = 0; |
97 | 97 | } |
98 | | - $outdata = substr ($contents, 33); |
| 98 | + $outdata = substr( $contents, 33 ); |
99 | 99 | |
100 | | - $i = strpos($outdata, "\000"); |
| 100 | + $i = strpos( $outdata, "\000" ); |
101 | 101 | |
102 | | - $this->html = substr($outdata, 0, $i); |
103 | | - $this->mathml = substr($outdata, $i+1); |
104 | | - } else if (($retval == 'c') || ($retval == 'm') || ($retval == 'l')) { |
105 | | - $this->html = substr ($contents, 33); |
106 | | - if ($retval == 'c') { |
| 102 | + $this->html = substr( $outdata, 0, $i ); |
| 103 | + $this->mathml = substr( $outdata, $i + 1 ); |
| 104 | + } elseif ( ( $retval == 'c' ) || ( $retval == 'm' ) || ( $retval == 'l' ) ) { |
| 105 | + $this->html = substr( $contents, 33 ); |
| 106 | + if ( $retval == 'c' ) { |
107 | 107 | $this->conservativeness = 2; |
108 | | - } else if ($retval == 'm') { |
| 108 | + } elseif ( $retval == 'm' ) { |
109 | 109 | $this->conservativeness = 1; |
110 | 110 | } else { |
111 | 111 | $this->conservativeness = 0; |
112 | 112 | } |
113 | 113 | $this->mathml = null; |
114 | | - } else if ($retval == 'X') { |
| 114 | + } elseif ( $retval == 'X' ) { |
115 | 115 | $this->html = null; |
116 | | - $this->mathml = substr ($contents, 33); |
| 116 | + $this->mathml = substr( $contents, 33 ); |
117 | 117 | $this->conservativeness = 0; |
118 | | - } else if ($retval == '+') { |
| 118 | + } elseif ( $retval == '+' ) { |
119 | 119 | $this->html = null; |
120 | 120 | $this->mathml = null; |
121 | 121 | $this->conservativeness = 0; |
122 | 122 | } else { |
123 | | - $errbit = htmlspecialchars( substr($contents, 1) ); |
| 123 | + $errbit = htmlspecialchars( substr( $contents, 1 ) ); |
124 | 124 | switch( $retval ) { |
125 | 125 | case 'E': |
126 | 126 | $errmsg = $this->_error( 'math_lexing_error', $errbit ); |
— | — | @@ -136,16 +136,16 @@ |
137 | 137 | } |
138 | 138 | |
139 | 139 | if ( !$errmsg ) { |
140 | | - $this->hash = substr ($contents, 1, 32); |
| 140 | + $this->hash = substr( $contents, 1, 32 ); |
141 | 141 | } |
142 | 142 | |
143 | 143 | wfRunHooks( 'MathAfterTexvc', array( &$this, &$errmsg ) ); |
144 | 144 | |
145 | 145 | if ( $errmsg ) { |
146 | | - return $errmsg; |
| 146 | + return $errmsg; |
147 | 147 | } |
148 | 148 | |
149 | | - if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) { |
| 149 | + if ( !preg_match( "/^[a-f0-9]{32}$/", $this->hash ) ) { |
150 | 150 | return $this->_error( 'math_unknown_error' ); |
151 | 151 | } |
152 | 152 | |
— | — | @@ -175,19 +175,22 @@ |
176 | 176 | |
177 | 177 | # Now save it back to the DB: |
178 | 178 | if ( !wfReadOnly() ) { |
179 | | - $outmd5_sql = pack('H32', $this->hash); |
| 179 | + $outmd5_sql = pack( 'H32', $this->hash ); |
180 | 180 | |
181 | | - $md5_sql = pack('H32', $this->md5); # Binary packed, not hex |
| 181 | + $md5_sql = pack( 'H32', $this->md5 ); # Binary packed, not hex |
182 | 182 | |
183 | 183 | $dbw = wfGetDB( DB_MASTER ); |
184 | | - $dbw->replace( 'math', array( 'math_inputhash' ), |
185 | | - array( |
186 | | - 'math_inputhash' => $dbw->encodeBlob($md5_sql), |
187 | | - 'math_outputhash' => $dbw->encodeBlob($outmd5_sql), |
188 | | - 'math_html_conservativeness' => $this->conservativeness, |
189 | | - 'math_html' => $this->html, |
190 | | - 'math_mathml' => $this->mathml, |
191 | | - ), __METHOD__ |
| 184 | + $dbw->replace( |
| 185 | + 'math', |
| 186 | + array( 'math_inputhash' ), |
| 187 | + array( |
| 188 | + 'math_inputhash' => $dbw->encodeBlob( $md5_sql ), |
| 189 | + 'math_outputhash' => $dbw->encodeBlob( $outmd5_sql ), |
| 190 | + 'math_html_conservativeness' => $this->conservativeness, |
| 191 | + 'math_html' => $this->html, |
| 192 | + 'math_mathml' => $this->mathml, |
| 193 | + ), |
| 194 | + __METHOD__ |
192 | 195 | ); |
193 | 196 | } |
194 | 197 | |
— | — | @@ -204,7 +207,7 @@ |
205 | 208 | } |
206 | 209 | |
207 | 210 | function _error( $msg, $append = '' ) { |
208 | | - $mf = htmlspecialchars( wfMsg( 'math_failure' ) ); |
| 211 | + $mf = htmlspecialchars( wfMsg( 'math_failure' ) ); |
209 | 212 | $errmsg = htmlspecialchars( wfMsg( $msg ) ); |
210 | 213 | $source = htmlspecialchars( str_replace( "\n", ' ', $this->tex ) ); |
211 | 214 | return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n"; |
— | — | @@ -215,16 +218,22 @@ |
216 | 219 | |
217 | 220 | $this->md5 = md5( $this->tex ); |
218 | 221 | $dbr = wfGetDB( DB_SLAVE ); |
219 | | - $rpage = $dbr->selectRow( 'math', |
220 | | - array( 'math_outputhash','math_html_conservativeness','math_html','math_mathml' ), |
221 | | - array( 'math_inputhash' => $dbr->encodeBlob(pack("H32", $this->md5))), # Binary packed, not hex |
| 222 | + $rpage = $dbr->selectRow( |
| 223 | + 'math', |
| 224 | + array( |
| 225 | + 'math_outputhash', 'math_html_conservativeness', 'math_html', |
| 226 | + 'math_mathml' |
| 227 | + ), |
| 228 | + array( |
| 229 | + 'math_inputhash' => $dbr->encodeBlob( pack( "H32", $this->md5 ) ) # Binary packed, not hex |
| 230 | + ), |
222 | 231 | __METHOD__ |
223 | 232 | ); |
224 | 233 | |
225 | 234 | if( $rpage !== false ) { |
226 | 235 | # Tailing 0x20s can get dropped by the database, add it back on if necessary: |
227 | | - $xhash = unpack( 'H32md5', $dbr->decodeBlob($rpage->math_outputhash) . " " ); |
228 | | - $this->hash = $xhash ['md5']; |
| 236 | + $xhash = unpack( 'H32md5', $dbr->decodeBlob( $rpage->math_outputhash ) . " " ); |
| 237 | + $this->hash = $xhash['md5']; |
229 | 238 | |
230 | 239 | $this->conservativeness = $rpage->math_html_conservativeness; |
231 | 240 | $this->html = $rpage->math_html; |
— | — | @@ -261,11 +270,11 @@ |
262 | 271 | } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) { |
263 | 272 | return false; |
264 | 273 | } |
265 | | - if ( function_exists( "link" ) ) { |
266 | | - return link ( $wgMathDirectory . "/{$this->hash}.png", |
| 274 | + if ( function_exists( 'link' ) ) { |
| 275 | + return link( $wgMathDirectory . "/{$this->hash}.png", |
267 | 276 | $hashpath . "/{$this->hash}.png" ); |
268 | 277 | } else { |
269 | | - return rename ( $wgMathDirectory . "/{$this->hash}.png", |
| 278 | + return rename( $wgMathDirectory . "/{$this->hash}.png", |
270 | 279 | $hashpath . "/{$this->hash}.png" ); |
271 | 280 | } |
272 | 281 | } |
— | — | @@ -286,9 +295,11 @@ |
287 | 296 | array( 'xmlns' => 'http://www.w3.org/1998/Math/MathML' ) ), |
288 | 297 | $this->mathml ); |
289 | 298 | } |
290 | | - if (($this->mode == MW_MATH_PNG) || ($this->html == '') || |
291 | | - (($this->mode == MW_MATH_SIMPLE) && ($this->conservativeness != 2)) || |
292 | | - (($this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML) && ($this->conservativeness == 0))) { |
| 299 | + if ( ( $this->mode == MW_MATH_PNG ) || ( $this->html == '' ) || |
| 300 | + ( ( $this->mode == MW_MATH_SIMPLE ) && ( $this->conservativeness != 2 ) ) || |
| 301 | + ( ( $this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML ) && ( $this->conservativeness == 0 ) ) |
| 302 | + ) |
| 303 | + { |
293 | 304 | return $this->_linkToMathImage(); |
294 | 305 | } else { |
295 | 306 | return Xml::tags( 'span', |
— | — | @@ -296,11 +307,12 @@ |
297 | 308 | array( 'class' => 'texhtml', |
298 | 309 | 'dir' => 'ltr' |
299 | 310 | ) ), |
300 | | - $this->html ); |
| 311 | + $this->html |
| 312 | + ); |
301 | 313 | } |
302 | 314 | } |
303 | 315 | |
304 | | - function _attribs( $tag, $defaults=array(), $overrides=array() ) { |
| 316 | + function _attribs( $tag, $defaults = array(), $overrides = array() ) { |
305 | 317 | $attribs = Sanitizer::validateTagAttributes( $this->params, $tag ); |
306 | 318 | $attribs = Sanitizer::mergeAttributes( $defaults, $attribs ); |
307 | 319 | $attribs = Sanitizer::mergeAttributes( $attribs, $overrides ); |
— | — | @@ -315,9 +327,13 @@ |
316 | 328 | 'img', |
317 | 329 | array( |
318 | 330 | 'class' => 'tex', |
319 | | - 'alt' => $this->tex ), |
| 331 | + 'alt' => $this->tex |
| 332 | + ), |
320 | 333 | array( |
321 | | - 'src' => $url ) ) ); |
| 334 | + 'src' => $url |
| 335 | + ) |
| 336 | + ) |
| 337 | + ); |
322 | 338 | } |
323 | 339 | |
324 | 340 | function _mathImageUrl() { |
— | — | @@ -328,21 +344,22 @@ |
329 | 345 | |
330 | 346 | function _getHashPath() { |
331 | 347 | global $wgMathDirectory; |
332 | | - $path = $wgMathDirectory .'/' . $this->_getHashSubPath(); |
| 348 | + $path = $wgMathDirectory . '/' . $this->_getHashSubPath(); |
333 | 349 | wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" ); |
334 | 350 | return $path; |
335 | 351 | } |
336 | 352 | |
337 | 353 | function _getHashSubPath() { |
338 | | - return substr($this->hash, 0, 1) |
339 | | - .'/'. substr($this->hash, 1, 1) |
340 | | - .'/'. substr($this->hash, 2, 1); |
| 354 | + return substr( $this->hash, 0, 1) |
| 355 | + . '/' . substr( $this->hash, 1, 1 ) |
| 356 | + . '/' . substr( $this->hash, 2, 1 ); |
341 | 357 | } |
342 | 358 | |
343 | | - public static function renderMath( $tex, $params=array(), ParserOptions $parserOptions = null ) { |
| 359 | + public static function renderMath( $tex, $params = array(), ParserOptions $parserOptions = null ) { |
344 | 360 | $math = new MathRenderer( $tex, $params ); |
345 | | - if ( $parserOptions ) |
| 361 | + if ( $parserOptions ) { |
346 | 362 | $math->setOutputMode( $parserOptions->getMath() ); |
| 363 | + } |
347 | 364 | return $math->render(); |
348 | 365 | } |
349 | 366 | } |
Index: trunk/extensions/Math/Math.i18n.php |
— | — | @@ -0,0 +1,38 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Internationalization file for the Math extension. |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + */ |
| 9 | + |
| 10 | +$messages = array(); |
| 11 | + |
| 12 | +/** English */ |
| 13 | +$messages['en'] = array( |
| 14 | + // Edit toolbar stuff shown on ?action=edit (example text & tooltip) |
| 15 | + 'math_sample' => 'Insert formula here', |
| 16 | + 'math_tip' => 'Mathematical formula (LaTeX)', |
| 17 | + |
| 18 | + // Header on Special:Preferences (or something) |
| 19 | + 'prefs-math' => 'Math', |
| 20 | + |
| 21 | + // Math options |
| 22 | + 'mw_math_png' => 'Always render PNG', |
| 23 | + 'mw_math_simple' => 'HTML if very simple or else PNG', |
| 24 | + 'mw_math_html' => 'HTML if possible or else PNG', |
| 25 | + 'mw_math_source' => 'Leave it as TeX (for text browsers)', |
| 26 | + 'mw_math_modern' => 'Recommended for modern browsers', |
| 27 | + 'mw_math_mathml' => 'MathML if possible (experimental)', |
| 28 | + |
| 29 | + // Math errors |
| 30 | + 'math_failure' => 'Failed to parse', |
| 31 | + 'math_unknown_error' => 'unknown error', |
| 32 | + 'math_unknown_function' => 'unknown function', |
| 33 | + 'math_lexing_error' => 'lexing error', |
| 34 | + 'math_syntax_error' => 'syntax error', |
| 35 | + 'math_image_error' => 'PNG conversion failed; check for correct installation of latex and dvipng (or dvips + gs + convert)', |
| 36 | + 'math_bad_tmpdir' => 'Cannot write to or create math temp directory', |
| 37 | + 'math_bad_output' => 'Cannot write to or create math output directory', |
| 38 | + 'math_notexvc' => 'Missing texvc executable; please see math/README to configure.', |
| 39 | +); |
\ No newline at end of file |
Property changes on: trunk/extensions/Math/Math.i18n.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 40 | + native |