2022-12-23 13:33:57 +00:00
|
|
|
<?php
|
|
|
|
|
2023-11-24 09:30:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math\WikiTexVC\MMLmappings\Util;
|
2022-12-23 13:33:57 +00:00
|
|
|
|
|
|
|
use DOMDocument;
|
|
|
|
use InvalidArgumentException;
|
2023-11-24 09:30:05 +00:00
|
|
|
use MediaWiki\Extension\Math\WikiTexVC\MMLnodes\MMLmath;
|
2022-12-23 13:33:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This Utility class has some methods for running
|
2023-11-24 09:30:05 +00:00
|
|
|
* tests for the Tex to MathML converters in WikiTexVC.
|
2022-12-23 13:33:57 +00:00
|
|
|
* @author Johannes Stegmüller
|
|
|
|
*/
|
|
|
|
class MMLTestUtil {
|
|
|
|
public static function getJSON( $filePath ) {
|
|
|
|
if ( !file_exists( $filePath ) ) {
|
|
|
|
throw new InvalidArgumentException( "No testfile found at specified path: " . $filePath );
|
|
|
|
}
|
2024-08-12 06:50:22 +00:00
|
|
|
return json_decode( file_get_contents( $filePath ) );
|
2022-12-23 13:33:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 15:50:11 +00:00
|
|
|
public static function createJSONstartEnd( $start, $file ) {
|
2024-08-12 06:50:22 +00:00
|
|
|
file_put_contents( $file, $start ? "[\n" : "\n]", FILE_APPEND );
|
2023-12-07 15:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function appendToJSONFile( $dataArray, $file ) {
|
|
|
|
$jsonData = json_encode( $dataArray, JSON_PRETTY_PRINT ) . ",";
|
2024-08-12 06:50:22 +00:00
|
|
|
file_put_contents( $file, $jsonData, FILE_APPEND );
|
2023-12-07 15:50:11 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 06:50:22 +00:00
|
|
|
public static function deleteFile( $file ): void {
|
2023-12-07 15:50:11 +00:00
|
|
|
if ( file_exists( $file ) ) {
|
2024-08-12 06:50:22 +00:00
|
|
|
unlink( $file );
|
2023-12-07 15:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-23 13:33:57 +00:00
|
|
|
public static function prettifyXML( $xml, $replaceHeader = true ) {
|
|
|
|
$dom = new DOMDocument();
|
|
|
|
// Initial block (must before load xml string)
|
|
|
|
$dom->preserveWhiteSpace = false;
|
|
|
|
$dom->formatOutput = true;
|
|
|
|
// End initial block
|
|
|
|
$dom->loadXML( $xml );
|
|
|
|
$out = $dom->saveXML();
|
|
|
|
if ( $replaceHeader ) {
|
|
|
|
// replacing the xml header in a hacky way
|
|
|
|
return substr_replace( $out, "", 0, 22 );
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getMMLwrapped( $input ) {
|
|
|
|
$math = new MMLmath();
|
2022-12-30 00:22:50 +00:00
|
|
|
$mml = $math->encapsulateRaw( $input->renderMML() );
|
2022-12-23 13:33:57 +00:00
|
|
|
return self::prettifyXML( $mml );
|
|
|
|
}
|
|
|
|
}
|