Debian Codegraph @

extensions/Math/src/MathConfig.php

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 <?php
2
3 namespace MediaWiki\Extension\Math;
4
5 use MediaWiki\Config\ServiceOptions;
6 use MediaWiki\Message\Message;
7 use MediaWiki\Registration\ExtensionRegistry;
8 use Wikibase\Client\WikibaseClient;
9
10 class MathConfig {
11
12 /** @var string[] */
13 public const CONSTRUCTOR_OPTIONS = [
14 'MathDisableTexFilter',
15 'MathValidModes',
16 'MathEntitySelectorFallbackUrl'
17 ];
18
19 /** @var string */
20 public const ALWAYS = 'always';
21
22 /** @var string */
23 public const NEVER = 'never';
24
25 /** @var string */
26 public const NEW = 'new';
27
28 /** @var string use input tex as formula rendering */
29 public const MODE_SOURCE = 'source';
30
31 /** @var string render formula into MathML */
32 public const MODE_MATHML = 'mathml';
33
34 /** @var string render formula into LateXML */
35 public const MODE_LATEXML = 'latexml';
36
37 /** @var string render formula into MathML using PHP (currently in development) */
38 public const MODE_NATIVE_MML = 'native';
39 /** @var string render formula into MathML using PHP and output it via MathJax */
40 public const MODE_NATIVE_JAX = 'mathjax';
41
42 /** @var string[] a list of all supported rendering modes */
43 private const SUPPORTED_MODES = [
44 self::MODE_SOURCE,
45 self::MODE_LATEXML,
46 self::MODE_MATHML,
47 self::MODE_NATIVE_MML,
48 ];
49
50 /**
51 * @var array mapping from rendering mode to user options value
52 */
53 private const MODES_TO_USER_OPTIONS = [
54 self::MODE_SOURCE => 3,
55 self::MODE_MATHML => 5,
56 self::MODE_LATEXML => 7,
57 self::MODE_NATIVE_MML => 8,
58 ];
59
60 private ServiceOptions $options;
61 private ExtensionRegistry $registry;
62
63 /**
64 * @param ServiceOptions $options
65 * @param ExtensionRegistry $registry
66 */
67 public function __construct(
68 ServiceOptions $options,
69 ExtensionRegistry $registry
70
71 ) {
72 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
73 $this->options = $options;
74 $this->registry = $registry;
75 }
76
77 /**
78 * Whether the TEX filter is disabled.
79 * @return string one of self::CHECK_* constants
80 */
81 public function texCheckDisabled(): string {
82 $setting = $this->options->get( 'MathDisableTexFilter' );
83 if ( $setting === true ) {
84 // ensure backwards compatibility
85 return self::NEVER;
86 }
87 $setting = strtolower( $setting );
88 if ( in_array( $setting, [ self::NEVER, self::ALWAYS, self::NEW ], true ) ) {
89 return $setting;
90 }
91 return self::ALWAYS;
92 }
93
94 /**
95 * Returns an array of valid rendering modes.
96 *
97 * @return string[]
98 */
99 public function getValidRenderingModes(): array {
100 // NOTE: this method is copy-pasted into HookHandlers\SchemaHooksHandler::onLoadExtensionSchemaUpdates
101 // since we can't inject services in there.
102
103 $modes = array_map(
104 [ __CLASS__, 'normalizeRenderingMode' ],
105 $this->options->get( 'MathValidModes' )
106 );
107 return array_unique( $modes );
108 }
109
110 /**
111 * Get message keys for the names of the valid rendering modes.
112 *
113 * @return string[]
114 */
115 public function getValidRenderingModeKeys(): array {
116 $result = [];
117 foreach ( $this->getValidRenderingModes() as $mode ) {
118 $result[$mode] = 'mw-math-' . $mode;
119 }
120 return $result;
121 }
122
123 /**
124 * Get Messages for the names of the valid rendering
125 * modes.
126 *
127 * @return Message[]
128 */
129 public function getValidRenderingModeNames(): array {
130 $result = [];
131 foreach ( $this->getValidRenderingModes() as $mode ) {
132 $result[$mode] = Message::newFromKey( 'mw-math-' . $mode );
133 }
134 return $result;
135 }
136
137 /**
138 * Get the human-readable name of a rendering mode.
139 *
140 * @param string $mode one of self::MODE_* constants.
141 * @return Message
142 */
143 public function getRenderingModeName( string $mode ): Message {
144 return Message::newFromKey( 'mw-math-' . $mode );
145 }
146
147 /**
148 * Checks whether $mode is a valid rendering mode.
149 *
150 * @param string $mode
151 * @return bool
152 */
153 public function isValidRenderingMode( string $mode ): bool {
154 return in_array( $mode, $this->getValidRenderingModes(), true );
155 }
156
157 /**
158 * Get the normalized name of the rendering mode
159 * @param string|int $mode
160 * @param string $default rendering mode to use by default on unrecognized input
161 * @return string one of the self::MODE_* constants.
162 */
163 public static function normalizeRenderingMode( $mode, string $default = self::MODE_NATIVE_MML ): string {
164 if ( is_int( $mode ) ) {
165 $userOptionToMode = array_flip( self::MODES_TO_USER_OPTIONS );
166 return $userOptionToMode[$mode] ?? $default;
167 }
168 $mode = strtolower( $mode );
169 if ( in_array( $mode, self::SUPPORTED_MODES, true ) ) {
170 return $mode;
171 }
172 return $default;
173 }
174
175 /**
176 * If the WikibaseClient is enabled the API url of that client is returned, otherwise the
177 * fallback url is used.
178 * @return string url of the Wikibase url
179 */
180 public function getMathEntitySelectorUrl(): string {
181 // @see WikibaseSettings::isClientEnabled()
182 if ( $this->registry->isLoaded( 'WikibaseClient' ) ) {
183 $settings = WikibaseClient::getSettings();
184 return $settings->getSetting( 'repoUrl' ) .
185 $settings->getSetting( 'repoScriptPath' ) .
186 '/api.php';
187
188 }
189 return $this->options->get( 'MathEntitySelectorFallbackUrl' );
190 }
191 }