2021-07-30 19:17:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math\Render;
|
|
|
|
|
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2021-08-11 18:15:35 +00:00
|
|
|
use MediaWiki\Extension\Math\MathConfig;
|
2021-07-30 19:17:20 +00:00
|
|
|
use MediaWiki\Extension\Math\MathLaTeXML;
|
2021-10-01 01:16:45 +00:00
|
|
|
use MediaWiki\Extension\Math\MathMathML;
|
2021-07-30 19:17:20 +00:00
|
|
|
use MediaWiki\Extension\Math\MathMathMLCli;
|
2023-03-02 15:19:29 +00:00
|
|
|
use MediaWiki\Extension\Math\MathNativeMML;
|
2021-07-30 19:17:20 +00:00
|
|
|
use MediaWiki\Extension\Math\MathRenderer;
|
|
|
|
use MediaWiki\Extension\Math\MathSource;
|
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
class RendererFactory {
|
|
|
|
|
|
|
|
/** @var string[] */
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
'MathoidCli',
|
|
|
|
'MathEnableExperimentalInputFormats',
|
|
|
|
'MathValidModes',
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @var ServiceOptions */
|
|
|
|
private $options;
|
|
|
|
|
|
|
|
/** @var UserOptionsLookup */
|
|
|
|
private $userOptionsLookup;
|
|
|
|
|
2021-08-11 18:15:35 +00:00
|
|
|
/** @var MathConfig */
|
|
|
|
private $mathConfig;
|
|
|
|
|
2021-07-30 19:17:20 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ServiceOptions $serviceOptions
|
2021-08-11 18:15:35 +00:00
|
|
|
* @param MathConfig $mathConfig
|
2021-07-30 19:17:20 +00:00
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ServiceOptions $serviceOptions,
|
2021-08-11 18:15:35 +00:00
|
|
|
MathConfig $mathConfig,
|
2021-07-30 19:17:20 +00:00
|
|
|
UserOptionsLookup $userOptionsLookup,
|
|
|
|
LoggerInterface $logger
|
|
|
|
) {
|
|
|
|
$serviceOptions->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
$this->options = $serviceOptions;
|
2021-08-11 18:15:35 +00:00
|
|
|
$this->mathConfig = $mathConfig;
|
2021-07-30 19:17:20 +00:00
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method for getting a renderer based on mode
|
|
|
|
*
|
|
|
|
* @param string $tex LaTeX markup
|
|
|
|
* @param array $params HTML attributes
|
2021-08-11 18:15:35 +00:00
|
|
|
* @param string $mode indicating rendering mode, one of MathConfig::MODE_*
|
2021-07-30 19:17:20 +00:00
|
|
|
* @return MathRenderer appropriate renderer for mode
|
|
|
|
*/
|
|
|
|
public function getRenderer(
|
|
|
|
string $tex,
|
|
|
|
array $params = [],
|
2022-11-18 22:50:15 +00:00
|
|
|
string $mode = MathConfig::MODE_MATHML
|
2021-07-30 19:17:20 +00:00
|
|
|
): MathRenderer {
|
|
|
|
if ( isset( $params['forcemathmode'] ) ) {
|
|
|
|
$mode = $params['forcemathmode'];
|
|
|
|
}
|
2021-08-11 18:15:35 +00:00
|
|
|
if ( !in_array( $mode, $this->mathConfig->getValidRenderingModes() ) ) {
|
2021-07-30 19:17:20 +00:00
|
|
|
$mode = $this->userOptionsLookup->getDefaultOption( 'math' );
|
|
|
|
}
|
|
|
|
if ( $this->options->get( 'MathEnableExperimentalInputFormats' ) === true &&
|
2021-08-11 18:15:35 +00:00
|
|
|
$mode == MathConfig::MODE_MATHML &&
|
2021-07-30 19:17:20 +00:00
|
|
|
isset( $params['type'] )
|
|
|
|
) {
|
|
|
|
// Support of MathML input (experimental)
|
|
|
|
// Currently support for mode 'mathml' only
|
|
|
|
if ( !in_array( $params['type'], [ 'pmml', 'ascii' ] ) ) {
|
|
|
|
unset( $params['type'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( isset( $params['chem'] ) ) {
|
2021-08-11 18:15:35 +00:00
|
|
|
$mode = MathConfig::MODE_MATHML;
|
2021-07-30 19:17:20 +00:00
|
|
|
$params['type'] = 'chem';
|
|
|
|
}
|
|
|
|
switch ( $mode ) {
|
2021-08-11 18:15:35 +00:00
|
|
|
case MathConfig::MODE_SOURCE:
|
2021-07-30 19:17:20 +00:00
|
|
|
$renderer = new MathSource( $tex, $params );
|
|
|
|
break;
|
2023-03-02 15:19:29 +00:00
|
|
|
case MathConfig::MODE_NATIVE_MML:
|
|
|
|
$renderer = new MathNativeMML( $tex, $params );
|
|
|
|
break;
|
2021-08-11 18:15:35 +00:00
|
|
|
case MathConfig::MODE_LATEXML:
|
2021-07-30 19:17:20 +00:00
|
|
|
$renderer = new MathLaTeXML( $tex, $params );
|
|
|
|
break;
|
2021-08-11 18:15:35 +00:00
|
|
|
case MathConfig::MODE_MATHML:
|
2021-07-30 19:17:20 +00:00
|
|
|
default:
|
|
|
|
if ( $this->options->get( 'MathoidCli' ) ) {
|
|
|
|
$renderer = new MathMathMLCli( $tex, $params );
|
|
|
|
} else {
|
|
|
|
$renderer = new MathMathML( $tex, $params );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->logger->debug(
|
|
|
|
'Start rendering "{tex}" in mode {mode}',
|
|
|
|
[
|
|
|
|
'tex' => $tex,
|
|
|
|
'mode' => $mode
|
|
|
|
]
|
|
|
|
);
|
|
|
|
return $renderer;
|
|
|
|
}
|
|
|
|
}
|