| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\HookHandlers; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Math\Hooks\HookRunner; |
| 6 |
use MediaWiki\Extension\Math\MathConfig; |
| 7 |
use MediaWiki\Extension\Math\MathMathML; |
| 8 |
use MediaWiki\Extension\Math\MathMathMLCli; |
| 9 |
use MediaWiki\Extension\Math\MathRenderer; |
| 10 |
use MediaWiki\Extension\Math\Render\RendererFactory; |
| 11 |
use MediaWiki\Hook\ParserAfterTidyHook; |
| 12 |
use MediaWiki\Hook\ParserFirstCallInitHook; |
| 13 |
use MediaWiki\Hook\ParserOptionsRegisterHook; |
| 14 |
use MediaWiki\HookContainer\HookContainer; |
| 15 |
use MediaWiki\Logger\LoggerFactory; |
| 16 |
use MediaWiki\Parser\Parser; |
| 17 |
use MediaWiki\Parser\ParserOptions; |
| 18 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class ParserHooksHandler implements |
| 24 |
ParserFirstCallInitHook, |
| 25 |
ParserAfterTidyHook, |
| 26 |
ParserOptionsRegisterHook |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
private $mathTagCounter = 1; |
| 31 |
|
| 32 |
|
| 33 |
private $mathLazyRenderBatch = []; |
| 34 |
|
| 35 |
|
| 36 |
private $rendererFactory; |
| 37 |
|
| 38 |
|
| 39 |
private $userOptionsLookup; |
| 40 |
|
| 41 |
|
| 42 |
private $hookRunner; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function __construct( |
| 50 |
RendererFactory $rendererFactory, |
| 51 |
UserOptionsLookup $userOptionsLookup, |
| 52 |
HookContainer $hookContainer |
| 53 |
) { |
| 54 |
$this->rendererFactory = $rendererFactory; |
| 55 |
$this->userOptionsLookup = $userOptionsLookup; |
| 56 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function onParserFirstCallInit( $parser ) { |
| 65 |
$parser->setHook( 'math', [ $this, 'mathTagHook' ] ); |
| 66 |
|
| 67 |
$parser->setHook( 'ce', [ $this, 'chemTagHook' ] ); |
| 68 |
$parser->setHook( 'chem', [ $this, 'chemTagHook' ] ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function mathTagHook( ?string $content, array $attributes, Parser $parser ) { |
| 80 |
global $wgMathSvgRenderer; |
| 81 |
$mode = $parser->getOptions()->getOption( 'math' ); |
| 82 |
if ( $mode === MathConfig::MODE_NATIVE_JAX ) { |
| 83 |
$parser->getOutput()->addModules( [ 'ext.math.mathjax' ] ); |
| 84 |
$mode = MathConfig::MODE_NATIVE_MML; |
| 85 |
} |
| 86 |
$renderer = $this->rendererFactory->getRenderer( $content ?? '', $attributes, $mode ); |
| 87 |
|
| 88 |
$parser->getOutput()->addModuleStyles( [ 'ext.math.styles' ] ); |
| 89 |
if ( array_key_exists( "qid", $attributes ) ) { |
| 90 |
$parser->getOutput()->addModules( [ 'ext.math.popup' ] ); |
| 91 |
} |
| 92 |
if ( $wgMathSvgRenderer === 'restbase' && $mode == MathConfig::MODE_MATHML ) { |
| 93 |
$marker = Parser::MARKER_PREFIX . |
| 94 |
'-postMath-' . sprintf( '%08X', $this->mathTagCounter++ ) . |
| 95 |
Parser::MARKER_SUFFIX; |
| 96 |
$this->mathLazyRenderBatch[$marker] = [ $renderer, $parser ]; |
| 97 |
return $marker; |
| 98 |
} |
| 99 |
return [ $this->mathPostTagHook( $renderer, $parser ), 'markerType' => 'nowiki' ]; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function chemTagHook( ?string $content, array $attributes, Parser $parser ) { |
| 111 |
$attributes['chem'] = true; |
| 112 |
return $this->mathTagHook( '\ce{' . $content . '}', $attributes, $parser ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
private function mathPostTagHook( MathRenderer $renderer, Parser $parser ) { |
| 123 |
$checkResult = $renderer->checkTeX(); |
| 124 |
|
| 125 |
if ( $checkResult !== true ) { |
| 126 |
$renderer->addTrackingCategories( $parser ); |
| 127 |
return $renderer->getLastError(); |
| 128 |
} |
| 129 |
|
| 130 |
if ( $renderer->render() ) { |
| 131 |
LoggerFactory::getInstance( 'Math' )->debug( "Rendering successful. Writing output" ); |
| 132 |
$renderedMath = $renderer->getHtmlOutput(); |
| 133 |
$renderer->addTrackingCategories( $parser ); |
| 134 |
} else { |
| 135 |
LoggerFactory::getInstance( 'Math' )->warning( |
| 136 |
"Rendering failed. Printing error message." ); |
| 137 |
|
| 138 |
|
| 139 |
$parser->getOutput()->updateCacheExpiry( 600 ); |
| 140 |
$renderer->addTrackingCategories( $parser ); |
| 141 |
return $renderer->getLastError(); |
| 142 |
} |
| 143 |
$this->hookRunner->onMathFormulaPostRender( |
| 144 |
$parser, $renderer, $renderedMath |
| 145 |
); |
| 146 |
|
| 147 |
|
| 148 |
$renderer->writeCache(); |
| 149 |
|
| 150 |
return $renderedMath; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function onParserAfterTidy( $parser, &$text ) { |
| 158 |
global $wgMathoidCli; |
| 159 |
$renderers = array_column( $this->mathLazyRenderBatch, 0 ); |
| 160 |
if ( $wgMathoidCli ) { |
| 161 |
MathMathMLCli::batchEvaluate( $renderers ); |
| 162 |
} else { |
| 163 |
MathMathML::batchEvaluate( $renderers ); |
| 164 |
} |
| 165 |
foreach ( $this->mathLazyRenderBatch as $key => [ $renderer, $renderParser ] ) { |
| 166 |
$value = $this->mathPostTagHook( $renderer, $renderParser ); |
| 167 |
$count = 0; |
| 168 |
$text = str_replace( $key, $value, $text, $count ); |
| 169 |
if ( $count ) { |
| 170 |
|
| 171 |
unset( $this->mathLazyRenderBatch[ $key ] ); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
public function onParserOptionsRegister( &$defaults, &$inCacheKey, &$lazyLoad ) { |
| 178 |
$defaults['math'] = $this->userOptionsLookup->getDefaultOption( 'math' ); |
| 179 |
$inCacheKey['math'] = true; |
| 180 |
$lazyLoad['math'] = function ( ParserOptions $options ) { |
| 181 |
return MathConfig::normalizeRenderingMode( |
| 182 |
$this->userOptionsLookup->getOption( $options->getUserIdentity(), 'math' ) |
| 183 |
); |
| 184 |
}; |
| 185 |
} |
| 186 |
} |