Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 11 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MathMLFilter | |
0.00% |
0 / 11 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
register | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
renderMath | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
replaceMath | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
writeRevision | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @ingroup Maintenance |
19 | */ |
20 | |
21 | use MediaWiki\Extension\Math\MathRenderer; |
22 | use MediaWiki\Parser\Sanitizer; |
23 | |
24 | /** |
25 | * Simple dump output filter to exclude all talk pages. |
26 | * |
27 | * @ingroup Dump |
28 | */ |
29 | class MathMLFilter extends DumpFilter { |
30 | |
31 | /** |
32 | * @param BackupDumper $backupDumper |
33 | */ |
34 | public static function register( $backupDumper ) { |
35 | $backupDumper->registerFilter( 'mathml', 'MathMLFilter' ); |
36 | } |
37 | |
38 | /** |
39 | * Callback function that replaces TeX by MathML |
40 | * |
41 | * @param array $match |
42 | * |
43 | * @return string |
44 | */ |
45 | private static function renderMath( $match ) { |
46 | $formula = $match[1]; |
47 | $renderer = MathRenderer::getRenderer( $formula, [], 'latexml' ); |
48 | $renderer->render(); |
49 | // TODO: check if there is a MediaWiki function for that |
50 | return htmlspecialchars( $renderer->getMathml() ); |
51 | } |
52 | |
53 | /** |
54 | * Replaces the math tags with rendered MathML |
55 | * |
56 | * @param string $pText |
57 | * |
58 | * @return string |
59 | */ |
60 | private static function replaceMath( $pText ) { |
61 | $pText = Sanitizer::removeHTMLcomments( $pText ); |
62 | return preg_replace_callback( '#<math>(.*?)</math>#s', [ self::class, 'renderMath' ], |
63 | $pText ); |
64 | } |
65 | |
66 | /** |
67 | * @param stdClass $rev |
68 | * @param string $string the revision text |
69 | */ |
70 | function writeRevision( $rev, $string ) { |
71 | if ( $this->sendingThisPage ) { |
72 | $string = $this->replaceMath( $string ); |
73 | $this->sink->writeRevision( $rev, $string ); |
74 | } |
75 | } |
76 | |
77 | } |