Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MathTestInputForm
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 3
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
2
 addOptions
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
 processInput
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace MediaWiki\Extension\Math\Widget;
4
5use MediaWiki\Extension\Math\MathConfig;
6use MediaWiki\Extension\Math\Render\RendererFactory;
7use MediaWiki\Extension\Math\SpecialMathStatus;
8use MediaWiki\HTMLForm\OOUIHTMLForm;
9
10class MathTestInputForm extends OOUIHTMLForm {
11
12    private SpecialMathStatus $specialPage;
13    private array $modes;
14    private RendererFactory $rendererFactory;
15
16    public function __construct( SpecialMathStatus $specialPage, array $modes, RendererFactory $rendererFactory ) {
17        $this->specialPage = $specialPage;
18        $this->modes = $modes;
19        $this->rendererFactory = $rendererFactory;
20        $formDescriptor = [
21            'tex' => [
22                'type' => 'text'
23            ],
24            'type' => [
25                'type' => 'radio',
26                'options' => [ 'tex', 'chem' ]
27            ],
28            'display' => [
29                'type' => 'radio',
30                'options' => [ 'default', 'inline', 'block' ]
31            ]
32        ];
33        $this->addOptions( $formDescriptor );
34        parent::__construct( $formDescriptor, $specialPage->getContext() );
35        $this->setSubmitCallback( [ $this, 'processInput' ] );
36    }
37
38    private function addOptions( array &$form ): void {
39        static $elements = [ 'label', 'help' ];
40        static $optionLabelPrefix = [
41            'display' => 'math-visualeditor-mwlatexinspector-display-',
42            'type' => 'math-form-type-'
43        ];
44        foreach ( $form as $key => $control ) {
45            foreach ( $elements as $element ) {
46                $msg = "math-form-$key-$element";
47                if ( wfMessage( $msg )->exists() ) {
48                    $form[$key]["$element-message"] = $msg;
49                }
50            }
51            if ( isset( $control[ 'options' ] ) ) {
52                $options = [];
53                foreach ( $control['options'] as $value ) {
54                    // Messages that can be used here:
55                    // * math-form-type-tex
56                    // * math-form-type-chem
57                    $txt = wfMessage( $optionLabelPrefix[$key] . $value )->parse();
58                    $options[$txt] = $value;
59                }
60                $form[$key]['options'] = $options;
61            }
62        }
63    }
64
65    public function processInput( $formData ) {
66        $out = $this->specialPage->getOutput();
67        foreach ( $this->modes as $mode => $modeName ) {
68            $out->wrapWikiMsg( '=== $1 ===', $modeName );
69            $out->addWikiMsgArray( 'math-test-start', [ $modeName ] );
70
71            $options = [
72                'type' => $formData['type']
73            ];
74            if ( $formData['display'] !== 'default' ) {
75                $options['display'] = $formData['display'];
76            }
77            $renderer = $this->rendererFactory->getRenderer( $formData['tex'], $options, $mode );
78            if ( ( $mode === MathConfig::MODE_SOURCE || $renderer->checkTeX() )
79                && $renderer->render() ) {
80                $html = $renderer->getHtmlOutput();
81            } else {
82                $html = $renderer->getLastError();
83            }
84            $out->addHTML( $html );
85            $out->addWikiMsgArray( 'math-test-end', [ $modeName ] );
86
87        }
88    }
89}