forked from wikimedia/mediawiki-extensions-Math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Math.hooks.php
419 lines (374 loc) · 12.7 KB
/
Math.hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/**
* MediaWiki math extension
*
* (c) 2002-2015 various MediaWiki contributors
* GPLv2 license; info in main package.
*/
use MediaWiki\Logger\LoggerFactory;
class MathHooks {
private static $tags = [];
const MATHCACHEKEY = 'math=';
public static function mathConstantToString( $value, array $defs, $prefix, $default ) {
foreach ( $defs as $defKey => $defValue ) {
if ( !defined( $defKey ) ) {
define( $defKey, $defValue );
} elseif ( $defValue !== constant( $defKey ) ) {
throw new Exception( 'Math constant "'. $defKey . '" has unexpected value "' .
constant( $defKey ) . '" instead of "' . $defValue );
}
}
$invDefs = array_flip( $defs );
if ( is_int( $value ) ) {
if ( array_key_exists( $value, $invDefs ) ) {
$value = $invDefs[$value];
} else {
return $default;
}
}
if ( is_string( $value ) ) {
$newValues = [];
foreach ( $defs as $k => $v ) {
$newValues[$k] = preg_replace_callback( '/_(.)/', function ( $matches ) {
return strtoupper( $matches[1] );
}, strtolower( substr( $k, strlen( $prefix ) ) ) );
}
if ( array_key_exists( $value, $defs ) ) {
return $newValues[$value];
} elseif ( in_array( $value, $newValues ) ) {
return $value;
}
}
return $default;
}
public static function mathStyleToString( $style, $default = 'inlineDisplaystyle' ) {
$defs = [
'MW_MATHSTYLE_INLINE_DISPLAYSTYLE' => 0, // default large operator inline
'MW_MATHSTYLE_DISPLAY' => 1, // large operators centered in a new line
'MW_MATHSTYLE_INLINE' => 2, // small operators inline
'MW_MATHSTYLE_LINEBREAK' => 3, // break long lines (experimental)
];
return self::mathConstantToString( $style, $defs, $prefix = 'MW_MATHSTYLE_', $default );
}
public static function mathCheckToString( $style, $default = 'always' ) {
$defs = [
'MW_MATH_CHECK_ALWAYS' => 0,
'MW_MATH_CHECK_NEVER' => 1,
'MW_MATH_CHECK_NEW' => 2,
];
return self::mathConstantToString( $style, $defs, $prefix = 'MW_MATH_CHECK_', $default );
}
public static function mathModeToString( $mode, $default = 'png' ) {
// The following deprecated modes have been removed:
// 'MW_MATH_SIMPLE' => 1
// 'MW_MATH_HTML' => 2
// 'MW_MATH_MODERN' => 4
// 'MW_MATH_MATHJAX' => 6
// 'MW_MATH_LATEXML_JAX' => 8
$defs = [
'MW_MATH_PNG' => 0,
'MW_MATH_SOURCE' => 3,
'MW_MATH_MATHML' => 5,
'MW_MATH_LATEXML'=> 7 ];
return self::mathConstantToString( $mode, $defs, $prefix = 'MW_MATH_', $default );
}
public static function mathModeToHashKey( $mode, $default = 0 ) {
$defs = [
'png' => 0,
'source' => 3,
'mathml' => 5,
'latexml'=> 7 ];
if ( array_key_exists( $mode, $defs ) ) {
return $defs[$mode];
} else {
return $default;
}
}
/*
* Generate a user dependent hash cache key.
* The hash key depends on the rendering mode.
* @param &$confstr The to-be-hashed key string that is being constructed
* @param User $user reference to the current user
* @param array &$forOptions userOptions used on that page
*/
public static function onPageRenderingHash( &$confstr, $user = false, &$forOptions = [] ) {
global $wgUser;
// To be independent of the MediaWiki core version,
// we check if the core caching logic for math is still available.
if ( !is_callable( 'ParserOptions::getMath' ) && in_array( 'math', $forOptions ) ) {
if ( $user === false ) {
$user = $wgUser;
}
$mathString = self::mathModeToString( $user->getOption( 'math' ) );
$mathOption = self::mathModeToHashKey( $mathString, 0 );
// Check if the key already contains the math option part
if (
!preg_match(
'/(^|!)' . self::MATHCACHEKEY . $mathOption . '(!|$)/',
$confstr
)
) {
// The math part of cache key starts with "math="
// followed by a star or a number for the math mode
if ( preg_match( '/(^|!)' . self::MATHCACHEKEY . '[*\d]m?(!|$)/', $confstr ) ) {
$confstr = preg_replace(
'/(^|!)' . self::MATHCACHEKEY . '[*\d]m?(!|$)/',
'\1' . self::MATHCACHEKEY . $mathOption . '\2',
$confstr
);
} else {
$confstr .= '!' . self::MATHCACHEKEY . $mathOption;
}
LoggerFactory::getInstance( 'Math' )->debug( "New cache key: $confstr" );
} else {
LoggerFactory::getInstance( 'Math' )->debug( "Cache key found: $confstr" );
}
}
return true;
}
/**
* Set up $wgMathPath and $wgMathDirectory globals if they're not already set.
*/
static function setup() {
global $wgMathPath, $wgMathDirectory,
$wgUploadPath, $wgUploadDirectory;
if ( $wgMathPath === false ) {
$wgMathPath = "{$wgUploadPath}/math";
}
if ( $wgMathDirectory === false ) {
$wgMathDirectory = "{$wgUploadDirectory}/math";
}
}
/**
* Register the <math> tag with the Parser.
*
* @param $parser Parser instance of Parser
* @return Boolean: true
*/
static function onParserFirstCallInit( $parser ) {
$parser->setHook( 'math', [ 'MathHooks', 'mathTagHook' ] );
$parser->setHook( 'ce', [ 'MathHooks', 'ceTagHook' ] );
return true;
}
/**
* Callback function for the <math> parser hook.
*
* @param $content (the LaTeX input)
* @param $attributes
* @param Parser $parser
* @return array
*/
static function mathTagHook( $content, $attributes, $parser ) {
static $n = 1;
if ( trim( $content ) === '' ) { // bug 8372
return '';
}
$mode = self::mathModeToString( $parser->getUser()->getOption( 'math' ) );
// Indicate that this page uses math.
// This affects the page caching behavior.
$parser->getOptions()->optionUsed( 'math' );
$renderer = MathRenderer::getRenderer( $content, $attributes, $mode );
$parser->getOutput()->addModuleStyles( [ 'ext.math.styles' ] );
if ( $mode == 'mathml' ) {
$parser->getOutput()->addModules( [ 'ext.math.scripts' ] );
$marker = Parser::MARKER_PREFIX .
'-postMath-' . sprintf( '%08X', $n ++ ) .
Parser::MARKER_SUFFIX;
self::$tags[$marker] = [ $renderer, $parser ];
return $marker;
}
return [ self::mathPostTagHook( $renderer, $parser ), 'markerType' => 'nowiki' ];
}
/**
* Callback function for the <math> parser hook.
*
* @param Parser $parser
* @param MathRenderer $renderer
* @return array
* @throws FatalError
* @throws MWException
*/
private static function mathPostTagHook( $renderer, $parser ) {
$checkResult = $renderer->checkTeX();
if ( $checkResult !== true ) {
// Returns the error message and add tracking category
$parser->addTrackingCategory( 'math-tracking-category-error' );
return $renderer->getLastError();
}
if ( $renderer->render() ) {
LoggerFactory::getInstance( 'Math' )->debug( "Rendering successful. Writing output" );
$renderedMath = $renderer->getHtmlOutput();
} else {
LoggerFactory::getInstance( 'Math' )->warning(
"Rendering failed. Printing error message." );
$parser->addTrackingCategory( 'math-tracking-category-error' );
return $renderer->getLastError();
}
Hooks::run( 'MathFormulaPostRender',
[ $parser, $renderer, &$renderedMath ] );// Enables indexing of math formula
// Writes cache if rendering was successful
$renderer->writeCache();
return $renderedMath;
}
/**
* Add the new math rendering options to Special:Preferences.
*
* @param $user Object: current User object
* @param $defaultPreferences Object: Preferences object
* @return Boolean: true
*/
static function onGetPreferences( $user, &$defaultPreferences ) {
global $wgDefaultUserOptions;
$defaultPreferences['math'] = [
'type' => 'radio',
'options' => array_flip( self::getMathNames() ),
'label' => ' ',
'section' => 'rendering/math',
];
// If the default option is not in the valid options the
// user interface throws an exception (BUG 64844)
$mode = MathHooks::mathModeToString( $wgDefaultUserOptions['math'] );
if ( ! in_array( $mode, MathRenderer::getValidModes() ) ) {
LoggerFactory::getInstance( 'Math' )->error( 'Misconfiguration: '.
"\$wgDefaultUserOptions['math'] is not in " . MathRenderer::getValidModes() . ".\n".
"Please check your LocalSetting.php file." );
// Display the checkbox in the first option.
$validModes = MathRenderer::getValidModes();
$wgDefaultUserOptions['math'] = $validModes[0];
}
return true;
}
/**
* List of message keys for the various math output settings.
*
* @return array of strings
*/
public static function getMathNames() {
$names = [];
foreach ( MathRenderer::getValidModes() as $mode ) {
$names[$mode] = wfMessage( 'mw_math_' . $mode )->escaped();
}
return $names;
}
/**
* MaintenanceRefreshLinksInit handler; optimize settings for refreshLinks batch job.
*
* @param Maintenance $maint
* @return boolean hook return code
*/
static function onMaintenanceRefreshLinksInit( $maint ) {
global $wgUser;
# Don't generate TeX PNGs (the lack of a sensible current directory causes errors anyway)
$wgUser->setOption( 'math', 'source' );
return true;
}
/**
* LoadExtensionSchemaUpdates handler; set up math table on install/upgrade.
*
* @param $updater DatabaseUpdater
* @throws Exception
* @return bool
*/
static function onLoadExtensionSchemaUpdates( $updater = null ) {
if ( is_null( $updater ) ) {
throw new Exception( 'Math extension is only necessary in 1.18 or above' );
}
$map = [ 'mysql', 'sqlite', 'postgres', 'oracle', 'mssql' ];
$type = $updater->getDB()->getType();
if ( !in_array( $type, $map ) ) {
throw new Exception( "Math extension does not currently support $type database." );
}
$sql = __DIR__ . '/db/math.' . $type . '.sql';
$updater->addExtensionTable( 'math', $sql );
if ( in_array( 'latexml', MathRenderer::getValidModes() ) ) {
if ( in_array( $type, [ 'mysql', 'sqlite', 'postgres' ] ) ) {
$sql = __DIR__ . '/db/mathlatexml.' . $type . '.sql';
$updater->addExtensionTable( 'mathlatexml', $sql );
if ( $type == 'mysql' ) {
$sql = __DIR__ . '/db/patches/mathlatexml.mathml-length-adjustment.mysql.sql';
$updater->modifyExtensionField( 'mathlatexml', 'math_mathml', $sql );
}
} else {
throw new Exception( "Math extension does not currently support $type database for LaTeXML." );
}
}
if ( in_array( 'mathml', MathRenderer::getValidModes() ) ) {
if ( in_array( $type, [ 'mysql', 'sqlite', 'postgres' ] ) ) {
$sql = __DIR__ . '/db/mathoid.' . $type . '.sql';
$updater->addExtensionTable( 'mathoid', $sql );
} else {
throw new Exception( "Math extension does not currently support $type database for Mathoid." );
}
}
return true;
}
/**
* Add 'math' and 'mathlatexml' tables to the list of tables that need to be copied to
* temporary tables for parser tests to run.
*
* @param array $tables
* @return bool
*/
static function onParserTestTables( &$tables ) {
$tables[] = 'math';
$tables[] = 'mathlatexml';
return true;
}
/**
* @param Parser $parser
* @param $text
* @return bool
*/
public static function onParserAfterTidy( &$parser, &$text ) {
$rbis = [];
foreach ( self::$tags as $key => $tag ){
/** @var MathRenderer $renderer */
$renderer = $tag[0];
$rbi = new MathRestbaseInterface( $renderer->getTex(), $renderer->getInputType() );
$renderer->setRestbaseInterface( $rbi );
$rbis[] = $rbi;
}
MathRestbaseInterface::batchEvaluate( $rbis );
foreach ( self::$tags as $key => $tag ){
$value = call_user_func_array( [ "MathHooks","mathPostTagHook" ], $tag );
// Workaround for https://phabricator.wikimedia.org/T103269
$text = preg_replace( '/(<mw:editsection[^>]*>.*?)' . preg_quote( $key ) .
'(.*?)<\/mw:editsection>/',
'\1 $' . htmlspecialchars( $tag[0]->getTex() ) . '\2</mw:editsection>', $text );
$text = str_replace( $key, $value, $text );
}
// This hook might be called multiple times. However one the tags are rendered the job is done.
self::$tags = [];
return true;
}
/**
*
* @global type $wgOut
* @param type $toolbar
*/
static function onEditPageBeforeEditToolbar( &$toolbar ) {
global $wgOut;
$wgOut->addModules( [ 'ext.math.editbutton.enabler' ] );
}
public static function registerExtension() {
global $wgDefaultUserOptions, $wgMathValidModes, $wgMathDisableTexFilter;
$wgMathValidModes = MathRenderer::getValidModes();
if ( $wgMathDisableTexFilter === true ) { // ensure backwards compatibility
$wgMathDisableTexFilter = 'never';
}
$wgMathDisableTexFilter = MathRenderer::getDisableTexFilter();
$wgDefaultUserOptions['math'] = self::mathModeToString( $wgDefaultUserOptions['math'] );
}
/**
* Callback function for the <ce> parser hook.
*
* @param $content (the LaTeX input)
* @param $attributes
* @param Parser $parser
* @return array
*/
static function ceTagHook( $content, $attributes, $parser ) {
$attributes['chem'] = true;
return MathHooks::mathTagHook( '\ce{' . $content . '}', $attributes, $parser );
}
}