Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.83% |
45 / 46 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RendererFactory | |
97.83% |
45 / 46 |
|
66.67% |
2 / 3 |
18 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getRenderer | |
96.55% |
28 / 29 |
|
0.00% |
0 / 1 |
15 | |||
| getFromHash | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | /** @var string[] */ |
| 21 | public const CONSTRUCTOR_OPTIONS = [ |
| 22 | 'MathoidCli', |
| 23 | 'MathEnableExperimentalInputFormats', |
| 24 | 'MathValidModes', |
| 25 | ]; |
| 26 | |
| 27 | /** @var ServiceOptions */ |
| 28 | private $options; |
| 29 | |
| 30 | /** @var UserOptionsLookup */ |
| 31 | private $userOptionsLookup; |
| 32 | |
| 33 | /** @var MathConfig */ |
| 34 | private $mathConfig; |
| 35 | |
| 36 | /** @var LoggerInterface */ |
| 37 | private $logger; |
| 38 | |
| 39 | private WANObjectCache $cache; |
| 40 | |
| 41 | /** |
| 42 | * @param ServiceOptions $serviceOptions |
| 43 | * @param MathConfig $mathConfig |
| 44 | * @param UserOptionsLookup $userOptionsLookup |
| 45 | * @param LoggerInterface $logger |
| 46 | * @param WANObjectCache $cache |
| 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 | * Factory method for getting a renderer based on mode |
| 65 | * |
| 66 | * @param string $tex LaTeX markup |
| 67 | * @param array $params HTML attributes |
| 68 | * @param string $mode indicating rendering mode, one of MathConfig::MODE_* |
| 69 | * @return MathRenderer appropriate renderer for mode |
| 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 | // Support of MathML input (experimental) |
| 87 | // Currently support for mode 'mathml' only |
| 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 | } |