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