2014-01-03 14:29:03 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
|
|
|
* (c) 2002-2013 Tomasz Wegrzanowski, Brion Vibber, Moritz Schubotz, and other MediaWiki contributors
|
|
|
|
* GPLv2 license; info in main package.
|
|
|
|
*
|
|
|
|
* @author Moritz Schubotz
|
|
|
|
*/
|
|
|
|
class MathInputCheckTexvc extends MathInputCheck {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an error returned by texvc to a localized exception
|
|
|
|
*
|
|
|
|
* @param string $texvcResult error result returned by texvc
|
2014-02-09 19:02:15 +00:00
|
|
|
* @param bool|MathRenderer $errorRenderer
|
|
|
|
* @return string
|
2014-01-03 14:29:03 +00:00
|
|
|
*/
|
|
|
|
public function convertTexvcError( $texvcResult, $errorRenderer = false ) {
|
|
|
|
$texvcStatus = substr( $texvcResult, 0, 1 );
|
|
|
|
$errDetails = htmlspecialchars( substr( $texvcResult, 1 ) );
|
|
|
|
|
|
|
|
if ( $errorRenderer === false ) {
|
|
|
|
$errorRenderer = new MathSource( $this->inputTeX );
|
|
|
|
}
|
|
|
|
|
2014-02-09 19:02:15 +00:00
|
|
|
switch ( $texvcStatus ) {
|
2014-01-03 14:29:03 +00:00
|
|
|
case 'E':
|
|
|
|
$errMsg = $errorRenderer->getError( 'math_lexing_error' );
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
$errMsg = $errorRenderer->getError( 'math_syntax_error' );
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
$errMsg = $errorRenderer->getError( 'math_unknown_function', $errDetails );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$errMsg = $errorRenderer->getError( 'math_unknown_error' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isValid() {
|
2014-02-06 22:53:45 +00:00
|
|
|
$us = $this;
|
|
|
|
$checkWork = new PoolCounterWorkViaCallback( 'MathTexvc-check', "", array(
|
|
|
|
'doWork' => function() use ( $us ) {
|
|
|
|
return $us->doValidCheck();
|
|
|
|
}
|
|
|
|
) );
|
|
|
|
return $checkWork->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function doValidCheck() {
|
2014-01-03 14:29:03 +00:00
|
|
|
global $wgMathTexvcCheckExecutable;
|
|
|
|
if ( !is_executable( $wgMathTexvcCheckExecutable ) ) {
|
2015-02-20 00:22:46 +00:00
|
|
|
$msg = 'Missing "texvccheck" executable. Please see math/README to configure.';
|
2014-01-03 14:29:03 +00:00
|
|
|
trigger_error( $msg, E_USER_NOTICE );
|
|
|
|
wfDebugLog( 'Math', $msg );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cmd = $wgMathTexvcCheckExecutable . ' ' . wfEscapeShellArg( $this->inputTeX );
|
|
|
|
|
|
|
|
if ( wfIsWindows() ) {
|
|
|
|
# Invoke it within cygwin sh, because texvc expects sh features in its default shell
|
2014-02-09 19:02:15 +00:00
|
|
|
$cmd = 'sh -c ' . wfEscapeShellArg( $cmd );
|
2014-01-03 14:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wfDebugLog( 'Math', "TeX check command: $cmd\n" );
|
|
|
|
$contents = wfShellExec( $cmd );
|
|
|
|
wfDebugLog( 'Math', "TeX check result:\n $contents\n---\n" );
|
|
|
|
|
2014-02-09 19:02:15 +00:00
|
|
|
if ( strlen( $contents ) === 0 ) {
|
2014-01-03 14:29:03 +00:00
|
|
|
wfDebugLog( 'Math', "TeX check output was empty. \n" );
|
2014-12-09 04:36:29 +00:00
|
|
|
$this->lastError = $this->convertTexvcError( $contents );
|
2014-01-03 14:29:03 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$retval = substr( $contents, 0, 1 );
|
|
|
|
|
|
|
|
if ( $retval !== '+' ) {
|
|
|
|
$this->lastError = $this->convertTexvcError( $contents );
|
|
|
|
wfDebugLog( 'Math', 'checkTex failed:' . $this->lastError );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$this->validTeX = substr( $contents, 1 );
|
2014-12-09 13:25:23 +00:00
|
|
|
$this->isValid = true;
|
2014-01-03 14:29:03 +00:00
|
|
|
wfDebugLog( 'Math', 'checkTex successful tex is now: ' . $this->validTeX );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|