2014-02-09 19:02:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @ingroup Maintenance
|
|
|
|
*/
|
|
|
|
|
2015-01-22 18:56:13 +00:00
|
|
|
require_once __DIR__ . '/../../../maintenance/Maintenance.php';
|
2014-02-09 19:02:15 +00:00
|
|
|
|
2017-06-20 07:11:57 +00:00
|
|
|
class MathGenerateTests extends Maintenance {
|
2014-02-09 19:02:15 +00:00
|
|
|
const REFERENCE_PAGE = 'mediawikiwiki:Extension:Math/CoverageTest';
|
|
|
|
|
2015-09-21 16:14:01 +00:00
|
|
|
public function __construct() {
|
2014-02-09 19:02:15 +00:00
|
|
|
parent::__construct();
|
2016-12-17 03:18:52 +00:00
|
|
|
$this->requireExtension( 'Math' );
|
2014-02-09 19:02:15 +00:00
|
|
|
$this->mDescription = 'Rebuilds the MathCoverage tests';
|
2014-04-08 10:39:12 +00:00
|
|
|
$this->addArg( 'page', "The page used for the testset generation.", false );
|
2015-09-21 16:14:01 +00:00
|
|
|
$this->addOption(
|
|
|
|
'offset', "If set the first n equations on the page are skipped", false, true, "o"
|
|
|
|
);
|
2014-02-09 19:02:15 +00:00
|
|
|
$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" );
|
|
|
|
}
|
|
|
|
|
2014-11-20 15:44:29 +00:00
|
|
|
private static function getMathTagsFromPage( $titleString ) {
|
2014-02-09 19:02:15 +00:00
|
|
|
global $wgEnableScaryTranscluding;
|
|
|
|
$title = Title::newFromText( $titleString );
|
|
|
|
if ( $title->exists() ) {
|
|
|
|
$article = new Article( $title );
|
|
|
|
$wikiText = $article->getPage()->getContent()->getNativeData();
|
|
|
|
} else {
|
|
|
|
if ( $title == self::REFERENCE_PAGE ) {
|
|
|
|
$wgEnableScaryTranscluding = true;
|
|
|
|
$parser = new Parser();
|
|
|
|
$wikiText = $parser->interwikiTransclude( $title, 'raw' );
|
|
|
|
} else {
|
|
|
|
return 'Page does not exist';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$wikiText = Sanitizer::removeHTMLcomments( $wikiText );
|
|
|
|
$wikiText = preg_replace( '#<nowiki>(.*)</nowiki>#', '', $wikiText );
|
2016-04-12 20:53:25 +00:00
|
|
|
$math = [];
|
|
|
|
Parser::extractTagsAndParams( [ 'math' ], $wikiText, $math );
|
2014-11-20 15:44:29 +00:00
|
|
|
return $math;
|
2014-02-09 19:02:15 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 16:14:01 +00:00
|
|
|
public function execute() {
|
2014-02-09 19:02:15 +00:00
|
|
|
global $wgUser;
|
2016-04-12 20:53:25 +00:00
|
|
|
$parserTests = [];
|
2014-02-09 19:02:15 +00:00
|
|
|
$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' );
|
|
|
|
$wgUser = 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 ) {
|
2015-07-22 22:10:46 +00:00
|
|
|
$output = MathRenderer::renderMath( $input[1], $input[2], 'png' );
|
2018-05-18 17:08:51 +00:00
|
|
|
$output = preg_replace( '#src="(.*?)/(([a-f]|\d)*)"#', 'src="\2"', $output );
|
|
|
|
$parserTests[] = [
|
|
|
|
'input' => (string)$input[1],
|
|
|
|
'params' => $input[2],
|
|
|
|
'output' => $output
|
|
|
|
];
|
2014-02-09 19:02:15 +00:00
|
|
|
$i++;
|
|
|
|
echo '.';
|
|
|
|
}
|
|
|
|
echo "Generated $i tests\n";
|
2015-09-21 16:14:01 +00:00
|
|
|
file_put_contents(
|
|
|
|
__DIR__ . '/../tests/ParserTest.json', json_encode( $parserTests, JSON_PRETTY_PRINT )
|
|
|
|
);
|
2014-02-09 19:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 10:13:04 +00:00
|
|
|
$maintClass = MathGenerateTests::class;
|
2015-01-22 18:56:13 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|