| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\Render; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\Extension\Math\MathConfig; |
| 8 |
use MediaWiki\Extension\Math\MathLaTeXML; |
| 9 |
use MediaWiki\Extension\Math\MathMathML; |
| 10 |
use MediaWiki\Extension\Math\MathMathMLCli; |
| 11 |
use MediaWiki\Extension\Math\MathNativeMML; |
| 12 |
use MediaWiki\Extension\Math\MathRenderer; |
| 13 |
use MediaWiki\Extension\Math\MathSource; |
| 14 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 15 |
use Psr\Log\LoggerInterface; |
| 16 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 17 |
|
| 18 |
class RendererFactory { |
| 19 |
|
| 20 |
|
| 21 |
public const CONSTRUCTOR_OPTIONS = [ |
| 22 |
'MathoidCli', |
| 23 |
'MathEnableExperimentalInputFormats', |
| 24 |
'MathValidModes', |
| 25 |
]; |
| 26 |
|
| 27 |
|
| 28 |
private $options; |
| 29 |
|
| 30 |
|
| 31 |
private $userOptionsLookup; |
| 32 |
|
| 33 |
|
| 34 |
private $mathConfig; |
| 35 |
|
| 36 |
|
| 37 |
private $logger; |
| 38 |
|
| 39 |
private WANObjectCache $cache; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __construct( |
| 49 |
ServiceOptions $serviceOptions, |
| 50 |
MathConfig $mathConfig, |
| 51 |
UserOptionsLookup $userOptionsLookup, |
| 52 |
LoggerInterface $logger, |
| 53 |
WANObjectCache $cache |
| 54 |
) { |
| 55 |
$serviceOptions->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 56 |
$this->options = $serviceOptions; |
| 57 |
$this->mathConfig = $mathConfig; |
| 58 |
$this->userOptionsLookup = $userOptionsLookup; |
| 59 |
$this->logger = $logger; |
| 60 |
$this->cache = $cache; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function getRenderer( |
| 72 |
string $tex, |
| 73 |
array $params = [], |
| 74 |
string $mode = MathConfig::MODE_MATHML |
| 75 |
): MathRenderer { |
| 76 |
if ( isset( $params['forcemathmode'] ) ) { |
| 77 |
$mode = $params['forcemathmode']; |
| 78 |
} |
| 79 |
if ( !in_array( $mode, $this->mathConfig->getValidRenderingModes(), true ) ) { |
| 80 |
$mode = $this->userOptionsLookup->getDefaultOption( 'math' ); |
| 81 |
} |
| 82 |
if ( $this->options->get( 'MathEnableExperimentalInputFormats' ) === true && |
| 83 |
$mode == MathConfig::MODE_MATHML && |
| 84 |
isset( $params['type'] ) |
| 85 |
) { |
| 86 |
|
| 87 |
|
| 88 |
if ( !in_array( $params['type'], [ 'pmml', 'ascii' ], true ) ) { |
| 89 |
unset( $params['type'] ); |
| 90 |
} |
| 91 |
} |
| 92 |
if ( isset( $params['chem'] ) ) { |
| 93 |
$mode = ( $mode == MathConfig::MODE_NATIVE_MML ) ? MathConfig::MODE_NATIVE_MML : MathConfig::MODE_MATHML; |
| 94 |
$params['type'] = 'chem'; |
| 95 |
} |
| 96 |
switch ( $mode ) { |
| 97 |
case MathConfig::MODE_SOURCE: |
| 98 |
$renderer = new MathSource( $tex, $params ); |
| 99 |
break; |
| 100 |
case MathConfig::MODE_NATIVE_MML: |
| 101 |
$renderer = new MathNativeMML( $tex, $params, $this->cache ); |
| 102 |
break; |
| 103 |
case MathConfig::MODE_LATEXML: |
| 104 |
$renderer = new MathLaTeXML( $tex, $params, $this->cache ); |
| 105 |
break; |
| 106 |
case MathConfig::MODE_MATHML: |
| 107 |
default: |
| 108 |
if ( $this->options->get( 'MathoidCli' ) ) { |
| 109 |
$renderer = new MathMathMLCli( $tex, $params, $this->cache ); |
| 110 |
} else { |
| 111 |
$renderer = new MathMathML( $tex, $params, $this->cache ); |
| 112 |
} |
| 113 |
} |
| 114 |
$this->logger->debug( |
| 115 |
'Start rendering "{tex}" in mode {mode}', |
| 116 |
[ |
| 117 |
'tex' => $tex, |
| 118 |
'mode' => $mode |
| 119 |
] |
| 120 |
); |
| 121 |
return $renderer; |
| 122 |
} |
| 123 |
|
| 124 |
public function getFromHash( string $inputHash ): MathRenderer { |
| 125 |
$key = $this->cache->makeGlobalKey( |
| 126 |
MathRenderer::class, |
| 127 |
$inputHash |
| 128 |
); |
| 129 |
$rpage = $this->cache->get( $key ); |
| 130 |
if ( $rpage === false ) { |
| 131 |
throw new InvalidArgumentException( 'Cache key is invalid' ); |
| 132 |
} |
| 133 |
$mode = $rpage['math_mode']; |
| 134 |
$renderer = $this->getRenderer( '', [], $mode ); |
| 135 |
$renderer->initializeFromCache( $rpage ); |
| 136 |
return $renderer; |
| 137 |
} |
| 138 |
} |