2021-08-11 18:15:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
|
2022-07-17 09:59:09 +00:00
|
|
|
use ExtensionRegistry;
|
2021-08-11 18:15:35 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2021-08-12 03:14:17 +00:00
|
|
|
use Message;
|
2022-07-17 09:59:09 +00:00
|
|
|
use Wikibase\Client\WikibaseClient;
|
2021-08-11 18:15:35 +00:00
|
|
|
|
|
|
|
class MathConfig {
|
|
|
|
|
|
|
|
/** @var string[] */
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
'MathDisableTexFilter',
|
|
|
|
'MathValidModes',
|
2022-07-17 09:59:09 +00:00
|
|
|
'MathEntitySelectorFallbackUrl'
|
2021-08-11 18:15:35 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public const ALWAYS = 'always';
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public const NEVER = 'never';
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public const NEW = 'new';
|
|
|
|
|
|
|
|
/** @var string use input tex as formula rendering */
|
|
|
|
public const MODE_SOURCE = 'source';
|
|
|
|
|
|
|
|
/** @var string render formula into PNG images */
|
|
|
|
public const MODE_PNG = 'png';
|
|
|
|
|
|
|
|
/** @var string render formula into MathML */
|
|
|
|
public const MODE_MATHML = 'mathml';
|
|
|
|
|
|
|
|
/** @var string render formula into LateXML */
|
|
|
|
public const MODE_LATEXML = 'latexml';
|
|
|
|
|
2023-02-28 19:20:34 +00:00
|
|
|
/** @var string render formula into MathML using PHP (currently in development) */
|
|
|
|
public const MODE_NATIVE_MML = 'native';
|
|
|
|
|
2021-08-11 18:15:35 +00:00
|
|
|
/** @var string[] a list of all supported rendering modes */
|
|
|
|
private const SUPPORTED_MODES = [
|
|
|
|
self::MODE_SOURCE,
|
|
|
|
self::MODE_LATEXML,
|
|
|
|
self::MODE_MATHML,
|
2023-02-28 19:20:34 +00:00
|
|
|
self::MODE_NATIVE_MML
|
2021-08-11 18:15:35 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array mapping from rendering mode to user options value
|
|
|
|
*/
|
|
|
|
private const MODES_TO_USER_OPTIONS = [
|
|
|
|
self::MODE_SOURCE => 3,
|
|
|
|
self::MODE_MATHML => 5,
|
|
|
|
self::MODE_LATEXML => 7,
|
2023-02-28 19:20:34 +00:00
|
|
|
self::MODE_NATIVE_MML => 8
|
2021-08-11 18:15:35 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/** @var ServiceOptions */
|
|
|
|
private $options;
|
2022-07-17 09:59:09 +00:00
|
|
|
/** @var ExtensionRegistry */
|
|
|
|
private $registry;
|
2021-08-11 18:15:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ServiceOptions $options
|
2022-07-17 09:59:09 +00:00
|
|
|
* @param ExtensionRegistry $registry
|
2021-08-11 18:15:35 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-07-17 09:59:09 +00:00
|
|
|
ServiceOptions $options,
|
|
|
|
ExtensionRegistry $registry
|
|
|
|
|
2021-08-11 18:15:35 +00:00
|
|
|
) {
|
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
$this->options = $options;
|
2022-07-17 09:59:09 +00:00
|
|
|
$this->registry = $registry;
|
2021-08-11 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the TEX filter is disabled.
|
|
|
|
* @return string one of self::CHECK_* constants
|
|
|
|
*/
|
|
|
|
public function texCheckDisabled(): string {
|
|
|
|
$setting = $this->options->get( 'MathDisableTexFilter' );
|
|
|
|
if ( $setting === true ) {
|
|
|
|
// ensure backwards compatibility
|
|
|
|
return self::NEVER;
|
|
|
|
}
|
|
|
|
$setting = strtolower( $setting );
|
|
|
|
if ( in_array( $setting, [ self::NEVER, self::ALWAYS, self::NEW ] ) ) {
|
|
|
|
return $setting;
|
|
|
|
}
|
|
|
|
return self::ALWAYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of valid rendering modes.
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getValidRenderingModes(): array {
|
|
|
|
// NOTE: this method is copy-pasted into Hooks::onLoadExtensionSchemaUpdates
|
|
|
|
// since we can't inject services in there.
|
|
|
|
|
2022-09-29 13:54:47 +00:00
|
|
|
$modes = array_map(
|
|
|
|
[ __CLASS__, 'normalizeRenderingMode' ],
|
|
|
|
$this->options->get( 'MathValidModes' )
|
|
|
|
);
|
2021-08-11 18:15:35 +00:00
|
|
|
return array_unique( $modes );
|
|
|
|
}
|
|
|
|
|
2021-08-12 00:59:21 +00:00
|
|
|
/**
|
|
|
|
* Get message keys for the names of the valid rendering modes.
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getValidRenderingModeKeys(): array {
|
|
|
|
$result = [];
|
|
|
|
foreach ( $this->getValidRenderingModes() as $mode ) {
|
|
|
|
$result[$mode] = 'mw_math_' . $mode;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-08-12 03:14:17 +00:00
|
|
|
/**
|
|
|
|
* Get Messages for the names of the valid rendering
|
|
|
|
* modes.
|
|
|
|
*
|
|
|
|
* @return Message[]
|
|
|
|
*/
|
|
|
|
public function getValidRenderingModeNames(): array {
|
|
|
|
$result = [];
|
|
|
|
foreach ( $this->getValidRenderingModes() as $mode ) {
|
|
|
|
$result[$mode] = Message::newFromKey( 'mw_math_' . $mode );
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the human-readable name of a rendering mode.
|
|
|
|
*
|
|
|
|
* @param string $mode one of self::MODE_* constants.
|
|
|
|
* @return Message
|
|
|
|
*/
|
|
|
|
public function getRenderingModeName( string $mode ): Message {
|
|
|
|
return Message::newFromKey( 'mw_math_' . $mode );
|
|
|
|
}
|
|
|
|
|
2021-08-11 18:15:35 +00:00
|
|
|
/**
|
|
|
|
* Checks whether $mode is a valid rendering mode.
|
|
|
|
*
|
|
|
|
* @param string $mode
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValidRenderingMode( string $mode ): bool {
|
|
|
|
return in_array( $mode, $this->getValidRenderingModes() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the normalized name of the rendering mode
|
|
|
|
* @param string|int $mode
|
|
|
|
* @param string $default rendering mode to use by default on unrecognized input
|
|
|
|
* @return string one of the self::MODE_* constants.
|
|
|
|
*/
|
2022-11-18 22:50:15 +00:00
|
|
|
public static function normalizeRenderingMode( $mode, string $default = self::MODE_MATHML ): string {
|
2021-08-11 18:15:35 +00:00
|
|
|
if ( is_int( $mode ) ) {
|
|
|
|
$userOptionToMode = array_flip( self::MODES_TO_USER_OPTIONS );
|
|
|
|
return $userOptionToMode[$mode] ?? $default;
|
|
|
|
}
|
|
|
|
$mode = strtolower( $mode );
|
|
|
|
if ( in_array( $mode, self::SUPPORTED_MODES ) ) {
|
|
|
|
return $mode;
|
|
|
|
}
|
|
|
|
return $default;
|
|
|
|
}
|
2022-07-17 09:59:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the WikibaseClient is enabled the API url of that client is returned, otherwise the
|
|
|
|
* fallback url is used.
|
|
|
|
* @return string url of the Wikibase url
|
|
|
|
*/
|
|
|
|
public function getMathEntitySelectorUrl(): string {
|
|
|
|
// @see WikibaseSettings::isClientEnabled()
|
|
|
|
if ( $this->registry->isLoaded( 'WikibaseClient' ) ) {
|
|
|
|
$settings = WikibaseClient::getSettings();
|
|
|
|
return $settings->getSetting( 'repoUrl' ) .
|
|
|
|
$settings->getSetting( 'repoScriptPath' ) .
|
|
|
|
'/api.php';
|
|
|
|
|
|
|
|
}
|
|
|
|
return $this->options->get( 'MathEntitySelectorFallbackUrl' );
|
|
|
|
}
|
2021-08-11 18:15:35 +00:00
|
|
|
}
|