| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Math\InputCheck; |
| 4 |
|
| 5 |
use MediaWiki\Http\HttpRequestFactory; |
| 6 |
use MediaWiki\Message\Message; |
| 7 |
use Psr\Log\LoggerInterface; |
| 8 |
use RuntimeException; |
| 9 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 10 |
|
| 11 |
class MathoidChecker extends BaseChecker { |
| 12 |
|
| 13 |
private const EXPECTED_RETURN_CODES = [ 200, 400 ]; |
| 14 |
public const VERSION = 1; |
| 15 |
|
| 16 |
private $url; |
| 17 |
|
| 18 |
private $timeout; |
| 19 |
|
| 20 |
private $cache; |
| 21 |
|
| 22 |
private $httpFactory; |
| 23 |
|
| 24 |
private $type; |
| 25 |
|
| 26 |
private $logger; |
| 27 |
|
| 28 |
private $statusCode; |
| 29 |
|
| 30 |
private $response; |
| 31 |
|
| 32 |
public function __construct( |
| 33 |
WANObjectCache $cache, |
| 34 |
HttpRequestFactory $httpFactory, |
| 35 |
LoggerInterface $logger, |
| 36 |
string $url, |
| 37 |
int $timeout, |
| 38 |
string $input, |
| 39 |
string $type, |
| 40 |
bool $purge |
| 41 |
) { |
| 42 |
parent::__construct( $input, $purge ); |
| 43 |
$this->url = $url; |
| 44 |
$this->timeout = $timeout; |
| 45 |
$this->cache = $cache; |
| 46 |
$this->httpFactory = $httpFactory; |
| 47 |
$this->type = $type; |
| 48 |
$this->logger = $logger; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function getCheckResponse(): array { |
| 55 |
if ( $this->statusCode === null ) { |
| 56 |
$cacheInputKey = $this->getCacheKey(); |
| 57 |
if ( $this->purge ) { |
| 58 |
$this->cache->delete( $cacheInputKey, WANObjectCache::TTL_INDEFINITE ); |
| 59 |
} |
| 60 |
[ $this->statusCode, $this->response ] = $this->cache->getWithSetCallback( |
| 61 |
$cacheInputKey, |
| 62 |
WANObjectCache::TTL_INDEFINITE, |
| 63 |
[ $this, 'runCheck' ], |
| 64 |
[ 'version' => self::VERSION ] |
| 65 |
); |
| 66 |
} |
| 67 |
return [ $this->statusCode, $this->response ]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function getCacheKey(): string { |
| 74 |
return $this->cache->makeGlobalKey( |
| 75 |
self::class, |
| 76 |
md5( $this->type . '-' . $this->inputTeX ) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function runCheck(): array { |
| 84 |
$url = "{$this->url}/texvcinfo"; |
| 85 |
$q = rawurlencode( $this->inputTeX ); |
| 86 |
$postData = "type=$this->type&q=$q"; |
| 87 |
$options = [ |
| 88 |
'method' => 'POST', |
| 89 |
'postData' => $postData, |
| 90 |
'timeout' => $this->timeout, |
| 91 |
]; |
| 92 |
$req = $this->httpFactory->create( $url, $options, __METHOD__ ); |
| 93 |
$req->execute(); |
| 94 |
$statusCode = $req->getStatus(); |
| 95 |
if ( in_array( $statusCode, self::EXPECTED_RETURN_CODES, true ) ) { |
| 96 |
return [ $statusCode, $req->getContent() ]; |
| 97 |
} |
| 98 |
$e = new RuntimeException( 'Mathoid check returned unexpected error code.' ); |
| 99 |
$this->logger->error( 'Mathoid check endpoint "{url}" returned ' . |
| 100 |
'HTTP status code "{statusCode}" for post data "{postData}": {exception}.', |
| 101 |
[ |
| 102 |
'url' => $url, |
| 103 |
'statusCode' => $statusCode, |
| 104 |
'postData' => $postData, |
| 105 |
'exception' => $e, |
| 106 |
] |
| 107 |
); |
| 108 |
throw $e; |
| 109 |
} |
| 110 |
|
| 111 |
public function isValid(): bool { |
| 112 |
[ $statusCode ] = $this->getCheckResponse(); |
| 113 |
if ( $statusCode === 200 ) { |
| 114 |
return true; |
| 115 |
} |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
public function getError(): ?Message { |
| 120 |
[ $statusCode, $content ] = $this->getCheckResponse(); |
| 121 |
if ( $statusCode !== 200 ) { |
| 122 |
|
| 123 |
$json = @json_decode( $content ); |
| 124 |
if ( $json && isset( $json->detail ) ) { |
| 125 |
return $this->errorObjectToMessage( $json->detail, $this->url ); |
| 126 |
} |
| 127 |
return $this->errorObjectToMessage( (object)[ 'error' => (object)[ |
| 128 |
'message' => 'Math extension cannot connect to mathoid.' ] ], $this->url ); |
| 129 |
} |
| 130 |
return null; |
| 131 |
} |
| 132 |
|
| 133 |
public function getValidTex(): ?string { |
| 134 |
[ $statusCode, $content ] = $this->getCheckResponse(); |
| 135 |
if ( $statusCode === 200 ) { |
| 136 |
$json = json_decode( $content ); |
| 137 |
return $json->checked; |
| 138 |
} |
| 139 |
return parent::getValidTex(); |
| 140 |
} |
| 141 |
|
| 142 |
} |