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