2017-08-16 05:50:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-03-17 07:14:13 +00:00
|
|
|
* @author Moritz Schubotz
|
2017-08-16 05:50:44 +00:00
|
|
|
*/
|
|
|
|
require_once __DIR__ . '/../../../maintenance/Maintenance.php';
|
|
|
|
|
2021-08-11 18:15:35 +00:00
|
|
|
use MediaWiki\Extension\Math\MathConfig;
|
2022-04-18 08:31:33 +00:00
|
|
|
use MediaWiki\Extension\Math\MathMathMLCli;
|
2024-06-09 16:48:28 +00:00
|
|
|
use MediaWiki\Parser\Parser;
|
2024-01-06 15:30:26 +00:00
|
|
|
use MediaWiki\Parser\Sanitizer;
|
2023-08-19 04:17:34 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2020-02-17 15:29:08 +00:00
|
|
|
|
2019-12-06 20:47:52 +00:00
|
|
|
class WfTest extends Maintenance {
|
2020-05-30 01:13:31 +00:00
|
|
|
private const REFERENCE_PAGE = 'mediawikiwiki:Extension:Math/CoverageTest';
|
2017-08-16 05:50:44 +00:00
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
$this->requireExtension( 'Math' );
|
2019-08-19 17:59:17 +00:00
|
|
|
$this->addDescription( 'Test Mathoid CLI' );
|
2017-08-16 05:50:44 +00:00
|
|
|
$this->addArg( 'page', "The page used for the testset generation.", false );
|
|
|
|
$this->addOption( 'offset', "If set the first n equations on the page are skipped", false,
|
|
|
|
true, "o" );
|
|
|
|
$this->addOption( 'length', "If set the only n equations were processed", false, true,
|
|
|
|
"l" );
|
|
|
|
$this->addOption( 'user', "User with rights to view the page", false, true, "u" );
|
|
|
|
}
|
|
|
|
|
2024-07-24 16:28:05 +00:00
|
|
|
private function getMathTagsFromPage( $titleString ) {
|
2017-08-16 05:50:44 +00:00
|
|
|
global $wgEnableScaryTranscluding;
|
|
|
|
$title = Title::newFromText( $titleString );
|
|
|
|
if ( $title->exists() ) {
|
|
|
|
$article = new Article( $title );
|
2022-04-18 08:31:33 +00:00
|
|
|
/** @var TextContent $content */
|
|
|
|
'@phan-var TextContent $content';
|
|
|
|
$content = $article->getPage()->getContent();
|
|
|
|
$wikiText = $content->getText();
|
2017-08-16 05:50:44 +00:00
|
|
|
} else {
|
|
|
|
if ( $title == self::REFERENCE_PAGE ) {
|
|
|
|
$wgEnableScaryTranscluding = true;
|
2024-07-24 16:28:05 +00:00
|
|
|
$parser = $this->getServiceContainer()->getParserFactory()->create();
|
2017-08-16 05:50:44 +00:00
|
|
|
$wikiText = $parser->interwikiTransclude( $title, 'raw' );
|
|
|
|
} else {
|
|
|
|
return 'Page does not exist';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$wikiText = Sanitizer::removeHTMLcomments( $wikiText );
|
|
|
|
$wikiText = preg_replace( '#<nowiki>(.*)</nowiki>#', '', $wikiText );
|
|
|
|
$math = [];
|
|
|
|
Parser::extractTagsAndParams( [ 'math' ], $wikiText, $math );
|
|
|
|
|
|
|
|
return $math;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
echo "This test accesses the Mathoid CLI.\n";
|
|
|
|
$page = $this->getArg( 0, self::REFERENCE_PAGE );
|
|
|
|
$offset = $this->getOption( 'offset', 0 );
|
|
|
|
$length = $this->getOption( 'length', PHP_INT_MAX );
|
|
|
|
$userName = $this->getOption( 'user', 'Maintenance script' );
|
2024-07-24 16:28:05 +00:00
|
|
|
$allEquations = $this->getMathTagsFromPage( $page );
|
2017-08-16 05:50:44 +00:00
|
|
|
if ( !is_array( $allEquations ) ) {
|
|
|
|
echo "Could not get equations from page '$page'\n";
|
|
|
|
echo $allEquations . PHP_EOL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
echo 'got ' . count( $allEquations ) . " math tags. Start processing.";
|
|
|
|
}
|
|
|
|
$i = 0;
|
|
|
|
$rend = [];
|
2024-07-24 16:28:05 +00:00
|
|
|
$rendererFactory = $this->getServiceContainer()->get( 'Math.RendererFactory' );
|
2017-08-16 05:50:44 +00:00
|
|
|
foreach ( array_slice( $allEquations, $offset, $length, true ) as $input ) {
|
2022-06-21 09:23:05 +00:00
|
|
|
$renderer = $rendererFactory->getRenderer( $input[1], $input[2], MathConfig::MODE_MATHML );
|
|
|
|
if ( $renderer->render() ) {
|
|
|
|
$output = $renderer->getHtmlOutput();
|
|
|
|
} else {
|
|
|
|
$output = $renderer->getLastError();
|
|
|
|
}
|
|
|
|
$rend[] = [ $renderer, $input ];
|
2017-08-16 05:50:44 +00:00
|
|
|
$parserTests[] = [ (string)$input[1], $output ];
|
2019-02-06 23:08:22 +00:00
|
|
|
$i++;
|
2017-08-16 05:50:44 +00:00
|
|
|
echo '.';
|
|
|
|
}
|
|
|
|
echo "Generated $i tests\n";
|
|
|
|
MathMathMLCli::batchEvaluate( $rend );
|
|
|
|
$retval = null;
|
|
|
|
$stdout = "[
|
|
|
|
{
|
|
|
|
\"query\": {
|
|
|
|
\"q\": \"E=mc^{2}\"
|
|
|
|
}}]";
|
|
|
|
|
|
|
|
// $f = TempFSFile::factory( 'mathoid', 'json', wfTempDir() );
|
|
|
|
// $f->autocollect();
|
|
|
|
// $fhandle = fopen( $f->getPath(), 'w' );
|
|
|
|
// if ( $fhandle ) {
|
|
|
|
// fwrite( $fhandle, $stdout );
|
|
|
|
// fclose( $fhandle );
|
|
|
|
// }
|
|
|
|
// $contents =
|
|
|
|
// wfShellExec( '/tmp/mathoid/cli.js -c /tmp/mathoid/config.dev.yaml ' . $f->getPath(),
|
|
|
|
// $retval );
|
|
|
|
// $contents =
|
|
|
|
// wfShellExecMath( '/tmp/mathoid/cli.js -c /tmp/mathoid/config.dev.yaml ',
|
|
|
|
// $retval, [], [], [], $stdout );
|
|
|
|
|
|
|
|
// if ( $retval == 0 ) {
|
|
|
|
// $res = json_decode( $contents, true );
|
|
|
|
// echo "JSON result" . var_export( $res, false ) . "\n";
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:47:52 +00:00
|
|
|
$maintClass = WfTest::class;
|
2017-08-16 05:50:44 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|