r85723 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85722‎ | r85723 | r85724 >
Date:15:13, 9 April 2011
Author:ashley
Status:ok
Tags:
Comment:
Math extension:
*coding style tweaks
*documentation
*check for MediaWiki environment in the main setup file
*add extension credits
*add i18n file (only English at the moment); messages from core MessagesEn.php

This is a follow-up to Brion's r85706.
Modified paths:
  • /trunk/extensions/Math/Math.body.php (modified) (history)
  • /trunk/extensions/Math/Math.hooks.php (modified) (history)
  • /trunk/extensions/Math/Math.i18n.php (added) (history)
  • /trunk/extensions/Math/Math.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Math/Math.php
@@ -2,21 +2,41 @@
33 /**
44 * MediaWiki math extension
55 *
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
815 */
916
 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>&lt;math&gt;</code> ... <code>&lt;/math&gt;</code> tags',
 27+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Math',
 28+);
 29+
1030 /** For back-compat */
1131 $wgUseTeX = true;
1232
1333 /** Location of the texvc binary */
1434 $wgTexvc = dirname( __FILE__ ) . '/math/texvc';
1535 /**
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+ */
2141 $wgTexvcBackgroundColor = 'transparent';
2242
2343 /**
@@ -32,14 +52,13 @@
3353 */
3454 $wgMathCheckFiles = true;
3555
36 -
3756 /**
3857 * The URL path of the math directory. Defaults to "{$wgUploadPath}/math".
3958 *
4059 * See http://www.mediawiki.org/wiki/Manual:Enable_TeX for details about how to
4160 * set up mathematical formula display.
4261 */
43 -$wgMathPath = false;
 62+$wgMathPath = false;
4463
4564 /**
4665 * The filesystem path of the math directory.
@@ -48,7 +67,7 @@
4968 * See http://www.mediawiki.org/wiki/Manual:Enable_TeX for details about how to
5069 * set up mathematical formula display.
5170 */
52 -$wgMathDirectory = false;
 71+$wgMathDirectory = false;
5372
5473
5574 ////////// end of config settings.
@@ -58,7 +77,10 @@
5978 $wgHooks['ParserFirstCallInit'][] = 'MathHooks::onParserFirstCallInit';
6079 $wgHooks['GetPreferences'][] = 'MathHooks::onGetPreferences';
6180
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';
6484
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 @@
88 */
99
1010 class MathHooks {
 11+ /**
 12+ * Set up $wgMathPath and $wgMathDirectory globals if they're not already
 13+ * set.
 14+ */
1115 static function setup() {
1216 global $wgMathPath, $wgMathDirectory;
1317 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+ }
1624 }
1725
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 ) {
2033 $parser->setHook( 'math', array( 'MathHooks', 'mathTagHook' ) );
2134 return true;
2235 }
2336
2437 /**
25 - * @param $content
26 - * @param $attributes
 38+ * Callback function for the <math> parser hook.
 39+ *
 40+ * @param $content
 41+ * @param $attributes
2742 * @param $parser Parser
2843 * @return
2944 */
3045 static function mathTagHook( $content, $attributes, $parser ) {
3146 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 );
3351 }
3452
 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+ */
