Remove call to deprecated ParserOptions::getMath

Since math has been moved from core to an extension
it should take care of the caching by its own.
Therefore, a call to the parserOptions::getMath is removed
and own caching logic is introduced, once getMath is removed
from core.

Bug: 14202
Change-Id: Ifa847b61264f8d640c9886fd374eb3d6cf482c0c
This commit is contained in:
physikerwelt 2014-01-02 17:18:51 +00:00 committed by Physikerwelt
parent bc4c6809f2
commit 52d206c4b4
2 changed files with 94 additions and 16 deletions

View File

@ -7,16 +7,65 @@
*/ */
class MathHooks { class MathHooks {
const mathCacheKey = 'math=';
/*
* Generate a user dependent hash cache key.
* The hash key depends on the rendering mode.
* @param &$confstr The to-be-hashed key string that is being constructed
* @param User $user reference to the current user
* @param array &$forOptions userOptions used on that page
*/
public static function onPageRenderingHash( &$confstr, $user = false, &$forOptions = array() ) {
global $wgUser;
// To be independent of the MediaWiki core version,
// we check if the core caching logic for math is still available.
if ( ! is_callable( 'ParserOptions::getMath' ) && in_array( 'math', $forOptions) ) {
if ( $user === false ) {
$user = $wgUser;
}
$mathOption = $user->getOption( 'math' );
// Check if the key already contains the math option part
if (
!preg_match(
'/(^|!)' . self::mathCacheKey . $mathOption . '(!|$)/',
$confstr
)
) {
// The math part of cache key starts with "math=" followed by a star or a number for the math mode
// and the optional letter j that indicates if clientside MathJax rendering is used.
if ( preg_match( '/(^|!)' . self::mathCacheKey.'[*\d]m?(!|$)/', $confstr ) ) {
$confstr = preg_replace(
'/(^|!)' . self::mathCacheKey . '[*\d]m?(!|$)/',
'\1' . self::mathCacheKey . $mathOption . '\2',
$confstr
);
} else {
$confstr .= '!' . self::mathCacheKey . $mathOption;
}
wfDebugLog( 'Math', "New cache key: $confstr" );
} else {
wfDebugLog( 'Math', "Cache key found $confstr" );
}
}
return true;
}
/** /**
* Set up $wgMathPath and $wgMathDirectory globals if they're not already * Set up $wgMathPath and $wgMathDirectory globals if they're not already set.
* set.
*/ */
static function setup() { static function setup() {
global $wgMathPath, $wgMathDirectory; global $wgMathPath, $wgMathDirectory,
global $wgUploadPath, $wgUploadDirectory; $wgUploadPath, $wgUploadDirectory;
if ( $wgMathPath === false ) { if ( $wgMathPath === false ) {
$wgMathPath = "{$wgUploadPath}/math"; $wgMathPath = "{$wgUploadPath}/math";
} }
if ( $wgMathDirectory === false ) { if ( $wgMathDirectory === false ) {
$wgMathDirectory = "{$wgUploadDirectory}/math"; $wgMathDirectory = "{$wgUploadDirectory}/math";
} }
@ -30,6 +79,7 @@ class MathHooks {
*/ */
static function onParserFirstCallInit( $parser ) { static function onParserFirstCallInit( $parser ) {
$parser->setHook( 'math', array( 'MathHooks', 'mathTagHook' ) ); $parser->setHook( 'math', array( 'MathHooks', 'mathTagHook' ) );
return true; return true;
} }
@ -38,35 +88,48 @@ class MathHooks {
* *
* @param $content (the LaTeX input) * @param $content (the LaTeX input)
* @param $attributes * @param $attributes
* @param $parser Parser * @param Parser $parser
* @return string * @return string
*/ */
static function mathTagHook( $content, $attributes, $parser ) { static function mathTagHook( $content, $attributes, $parser ) {
global $wgMathDisableTexFilter, $wgContLang, $wgUseMathJax; global $wgContLang, $wgUseMathJax, $wgMathDisableTexFilter;
if ( trim( $content ) === "" ) { // bug 8372
return ""; if ( trim( $content ) === '' ) { // bug 8372
return '';
} }
wfProfileIn( __METHOD__ ); wfProfileIn( __METHOD__ );
$mode = $parser->getOptions()->getMath(); $mode = (int)$parser->getUser()->getOption( 'math' );
$renderer = MathRenderer::getRenderer(
$content, $attributes, $mode // Indicate that this page uses math.
); // This affects the page caching behavior.
if ( is_callable( 'ParserOptions::getMath' ) ) {
$parser->getOptions()->getMath();
} else {
$parser->getOptions()->optionUsed( 'math' );
}
$renderer = MathRenderer::getRenderer( $content, $attributes, $mode );
if ( !$wgMathDisableTexFilter ) { if ( !$wgMathDisableTexFilter ) {
$checkResult = $renderer->checkTex(); $checkResult = $renderer->checkTex();
if ( $checkResult !== true ){
// returns the error message if ( $checkResult !== true ) {
// Returns the error message
return $renderer->getLastError(); return $renderer->getLastError();
} }
} }
$renderedMath = $renderer->render(); $renderedMath = $renderer->render();
if ( $wgUseMathJax && $mode == MW_MATH_MATHJAX ) { if ( $wgUseMathJax && $mode == MW_MATH_MATHJAX ) {
$parser->getOutput()->addModules( array( 'ext.math.mathjax.enabler' ) ); $parser->getOutput()->addModules( array( 'ext.math.mathjax.enabler' ) );
} }
$renderer->writeCache(); $renderer->writeCache();
$result = $wgContLang->armourMath( $renderedMath ); $result = $wgContLang->armourMath( $renderedMath );
wfProfileOut( __METHOD__ ); wfProfileOut( __METHOD__ );
return $result; return $result;
} }
@ -84,6 +147,7 @@ class MathHooks {
'label' => ' ', 'label' => ' ',
'section' => 'rendering/math', 'section' => 'rendering/math',
); );
return true; return true;
} }
@ -94,16 +158,20 @@ class MathHooks {
*/ */
private static function getMathNames() { private static function getMathNames() {
global $wgUseMathJax, $wgUseLaTeXML; global $wgUseMathJax, $wgUseLaTeXML;
$names = array( $names = array(
MW_MATH_PNG => wfMessage( 'mw_math_png' )->escaped(), MW_MATH_PNG => wfMessage( 'mw_math_png' )->escaped(),
MW_MATH_SOURCE => wfMessage( 'mw_math_source' )->escaped(), MW_MATH_SOURCE => wfMessage( 'mw_math_source' )->escaped(),
); );
if ( $wgUseMathJax ) { if ( $wgUseMathJax ) {
$names[MW_MATH_MATHJAX] = wfMessage( 'mw_math_mathjax' )->escaped(); $names[MW_MATH_MATHJAX] = wfMessage( 'mw_math_mathjax' )->escaped();
} }
if ( $wgUseLaTeXML ) { if ( $wgUseLaTeXML ) {
$names[MW_MATH_LATEXML] = wfMessage( 'mw_math_latexml' )->escaped(); $names[MW_MATH_LATEXML] = wfMessage( 'mw_math_latexml' )->escaped();
} }
return $names; return $names;
} }
@ -116,8 +184,9 @@ class MathHooks {
static function onMaintenanceRefreshLinksInit( $maint ) { static function onMaintenanceRefreshLinksInit( $maint ) {
global $wgUser; global $wgUser;
# Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway) # Don't generate TeX PNGs (the lack of a sensible current directory causes errors anyway)
$wgUser->setOption( 'math', MW_MATH_SOURCE ); $wgUser->setOption( 'math', MW_MATH_SOURCE );
return true; return true;
} }
@ -130,8 +199,9 @@ class MathHooks {
*/ */
static function onLoadExtensionSchemaUpdates( $updater = null ) { static function onLoadExtensionSchemaUpdates( $updater = null ) {
if ( is_null( $updater ) ) { if ( is_null( $updater ) ) {
throw new MWException( "Math extension is only necessary in 1.18 or above" ); throw new MWException( 'Math extension is only necessary in 1.18 or above' );
} }
$map = array( $map = array(
'mysql' => 'math.sql', 'mysql' => 'math.sql',
'sqlite' => 'math.sql', 'sqlite' => 'math.sql',
@ -139,13 +209,16 @@ class MathHooks {
'oracle' => 'math.oracle.sql', 'oracle' => 'math.oracle.sql',
'mssql' => 'math.mssql.sql', 'mssql' => 'math.mssql.sql',
); );
$type = $updater->getDB()->getType(); $type = $updater->getDB()->getType();
if ( isset( $map[$type] ) ) { if ( isset( $map[$type] ) ) {
$sql = dirname( __FILE__ ) . '/db/' . $map[$type]; $sql = dirname( __FILE__ ) . '/db/' . $map[$type];
$updater->addExtensionTable( 'math', $sql ); $updater->addExtensionTable( 'math', $sql );
} else { } else {
throw new MWException( "Math extension does not currently support $type database." ); throw new MWException( "Math extension does not currently support $type database." );
} }
return true; return true;
} }
@ -158,6 +231,7 @@ class MathHooks {
*/ */
static function onParserTestTables( &$tables ) { static function onParserTestTables( &$tables ) {
$tables[] = 'math'; $tables[] = 'math';
return true; return true;
} }
@ -171,7 +245,9 @@ class MathHooks {
*/ */
static function onParserTestParser( &$parser ) { static function onParserTestParser( &$parser ) {
global $wgMathPath; global $wgMathPath;
$wgMathPath = '/images/math'; $wgMathPath = '/images/math';
return true; return true;
} }
@ -184,6 +260,7 @@ class MathHooks {
static function onRegisterUnitTests( &$files ) { static function onRegisterUnitTests( &$files ) {
$testDir = __DIR__ . '/tests/'; $testDir = __DIR__ . '/tests/';
$files = array_merge( $files, glob( "$testDir/*Test.php" ) ); $files = array_merge( $files, glob( "$testDir/*Test.php" ) );
return true; return true;
} }
} }

View File

@ -150,6 +150,7 @@ $wgHooks['LoadExtensionSchemaUpdates'][] = 'MathHooks::onLoadExtensionSchemaUpda
$wgHooks['ParserTestTables'][] = 'MathHooks::onParserTestTables'; $wgHooks['ParserTestTables'][] = 'MathHooks::onParserTestTables';
$wgHooks['ParserTestParser'][] = 'MathHooks::onParserTestParser'; $wgHooks['ParserTestParser'][] = 'MathHooks::onParserTestParser';
$wgHooks['UnitTestsList'][] = 'MathHooks::onRegisterUnitTests'; $wgHooks['UnitTestsList'][] = 'MathHooks::onRegisterUnitTests';
$wgHooks['PageRenderingHash'][] = 'MathHooks::onPageRenderingHash';
$dir = dirname( __FILE__ ) . '/'; $dir = dirname( __FILE__ ) . '/';
$wgAutoloadClasses['MathHooks'] = $dir . 'Math.hooks.php'; $wgAutoloadClasses['MathHooks'] = $dir . 'Math.hooks.php';