2023-03-02 15:19:29 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
|
|
|
* @copyright 2002-2023 various MediaWiki contributors
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
|
|
|
|
use MediaWiki\Extension\Math\InputCheck\LocalChecker;
|
|
|
|
use MediaWiki\Extension\Math\TexVC\MMLnodes\MMLmath;
|
2023-03-30 18:24:25 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-08-19 04:17:34 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-03-30 18:24:25 +00:00
|
|
|
use SpecialPage;
|
2023-03-02 15:19:29 +00:00
|
|
|
use StatusValue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts LaTeX to MathML using PHP
|
|
|
|
*/
|
|
|
|
class MathNativeMML extends MathMathML {
|
|
|
|
private LocalChecker $checker;
|
|
|
|
|
|
|
|
public function __construct( $tex = '', $params = [] ) {
|
|
|
|
parent::__construct( $tex, $params );
|
|
|
|
$this->setMode( MathConfig::MODE_NATIVE_MML );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doRender(): StatusValue {
|
2023-10-16 12:41:16 +00:00
|
|
|
$checker = $this->getChecker();
|
|
|
|
$checker->setPurge( $this->isPurge() );
|
2023-10-22 09:41:39 +00:00
|
|
|
$checker->setContext( $this );
|
|
|
|
$checker->setHookContainer( MediaWikiServices::getInstance()->getHookContainer() );
|
2023-10-16 12:41:16 +00:00
|
|
|
$presentation = $checker->getPresentationMathMLFragment();
|
2023-03-30 18:24:25 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig();
|
|
|
|
$attributes = [ 'class' => 'mwe-math-element' ];
|
|
|
|
if ( $this->getID() !== '' ) {
|
|
|
|
$attributes['id'] = $this->getID();
|
|
|
|
}
|
|
|
|
if ( $config->get( 'MathEnableFormulaLinks' ) &&
|
|
|
|
isset( $this->params['qid'] ) &&
|
|
|
|
preg_match( '/Q\d+/', $this->params['qid'] ) ) {
|
|
|
|
$titleObj = Title::newFromLinkTarget( SpecialPage::getTitleValueFor( 'MathWikibase' ) );
|
|
|
|
$attributes['href'] = $titleObj->getLocalURL( [ 'qid' => $this->params['qid'] ] );
|
|
|
|
}
|
|
|
|
if ( $this->getMathStyle() == 'display' ) {
|
|
|
|
$attributes['display'] = 'block';
|
|
|
|
}
|
|
|
|
$root = new MMLmath( "", $attributes );
|
|
|
|
|
2023-06-19 20:14:14 +00:00
|
|
|
$this->setMathml( $root->encapsulateRaw( $presentation ?? '' ) );
|
2023-03-02 15:19:29 +00:00
|
|
|
return StatusValue::newGood();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getChecker(): LocalChecker {
|
|
|
|
$this->checker ??= Math::getCheckerFactory()
|
|
|
|
->newLocalChecker( $this->tex, $this->getInputType() );
|
|
|
|
return $this->checker;
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:24:25 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getHtmlOutput(): string {
|
|
|
|
return $this->getMathml();
|
|
|
|
}
|
|
|
|
|
2023-10-21 09:04:30 +00:00
|
|
|
public function readFromDatabase() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function writeCache() {
|
|
|
|
return true;
|
|
|
|
}
|
2023-03-02 15:19:29 +00:00
|
|
|
}
|