3560 static function onGetPreferences( $user, &$defaultPreferences ) {
3661 global $wgLang;
3762 $defaultPreferences['math'] = array(
Index: trunk/extensions/Math/Math.body.php
@@ -5,7 +5,7 @@
66 * (c) 2002-2011 Tomasz Wegrzanowski, Brion Vibber, and other MediaWiki contributors
77 * GPLv2 license; info in main package.
88 *
9 - * Contain everything related to <math> </math> parsing
 9+ * Contains everything related to <math> </math> parsing
1010 * @file
1111 * @ingroup Parser
1212 */
@@ -27,7 +27,7 @@
2828 var $mathml = '';
2929 var $conservativeness = 0;
3030
31 - function __construct( $tex, $params=array() ) {
 31+ function __construct( $tex, $params = array() ) {
3232 $this->tex = $tex;
3333 $this->params = $params;
3434 }
@@ -80,46 +80,46 @@
8181 $contents = wfShellExec( $cmd );
8282 wfDebug( "TeX output:\n $contents\n---\n" );
8383
84 - if (strlen($contents) == 0) {
 84+ if ( strlen( $contents ) == 0 ) {
8585 return $this->_error( 'math_unknown_error' );
8686 }
8787
88 - $retval = substr ($contents, 0, 1);
 88+ $retval = substr( $contents, 0, 1 );
8989 $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' ) {
9292 $this->conservativeness = 2;
93 - } else if ($retval == 'M') {
 93+ } elseif ( $retval == 'M' ) {
9494 $this->conservativeness = 1;
9595 } else {
9696 $this->conservativeness = 0;
9797 }
98 - $outdata = substr ($contents, 33);
 98+ $outdata = substr( $contents, 33 );
9999
100 - $i = strpos($outdata, "\000");
 100+ $i = strpos( $outdata, "\000" );
101101
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' ) {
107107 $this->conservativeness = 2;
108 - } else if ($retval == 'm') {
 108+ } elseif ( $retval == 'm' ) {
109109 $this->conservativeness = 1;
110110 } else {
111111 $this->conservativeness = 0;
112112 }
113113 $this->mathml = null;
114 - } else if ($retval == 'X') {
 114+ } elseif ( $retval == 'X' ) {
115115 $this->html = null;
116 - $this->mathml = substr ($contents, 33);
 116+ $this->mathml = substr( $contents, 33 );
117117 $this->conservativeness = 0;
118 - } else if ($retval == '+') {
 118+ } elseif ( $retval == '+' ) {
119119 $this->html = null;
120120 $this->mathml = null;
121121 $this->conservativeness = 0;
122122 } else {
123 - $errbit = htmlspecialchars( substr($contents, 1) );
 123+ $errbit = htmlspecialchars( substr( $contents, 1 ) );
124124 switch( $retval ) {
125125 case 'E':
126126 $errmsg = $this->_error( 'math_lexing_error', $errbit );
@@ -136,16 +136,16 @@
137137 }
138138
139139 if ( !$errmsg ) {
140 - $this->hash = substr ($contents, 1, 32);
 140+ $this->hash = substr( $contents, 1, 32 );
141141 }
142142
143143 wfRunHooks( 'MathAfterTexvc', array( &$this, &$errmsg ) );
144144
145145 if ( $errmsg ) {
146 - return $errmsg;
 146+ return $errmsg;
147147 }
148148
149 - if (!preg_match("/^[a-f0-9]{32}$/", $this->hash)) {
 149+ if ( !preg_match( "/^[a-f0-9]{32}$/", $this->hash ) ) {
150150 return $this->_error( 'math_unknown_error' );
151151 }
152152
@@ -175,19 +175,22 @@
176176
177177 # Now save it back to the DB:
178178 if ( !wfReadOnly() ) {
179 - $outmd5_sql = pack('H32', $this->hash);
 179+ $outmd5_sql = pack( 'H32', $this->hash );
180180
181 - $md5_sql = pack('H32', $this->md5); # Binary packed, not hex
 181+ $md5_sql = pack( 'H32', $this->md5 ); # Binary packed, not hex
182182
183183 $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__
192195 );
193196 }
194197
@@ -204,7 +207,7 @@
205208 }
206209
207210 function _error( $msg, $append = '' ) {
208 - $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
 211+ $mf = htmlspecialchars( wfMsg( 'math_failure' ) );
209212 $errmsg = htmlspecialchars( wfMsg( $msg ) );
210213 $source = htmlspecialchars( str_replace( "\n", ' ', $this->tex ) );
211214 return "<strong class='error'>$mf ($errmsg$append): $source</strong>\n";
@@ -215,16 +218,22 @@
216219
217220 $this->md5 = md5( $this->tex );
218221 $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+ ),
222231 __METHOD__
223232 );
224233
225234 if( $rpage !== false ) {
226235 # 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'];
229238
230239 $this->conservativeness = $rpage->math_html_conservativeness;
231240 $this->html = $rpage->math_html;
@@ -261,11 +270,11 @@
262271 } elseif( !is_dir( $hashpath ) || !is_writable( $hashpath ) ) {
263272 return false;
264273 }
265 - if ( function_exists( "link" ) ) {
266 - return link ( $wgMathDirectory . "/{$this->hash}.png",
 274+ if ( function_exists( 'link' ) ) {
 275+ return link( $wgMathDirectory . "/{$this->hash}.png",
267276 $hashpath . "/{$this->hash}.png" );
268277 } else {
269 - return rename ( $wgMathDirectory . "/{$this->hash}.png",
 278+ return rename( $wgMathDirectory . "/{$this->hash}.png",
270279 $hashpath . "/{$this->hash}.png" );
271280 }
272281 }
@@ -286,9 +295,11 @@
287296 array( 'xmlns' => 'http://www.w3.org/1998/Math/MathML' ) ),
288297 $this->mathml );
289298 }
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+ {
293304 return $this->_linkToMathImage();
294305 } else {
295306 return Xml::tags( 'span',
@@ -296,11 +307,12 @@
297308 array( 'class' => 'texhtml',
298309 'dir' => 'ltr'
299310 ) ),
300 - $this->html );
 311+ $this->html
 312+ );
301313 }
302314 }
303315
304 - function _attribs( $tag, $defaults=array(), $overrides=array() ) {
 316+ function _attribs( $tag, $defaults = array(), $overrides = array() ) {
305317 $attribs = Sanitizer::validateTagAttributes( $this->params, $tag );
306318 $attribs = Sanitizer::mergeAttributes( $defaults, $attribs );
307319 $attribs = Sanitizer::mergeAttributes( $attribs, $overrides );
@@ -315,9 +327,13 @@
316328 'img',
317329 array(
318330 'class' => 'tex',
319 - 'alt' => $this->tex ),
 331+ 'alt' => $this->tex
 332+ ),
320333 array(
321 - 'src' => $url ) ) );
 334+ 'src' => $url
 335+ )
 336+ )
 337+ );
322338 }
323339
324340 function _mathImageUrl() {
@@ -328,21 +344,22 @@
329345
330346 function _getHashPath() {
331347 global $wgMathDirectory;
332 - $path = $wgMathDirectory .'/' . $this->_getHashSubPath();
 348+ $path = $wgMathDirectory . '/' . $this->_getHashSubPath();
333349 wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" );
334350 return $path;
335351 }
336352
337353 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 );
341357 }
342358
343 - public static function renderMath( $tex, $params=array(), ParserOptions $parserOptions = null ) {
 359+ public static function renderMath( $tex, $params = array(), ParserOptions $parserOptions = null ) {
344360 $math = new MathRenderer( $tex, $params );
345 - if ( $parserOptions )
 361+ if ( $parserOptions ) {
346362 $math->setOutputMode( $parserOptions->getMath() );
 363+ }
347364 return $math->render();
348365 }
349366 }
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
140 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r85724...and remove the math-related messages from MessagesEn.php; follow-up to r85723ashley15:18, 9 April 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r85706Initial stab at breaking math/texvc out to Math extension....brion00:39, 9 April 2011

Status & tagging log