Debian Codegraph @

extensions/Math/src/MathNativeMML.php

raw symbols SCIP
1 <?php
2 /**
3 * MediaWiki math extension
4 *
5 * @copyright 2002-2023 various MediaWiki contributors
6 * @license GPL-2.0-or-later
7 */
8
9 namespace MediaWiki\Extension\Math;
10
11 use MediaWiki\Config\Config;
12 use MediaWiki\Extension\Math\InputCheck\LocalChecker;
13 use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmath;
14 use MediaWiki\HookContainer\HookContainer;
15 use MediaWiki\MediaWikiServices;
16 use MediaWiki\SpecialPage\SpecialPage;
17 use MediaWiki\Title\Title;
18 use StatusValue;
19 use Wikimedia\ObjectCache\WANObjectCache;
20
21 /**
22 * Converts LaTeX to MathML using PHP
23 */
24 class MathNativeMML extends MathMathML {
25 private LocalChecker $checker;
26 private Config $mainConfig;
27 private HookContainer $hookContainer;
28
29 /** @inheritDoc */
30 public function __construct( $tex = '', $params = [], $cache = null, $mathConfig = null ) {
31 parent::__construct( $tex, $params, $cache, $mathConfig );
32 $this->setMode( MathConfig::MODE_NATIVE_MML );
33 }
34
35 public static function renderReferenceEntry(
36 array &$entry,
37 ?MathConfig $mathConfig = null,
38 ?HookContainer $hookContainer = null,
39 ?Config $config = null ): bool {
40 $mathConfig ??= Math::getMathConfig();
41 $hookContainer ??= MediaWikiServices::getInstance()->getHookContainer();
42 $config ??= MediaWikiServices::getInstance()->getMainConfig();
43 $renderer = new MathNativeMML( $entry['input'], $entry['params'], WANObjectCache::newEmpty(), $mathConfig );
44 $renderer->setRawError( true );
45 $renderer->setHookContainer( $hookContainer );
46 $renderer->setMainConfig( $config );
47 $renderer->setChecker( new LocalChecker( WANObjectCache::newEmpty(), $entry['input'], 'tex' ) );
48 $result = $renderer->render();
49 $entry['output'] = $renderer->getMathml();
50 if ( !$result ) {
51 $entry['skipped'] = true;
52 $entry['error'] = $renderer->getLastError();
53 }
54 return $result;
55 }
56
57 public function getMainConfig(): Config {
58 $this->mainConfig ??= MediaWikiServices::getInstance()->getMainConfig();
59 return $this->mainConfig;
60 }
61
62 public function getHookContainer(): HookContainer {
63 $this->hookContainer ??= MediaWikiServices::getInstance()->getHookContainer();
64 return $this->hookContainer;
65 }
66
67 protected function doRender(): StatusValue {
68 $checker = $this->getChecker();
69 $checker->setContext( $this );
70 $checker->setHookContainer( $this->getHookContainer() );
71 $presentation = $checker->getPresentationMathMLFragment();
72 $config = $this->getMainConfig();
73 $attributes = [ 'class' => 'mwe-math-element' ];
74 if ( $this->getID() !== '' ) {
75 $attributes['id'] = $this->getID();
76 }
77 if ( $config->get( 'MathEnableFormulaLinks' ) &&
78 isset( $this->params['qid'] ) &&
79 preg_match( '/Q\d+/', $this->params['qid'] ) ) {
80 $titleObj = Title::newFromLinkTarget( SpecialPage::getTitleValueFor( 'MathWikibase' ) );
81 $attributes['href'] = $titleObj->getLocalURL( [ 'qid' => $this->params['qid'] ] );
82 }
83 if ( $this->getMathStyle() == 'display' ) {
84 $attributes['display'] = 'block';
85 }
86 $root = new MMLmath( "", $attributes );
87
88 $this->setMathml( $root->encapsulateRaw( $presentation ?? '' ) );
89 return StatusValue::newGood();
90 }
91
92 protected function getChecker(): LocalChecker {
93 $this->checker ??= Math::getCheckerFactory()
94 ->newLocalChecker( $this->tex, $this->getInputType(), $this->isPurge() );
95 return $this->checker;
96 }
97
98 /**
99 * @inheritDoc
100 */
101 public function getHtmlOutput( bool $svg = true ): string {
102 return $this->getMathml();
103 }
104
105 public function readFromCache(): bool {
106 return false;
107 }
108
109 public function writeCache() {
110 return true;
111 }
112
113 private function setHookContainer( HookContainer $hookContainer ) {
114 $this->hookContainer = $hookContainer;
115 }
116
117 private function setMainConfig( Config $config ) {
118 $this->mainConfig = $config;
119 }
120
121 private function setChecker( LocalChecker $checker ) {
122 $this->checker = $checker;
123 }
124 }