Special:MathStatus Add form for custom input

Allow running tests with custom user input.

Bug: T351368
Change-Id: I148ac88513906042dc1aebe7a637180333d32016
This commit is contained in:
Moritz Schubotz (physikerwelt) 2024-01-04 10:40:41 +01:00
parent 78e45e9ee1
commit 6db2a4c87e
No known key found for this signature in database
GPG key ID: F803DB146DDF36C3
4 changed files with 113 additions and 12 deletions

View file

@ -67,6 +67,12 @@
"math-visualeditor-symbol-group-math-symbols": "Symbols and constants",
"math-visualeditor-symbol-group-math-typefaces": "Typefaces",
"math-visualeditor-symbol-group-math-unsorted": "Unsorted",
"math-form-tex-label": "Custom TeX input",
"math-form-tex-help": "Test with your own TeX input. See [[wp:MATH]] for more help.",
"math-form-type-label": "Type of the input",
"math-form-type-tex": "tex (normal input)",
"math-form-type-chem": "chem (input with chemistry support; e.g. \\ce{H20} for water)",
"math-form-display-label": "Math display mode",
"math_failure": "Failed to parse",
"math_invalidjson": "$1 server response is invalid JSON.",
"math_invalidresponse": "$1: Invalid response (\"$3\") from server \"$2\":",

View file

@ -67,6 +67,12 @@
"math-visualeditor-symbol-group-math-symbols": "Label for the symbols group of math symbols",
"math-visualeditor-symbol-group-math-typefaces": "Label for the typefaces group of math symbols",
"math-visualeditor-symbol-group-math-unsorted": "Label for the unsorted group of math symbols",
"math-form-tex-label": "Label for text field in a form, where a user should specify tex input.",
"math-form-tex-help": "Help text for text field in a form, where a user should specify tex input.",
"math-form-type-label": "Label for radio button group in a form, where a user should select the input type.",
"math-form-type-tex": "Label for normal tex input type.",
"math-form-type-chem": "Label for chemical input type.",
"math-form-display-label": "Label for radio button in a form, where a user should select the display mode.",
"math_failure": "Used as error message.\n\nThis message is followed by \"(\", Error message(*1), Additional message, \"): \" and Source code.\n\n(*1) The error message is any one of the following messages:\n* {{msg-mw|Math unknown error}}\n* {{msg-mw|Math unknown function}}\n* {{msg-mw|Math lexing error}}\n* {{msg-mw|Math syntax error}}\n* {{msg-mw|Math image error}}\n* {{msg-mw|Math bad tmpdir}}\n* {{msg-mw|Math bad output}}\n* {{msg-mw|Math notexvc}}\n* {{msg-mw|Math notexvccheck}}\n* {{msg-mw|Math output error}}\n* {{msg-mw|Math timeout}}\n* {{msg-mw|Math invalidresponse}}\n* {{msg-mw|Math invalidxml}}\n* {{msg-mw|Math invalidjson}}",
"math_invalidjson": "Used as error message.\n\nParameters:\n* $1 - rendering type (either LaTeXML or Mathoid)\n\nThis message follows the message {{msg-mw|Math failure}}.",
"math_invalidresponse": "Used as error message.\n\nFollows the message {{msg-mw|Math failure}}.\n\nParameters:\n* $1 - rendering type (either LaTeXML or Mathoid)\n* $2 - hostname\n* $3 - error message",

View file

