Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
24 / 26
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MathNativeMML
92.31% covered (success)
92.31%
24 / 26
66.67% covered (warning)
66.67%
4 / 6
11.06
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 doRender
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
6
 getChecker
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getHtmlOutput
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 readFromCache
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 writeCache
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * MediaWiki math extension
4 *
5 * @copyright 2002-2023 various MediaWiki contributors
6 * @license GPL-2.0-or-later
7 */
8
9namespace MediaWiki\Extension\Math;
10
11use MediaWiki\Extension\Math\InputCheck\LocalChecker;
12use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmath;
13use MediaWiki\MediaWikiServices;
14use MediaWiki\SpecialPage\SpecialPage;
15use MediaWiki\Title\Title;
16use StatusValue;
17
18/**
19 * Converts LaTeX to MathML using PHP
20 */
21class MathNativeMML extends MathMathML {
22    private LocalChecker $checker;
23
24    public function __construct( $tex = '', $params = [], $cache = null ) {
25        parent::__construct( $tex, $params, $cache );
26        $this->setMode( MathConfig::MODE_NATIVE_MML );
27    }
28
29    protected function doRender(): StatusValue {
30        $checker = $this->getChecker();
31        $checker->setContext( $this );
32        $checker->setHookContainer( MediaWikiServices::getInstance()->getHookContainer() );
33        $presentation = $checker->getPresentationMathMLFragment();
34        $config = MediaWikiServices::getInstance()->getMainConfig();
35        $attributes = [ 'class' => 'mwe-math-element' ];
36        if ( $this->getID() !== '' ) {
37            $attributes['id'] = $this->getID();
38        }
39        if ( $config->get( 'MathEnableFormulaLinks' ) &&
40            isset( $this->params['qid'] ) &&
41            preg_match( '/Q\d+/', $this->params['qid'] ) ) {
42            $titleObj = Title::newFromLinkTarget( SpecialPage::getTitleValueFor( 'MathWikibase' ) );
43            $attributes['href'] = $titleObj->getLocalURL( [ 'qid' => $this->params['qid'] ] );
44        }
45        if ( $this->getMathStyle() == 'display' ) {
46            $attributes['display'] = 'block';
47        }
48        $root = new MMLmath( "", $attributes );
49
50        $this->setMathml( $root->encapsulateRaw( $presentation ?? '' ) );
51        return StatusValue::newGood();
52    }
53
54    protected function getChecker(): LocalChecker {
55        $this->checker ??= Math::getCheckerFactory()
56            ->newLocalChecker( $this->tex, $this->getInputType(), $this->isPurge() );
57        return $this->checker;
58    }
59
60    /**
61     * @inheritDoc
62     */
63    public function getHtmlOutput(): string {
64        return $this->getMathml();
65    }
66
67    public function readFromCache(): bool {
68        return false;
69    }
70
71    public function writeCache() {
72        return true;
73    }
74}