2011-04-09 00:39:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
2012-03-05 22:25:09 +00:00
|
|
|
* (c) 2002-2012 various MediaWiki contributors
|
2011-04-09 00:39:40 +00:00
|
|
|
* GPLv2 license; info in main package.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class MathHooks {
|
2011-04-09 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* Set up $wgMathPath and $wgMathDirectory globals if they're not already
|
|
|
|
* set.
|
|
|
|
*/
|
2011-04-09 00:39:40 +00:00
|
|
|
static function setup() {
|
|
|
|
global $wgMathPath, $wgMathDirectory;
|
|
|
|
global $wgUploadPath, $wgUploadDirectory;
|
2011-04-09 15:13:22 +00:00
|
|
|
if ( $wgMathPath === false ) {
|
|
|
|
$wgMathPath = "{$wgUploadPath}/math";
|
|
|
|
}
|
|
|
|
if ( $wgMathDirectory === false ) {
|
|
|
|
$wgMathDirectory = "{$wgUploadDirectory}/math";
|
|
|
|
}
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|
|
|
|
|
2011-04-09 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* Register the <math> tag with the Parser.
|
|
|
|
*
|
2011-07-17 21:11:24 +00:00
|
|
|
* @param $parser Parser instance of Parser
|
2011-04-09 15:13:22 +00:00
|
|
|
* @return Boolean: true
|
|
|
|
*/
|
|
|
|
static function onParserFirstCallInit( $parser ) {
|
2011-04-09 00:39:40 +00:00
|
|
|
$parser->setHook( 'math', array( 'MathHooks', 'mathTagHook' ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-09 15:13:22 +00:00
|
|
|
* Callback function for the <math> parser hook.
|
|
|
|
*
|
|
|
|
* @param $content
|
|
|
|
* @param $attributes
|
2011-04-09 00:39:40 +00:00
|
|
|
* @param $parser Parser
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
static function mathTagHook( $content, $attributes, $parser ) {
|
2011-11-29 00:37:13 +00:00
|
|
|
global $wgContLang, $wgUseMathJax;
|
2011-04-09 15:13:22 +00:00
|
|
|
$renderedMath = MathRenderer::renderMath(
|
|
|
|
$content, $attributes, $parser->getOptions()
|
|
|
|
);
|
2012-03-05 20:34:29 +00:00
|
|
|
|
|
|
|
if ( $wgUseMathJax && $parser->getOptions()->getMath() == MW_MATH_MATHJAX ) {
|
|
|
|
$parser->getOutput()->addModules( array( 'ext.math.mathjax.enabler' ) );
|
2011-11-29 00:37:13 +00:00
|
|
|
}
|
2011-12-06 01:17:35 +00:00
|
|
|
$output = $renderedMath;
|
2011-11-29 00:37:13 +00:00
|
|
|
|
|
|
|
return $wgContLang->armourMath( $output );
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|
|
|
|
|
2011-04-09 15:13:22 +00:00
|
|
|
/**
|
|
|
|
* Add the new math rendering options to Special:Preferences.
|
|
|
|
*
|
|
|
|
* @param $user Object: current User object
|
|
|
|
* @param $defaultPreferences Object: Preferences object
|
|
|
|
* @return Boolean: true
|
|
|
|
*/
|
2011-04-09 00:39:40 +00:00
|
|
|
static function onGetPreferences( $user, &$defaultPreferences ) {
|
|
|
|
$defaultPreferences['math'] = array(
|
|
|
|
'type' => 'radio',
|
2011-04-09 19:57:35 +00:00
|
|
|
'options' => array_flip( array_map( 'wfMsgHtml', self::getMathNames() ) ),
|
2011-04-09 00:39:40 +00:00
|
|
|
'label' => ' ',
|
|
|
|
'section' => 'rendering/math',
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-09 19:57:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of message keys for the various math output settings.
|
|
|
|
*
|
|
|
|
* @return array of strings
|
|
|
|
*/
|
|
|
|
private static function getMathNames() {
|
2012-03-05 20:34:29 +00:00
|
|
|
$names = array(
|
2011-04-09 19:57:35 +00:00
|
|
|
MW_MATH_PNG => 'mw_math_png',
|
2012-03-05 20:34:29 +00:00
|
|
|
MW_MATH_SOURCE => 'mw_math_source',
|
2011-04-09 19:57:35 +00:00
|
|
|
);
|
2012-03-05 20:34:29 +00:00
|
|
|
|
|
|
|
global $wgUseMathJax;
|
|
|
|
if( $wgUseMathJax ) {
|
|
|
|
$names[MW_MATH_MATHJAX] = 'mw_math_mathjax';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $names;
|
2011-04-09 19:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MaintenanceRefreshLinksInit handler; optimize settings for refreshLinks batch job.
|
|
|
|
*
|
|
|
|
* @param Maintenance $maint
|
|
|
|
* @return boolean hook return code
|
|
|
|
*/
|
|
|
|
static function onMaintenanceRefreshLinksInit( $maint ) {
|
|
|
|
global $wgUser;
|
|
|
|
|
|
|
|
# Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
|
|
|
|
$wgUser->setOption( 'math', MW_MATH_SOURCE );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-22 21:37:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* LoadExtensionSchemaUpdates handler; set up math table on install/upgrade.
|
|
|
|
*
|
2011-07-17 21:11:24 +00:00
|
|
|
* @param $updater DatabaseUpdater
|
2011-04-22 21:37:16 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2011-06-07 17:33:34 +00:00
|
|
|
static function onLoadExtensionSchemaUpdates( $updater = null ) {
|
|
|
|
if( is_null( $updater ) ) {
|
|
|
|
throw new MWException( "Math extension is only necessary in 1.18 or above" );
|
|
|
|
}
|
2011-04-22 21:37:16 +00:00
|
|
|
$map = array(
|
|
|
|
'mysql' => 'math.sql',
|
|
|
|
'sqlite' => 'math.sql',
|
|
|
|
'postgres' => 'math.pg.sql',
|
|
|
|
'oracle' => 'math.oracle.sql',
|
|
|
|
'mssql' => 'math.mssql.sql',
|
|
|
|
'db2' => 'math.db2.sql',
|
|
|
|
);
|
|
|
|
$type = $updater->getDB()->getType();
|
2011-06-07 17:33:34 +00:00
|
|
|
if ( isset( $map[$type] ) ) {
|
|
|
|
$sql = dirname( __FILE__ ) . '/db/' . $map[$type];
|
2011-04-22 21:37:16 +00:00
|
|
|
$updater->addExtensionTable( 'math', $sql );
|
|
|
|
} else {
|
|
|
|
throw new MWException( "Math extension does not currently support $type database." );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add 'math' table to the list of tables that need to be copied to
|
|
|
|
* temporary tables for parser tests to run.
|
|
|
|
*
|
|
|
|
* @param array $tables
|
|
|
|
* @return bool
|
|
|
|
*/
|
2011-05-23 17:05:39 +00:00
|
|
|
static function onParserTestTables( &$tables ) {
|
2011-04-22 21:37:16 +00:00
|
|
|
$tables[] = 'math';
|
|
|
|
return true;
|
|
|
|
}
|
2011-09-13 21:03:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hack to fake a default $wgMathPath value so parser test output
|
|
|
|
* that renders to images doesn't vary by who runs it.
|
|
|
|
*
|
|
|
|
* @global string $wgMathPath
|
|
|
|
* @param Parser $parser
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static function onParserTestParser( &$parser ) {
|
|
|
|
global $wgMathPath;
|
|
|
|
$wgMathPath = '/images/math';
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|