@ -4,6 +4,7 @@ namespace MediaWiki\Extension\Math;
use ExtensionRegistry;
use MediaWiki\Extension\Math\Render\RendererFactory;
use MediaWiki\Extension\Math\Widget\MathTestInputForm;
use MediaWiki\Logger\LoggerFactory;
use Psr\Log\LoggerInterface;
use SpecialPage;
@ -45,21 +46,29 @@ class SpecialMathStatus extends SpecialPage {
$out = $this->getOutput();
$enabledMathModes = $this->mathConfig->getValidRenderingModeNames();
$out->addWikiMsg( 'math-status-introduction', count( $enabledMathModes ) );
$req = $this->getRequest();
$tex = $req->getText( 'wptex' );
foreach ( $enabledMathModes as $modeNr => $modeName ) {
$out->wrapWikiMsg( '=== $1 ===', $modeName );
switch ( $modeNr ) {
case MathConfig::MODE_MATHML:
$this->runMathMLTest( $modeName );
break;
case MathConfig::MODE_LATEXML:
$this->runMathLaTeXMLTest( $modeName );
break;
case MathConfig::MODE_NATIVE_MML:
$this->runNativeTest( $modeName );
if ( $tex === '' ) {
$out->addWikiMsg( 'math-status-introduction', count( $enabledMathModes ) );
foreach ( $enabledMathModes as $modeNr => $modeName ) {
$out->wrapWikiMsg( '=== $1 ===', $modeName );
switch ( $modeNr ) {
case MathConfig::MODE_MATHML:
$this->runMathMLTest( $modeName );
break;
case MathConfig::MODE_LATEXML:
$this->runMathLaTeXMLTest( $modeName );
break;
case MathConfig::MODE_NATIVE_MML:
$this->runNativeTest( $modeName );
}
}
}
$form = new MathTestInputForm( $this, $enabledMathModes, $this->rendererFactory );
$form->show();
}
private function runNativeTest( $modeName ) {

View file

@ -0,0 +1,80 @@
<?php
namespace MediaWiki\Extension\Math\Widget;
use MediaWiki\Extension\Math\Render\RendererFactory;
use MediaWiki\Extension\Math\SpecialMathStatus;
use OOUIHTMLForm;
class MathTestInputForm extends OOUIHTMLForm {
private SpecialMathStatus $specialPage;
private array $modes;
private RendererFactory $rendererFactory;
public function __construct( SpecialMathStatus $specialPage, array $modes, RendererFactory $rendererFactory ) {
$this->specialPage = $specialPage;
$this->modes = $modes;
$this->rendererFactory = $rendererFactory;
$formDescriptor = [
'tex' => [
'type' => 'text'
],
'type' => [
'type' => 'radio',
'options' => [ 'tex', 'chem' ]
],
'display' => [
'type' => 'radio',
'options' => [ 'default', 'inline', 'block' ]
]
];
$this->addOptions( $formDescriptor );
parent::__construct( $formDescriptor, $specialPage->getContext() );
$this->setSubmitCallback( [ $this, 'processInput' ] );
}
private function addOptions( array &$form ): void {
static $elements = [ 'label', 'help' ];
static $optionLabelPrefix = [
'display' => 'math-visualeditor-mwlatexinspector-display-',
'type' => 'math-form-type-'
];
foreach ( $form as $key => $control ) {
foreach ( $elements as $element ) {
$msg = "math-form-$key-$element";
if ( wfMessage( $msg )->exists() ) {
$form[$key]["$element-message"] = $msg;
}
}
if ( isset( $control[ 'options' ] ) ) {
$options = [];
foreach ( $control['options'] as $value ) {
$txt = wfMessage( $optionLabelPrefix[$key] . $value )->parseAsBlock();
$options[$txt] = $value;
}
$form[$key]['options'] = $options;
}
}
}
public function processInput( $formData ) {
$out = $this->specialPage->getOutput();
foreach ( $this->modes as $mode => $modeName ) {
$out->wrapWikiMsg( '=== $1 ===', $modeName );
$out->addWikiMsgArray( 'math-test-start', [ $modeName ] );
$options = [
'type' => $formData['type']
];
if ( $formData['display'] !== 'default' ) {
$options['display'] = $formData['display'];
}
$renderer = $this->rendererFactory->getRenderer( $formData['tex'], $options, $mode );
$renderer->render();
$out->addHTML( $renderer->getHtmlOutput() );
$out->addWikiMsgArray( 'math-test-end', [ $modeName ] );
}
}
}