Debian Codegraph @

extensions/Math/src/InputCheck/BaseChecker.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Math\InputCheck;
4
5 use MediaWiki\Extension\Math\MathConfig;
6 use MediaWiki\MediaWikiServices;
7 use MediaWiki\Message\Message;
8 use 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 */
18 abstract 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 }