Debian Codegraph @

extensions/Math/src/MathConfig.php

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