| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Math\Render\RendererFactory; |
| 6 |
use MediaWiki\Extension\Math\Widget\MathTestInputForm; |
| 7 |
use MediaWiki\Logger\LoggerFactory; |
| 8 |
use MediaWiki\Registration\ExtensionRegistry; |
| 9 |
use MediaWiki\SpecialPage\UnlistedSpecialPage; |
| 10 |
use Psr\Log\LoggerInterface; |
| 11 |
use UserNotLoggedIn; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class SpecialMathStatus extends UnlistedSpecialPage { |
| 22 |
|
| 23 |
private $logger; |
| 24 |
|
| 25 |
|
| 26 |
private $mathConfig; |
| 27 |
|
| 28 |
|
| 29 |
private $rendererFactory; |
| 30 |
|
| 31 |
public function __construct( |
| 32 |
MathConfig $mathConfig, |
| 33 |
RendererFactory $rendererFactory |
| 34 |
) { |
| 35 |
parent::__construct( 'MathStatus' ); |
| 36 |
|
| 37 |
$this->mathConfig = $mathConfig; |
| 38 |
$this->rendererFactory = $rendererFactory; |
| 39 |
$this->logger = LoggerFactory::getInstance( 'Math' ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function execute( $query ) { |
| 46 |
$this->setHeaders(); |
| 47 |
|
| 48 |
if ( !( $this->getUser()->isNamed() ) ) { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
throw new UserNotLoggedIn(); |
| 54 |
} |
| 55 |
|
| 56 |
$out = $this->getOutput(); |
| 57 |
$enabledMathModes = $this->mathConfig->getValidRenderingModeNames(); |
| 58 |
$req = $this->getRequest(); |
| 59 |
$tex = $req->getText( 'wptex' ); |
| 60 |
|
| 61 |
if ( $tex === '' ) { |
| 62 |
$out->addWikiMsg( 'math-status-introduction', count( $enabledMathModes ) ); |
| 63 |
|
| 64 |
foreach ( $enabledMathModes as $modeNr => $modeName ) { |
| 65 |
$out->wrapWikiMsg( '=== $1 ===', $modeName ); |
| 66 |
switch ( $modeNr ) { |
| 67 |
case MathConfig::MODE_MATHML: |
| 68 |
$this->runMathMLTest( $modeName ); |
| 69 |
break; |
| 70 |
case MathConfig::MODE_LATEXML: |
| 71 |
$this->runMathLaTeXMLTest( $modeName ); |
| 72 |
break; |
| 73 |
case MathConfig::MODE_NATIVE_MML: |
| 74 |
$this->runNativeTest( $modeName ); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$form = new MathTestInputForm( $this, $enabledMathModes, $this->rendererFactory ); |
| 80 |
$form->show(); |
| 81 |
} |
| 82 |
|
| 83 |
private function runNativeTest( string $modeName ) { |
| 84 |
$this->getOutput()->addWikiMsgArray( 'math-test-start', [ $modeName ] ); |
| 85 |
$renderer = $this->rendererFactory->getRenderer( "a+b", [], MathConfig::MODE_NATIVE_MML ); |
| 86 |
if ( !$this->assertTrue( $renderer->render(), "Rendering of a+b in $modeName" ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
$real = str_replace( "\n", '', $renderer->getHtmlOutput() ); |
| 90 |
$expected = '<mo stretchy="false">+</mo>'; |
| 91 |
$this->assertContains( $expected, $real, "Checking the presence of '+' in the MathML output" ); |
| 92 |
$this->getOutput()->addWikiMsgArray( 'math-test-end', [ $modeName ] ); |
| 93 |
} |
| 94 |
|
| 95 |
private function runMathMLTest( string $modeName ) { |
| 96 |
$this->getOutput()->addWikiMsgArray( 'math-test-start', [ $modeName ] ); |
| 97 |
$this->testSpecialCaseText(); |
| 98 |
$this->testMathMLIntegration(); |
| 99 |
$this->testPmmlInput(); |
| 100 |
$this->getOutput()->addWikiMsgArray( 'math-test-end', [ $modeName ] ); |
| 101 |
} |
| 102 |
|
| 103 |
private function runMathLaTeXMLTest( string $modeName ) { |
| 104 |
$this->getOutput()->addWikiMsgArray( 'math-test-start', [ $modeName ] ); |
| 105 |
$this->testLaTeXMLIntegration(); |
| 106 |
$this->testLaTeXMLLinebreak(); |
| 107 |
$this->getOutput()->addWikiMsgArray( 'math-test-end', [ $modeName ] ); |
| 108 |
} |
| 109 |
|
| 110 |
public function testSpecialCaseText() { |
| 111 |
$renderer = $this->rendererFactory->getRenderer( 'x^2+\text{a sample Text}', [], MathConfig::MODE_MATHML ); |
| 112 |
$expected = 'a sample Text</mtext>'; |
| 113 |
if ( !$this->assertTrue( $renderer->render(), 'Rendering the input "x^2+\text{a sample Text}"' ) ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
$this->assertContains( |
| 117 |
$expected, $renderer->getHtmlOutput(), 'Comparing to the reference rendering' |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function testMathMLIntegration() { |
| 126 |
$renderer = $this->rendererFactory->getRenderer( "a+b", [], MathConfig::MODE_MATHML ); |
| 127 |
if ( !$this->assertTrue( $renderer->render(), "Rendering of a+b in plain MathML mode" ) ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
$real = str_replace( "\n", '', $renderer->getHtmlOutput() ); |
| 131 |
$expected = '<mo>+</mo>'; |
| 132 |
$this->assertContains( $expected, $real, "Checking the presence of '+' in the MathML output" ); |
| 133 |
$this->assertContains( |
| 134 |
'<svg xmlns:xlink="http://www.w3.org/1999/xlink" ', |
| 135 |
$renderer->getSvg(), |
| 136 |
"Check that the generated SVG image contains the xlink namespace" |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function testPmmlInput() { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
$inputSample = '<msub> <mrow> <mi> P</mi> </mrow> <mrow> <mi> i</mi> <mi> j</mi> </mrow> </msub> <mo> =</mo> <mfrac> <mrow> <mn> 100</mn> <msub> <mrow> <mi> d</mi> </mrow> <mrow> <mi> i</mi> <mi> j</mi> </mrow> </msub> </mrow> <mrow> <mn> 6.75</mn> <msub> <mrow> <mi> r</mi> </mrow> <mrow> <mi> j</mi> </mrow> </msub> </mrow> </mfrac> <mo> ,</mo> </math>'; |
| 150 |
$attribs = [ 'type' => 'pmml' ]; |
| 151 |
$renderer = $this->rendererFactory->getRenderer( $inputSample, $attribs, MathConfig::MODE_MATHML ); |
| 152 |
$this->assertEquals( 'pmml', $renderer->getInputType(), 'Checking if MathML input is supported' ); |
| 153 |
if ( !$this->assertTrue( $renderer->render(), 'Rendering Presentation MathML sample' ) ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
$real = $renderer->getHtmlOutput(); |
| 157 |
$this->assertContains( 'mode=mathml', $real, 'Checking if the link to SVG image is in correct mode' ); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function testLaTeXMLIntegration() { |
| 165 |
$renderer = $this->rendererFactory->getRenderer( "a+b", [], MathConfig::MODE_LATEXML ); |
| 166 |
if ( !$this->assertTrue( $renderer->render(), "Rendering of a+b in LaTeXML mode" ) ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$expected = '<math xmlns="http://www.w3.org/1998/Math/MathML" '; |
| 171 |
$real = preg_replace( "/\n\\s*/", '', $renderer->getHtmlOutput() ); |
| 172 |
$this->assertContains( $expected, $real, |
| 173 |
"Comparing the output to the MathML reference rendering" . |
| 174 |
$renderer->getLastError() ); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function testLaTeXMLLinebreak() { |
| 183 |
$mathDefaultLaTeXMLSetting = $this->getConfig()->get( 'MathDefaultLaTeXMLSetting' ); |
| 184 |
$tex = ''; |
| 185 |
$testMax = ceil( $mathDefaultLaTeXMLSetting[ 'linelength' ] / 2 ); |
| 186 |
for ( $i = 0; $i < $testMax; $i++ ) { |
| 187 |
$tex .= "$i+"; |
| 188 |
} |
| 189 |
$tex .= $testMax; |
| 190 |
$renderer = new MathLaTeXML( $tex, [ 'display' => 'linebreak' ] ); |
| 191 |
$renderer->setPurge(); |
| 192 |
if ( !$this->assertTrue( $renderer->render(), "Rendering of linebreak test in LaTeXML mode" ) ) { |
| 193 |
return; |
| 194 |
} |
| 195 |
$expected = 'mtr'; |
| 196 |
$real = preg_replace( "/\n\\s*/", '', $renderer->getHtmlOutput() ); |
| 197 |
$this->assertContains( $expected, $real, "Checking for linebreak" . |
| 198 |
$renderer->getLastError() ); |
| 199 |
} |
| 200 |
|
| 201 |
private function assertTrue( bool $expression, string $message = '' ): bool { |
| 202 |
if ( $expression ) { |
| 203 |
$this->getOutput()->addWikiMsgArray( 'math-test-success', [ $message ] ); |
| 204 |
return true; |
| 205 |
} else { |
| 206 |
$this->getOutput()->addWikiMsgArray( 'math-test-fail', [ $message ] ); |
| 207 |
return false; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
private function assertContains( string $expected, string $real, string $message = '' ) { |
| 212 |
if ( !$this->assertTrue( strpos( $real, $expected ) !== false, $message ) ) { |
| 213 |
$this->printDiff( $expected, $real, 'math-test-contains-diff' ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
private function assertEquals( $expected, $real, string $message = '' ): bool { |
| 223 |
if ( is_array( $expected ) ) { |
| 224 |
foreach ( $expected as $alternative ) { |
| 225 |
if ( $alternative === $real ) { |
| 226 |
$this->getOutput()->addWikiMsgArray( 'math-test-success', [ $message ] ); |
| 227 |
return true; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
$this->getOutput()->addWikiMsgArray( 'math-test-fail', [ $message ] ); |
| 232 |
return false; |
| 233 |
} |
| 234 |
if ( !$this->assertTrue( $expected === $real, $message ) ) { |
| 235 |
$this->printDiff( $expected, $real, 'math-test-equals-diff' ); |
| 236 |
return false; |
| 237 |
} |
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
private function printDiff( string $expected, string $real, string $message = '' ) { |
| 242 |
if ( ExtensionRegistry::getInstance()->isLoaded( "SyntaxHighlight" ) ) { |
| 243 |
$expected = "<syntaxhighlight lang=\"xml\">$expected</syntaxhighlight>"; |
| 244 |
$real = "<syntaxhighlight lang=\"xml\">$real</syntaxhighlight>"; |
| 245 |
$this->getOutput()->addWikiMsgArray( $message, [ $real, $expected ] ); |
| 246 |
} else { |
| 247 |
$this->logger->warning( 'Can not display expected and real value.' . |
| 248 |
'SyntaxHighlight is not installed.' ); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
protected function getGroupName(): string { |
| 253 |
return 'other'; |
| 254 |
} |
| 255 |
} |