Debian Codegraph @

extensions/Math/tests/wfTest.php

raw symbols SCIP
1 <?php
2 /**
3 * @author Moritz Schubotz
4 */
5 require_once __DIR__ . '/../../../maintenance/Maintenance.php';
6
7 use MediaWiki\Content\TextContent;
8 use MediaWiki\Extension\Math\MathConfig;
9 use MediaWiki\Extension\Math\MathMathMLCli;
10 use MediaWiki\Maintenance\Maintenance;
11 use MediaWiki\Parser\Parser;
12 use MediaWiki\Parser\Sanitizer;
13 use MediaWiki\Title\Title;
14
15 class WfTest extends Maintenance {
16 private const REFERENCE_PAGE = 'mediawikiwiki:Extension:Math/CoverageTest';
17
18 public function __construct() {
19 parent::__construct();
20 $this->requireExtension( 'Math' );
21 $this->addDescription( 'Test Mathoid CLI' );
22 $this->addArg( 'page', "The page used for the testset generation.", false );
23 $this->addOption( 'offset', "If set the first n equations on the page are skipped", false,
24 true, "o" );
25 $this->addOption( 'length', "If set the only n equations were processed", false, true,
26 "l" );
27 $this->addOption( 'user', "User with rights to view the page", false, true, "u" );
28 }
29
30 private function getMathTagsFromPage( $titleString ) {
31 global $wgEnableScaryTranscluding;
32 $title = Title::newFromText( $titleString );
33 if ( $title->exists() ) {
34 $article = new Article( $title );
35 /** @var TextContent $content */
36 '@phan-var TextContent $content';
37 $content = $article->getPage()->getContent();
38 $wikiText = $content->getText();
39 } else {
40 if ( $title == self::REFERENCE_PAGE ) {
41 $wgEnableScaryTranscluding = true;
42 $parser = $this->getServiceContainer()->getParserFactory()->create();
43 $wikiText = $parser->interwikiTransclude( $title, 'raw' );
44 } else {
45 return 'Page does not exist';
46 }
47 }
48
49 $wikiText = Sanitizer::removeHTMLcomments( $wikiText );
50 $wikiText = preg_replace( '#<nowiki>(.*)</nowiki>#', '', $wikiText );
51 $math = [];
52 Parser::extractTagsAndParams( [ 'math' ], $wikiText, $math );
53
54 return $math;
55 }
56
57 public function execute() {
58 echo "This test accesses the Mathoid CLI.\n";
59 $page = $this->getArg( 0, self::REFERENCE_PAGE );
60 $offset = $this->getOption( 'offset', 0 );
61 $length = $this->getOption( 'length', PHP_INT_MAX );
62 $userName = $this->getOption( 'user', 'Maintenance script' );
63 $allEquations = $this->getMathTagsFromPage( $page );
64 if ( !is_array( $allEquations ) ) {
65 echo "Could not get equations from page '$page'\n";
66 echo $allEquations . PHP_EOL;
67
68 return;
69 } else {
70 echo 'got ' . count( $allEquations ) . " math tags. Start processing.";
71 }
72 $i = 0;
73 $rend = [];
74 $rendererFactory = $this->getServiceContainer()->get( 'Math.RendererFactory' );
75 foreach ( array_slice( $allEquations, $offset, $length, true ) as $input ) {
76 $renderer = $rendererFactory->getRenderer( $input[1], $input[2], MathConfig::MODE_MATHML );
77 if ( $renderer->render() ) {
78 $output = $renderer->getHtmlOutput();
79 } else {
80 $output = $renderer->getLastError();
81 }
82 $rend[] = [ $renderer, $input ];
83 $parserTests[] = [ (string)$input[1], $output ];
84 $i++;
85 echo '.';
86 }
87 echo "Generated $i tests\n";
88 MathMathMLCli::batchEvaluate( $rend );
89 $retval = null;
90 $stdout = "[
91 {
92 \"query\": {
93 \"q\": \"E=mc^{2}\"
94 }}]";
95
96 // $f = TempFSFile::factory( 'mathoid', 'json', wfTempDir() );
97 // $f->autocollect();
98 // $fhandle = fopen( $f->getPath(), 'w' );
99 // if ( $fhandle ) {
100 // fwrite( $fhandle, $stdout );
101 // fclose( $fhandle );
102 // }
103 // $contents =
104 // wfShellExec( '/tmp/mathoid/cli.js -c /tmp/mathoid/config.dev.yaml ' . $f->getPath(),
105 // $retval );
106 // $contents =
107 // wfShellExecMath( '/tmp/mathoid/cli.js -c /tmp/mathoid/config.dev.yaml ',
108 // $retval, [], [], [], $stdout );
109
110 // if ( $retval == 0 ) {
111 // $res = json_decode( $contents, true );
112 // echo "JSON result" . var_export( $res, false ) . "\n";
113 // }
114 }
115 }
116
117 $maintClass = WfTest::class;
118 require_once RUN_MAINTENANCE_IF_MAIN;