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:
parent
bc4c6809f2
commit
52d206c4b4
109
Math.hooks.php
109
Math.hooks.php
|
@ -7,16 +7,65 @@
|
|||
*/
|
||||
|
||||
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.
|
||||
* Set up $wgMathPath and $wgMathDirectory globals if they're not already set.
|
||||
*/
|
||||
static function setup() {
|
||||
global $wgMathPath, $wgMathDirectory;
|
||||
global $wgUploadPath, $wgUploadDirectory;
|
||||
global $wgMathPath, $wgMathDirectory,
|
||||
$wgUploadPath, $wgUploadDirectory;
|
||||
|
||||
if ( $wgMathPath === false ) {
|
||||
$wgMathPath = "{$wgUploadPath}/math";
|
||||
}
|
||||
|
||||
if ( $wgMathDirectory === false ) {
|
||||
$wgMathDirectory = "{$wgUploadDirectory}/math";
|
||||
}
|
||||
|
@ -30,6 +79,7 @@ class MathHooks {
|
|||
*/
|
||||
static function onParserFirstCallInit( $parser ) {
|
||||
$parser->setHook( 'math', array( 'MathHooks', 'mathTagHook' ) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -38,35 +88,48 @@ class MathHooks {
|
|||
*
|
||||
* @param $content (the LaTeX input)
|
||||
* @param $attributes
|
||||
* @param $parser Parser
|
||||
* @param Parser $parser
|
||||
* @return string
|
||||
*/
|
||||
static function mathTagHook( $content, $attributes, $parser ) {
|
||||
global $wgMathDisableTexFilter, $wgContLang, $wgUseMathJax;
|
||||
if ( trim( $content ) === "" ) { // bug 8372
|
||||
return "";
|
||||
global $wgContLang, $wgUseMathJax, $wgMathDisableTexFilter;
|
||||
|
||||
if ( trim( $content ) === '' ) { // bug 8372
|
||||
return '';
|
||||
}
|
||||
|
||||
wfProfileIn( __METHOD__ );
|
||||
$mode = $parser->getOptions()->getMath();
|
||||
$renderer = MathRenderer::getRenderer(
|
||||
$content, $attributes, $mode
|
||||
);
|
||||
$mode = (int)$parser->getUser()->getOption( 'math' );
|
||||
|
||||
// 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 ) {
|
||||
$checkResult = $renderer->checkTex();
|
||||
if ( $checkResult !== true ){
|
||||
// returns the error message
|
||||
|
||||
if ( $checkResult !== true ) {
|
||||
// Returns the error message
|
||||
return $renderer->getLastError();
|
||||
}
|
||||
}
|
||||
|
||||
$renderedMath = $renderer->render();
|
||||
|
||||
if ( $wgUseMathJax && $mode == MW_MATH_MATHJAX ) {
|
||||
$parser->getOutput()->addModules( array( 'ext.math.mathjax.enabler' ) );
|
||||
}
|
||||
|
||||
$renderer->writeCache();
|
||||
$result = $wgContLang->armourMath( $renderedMath );
|
||||
wfProfileOut( __METHOD__ );
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
@ -84,6 +147,7 @@ class MathHooks {
|
|||
'label' => ' ',
|
||||
'section' => 'rendering/math',
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -94,16 +158,20 @@ class MathHooks {
|
|||
*/
|
||||
private static function getMathNames() {
|
||||
global $wgUseMathJax, $wgUseLaTeXML;
|
||||
|
||||
$names = array(
|
||||
MW_MATH_PNG => wfMessage( 'mw_math_png' )->escaped(),
|
||||
MW_MATH_SOURCE => wfMessage( 'mw_math_source' )->escaped(),
|
||||
);
|
||||
|
||||
if ( $wgUseMathJax ) {
|
||||
$names[MW_MATH_MATHJAX] = wfMessage( 'mw_math_mathjax' )->escaped();
|
||||
}
|
||||
|
||||
if ( $wgUseLaTeXML ) {
|
||||
$names[MW_MATH_LATEXML] = wfMessage( 'mw_math_latexml' )->escaped();
|
||||
}
|
||||
|
||||
return $names;
|
||||
}
|
||||
|
||||
|
@ -116,8 +184,9 @@ class MathHooks {
|
|||
static function onMaintenanceRefreshLinksInit( $maint ) {
|
||||
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 );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -130,8 +199,9 @@ class MathHooks {
|
|||
*/
|
||||
static function onLoadExtensionSchemaUpdates( $updater = null ) {
|
||||
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(
|
||||
'mysql' => 'math.sql',
|
||||
'sqlite' => 'math.sql',
|
||||
|
@ -139,13 +209,16 @@ class MathHooks {
|
|||
'oracle' => 'math.oracle.sql',
|
||||
'mssql' => 'math.mssql.sql',
|
||||
);
|
||||
|
||||
$type = $updater->getDB()->getType();
|
||||
|
||||
if ( isset( $map[$type] ) ) {
|
||||
$sql = dirname( __FILE__ ) . '/db/' . $map[$type];
|
||||
$updater->addExtensionTable( 'math', $sql );
|
||||
} else {
|
||||
throw new MWException( "Math extension does not currently support $type database." );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -158,6 +231,7 @@ class MathHooks {
|
|||
*/
|
||||
static function onParserTestTables( &$tables ) {
|
||||
$tables[] = 'math';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -171,7 +245,9 @@ class MathHooks {
|
|||
*/
|
||||
static function onParserTestParser( &$parser ) {
|
||||
global $wgMathPath;
|
||||
|
||||
$wgMathPath = '/images/math';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -184,6 +260,7 @@ class MathHooks {
|
|||
static function onRegisterUnitTests( &$files ) {
|
||||
$testDir = __DIR__ . '/tests/';
|
||||
$files = array_merge( $files, glob( "$testDir/*Test.php" ) );
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
1
Math.php
1
Math.php
|
@ -150,6 +150,7 @@ $wgHooks['LoadExtensionSchemaUpdates'][] = 'MathHooks::onLoadExtensionSchemaUpda
|
|||
$wgHooks['ParserTestTables'][] = 'MathHooks::onParserTestTables';
|
||||
$wgHooks['ParserTestParser'][] = 'MathHooks::onParserTestParser';
|
||||
$wgHooks['UnitTestsList'][] = 'MathHooks::onRegisterUnitTests';
|
||||
$wgHooks['PageRenderingHash'][] = 'MathHooks::onPageRenderingHash';
|
||||
|
||||
$dir = dirname( __FILE__ ) . '/';
|
||||
$wgAutoloadClasses['MathHooks'] = $dir . 'Math.hooks.php';
|
||||
|
|
Loading…
Reference in New Issue