2015-11-28 09:24:54 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
|
|
|
* (c) 2002-2015 Tomasz Wegrzanowski, Brion Vibber, Moritz Schubotz,
|
|
|
|
* and other MediaWiki contributors
|
|
|
|
* GPLv2 license; info in main package.
|
|
|
|
*
|
|
|
|
* @author Moritz Schubotz
|
|
|
|
*/
|
|
|
|
|
|
|
|
class MathInputCheckRestbase extends MathInputCheck {
|
|
|
|
private $restbaseInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
* (performs no checking)
|
|
|
|
* @param string $tex the TeX input string to be checked
|
2016-01-29 16:50:32 +00:00
|
|
|
* @param string $type
|
2016-01-31 21:11:39 +00:00
|
|
|
* @param MathRestbaseInterface $ref
|
2015-11-28 09:24:54 +00:00
|
|
|
*/
|
2016-01-31 21:11:39 +00:00
|
|
|
public function __construct( $tex = '', $type = 'tex', &$ref = null ) {
|
2015-11-28 09:24:54 +00:00
|
|
|
parent::__construct( $tex );
|
2016-01-31 21:11:39 +00:00
|
|
|
if ( $ref ) {
|
|
|
|
$this->restbaseInterface = $ref;
|
|
|
|
} else {
|
|
|
|
$this->restbaseInterface = new MathRestbaseInterface( $tex, $type );
|
|
|
|
$ref = $this->restbaseInterface;
|
|
|
|
}
|
2015-11-28 09:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://phabricator.wikimedia.org/T119300
|
|
|
|
* @param stdClass $e
|
|
|
|
* @param MathRenderer $errorRenderer
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function errorObjectToHtml( stdClass $e, $errorRenderer = null ) {
|
|
|
|
if ( $errorRenderer === null ) {
|
|
|
|
$errorRenderer = new MathSource( $this->inputTeX );
|
|
|
|
}
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( isset( $e->error->message ) ) {
|
2015-11-28 09:24:54 +00:00
|
|
|
if ( $e->error->message === 'Illegal TeX function' ) {
|
|
|
|
return $errorRenderer->getError( 'math_unknown_function', $e->error->found );
|
2015-12-11 07:23:45 +00:00
|
|
|
} elseif ( preg_match( '/Math extension/', $e->error->message ) ) {
|
|
|
|
$names = MathHooks::getMathNames();
|
|
|
|
$mode = $names['mathml'];
|
|
|
|
try {
|
|
|
|
$host = $this->restbaseInterface->getUrl( '' );
|
|
|
|
}
|
|
|
|
catch ( Exception $ignore ) {
|
|
|
|
$host = 'invalid';
|
|
|
|
}
|
|
|
|
$msg = $e->error->message;
|
|
|
|
return $errorRenderer->getError( 'math_invalidresponse', $mode, $host, $msg );
|
2015-11-28 09:24:54 +00:00
|
|
|
}
|
|
|
|
return $errorRenderer->getError( 'math_syntax_error' );
|
|
|
|
}
|
|
|
|
return $errorRenderer->getError( 'math_unknown_error' );
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isValid() {
|
2016-01-31 21:11:39 +00:00
|
|
|
return $this->restbaseInterface->getSuccess();
|
2015-11-28 09:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some TeX checking programs may return
|
|
|
|
* a modified tex string after having checked it.
|
|
|
|
* You can get the altered tex string with this method
|
|
|
|
* @return string A valid Tex string
|
|
|
|
*/
|
|
|
|
public function getValidTex() {
|
|
|
|
return $this->restbaseInterface->getCheckedTex();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the string of the last error.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getError() {
|
|
|
|
$err = $this->restbaseInterface->getError();
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( $err === null ) {
|
2015-11-28 09:24:54 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->errorObjectToHtml( $err );
|
|
|
|
}
|
|
|
|
|
2016-01-29 16:50:32 +00:00
|
|
|
public function getRbi() {
|
|
|
|
return $this->restbaseInterface;
|
|
|
|
}
|
|
|
|
|
2015-11-28 09:24:54 +00:00
|
|
|
}
|