2012-10-27 14:30:50 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MediaWiki math extension
|
|
|
|
*
|
2015-09-21 16:14:01 +00:00
|
|
|
* (c) 2002-2012 Tomasz Wegrzanowski, Brion Vibber, Moritz Schubotz,
|
|
|
|
* and other MediaWiki contributors
|
2012-10-27 14:30:50 +00:00
|
|
|
* GPLv2 license; info in main package.
|
|
|
|
*
|
|
|
|
* Contains the driver function for the texvc program
|
|
|
|
* @file
|
|
|
|
*/
|
2015-04-14 19:42:48 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
|
|
|
* Takes LaTeX fragments, sends them to a helper program (texvc) for rendering
|
|
|
|
* to rasterized PNG and HTML and MathML approximations. An appropriate
|
|
|
|
* rendering form is picked and returned.
|
|
|
|
*
|
|
|
|
* @author Tomasz Wegrzanowski
|
|
|
|
* @author Brion Vibber
|
|
|
|
* @author Moritz Schubotz
|
2014-05-28 19:31:41 +00:00
|
|
|
* @deprecated will be deleted in one of the next versions without further notice
|
2012-10-27 14:30:50 +00:00
|
|
|
*/
|
|
|
|
class MathTexvc extends MathRenderer {
|
2014-05-28 19:31:41 +00:00
|
|
|
private $hash = '';
|
|
|
|
private $html = '';
|
|
|
|
private $conservativeness = 0;
|
2012-10-27 14:30:50 +00:00
|
|
|
const CONSERVATIVE = 2;
|
|
|
|
const MODERATE = 1;
|
|
|
|
const LIBERAL = 0;
|
2013-06-07 15:37:56 +00:00
|
|
|
const MW_TEXVC_SUCCESS = -1;
|
2012-10-27 14:30:50 +00:00
|
|
|
|
2015-09-21 16:14:01 +00:00
|
|
|
/**
|
2014-05-28 19:31:41 +00:00
|
|
|
* Gets an array that matches the variables of the class to the database columns
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dbOutArray() {
|
|
|
|
$out = array();
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
$outmd5_sql = $dbr->encodeBlob( pack( 'H32', $this->hash ) );
|
|
|
|
if ( $outmd5_sql instanceof Blob ) {
|
|
|
|
$outmd5_sql = $outmd5_sql->fetch();
|
|
|
|
}
|
|
|
|
$out['math_outputhash'] = $outmd5_sql;
|
|
|
|
$out['math_html_conservativeness'] = $this->conservativeness;
|
|
|
|
$out['math_html'] = $this->html;
|
2014-06-05 21:06:43 +00:00
|
|
|
$out['math_mathml'] = utf8_encode( $this->getMathml() );
|
2014-05-28 19:31:41 +00:00
|
|
|
$out['math_inputhash'] = $this->getInputHash();
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->info( 'Store Hashpath of image' .
|
2015-03-16 21:43:20 +00:00
|
|
|
bin2hex( $outmd5_sql ) );
|
2014-05-28 19:31:41 +00:00
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function dbInArray() {
|
|
|
|
return array( 'math_inputhash', 'math_outputhash',
|
|
|
|
'math_html_conservativeness', 'math_html', 'math_mathml' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param database_row $rpage
|
2014-12-09 14:35:33 +00:00
|
|
|
* @return bool
|
2014-05-28 19:31:41 +00:00
|
|
|
*/
|
|
|
|
protected function initializeFromDatabaseRow( $rpage ) {
|
|
|
|
parent::initializeFromDatabaseRow( $rpage );
|
|
|
|
// get deprecated fields
|
|
|
|
if ( $rpage->math_outputhash ) {
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
$xhash = unpack( 'H32md5',
|
|
|
|
$dbr->decodeBlob( $rpage->math_outputhash ) . " " );
|
|
|
|
$this->hash = $xhash['md5'];
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->info( 'Hashpath of PNG-File:' .
|
2015-03-16 21:43:20 +00:00
|
|
|
bin2hex( $this->hash ) );
|
2014-05-28 19:31:41 +00:00
|
|
|
$this->conservativeness = $rpage->math_html_conservativeness;
|
|
|
|
$this->html = $rpage->math_html;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
|
|
|
* Renders TeX using texvc
|
|
|
|
*
|
|
|
|
* @return string rendered TeK
|
|
|
|
*/
|
2013-06-07 15:37:56 +00:00
|
|
|
public function render() {
|
2012-10-27 14:30:50 +00:00
|
|
|
if ( !$this->readCache() ) { // cache miss
|
|
|
|
$result = $this->callTexvc();
|
2014-06-05 21:06:43 +00:00
|
|
|
if ( $result === self::MW_TEXVC_SUCCESS ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$this->lastError = $result;
|
|
|
|
return false;
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-05 21:06:43 +00:00
|
|
|
return true;
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets path to store hashes in
|
|
|
|
*
|
|
|
|
* @return string Storage directory
|
|
|
|
*/
|
2013-06-07 15:37:56 +00:00
|
|
|
public function getHashPath() {
|
2012-10-27 14:30:50 +00:00
|
|
|
$path = $this->getBackend()->getRootStoragePath() .
|
|
|
|
'/math-render/' . $this->getHashSubPath();
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug(
|
2015-03-18 17:28:34 +00:00
|
|
|
"TeX: getHashPath, hash is: {$this->getHash()}, path is: $path" );
|
2012-10-27 14:30:50 +00:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets relative directory for this specific hash
|
|
|
|
*
|
|
|
|
* @return string Relative directory
|
|
|
|
*/
|
2013-06-07 15:37:56 +00:00
|
|
|
public function getHashSubPath() {
|
2013-04-25 17:07:25 +00:00
|
|
|
return substr( $this->getHash(), 0, 1 )
|
|
|
|
. '/' . substr( $this->getHash(), 1, 1 )
|
|
|
|
. '/' . substr( $this->getHash(), 2, 1 );
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets URL for math image
|
|
|
|
*
|
|
|
|
* @return string image URL
|
|
|
|
*/
|
2013-06-07 15:37:56 +00:00
|
|
|
public function getMathImageUrl() {
|
2012-10-27 14:30:50 +00:00
|
|
|
global $wgMathPath;
|
|
|
|
$dir = $this->getHashSubPath();
|
2013-04-25 17:07:25 +00:00
|
|
|
return "$wgMathPath/$dir/{$this->getHash()}.png";
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets img tag for math image
|
|
|
|
*
|
|
|
|
* @return string img HTML
|
|
|
|
*/
|
2013-06-07 15:37:56 +00:00
|
|
|
public function getMathImageHTML() {
|
2012-10-27 14:30:50 +00:00
|
|
|
$url = $this->getMathImageUrl();
|
2014-03-17 06:14:02 +00:00
|
|
|
$attributes = array(
|
|
|
|
// the former class name was 'tex'
|
|
|
|
// for backwards compatibility we keep that classname
|
2014-10-12 09:26:57 +00:00
|
|
|
'class' => 'mwe-math-fallback-image-inline tex',
|
2014-03-17 06:14:02 +00:00
|
|
|
'alt' => $this->getTex()
|
|
|
|
);
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $this->getMathStyle() === 'display' ){
|
2014-03-17 06:14:02 +00:00
|
|
|
// if DisplayStyle is true, the equation will be centered in a new line
|
2014-10-12 09:26:57 +00:00
|
|
|
$attributes[ 'class' ] = 'mwe-math-fallback-image-display tex';
|
2014-03-17 06:14:02 +00:00
|
|
|
}
|
2012-10-27 14:30:50 +00:00
|
|
|
return Xml::element( 'img',
|
|
|
|
$this->getAttributes(
|
|
|
|
'img',
|
2014-03-17 06:14:02 +00:00
|
|
|
$attributes,
|
2012-10-27 14:30:50 +00:00
|
|
|
array(
|
|
|
|
'src' => $url
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-03-17 06:14:02 +00:00
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 07:30:51 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
* @return string
|
2013-05-17 07:30:51 +00:00
|
|
|
*/
|
|
|
|
public function convertTexvcError( $texvcResult ) {
|
2014-01-03 14:29:03 +00:00
|
|
|
$errorConverter = new MathInputCheckTexvc();
|
|
|
|
return $errorConverter->convertTexvcError( $texvcResult, $this );
|
2013-05-17 07:30:51 +00:00
|
|
|
}
|
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
|
|
|
* Does the actual call to texvc
|
|
|
|
*
|
|
|
|
* @return int|string MW_TEXVC_SUCCESS or error string
|
|
|
|
*/
|
2013-06-07 15:37:56 +00:00
|
|
|
public function callTexvc() {
|
2014-02-07 08:32:23 +00:00
|
|
|
global $wgTexvc, $wgTexvcBackgroundColor, $wgHooks;
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $wgTexvc === false ){
|
|
|
|
$texvc = __DIR__ . '/math/texvc';
|
|
|
|
} else {
|
|
|
|
$texvc = $wgTexvc;
|
|
|
|
}
|
2012-10-27 14:30:50 +00:00
|
|
|
$tmpDir = wfTempDir();
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( !is_executable( $texvc ) ) {
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->error(
|
2015-07-22 22:10:46 +00:00
|
|
|
"$texvc does not exist or is not executable." );
|
2012-10-27 14:30:50 +00:00
|
|
|
return $this->getError( 'math_notexvc' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$escapedTmpDir = wfEscapeShellArg( $tmpDir );
|
|
|
|
|
2015-07-22 22:10:46 +00:00
|
|
|
$cmd = $texvc . ' ' .
|
2012-10-27 14:30:50 +00:00
|
|
|
$escapedTmpDir . ' ' .
|
|
|
|
$escapedTmpDir . ' ' .
|
2014-02-07 08:32:23 +00:00
|
|
|
wfEscapeShellArg( $this->getUserInputTex() ) . ' ' .
|
2012-10-27 14:30:50 +00:00
|
|
|
wfEscapeShellArg( 'UTF-8' ) . ' ' .
|
|
|
|
wfEscapeShellArg( $wgTexvcBackgroundColor );
|
|
|
|
|
|
|
|
if ( wfIsWindows() ) {
|
|
|
|
# Invoke it within cygwin sh, because texvc expects sh features in its default shell
|
|
|
|
$cmd = 'sh -c ' . wfEscapeShellArg( $cmd );
|
|
|
|
}
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug( "TeX: $cmd" );
|
|
|
|
LoggerFactory::getInstance( 'Math' )->debug( "Executing '$cmd'." );
|
2013-10-14 22:28:43 +00:00
|
|
|
$retval = null;
|
|
|
|
$contents = wfShellExec( $cmd, $retval );
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug( "TeX output:\n $contents\n---" );
|
2012-10-27 14:30:50 +00:00
|
|
|
|
|
|
|
if ( strlen( $contents ) == 0 ) {
|
|
|
|
if ( !file_exists( $tmpDir ) || !is_writable( $tmpDir ) ) {
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->error(
|
2015-03-16 21:43:20 +00:00
|
|
|
"TeX output directory $tmpDir is missing or not writable" );
|
2012-10-27 14:30:50 +00:00
|
|
|
return $this->getError( 'math_bad_tmpdir' );
|
|
|
|
} else {
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->error(
|
2015-03-16 21:43:20 +00:00
|
|
|
"TeX command '$cmd' returned no output and status code $retval." );
|
2012-10-27 14:30:50 +00:00
|
|
|
return $this->getError( 'math_unknown_error' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 17:07:25 +00:00
|
|
|
$tempFsFile = new TempFSFile( "$tmpDir/{$this->getHash()}.png" );
|
2012-10-27 14:30:50 +00:00
|
|
|
$tempFsFile->autocollect(); // destroy file when $tempFsFile leaves scope
|
|
|
|
|
|
|
|
$retval = substr( $contents, 0, 1 );
|
|
|
|
$errmsg = '';
|
|
|
|
if ( ( $retval == 'C' ) || ( $retval == 'M' ) || ( $retval == 'L' ) ) {
|
|
|
|
if ( $retval == 'C' ) {
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->setConservativeness( self::CONSERVATIVE );
|
2012-10-27 14:30:50 +00:00
|
|
|
} elseif ( $retval == 'M' ) {
|
2015-03-16 19:51:16 +00:00
|
|
|
$this->setConservativeness( self::MODERATE );
|
2012-10-27 14:30:50 +00:00
|
|
|
} else {
|
2015-03-16 19:51:16 +00:00
|
|
|
$this->setConservativeness( self::LIBERAL );
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
$outdata = substr( $contents, 33 );
|
|
|
|
|
|
|
|
$i = strpos( $outdata, "\000" );
|
|
|
|
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->setHtml( substr( $outdata, 0, $i ) );
|
|
|
|
$this->setMathml( substr( $outdata, $i + 1 ) );
|
2012-10-27 14:30:50 +00:00
|
|
|
} elseif ( ( $retval == 'c' ) || ( $retval == 'm' ) || ( $retval == 'l' ) ) {
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->setHtml( substr( $contents, 33 ) );
|
2012-10-27 14:30:50 +00:00
|
|
|
if ( $retval == 'c' ) {
|
2015-03-16 19:51:16 +00:00
|
|
|
$this->setConservativeness( self::CONSERVATIVE );
|
2012-10-27 14:30:50 +00:00
|
|
|
} elseif ( $retval == 'm' ) {
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->setConservativeness( self::MODERATE );
|
2012-10-27 14:30:50 +00:00
|
|
|
} else {
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->setConservativeness( self::LIBERAL );
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->setMathml( null );
|
2012-10-27 14:30:50 +00:00
|
|
|
} elseif ( $retval == 'X' ) {
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->setHtml( null );
|
|
|
|
$this->setMathml( substr( $contents, 33 ) );
|
|
|
|
$this->setConservativeness( self::LIBERAL );
|
2012-10-27 14:30:50 +00:00
|
|
|
} elseif ( $retval == '+' ) {
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->setHtml( null );
|
|
|
|
$this->setMathml( null );
|
|
|
|
$this->setConservativeness( self::LIBERAL );
|
2012-10-27 14:30:50 +00:00
|
|
|
} else {
|
2013-05-17 07:30:51 +00:00
|
|
|
$errmsg = $this->convertTexvcError( $contents );
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$errmsg ) {
|
2014-02-14 23:35:11 +00:00
|
|
|
$newHash = substr( $contents, 1, 32 );
|
|
|
|
if ( $this->hash !== $newHash ) {
|
2014-05-28 19:31:41 +00:00
|
|
|
$this->isInDatabase( false ); // DB needs update in writeCache() (bug 60997)
|
2014-02-14 23:35:11 +00:00
|
|
|
}
|
|
|
|
$this->setHash( $newHash );
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 17:58:42 +00:00
|
|
|
Hooks::run( 'MathAfterTexvc', array( &$this, &$errmsg ) );
|
2012-10-27 14:30:50 +00:00
|
|
|
|
|
|
|
if ( $errmsg ) {
|
|
|
|
return $errmsg;
|
2013-04-25 17:07:25 +00:00
|
|
|
} elseif ( !preg_match( "/^[a-f0-9]{32}$/", $this->getHash() ) ) {
|
2012-10-27 14:30:50 +00:00
|
|
|
return $this->getError( 'math_unknown_error' );
|
2013-04-25 17:07:25 +00:00
|
|
|
} elseif ( !file_exists( "$tmpDir/{$this->getHash()}.png" ) ) {
|
2012-10-27 14:30:50 +00:00
|
|
|
return $this->getError( 'math_image_error' );
|
2013-04-25 17:07:25 +00:00
|
|
|
} elseif ( filesize( "$tmpDir/{$this->getHash()}.png" ) == 0 ) {
|
2012-10-27 14:30:50 +00:00
|
|
|
return $this->getError( 'math_image_error' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$hashpath = $this->getHashPath(); // final storage directory
|
|
|
|
|
|
|
|
$backend = $this->getBackend();
|
|
|
|
# Create any containers/directories as needed...
|
|
|
|
if ( !$backend->prepare( array( 'dir' => $hashpath ) )->isOK() ) {
|
|
|
|
return $this->getError( 'math_output_error' );
|
|
|
|
}
|
|
|
|
// Store the file at the final storage path...
|
2013-12-25 08:28:33 +00:00
|
|
|
// Bug 56769: buffer the writes and do them at the end.
|
|
|
|
if ( !isset( $wgHooks['ParserAfterParse']['FlushMathBackend'] ) ) {
|
|
|
|
$backend->mathBufferedWrites = array();
|
2015-03-16 19:51:16 +00:00
|
|
|
$wgHooks['ParserAfterParse']['FlushMathBackend'] = function () use ( $backend ) {
|
2013-12-25 08:28:33 +00:00
|
|
|
global $wgHooks;
|
|
|
|
unset( $wgHooks['ParserAfterParse']['FlushMathBackend'] );
|
|
|
|
$backend->doQuickOperations( $backend->mathBufferedWrites );
|
|
|
|
unset( $backend->mathBufferedWrites );
|
2015-03-16 19:51:16 +00:00
|
|
|
};
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
2013-12-25 08:28:33 +00:00
|
|
|
$backend->mathBufferedWrites[] = array(
|
|
|
|
'op' => 'store',
|
|
|
|
'src' => "$tmpDir/{$this->getHash()}.png",
|
|
|
|
'dst' => "$hashpath/{$this->getHash()}.png",
|
|
|
|
'ref' => $tempFsFile // keep file alive
|
|
|
|
);
|
|
|
|
|
2013-06-07 15:37:56 +00:00
|
|
|
return self::MW_TEXVC_SUCCESS;
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets file backend
|
|
|
|
*
|
|
|
|
* @return FileBackend appropriate file backend
|
|
|
|
*/
|
2013-06-07 15:37:56 +00:00
|
|
|
public function getBackend() {
|
2012-10-27 14:30:50 +00:00
|
|
|
global $wgMathFileBackend, $wgMathDirectory;
|
|
|
|
if ( $wgMathFileBackend ) {
|
|
|
|
return FileBackendGroup::singleton()->get( $wgMathFileBackend );
|
|
|
|
} else {
|
|
|
|
static $backend = null;
|
|
|
|
if ( !$backend ) {
|
|
|
|
$backend = new FSFileBackend( array(
|
|
|
|
'name' => 'math-backend',
|
2013-12-27 17:55:47 +00:00
|
|
|
'wikiId' => wfWikiId(),
|
2014-02-09 19:02:15 +00:00
|
|
|
'lockManager' => new NullLockManager( array() ),
|
2012-10-27 14:30:50 +00:00
|
|
|
'containerPaths' => array( 'math-render' => $wgMathDirectory ),
|
|
|
|
'fileMode' => 0777
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
return $backend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the HTML rendering
|
|
|
|
*
|
|
|
|
* @return string HTML string
|
|
|
|
*/
|
2014-06-05 21:06:43 +00:00
|
|
|
public function getHtmlOutput() {
|
2015-07-22 22:10:46 +00:00
|
|
|
return $this->getMathImageHTML();
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides base class. Writes to database, and if configured, squid.
|
|
|
|
*/
|
|
|
|
public function writeCache() {
|
|
|
|
global $wgUseSquid;
|
2013-11-18 10:45:43 +00:00
|
|
|
|
2014-05-28 19:31:41 +00:00
|
|
|
$updated = parent::writeCache();
|
2012-10-27 14:30:50 +00:00
|
|
|
// If we're replacing an older version of the image, make sure it's current.
|
2014-05-28 19:31:41 +00:00
|
|
|
if ( $updated && $wgUseSquid ) {
|
2012-10-27 14:30:50 +00:00
|
|
|
$urls = array( $this->getMathImageUrl() );
|
|
|
|
$u = new SquidUpdate( $urls );
|
|
|
|
$u->doUpdate();
|
|
|
|
}
|
2015-02-12 20:14:24 +00:00
|
|
|
|
2014-06-05 21:06:43 +00:00
|
|
|
return $updated;
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the rendering information from the database. If configured, checks whether files exist
|
|
|
|
*
|
|
|
|
* @return boolean true if retrieved, false otherwise
|
|
|
|
*/
|
2013-06-07 15:37:56 +00:00
|
|
|
public function readCache() {
|
2012-10-27 14:30:50 +00:00
|
|
|
global $wgMathCheckFiles;
|
2013-11-18 10:45:43 +00:00
|
|
|
|
2014-09-06 01:55:54 +00:00
|
|
|
if ( $this->isInDatabase() ) {
|
2012-10-27 14:30:50 +00:00
|
|
|
if ( !$wgMathCheckFiles ) {
|
|
|
|
// Short-circuit the file existence & migration checks
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-25 17:07:25 +00:00
|
|
|
$filename = $this->getHashPath() . "/{$this->getHash()}.png"; // final storage path
|
2012-10-27 14:30:50 +00:00
|
|
|
$backend = $this->getBackend();
|
|
|
|
if ( $backend->fileExists( array( 'src' => $filename ) ) ) {
|
|
|
|
if ( $backend->getFileSize( array( 'src' => $filename ) ) == 0 ) {
|
|
|
|
// Some horrible error corrupted stuff :(
|
|
|
|
$backend->quickDelete( array( 'src' => $filename ) );
|
|
|
|
} else {
|
|
|
|
return true; // cache hit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-18 10:45:43 +00:00
|
|
|
return false;
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
2015-04-17 13:26:20 +00:00
|
|
|
|
2014-05-28 19:31:41 +00:00
|
|
|
public function getPng() {
|
2015-05-16 14:02:22 +00:00
|
|
|
global $wgHooks;
|
2015-04-17 13:26:20 +00:00
|
|
|
// Workaround for bugfix for Bug 56769
|
2015-10-22 20:42:19 +00:00
|
|
|
if ( isset( $wgHooks['ParserAfterParse']['FlushMathBackend'] ) ) {
|
|
|
|
// XXX: save any pending files so the read below works
|
|
|
|
call_user_func( $wgHooks['ParserAfterParse']['FlushMathBackend'] );
|
2015-04-17 13:26:20 +00:00
|
|
|
}
|
2014-05-28 19:31:41 +00:00
|
|
|
$backend = $this->getBackend();
|
2015-09-21 16:14:01 +00:00
|
|
|
return $backend->getFileContents(
|
|
|
|
array( 'src' => $this->getHashPath() . "/" . $this->getHash() . '.png' )
|
|
|
|
);
|
2014-05-28 19:31:41 +00:00
|
|
|
}
|
|
|
|
|
2014-09-06 01:55:54 +00:00
|
|
|
public function isInDatabase() {
|
|
|
|
$return = parent::isInDatabase();
|
2014-05-28 19:31:41 +00:00
|
|
|
if ( $this->hash && $return ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the hash calculated by texvc
|
|
|
|
*
|
|
|
|
* @return string hash
|
|
|
|
*/
|
|
|
|
public function getHash() {
|
|
|
|
return $this->hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $hash
|
|
|
|
*/
|
|
|
|
public function setHash( $hash ) {
|
|
|
|
$this->changed = true;
|
|
|
|
$this->hash = $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the html-representation of the mathematical formula.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getHtml() {
|
|
|
|
return $this->html;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $html
|
|
|
|
*/
|
|
|
|
public function setHtml( $html ) {
|
|
|
|
$this->changed = true;
|
|
|
|
$this->html = $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the so called 'conservativeness' calculated by texvc
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getConservativeness() {
|
|
|
|
return $this->conservativeness;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $conservativeness
|
|
|
|
*/
|
|
|
|
public function setConservativeness( $conservativeness ) {
|
|
|
|
$this->changed = true;
|
|
|
|
$this->conservativeness = $conservativeness;
|
|
|
|
}
|
2012-10-27 14:30:50 +00:00
|
|
|
|
2014-05-27 04:46:53 +00:00
|
|
|
protected function getMathTableName() {
|
|
|
|
return 'math';
|
|
|
|
}
|
|
|
|
|
2014-05-28 19:31:41 +00:00
|
|
|
public function setOutputHash( $hash ) {
|
|
|
|
$this->hash = $hash;
|
|
|
|
}
|
2015-12-11 07:23:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip tex check for texvc rendering mode.
|
|
|
|
* Checking the tex code in texvc mode just adds a dependency to the
|
|
|
|
* texvccheck binary which does not improve security since the same
|
|
|
|
* checks are performed by texvc anyhow. Especially given the fact that
|
|
|
|
* texvccheck was derived from texvc.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkTex() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-03 14:29:03 +00:00
|
|
|
}
|