requireExtension( 'Math' ); $this->addDescription( 'Rebuilds the MathCoverage tests' ); $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" ); } private static function getMathTagsFromPage( $titleString ) { global $wgEnableScaryTranscluding; $title = Title::newFromText( $titleString ); if ( $title->exists() ) { $article = new Article( $title ); $wikiText = $article->getPage()->getContent()->getNativeData(); } else { if ( $title->getPrefixedDBkey() === self::REFERENCE_PAGE ) { $wgEnableScaryTranscluding = true; $parser = MediaWikiServices::getInstance()->getParserFactory()->create(); $wikiText = $parser->interwikiTransclude( $title, 'raw' ); } else { return 'Page does not exist'; } } $wikiText = Sanitizer::removeHTMLcomments( $wikiText ); $wikiText = preg_replace( '#(.*)#', '', $wikiText ); $math = []; Parser::extractTagsAndParams( [ 'math' ], $wikiText, $math ); return $math; } public function execute() { $parserTests = []; $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' ); RequestContext::getMain()->setUser( User::newFromName( $userName ) ); $allEquations = self::getMathTagsFromPage( $page ); 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; foreach ( array_slice( $allEquations, $offset, $length, true ) as $input ) { // @phan-suppress-next-line PhanTypeMismatchArgumentNullable $output = MathRenderer::renderMath( $input[1], $input[2], 'png' ); $output = preg_replace( '#src="(.*?)/(([a-f]|\d)*)"#', 'src="\2"', $output ); $parserTests[] = [ 'input' => (string)$input[1], 'params' => $input[2], 'output' => $output ]; $i++; echo '.'; } echo "Generated $i tests\n"; file_put_contents( __DIR__ . '/../tests/ParserTest.json', json_encode( $parserTests, JSON_PRETTY_PRINT ) ); } } $maintClass = MathGenerateTests::class; require_once RUN_MAINTENANCE_IF_MAIN;