Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseChecker
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 isValid
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getError
n/a
0 / 0
n/a
0 / 0
0
 getValidTex
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPurge
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 errorObjectToMessage
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace MediaWiki\Extension\Math\InputCheck;
4
5use MediaWiki\Extension\Math\MathConfig;
6use MediaWiki\MediaWikiServices;
7use Message;
8use stdClass;
9
10/**
11 * MediaWiki math extension
12 *
13 * @copyright 2002-2014 Tomasz Wegrzanowski, Brion Vibber, Moritz Schubotz,
14 * and other MediaWiki contributors
15 * @license GPL-2.0-or-later
16 * @author Moritz Schubotz
17 */
18abstract class BaseChecker {
19    protected string $inputTeX;
20    protected ?string $validTeX = null;
21    /** @var bool */
22    protected $isValid = false;
23    protected bool $purge = false;
24
25    /**
26     * @param string $tex the TeX InputString to be checked
27     * @param bool $purge if true, the cache will be purged
28     */
29    public function __construct( $tex = '', bool $purge = false ) {
30        $this->inputTeX = $tex;
31        $this->purge = $purge;
32        $this->isValid = false;
33    }
34
35    /**
36     * Returns true if the TeX input String is valid
37     * @return bool
38     */
39    public function isValid() {
40        return $this->isValid;
41    }
42
43    /**
44     * Returns the string of the last error.
45     * @return ?Message
46     */
47    abstract public function getError(): ?Message;
48
49    /**
50     * Some TeX checking programs may return
51     * a modified tex string after having checked it.
52     * You can get the altered tex string with this method
53     * @return ?string A valid Tex string
54     */
55    public function getValidTex(): ?string {
56        return $this->validTeX;
57    }
58
59    public function setPurge( bool $purge ) {
60        $this->purge = $purge;
61    }
62
63    /**
64     * @see https://phabricator.wikimedia.org/T119300
65     * @param stdClass $e
66     * @param string $host
67     * @return Message
68     */
69    protected function errorObjectToMessage( stdClass $e, $host = 'invalid' ): Message {
70        if ( isset( $e->error->message ) ) {
71            if ( $e->error->message === 'Illegal TeX function' ) {
72                return Message::newFromKey( 'math_unknown_function', $e->error->found );
73            } elseif ( preg_match( '/Math extension/', $e->error->message ) ) {
74                // TODO: inject once checker is refactored more
75                $mode = MediaWikiServices::getInstance()
76                    ->get( 'Math.Config' )
77                    ->getRenderingModeName( MathConfig::MODE_MATHML );
78                $msg = $e->error->message;
79                return Message::newFromKey( 'math_invalidresponse', $mode, $host, $msg );
80            }
81
82            return Message::newFromKey( 'math_syntax_error' );
83        }
84
85        return Message::newFromKey( 'math_unknown_error' );
86    }
87}