| 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 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
abstract class BaseChecker { |
| 19 |
protected string $inputTeX; |
| 20 |
protected ?string $validTeX = null; |
| 21 |
|
| 22 |
protected $isValid = false; |
| 23 |
protected bool $purge = false; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
public function isValid() { |
| 40 |
return $this->isValid; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
abstract public function getError(): ?Message; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 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 |
} |