2015-11-26 19:31:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
2018-04-13 14:18:16 +00:00
|
|
|
* @copyright 2002-2015 various MediaWiki contributors
|
|
|
|
* @license GPL-2.0-or-later
|
2015-11-26 19:31:09 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-07 22:22:05 +00:00
|
|
|
namespace MediaWiki\Extension\Math;
|
|
|
|
|
|
|
|
use Exception;
|
2015-11-26 19:31:09 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2020-05-21 04:37:59 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-04-07 22:22:05 +00:00
|
|
|
use MWException;
|
2019-11-18 08:51:05 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2021-04-07 22:22:05 +00:00
|
|
|
use stdClass;
|
2015-11-26 19:31:09 +00:00
|
|
|
|
|
|
|
class MathRestbaseInterface {
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var string|false */
|
2015-11-26 19:31:09 +00:00
|
|
|
private $hash = false;
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var string */
|
2015-11-26 19:31:09 +00:00
|
|
|
private $tex;
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var string */
|
2015-11-26 19:31:09 +00:00
|
|
|
private $type;
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var string|null */
|
2015-11-26 19:31:09 +00:00
|
|
|
private $checkedTex;
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var bool|null */
|
2015-11-26 19:31:09 +00:00
|
|
|
private $success;
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var array */
|
2015-11-26 19:31:09 +00:00
|
|
|
private $identifiers;
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var stdClass|null */
|
2015-11-26 19:31:09 +00:00
|
|
|
private $error;
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var string|null */
|
2016-01-31 21:11:39 +00:00
|
|
|
private $mathoidStyle;
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var string|null */
|
2016-01-31 21:11:39 +00:00
|
|
|
private $mml;
|
2020-12-17 20:53:28 +00:00
|
|
|
/** @var array */
|
2018-06-25 16:08:42 +00:00
|
|
|
private $warnings = [];
|
2017-08-09 20:56:07 +00:00
|
|
|
/** @var bool is there a request to purge the existing mathematical content */
|
2016-06-02 14:30:48 +00:00
|
|
|
private $purge = false;
|
2019-11-18 08:51:05 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
private $logger;
|
2015-11-26 19:31:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $tex
|
2016-01-29 16:50:32 +00:00
|
|
|
* @param string $type
|
2015-11-26 19:31:09 +00:00
|
|
|
*/
|
2016-01-29 16:50:32 +00:00
|
|
|
public function __construct( $tex = '', $type = 'tex' ) {
|
2015-11-26 19:31:09 +00:00
|
|
|
$this->tex = $tex;
|
2016-01-29 16:50:32 +00:00
|
|
|
$this->type = $type;
|
2019-11-18 08:51:05 +00:00
|
|
|
$this->logger = LoggerFactory::getInstance( 'Math' );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 19:27:43 +00:00
|
|
|
/**
|
|
|
|
* Bundles several requests for fetching MathML.
|
2018-08-15 07:58:48 +00:00
|
|
|
* Does not send requests, if the input TeX is invalid.
|
2017-07-10 09:40:12 +00:00
|
|
|
* @param MathRestbaseInterface[] $rbis
|
2023-05-12 17:03:01 +00:00
|
|
|
* @param \MultiHttpClient $multiHttpClient
|
2016-04-08 19:27:43 +00:00
|
|
|
*/
|
2023-05-12 17:03:01 +00:00
|
|
|
private static function batchGetMathML( array $rbis, \MultiHttpClient $multiHttpClient ) {
|
2016-04-12 20:53:25 +00:00
|
|
|
$requests = [];
|
|
|
|
$skips = [];
|
2016-04-08 19:27:43 +00:00
|
|
|
$i = 0;
|
|
|
|
foreach ( $rbis as $rbi ) {
|
|
|
|
/** @var MathRestbaseInterface $rbi */
|
2016-04-30 00:18:41 +00:00
|
|
|
if ( $rbi->getSuccess() ) {
|
2016-04-08 19:27:43 +00:00
|
|
|
$requests[] = $rbi->getContentRequest( 'mml' );
|
|
|
|
} else {
|
|
|
|
$skips[] = $i;
|
|
|
|
}
|
2019-02-06 23:08:22 +00:00
|
|
|
$i++;
|
2016-04-08 19:27:43 +00:00
|
|
|
}
|
2023-05-12 17:03:01 +00:00
|
|
|
$results = $multiHttpClient->runMulti( $requests );
|
2016-06-01 17:47:20 +00:00
|
|
|
$lenRbis = count( $rbis );
|
2016-04-08 19:27:43 +00:00
|
|
|
$j = 0;
|
2019-02-06 23:08:22 +00:00
|
|
|
for ( $i = 0; $i < $lenRbis; $i++ ) {
|
2016-04-08 19:27:43 +00:00
|
|
|
if ( !in_array( $i, $skips ) ) {
|
|
|
|
/** @var MathRestbaseInterface $rbi */
|
|
|
|
$rbi = $rbis[$i];
|
|
|
|
try {
|
2023-05-12 17:03:01 +00:00
|
|
|
$response = $results[ $j ][ 'response' ];
|
|
|
|
$mml = $rbi->evaluateContentResponse( 'mml', $response, $requests[$j] );
|
2016-04-08 19:27:43 +00:00
|
|
|
$rbi->mml = $mml;
|
2022-06-23 00:22:59 +00:00
|
|
|
} catch ( Exception $e ) {
|
2022-09-29 13:54:47 +00:00
|
|
|
// FIXME: Why is this silenced? Doesn't this leave invalid data behind?
|
2016-04-08 19:27:43 +00:00
|
|
|
}
|
2019-02-06 23:08:22 +00:00
|
|
|
$j++;
|
2016-04-08 19:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 14:30:48 +00:00
|
|
|
/**
|
|
|
|
* Lets this instance know if this is a purge request. When set to true,
|
|
|
|
* it will cause the object to issue the first content request with a
|
|
|
|
* 'Cache-Control: no-cache' header to prompt the regeneration of the
|
|
|
|
* renders.
|
|
|
|
*
|
|
|
|
* @param bool $purge whether this is a purge request
|
|
|
|
*/
|
|
|
|
public function setPurge( $purge = true ) {
|
|
|
|
$this->purge = $purge;
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:31:09 +00:00
|
|
|
/**
|
|
|
|
* @return string MathML code
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function getMathML() {
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( !$this->mml ) {
|
2016-01-31 21:11:39 +00:00
|
|
|
$this->mml = $this->getContent( 'mml' );
|
|
|
|
}
|
|
|
|
return $this->mml;
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getContent( $type ) {
|
2016-04-08 19:27:43 +00:00
|
|
|
$request = $this->getContentRequest( $type );
|
2023-05-12 17:03:01 +00:00
|
|
|
$multiHttpClient = $this->getMultiHttpClient();
|
|
|
|
$response = $multiHttpClient->run( $request );
|
2016-04-08 19:27:43 +00:00
|
|
|
return $this->evaluateContentResponse( $type, $response, $request );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function calculateHash() {
|
|
|
|
if ( !$this->hash ) {
|
|
|
|
if ( !$this->checkTeX() ) {
|
|
|
|
throw new MWException( "TeX input is invalid." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkTeX() {
|
2016-01-31 21:11:39 +00:00
|
|
|
$request = $this->getCheckRequest();
|
|
|
|
$requestResult = $this->executeRestbaseCheckRequest( $request );
|
|
|
|
return $this->evaluateRestbaseCheckResponse( $requestResult );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a service request
|
|
|
|
* Generates error messages on failure
|
2022-12-11 14:04:59 +00:00
|
|
|
* @see MediaWiki\Http\HttpRequestFactory::post()
|
2015-11-26 19:31:09 +00:00
|
|
|
*
|
2020-10-05 08:33:50 +00:00
|
|
|
* @param array $request
|
2018-04-07 19:03:56 +00:00
|
|
|
* @return array
|
2015-11-26 19:31:09 +00:00
|
|
|
*/
|
2016-01-31 21:11:39 +00:00
|
|
|
private function executeRestbaseCheckRequest( $request ) {
|
2015-11-26 19:31:09 +00:00
|
|
|
$res = null;
|
2023-05-12 17:03:01 +00:00
|
|
|
$multiHttpClient = $this->getMultiHttpClient();
|
|
|
|
$response = $multiHttpClient->run( $request );
|
2016-01-31 21:11:39 +00:00
|
|
|
if ( $response['code'] !== 200 ) {
|
2020-02-14 21:01:15 +00:00
|
|
|
$this->log()->info( 'Tex check failed', [
|
2020-02-14 20:57:23 +00:00
|
|
|
'post' => $request['body'],
|
|
|
|
'error' => $response['error'],
|
2020-02-14 20:59:55 +00:00
|
|
|
'urlparams' => $request['url']
|
2016-04-12 20:53:25 +00:00
|
|
|
] );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
2016-01-31 21:11:39 +00:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-10 09:40:12 +00:00
|
|
|
* @param MathRestbaseInterface[] $rbis
|
2016-01-31 21:11:39 +00:00
|
|
|
*/
|
2017-07-10 09:48:18 +00:00
|
|
|
public static function batchEvaluate( array $rbis ) {
|
2016-04-08 19:27:43 +00:00
|
|
|
if ( count( $rbis ) == 0 ) {
|
2016-01-31 21:11:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-04-12 20:53:25 +00:00
|
|
|
$requests = [];
|
2016-01-31 21:11:39 +00:00
|
|
|
/** @var MathRestbaseInterface $first */
|
|
|
|
$first = $rbis[0];
|
2023-05-12 17:03:01 +00:00
|
|
|
$multiHttpClient = $first->getMultiHttpClient();
|
2016-01-31 21:11:39 +00:00
|
|
|
foreach ( $rbis as $rbi ) {
|
|
|
|
/** @var MathRestbaseInterface $rbi */
|
|
|
|
$requests[] = $rbi->getCheckRequest();
|
|
|
|
}
|
2023-05-12 17:03:01 +00:00
|
|
|
$results = $multiHttpClient->runMulti( $requests );
|
2016-01-31 21:11:39 +00:00
|
|
|
$i = 0;
|
2023-05-12 17:03:01 +00:00
|
|
|
foreach ( $results as $requestResponse ) {
|
2016-01-31 21:11:39 +00:00
|
|
|
/** @var MathRestbaseInterface $rbi */
|
2019-02-06 23:08:22 +00:00
|
|
|
$rbi = $rbis[$i++];
|
2016-04-08 19:27:43 +00:00
|
|
|
try {
|
2023-05-12 17:03:01 +00:00
|
|
|
$response = $requestResponse[ 'response' ];
|
2016-04-08 19:27:43 +00:00
|
|
|
$rbi->evaluateRestbaseCheckResponse( $response );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
}
|
2016-01-31 21:11:39 +00:00
|
|
|
}
|
2023-05-12 17:03:01 +00:00
|
|
|
self::batchGetMathML( $rbis, $multiHttpClient );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 17:03:01 +00:00
|
|
|
private function getMultiHttpClient() {
|
|
|
|
global $wgMathConcurrentReqs;
|
|
|
|
$multiHttpClient = MediaWikiServices::getInstance()->getHttpRequestFactory()->createMultiClient(
|
2020-05-21 04:37:59 +00:00
|
|
|
[ 'maxConnsPerHost' => $wgMathConcurrentReqs ] );
|
2023-05-12 17:03:01 +00:00
|
|
|
|
|
|
|
return $multiHttpClient;
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-17 02:36:28 +00:00
|
|
|
* The URL is generated according to the following logic:
|
2015-11-26 19:31:09 +00:00
|
|
|
*
|
2022-05-17 02:36:28 +00:00
|
|
|
* Case A: <code>$internal = false</code>, which means one needs a URL that is accessible from
|
2015-11-26 19:31:09 +00:00
|
|
|
* outside:
|
|
|
|
*
|
|
|
|
* --> If <code>$wgMathFullRestbaseURL</code> is configured use it, otherwise fall back try to
|
|
|
|
* <code>$wgVisualEditorFullRestbaseURL</code>. (Note, that this is not be worse than failing
|
|
|
|
* immediately.)
|
|
|
|
*
|
|
|
|
* Case B: <code> $internal= true</code>, which means one needs to access content from Restbase
|
|
|
|
* which does not need to be accessible from outside:
|
|
|
|
*
|
2022-05-17 02:36:28 +00:00
|
|
|
* --> Use the mount point when it is available and <code> $wgMathUseInternalRestbasePath=
|
|
|
|
* true</code>. If not, use <code>$wgMathFullRestbaseURL</code> with fallback to
|
|
|
|
* <code>wgVisualEditorFullRestbaseURL</code>
|
2015-11-26 19:31:09 +00:00
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param bool|true $internal
|
|
|
|
* @return string
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
2015-12-11 07:23:45 +00:00
|
|
|
public function getUrl( $path, $internal = true ) {
|
2022-05-17 02:36:28 +00:00
|
|
|
global $wgMathUseInternalRestbasePath, $wgVirtualRestConfig, $wgMathFullRestbaseURL,
|
|
|
|
$wgVisualEditorFullRestbaseURL;
|
|
|
|
if ( $internal && $wgMathUseInternalRestbasePath && isset( $wgVirtualRestConfig['modules']['restbase'] ) ) {
|
2023-05-12 17:03:01 +00:00
|
|
|
$restBaseUrl = $wgVirtualRestConfig['modules']['restbase']['url'];
|
|
|
|
$restBaseDomain = $wgVirtualRestConfig['modules']['restbase']['domain'] ?? 'localhost';
|
|
|
|
$restBaseUrl = rtrim( $restBaseUrl, '/' );
|
|
|
|
return "$restBaseUrl/$restBaseDomain/v1/$path";
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
if ( $wgMathFullRestbaseURL ) {
|
|
|
|
return "{$wgMathFullRestbaseURL}v1/$path";
|
|
|
|
}
|
|
|
|
if ( $wgVisualEditorFullRestbaseURL ) {
|
|
|
|
return "{$wgVisualEditorFullRestbaseURL}v1/$path";
|
|
|
|
}
|
2015-12-11 07:23:45 +00:00
|
|
|
$msg = 'Math extension can not find Restbase URL. Please specify $wgMathFullRestbaseURL.';
|
|
|
|
$this->setErrorMessage( $msg );
|
|
|
|
throw new MWException( $msg );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Psr\Log\LoggerInterface
|
|
|
|
*/
|
|
|
|
private function log() {
|
2019-11-18 08:51:05 +00:00
|
|
|
return $this->logger;
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSvg() {
|
|
|
|
return $this->getContent( 'svg' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a unique TeX string, renders it and gets it via a public URL.
|
|
|
|
* The method fails, if the public URL does not point to the same server, who did render
|
|
|
|
* the unique TeX input in the first place.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function checkConfig() {
|
|
|
|
// Generates a TeX string that probably has not been generated before
|
|
|
|
$uniqueTeX = uniqid( 't=', true );
|
|
|
|
$testInterface = new MathRestbaseInterface( $uniqueTeX );
|
2019-02-06 23:08:22 +00:00
|
|
|
if ( !$testInterface->checkTeX() ) {
|
2015-11-26 19:31:09 +00:00
|
|
|
$this->log()->warning( 'Config check failed, since test expression was considered as invalid.',
|
2016-04-12 20:53:25 +00:00
|
|
|
[ 'uniqueTeX' => $uniqueTeX ] );
|
2015-11-26 19:31:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-10 09:40:12 +00:00
|
|
|
|
2015-11-26 19:31:09 +00:00
|
|
|
try {
|
|
|
|
$url = $testInterface->getFullSvgUrl();
|
2020-06-07 10:09:08 +00:00
|
|
|
$req = MediaWikiServices::getInstance()->getHttpRequestFactory()->create( $url, [], __METHOD__ );
|
2015-11-26 19:31:09 +00:00
|
|
|
$status = $req->execute();
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( $status->isOK() ) {
|
2015-11-26 19:31:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-10 09:48:18 +00:00
|
|
|
|
2015-11-26 19:31:09 +00:00
|
|
|
$this->log()->warning( 'Config check failed, due to an invalid response code.',
|
2016-04-12 20:53:25 +00:00
|
|
|
[ 'responseCode' => $status ] );
|
2015-11-26 19:31:09 +00:00
|
|
|
} catch ( Exception $e ) {
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->log()->warning( 'Config check failed, due to an exception.', [ $e ] );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
2017-07-10 09:48:18 +00:00
|
|
|
|
|
|
|
return false;
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a publicly accessible link to the generated SVG image.
|
|
|
|
* @return string
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function getFullSvgUrl() {
|
|
|
|
$this->calculateHash();
|
2015-12-11 07:23:45 +00:00
|
|
|
return $this->getUrl( "media/math/render/svg/{$this->hash}", false );
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 17:08:51 +00:00
|
|
|
/**
|
|
|
|
* Gets a publicly accessible link to the generated SVG image.
|
|
|
|
* @return string
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function getFullPngUrl() {
|
|
|
|
$this->calculateHash();
|
|
|
|
return $this->getUrl( "media/math/render/png/{$this->hash}", false );
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:31:09 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCheckedTex() {
|
|
|
|
return $this->checkedTex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-26 20:43:39 +00:00
|
|
|
* @return bool
|
2015-11-26 19:31:09 +00:00
|
|
|
*/
|
|
|
|
public function getSuccess() {
|
2016-01-31 21:11:39 +00:00
|
|
|
if ( $this->success === null ) {
|
|
|
|
$this->checkTeX();
|
|
|
|
}
|
2015-11-26 19:31:09 +00:00
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getIdentifiers() {
|
|
|
|
return $this->identifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return stdClass
|
|
|
|
*/
|
|
|
|
public function getError() {
|
|
|
|
return $this->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTex() {
|
|
|
|
return $this->tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getType() {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2015-12-11 07:23:45 +00:00
|
|
|
private function setErrorMessage( $msg ) {
|
2016-04-12 20:53:25 +00:00
|
|
|
$this->error = (object)[ 'error' => (object)[ 'message' => $msg ] ];
|
2015-12-11 07:23:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 16:08:42 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getWarnings() {
|
|
|
|
return $this->warnings;
|
|
|
|
}
|
|
|
|
|
2016-01-31 21:11:39 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public function getCheckRequest() {
|
2021-04-07 22:22:05 +00:00
|
|
|
return [
|
|
|
|
'method' => 'POST',
|
|
|
|
'body' => [
|
|
|
|
'type' => $this->type,
|
|
|
|
'q' => $this->tex
|
|
|
|
],
|
|
|
|
'url' => $this->getUrl( "media/math/check/{$this->type}" )
|
2016-04-12 20:53:25 +00:00
|
|
|
];
|
2016-01-31 21:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 20:56:07 +00:00
|
|
|
* @param array $response
|
2016-01-31 21:11:39 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function evaluateRestbaseCheckResponse( $response ) {
|
|
|
|
$json = json_decode( $response['body'] );
|
|
|
|
if ( $response['code'] === 200 ) {
|
|
|
|
$headers = $response['headers'];
|
|
|
|
$this->hash = $headers['x-resource-location'];
|
|
|
|
$this->success = $json->success;
|
|
|
|
$this->checkedTex = $json->checked;
|
|
|
|
$this->identifiers = $json->identifiers;
|
2018-06-25 16:08:42 +00:00
|
|
|
if ( isset( $json->warnings ) ) {
|
|
|
|
$this->warnings = $json->warnings;
|
|
|
|
}
|
2016-01-31 21:11:39 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if ( isset( $json->detail ) && isset( $json->detail->success ) ) {
|
|
|
|
$this->success = $json->detail->success;
|
|
|
|
$this->error = $json->detail;
|
|
|
|
} else {
|
|
|
|
$this->success = false;
|
|
|
|
$this->setErrorMessage( 'Math extension cannot connect to Restbase.' );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-04 18:41:47 +00:00
|
|
|
* @return string
|
2016-01-31 21:11:39 +00:00
|
|
|
*/
|
|
|
|
public function getMathoidStyle() {
|
|
|
|
return $this->mathoidStyle;
|
|
|
|
}
|
2016-04-08 19:27:43 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-20 09:10:23 +00:00
|
|
|
* @param string $type
|
2016-04-08 19:27:43 +00:00
|
|
|
* @return array
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
private function getContentRequest( $type ) {
|
|
|
|
$this->calculateHash();
|
2016-04-12 20:53:25 +00:00
|
|
|
$request = [
|
2016-04-08 19:27:43 +00:00
|
|
|
'method' => 'GET',
|
|
|
|
'url' => $this->getUrl( "media/math/render/$type/{$this->hash}" )
|
2016-04-12 20:53:25 +00:00
|
|
|
];
|
2016-06-02 14:30:48 +00:00
|
|
|
if ( $this->purge ) {
|
|
|
|
$request['headers'] = [
|
|
|
|
'Cache-Control' => 'no-cache'
|
|
|
|
];
|
|
|
|
$this->purge = false;
|
|
|
|
}
|
2016-04-08 19:27:43 +00:00
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-20 09:10:23 +00:00
|
|
|
* @param string $type
|
|
|
|
* @param array $response
|
|
|
|
* @param array $request
|
|
|
|
* @return string
|
2016-04-08 19:27:43 +00:00
|
|
|
* @throws MWException
|
|
|
|
*/
|
2018-06-20 09:10:23 +00:00
|
|
|
private function evaluateContentResponse( $type, array $response, array $request ) {
|
2016-04-08 19:27:43 +00:00
|
|
|
if ( $response['code'] === 200 ) {
|
|
|
|
if ( array_key_exists( 'x-mathoid-style', $response['headers'] ) ) {
|
|
|
|
$this->mathoidStyle = $response['headers']['x-mathoid-style'];
|
|
|
|
}
|
|
|
|
return $response['body'];
|
|
|
|
}
|
2016-06-23 22:08:03 +00:00
|
|
|
// Remove "convenience" duplicate keys put in place by MultiHttpClient
|
|
|
|
unset( $response[0], $response[1], $response[2], $response[3], $response[4] );
|
2020-02-14 21:01:15 +00:00
|
|
|
$this->log()->error( 'Restbase math server problem', [
|
2020-02-14 20:59:55 +00:00
|
|
|
'urlparams' => $request['url'],
|
2019-11-14 16:16:03 +00:00
|
|
|
'response' => [ 'code' => $response['code'], 'body' => $response['body'] ],
|
2016-06-23 22:08:03 +00:00
|
|
|
'math_type' => $type,
|
2016-04-08 19:27:43 +00:00
|
|
|
'tex' => $this->tex
|
2016-04-12 20:53:25 +00:00
|
|
|
] );
|
2016-08-17 17:41:04 +00:00
|
|
|
self::throwContentError( $type, $response['body'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 20:56:07 +00:00
|
|
|
* @param string $type
|
|
|
|
* @param string $body
|
2016-08-17 17:41:04 +00:00
|
|
|
* @throws MWException
|
2021-09-08 19:45:48 +00:00
|
|
|
* @return never
|
2016-08-17 17:41:04 +00:00
|
|
|
*/
|
|
|
|
public static function throwContentError( $type, $body ) {
|
|
|
|
$detail = 'Server problem.';
|
|
|
|
$json = json_decode( $body );
|
|
|
|
if ( isset( $json->detail ) ) {
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( is_array( $json->detail ) ) {
|
2016-08-17 17:41:04 +00:00
|
|
|
$detail = $json->detail[0];
|
|
|
|
} elseif ( is_string( $json->detail ) ) {
|
|
|
|
$detail = $json->detail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new MWException( "Cannot get $type. $detail" );
|
2016-04-08 19:27:43 +00:00
|
|
|
}
|
2015-11-26 19:31:09 +00:00
|
|
|
}
|