Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RendererFactory
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 3
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getRenderer
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
240
 getFromHash
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\Math\Render;
4
5use InvalidArgumentException;
6use MediaWiki\Config\ServiceOptions;
7use MediaWiki\Extension\Math\MathConfig;
8use MediaWiki\Extension\Math\MathLaTeXML;
9use MediaWiki\Extension\Math\MathMathML;
10use MediaWiki\Extension\Math\MathMathMLCli;
11use MediaWiki\Extension\Math\MathNativeMML;
12use MediaWiki\Extension\Math\MathRenderer;
13use MediaWiki\Extension\Math\MathSource;
14use MediaWiki\User\Options\UserOptionsLookup;
15use Psr\Log\LoggerInterface;
16use WANObjectCache;
17
18class 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( $hash ) {
125        $rpage = $this->cache->get( $hash );
126        if ( $rpage === false ) {
127            throw new InvalidArgumentException( 'Cache key is invalid' );
128        }
129        $mode = $rpage['math_mode'];
130        $renderer = $this->getRenderer( '', [], $mode );
131        $renderer->initializeFromCache( $rpage );
132        return $renderer;
133    }
134}