Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MathMLFilter
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 renderMath
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 replaceMath
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 writeRevision
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
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
21use MediaWiki\Extension\Math\MathRenderer;
22
23/**
24 * Simple dump output filter to exclude all talk pages.
25 *
26 * @ingroup Dump
27 */
28class MathMLFilter extends DumpFilter {
29
30    /**
31     * @param BackupDumper $backupDumper
32     */
33    public static function register( $backupDumper ) {
34        $backupDumper->registerFilter( 'mathml', 'MathMLFilter' );
35    }
36
37    /**
38     * Callback function that replaces TeX by MathML
39     *
40     * @param array $match
41     *
42     * @return string
43     */
44    private static function renderMath( $match ) {
45        $formula = $match[1];
46        $renderer = MathRenderer::getRenderer( $formula, [], 'latexml' );
47        $renderer->render();
48        // TODO: check if there is a MediaWiki function for that
49        return htmlspecialchars( $renderer->getMathml() );
50    }
51
52    /**
53     * Replaces the math tags with rendered MathML
54     *
55     * @param string $pText
56     *
57     * @return string
58     */
59    private static function replaceMath( $pText ) {
60        $pText = Sanitizer::removeHTMLcomments( $pText );
61        return preg_replace_callback( '#&lt;math&gt;(.*?)&lt;/math&gt;#s', 'self::renderMath',
62            $pText );
63    }
64
65    /**
66     * @param stdClass $rev
67     * @param string $string the revision text
68     */
69    function writeRevision( $rev, $string ) {
70        if ( $this->sendingThisPage ) {
71            $string = $this->replaceMath( $string );
72            $this->sink->writeRevision( $rev, $string );
73        }
74    }
75
76}