2011-04-09 00:39:40 +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
|
2011-04-09 00:39:40 +00:00
|
|
|
* GPLv2 license; info in main package.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
2015-04-14 19:42:48 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2011-04-09 00:39:40 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-14 21:49:06 +00:00
|
|
|
* Abstract base class with static methods for rendering the <math> tags using
|
|
|
|
* different technologies. These static methods create a new instance of the
|
2013-04-24 08:21:35 +00:00
|
|
|
* extending classes and render the math tags based on the mode setting of the user.
|
2015-07-12 19:40:34 +00:00
|
|
|
* Furthermore this class handles the caching of the rendered output.
|
2011-04-09 00:39:40 +00:00
|
|
|
*
|
2012-10-27 14:30:50 +00:00
|
|
|
* @author Tomasz Wegrzanowski
|
|
|
|
* @author Brion Vibber
|
|
|
|
* @author Moritz Schubotz
|
2011-04-09 00:39:40 +00:00
|
|
|
*/
|
2012-10-27 14:30:50 +00:00
|
|
|
abstract class MathRenderer {
|
2014-05-28 06:00:30 +00:00
|
|
|
|
|
|
|
// REPRESENTATIONS OF THE MATHEMATICAL CONTENT
|
|
|
|
/** @var string tex representation */
|
2013-05-14 21:49:06 +00:00
|
|
|
protected $tex = '';
|
2014-05-28 06:00:30 +00:00
|
|
|
/** @var string MathML content and presentation */
|
|
|
|
protected $mathml = '';
|
|
|
|
/** @var string SVG layout only (no semantics) */
|
|
|
|
protected $svg = '';
|
|
|
|
/** @var string the original user input string (which was used to calculate the inputhash) */
|
2014-01-03 14:29:03 +00:00
|
|
|
protected $userInputTex = '';
|
2014-05-28 06:00:30 +00:00
|
|
|
// FURTHER PROPERTIES OF THE MATHEMATICAL CONTENT
|
2015-07-22 22:10:46 +00:00
|
|
|
/** @var ('inlineDisplaystyle'|'display'|'inline'|'linebreak') the rendering style */
|
2015-07-22 22:10:46 +00:00
|
|
|
protected $mathStyle = 'inlineDisplaystyle';
|
2014-05-28 06:00:30 +00:00
|
|
|
/** @var array with userdefined parameters passed to the extension (not used) */
|
2016-04-12 20:53:25 +00:00
|
|
|
protected $params = [];
|
2014-05-28 06:00:30 +00:00
|
|
|
/** @var string a userdefined identifier to link to the equation. */
|
|
|
|
protected $id = '';
|
|
|
|
|
|
|
|
// STATE OF THE CLASS INSTANCE
|
|
|
|
/** @var boolean has variable tex been security-checked */
|
|
|
|
protected $texSecure = false;
|
|
|
|
/** @var boolean has the mathematical content changed */
|
|
|
|
protected $changed = false;
|
|
|
|
/** @var boolean is there a database entry for the mathematical content */
|
|
|
|
protected $storedInDatabase = null;
|
|
|
|
/** @var boolean is there a request to purge the existing mathematical content */
|
|
|
|
protected $purge = false;
|
|
|
|
/** @var string with last occurred error */
|
|
|
|
protected $lastError = '';
|
|
|
|
/** @var string md5 value from userInputTex */
|
|
|
|
protected $md5 = '';
|
2015-07-22 22:10:46 +00:00
|
|
|
/** @var string binary packed inputhash */
|
2014-05-28 06:00:30 +00:00
|
|
|
protected $inputHash = '';
|
2015-07-22 22:10:46 +00:00
|
|
|
/** @var string rendering mode */
|
|
|
|
protected $mode = 'png';
|
2016-01-29 16:50:32 +00:00
|
|
|
/** @var string input type */
|
|
|
|
protected $inputType = 'tex';
|
|
|
|
/** @var MathRestbaseInterface used for checking */
|
|
|
|
protected $rbi;
|
2014-05-28 06:00:30 +00:00
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
|
|
|
* Constructs a base MathRenderer
|
|
|
|
*
|
2013-04-24 08:21:35 +00:00
|
|
|
* @param string $tex (optional) LaTeX markup
|
|
|
|
* @param array $params (optional) HTML attributes
|
2012-10-27 14:30:50 +00:00
|
|
|
*/
|
2016-04-12 20:53:25 +00:00
|
|
|
public function __construct( $tex = '', $params = [] ) {
|
2011-04-09 00:39:40 +00:00
|
|
|
$this->params = $params;
|
2014-07-22 22:42:48 +00:00
|
|
|
if ( isset( $params['id'] ) ) {
|
|
|
|
$this->id = $params['id'];
|
|
|
|
}
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( isset( $params['display'] ) ) {
|
|
|
|
$layoutMode = $params['display'];
|
|
|
|
if ( $layoutMode == 'block' ) {
|
2015-08-05 09:53:10 +00:00
|
|
|
$this->mathStyle = 'display';
|
2015-07-22 22:10:46 +00:00
|
|
|
$tex = '{\displaystyle ' . $tex . '}';
|
2016-01-29 16:50:32 +00:00
|
|
|
$this->inputType = 'tex';
|
2015-07-22 22:10:46 +00:00
|
|
|
} elseif ( $layoutMode == 'inline' ) {
|
2015-08-05 09:53:10 +00:00
|
|
|
$this->mathStyle = 'inline';
|
2016-01-29 16:50:32 +00:00
|
|
|
$this->inputType = 'inline-tex';
|
2015-07-22 22:10:46 +00:00
|
|
|
$tex = '{\textstyle ' . $tex . '}';
|
|
|
|
} elseif ( $layoutMode == 'linebreak' ) {
|
2015-08-05 09:53:10 +00:00
|
|
|
$this->mathStyle = 'linebreak';
|
2015-07-22 22:10:46 +00:00
|
|
|
$tex = '\[ ' . $tex . ' \]';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Implement caching for attributes of the math tag
|
|
|
|
// Currently the key for the database entry relating to an equation
|
|
|
|
// is md5($tex) the new option to determine if the tex input
|
|
|
|
// is rendered in displaystyle or textstyle would require a database
|
|
|
|
// layout change to use a composite key e.g. (md5($tex),$mathStyle).
|
|
|
|
// As a workaround we use the prefix \displaystyle so that the key becomes
|
|
|
|
// md5((\{\\displaystyle|\{\\textstyle)?\s?$tex\}?)
|
|
|
|
// The new value of $tex string describes now how the rendering should look like.
|
|
|
|
// The variable MathRenderer::mathStyle determines if the rendered equation should
|
|
|
|
// be centered in a new line, or just in be displayed in the current line.
|
|
|
|
$this->userInputTex = $tex;
|
|
|
|
$this->tex = $tex;
|
2012-04-06 15:19:12 +00:00
|
|
|
}
|
2011-04-09 00:39:40 +00:00
|
|
|
|
2012-08-20 22:11:16 +00:00
|
|
|
/**
|
2012-10-27 14:30:50 +00:00
|
|
|
* Static method for rendering math tag
|
|
|
|
*
|
|
|
|
* @param string $tex LaTeX markup
|
|
|
|
* @param array $params HTML attributes
|
2015-07-22 22:10:46 +00:00
|
|
|
* @param string $mode constant indicating rendering mode
|
2012-10-27 14:30:50 +00:00
|
|
|
* @return string HTML for math tag
|
2012-08-20 22:11:16 +00:00
|
|
|
*/
|
2016-04-12 20:53:25 +00:00
|
|
|
public static function renderMath( $tex, $params = [], $mode = 'png' ) {
|
2013-02-16 12:38:02 +00:00
|
|
|
$renderer = self::getRenderer( $tex, $params, $mode );
|
2014-06-05 21:06:43 +00:00
|
|
|
if ( $renderer->render() ) {
|
|
|
|
return $renderer->getHtmlOutput();
|
|
|
|
} else {
|
|
|
|
return $renderer->getLastError();
|
|
|
|
}
|
2012-08-20 22:11:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 11:14:56 +00:00
|
|
|
/**
|
|
|
|
*
|
2014-12-09 14:30:55 +00:00
|
|
|
* @param string $md5
|
2014-06-10 11:14:56 +00:00
|
|
|
* @return MathRenderer the MathRenderer generated from md5
|
|
|
|
*/
|
|
|
|
public static function newFromMd5( $md5 ) {
|
|
|
|
$class = get_called_class();
|
2014-12-09 13:59:09 +00:00
|
|
|
/** @var MathRenderer $instance */
|
2014-06-10 11:14:56 +00:00
|
|
|
$instance = new $class;
|
|
|
|
$instance->setMd5( $md5 );
|
|
|
|
$instance->readFromDatabase();
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
|
|
|
* Static factory method for getting a renderer based on mode
|
|
|
|
*
|
|
|
|
* @param string $tex LaTeX markup
|
|
|
|
* @param array $params HTML attributes
|
2015-07-22 22:10:46 +00:00
|
|
|
* @param string $mode indicating rendering mode
|
2012-10-27 14:30:50 +00:00
|
|
|
* @return MathRenderer appropriate renderer for mode
|
|
|
|
*/
|
2016-04-12 20:53:25 +00:00
|
|
|
public static function getRenderer( $tex, $params = [], $mode = 'png' ) {
|
2015-07-22 22:10:46 +00:00
|
|
|
global $wgDefaultUserOptions, $wgMathEnableExperimentalInputFormats;
|
2014-07-22 22:42:48 +00:00
|
|
|
|
2014-06-10 11:18:26 +00:00
|
|
|
if ( isset( $params['forcemathmode'] ) ) {
|
|
|
|
$mode = $params['forcemathmode'];
|
|
|
|
}
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( !in_array( $mode, self::getValidModes() ) ) {
|
2012-10-27 14:30:50 +00:00
|
|
|
$mode = $wgDefaultUserOptions['math'];
|
2014-04-08 13:57:15 +00:00
|
|
|
}
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( $wgMathEnableExperimentalInputFormats === true && $mode == 'mathml' &&
|
2015-03-16 19:51:16 +00:00
|
|
|
isset( $params['type'] ) ) {
|
2014-06-10 11:18:26 +00:00
|
|
|
// Support of MathML input (experimental)
|
2015-07-22 22:10:46 +00:00
|
|
|
// Currently support for mode 'mathml' only
|
2016-04-12 20:53:25 +00:00
|
|
|
if ( !in_array( $params['type'], [ 'pmml', 'ascii' ] ) ) {
|
2014-06-10 11:18:26 +00:00
|
|
|
unset( $params['type'] );
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 13:05:54 +00:00
|
|
|
if ( isset( $params['chem'] ) ) {
|
|
|
|
$mode = 'mathml';
|
|
|
|
$params['type'] = 'chem';
|
|
|
|
}
|
2012-10-27 14:30:50 +00:00
|
|
|
switch ( $mode ) {
|
2015-07-22 22:10:46 +00:00
|
|
|
case 'source':
|
2012-10-27 14:30:50 +00:00
|
|
|
$renderer = new MathSource( $tex, $params );
|
|
|
|
break;
|
2015-07-22 22:10:46 +00:00
|
|
|
case 'png':
|
2014-06-10 16:49:20 +00:00
|
|
|
$renderer = new MathTexvc( $tex, $params );
|
|
|
|
break;
|
2015-07-22 22:10:46 +00:00
|
|
|
case 'latexml':
|
2013-05-14 21:49:06 +00:00
|
|
|
$renderer = new MathLaTeXML( $tex, $params );
|
|
|
|
break;
|
2015-07-22 22:10:46 +00:00
|
|
|
case 'mathml':
|
2012-10-27 14:30:50 +00:00
|
|
|
default:
|
2014-06-10 16:49:20 +00:00
|
|
|
$renderer = new MathMathML( $tex, $params );
|
2011-11-28 22:30:33 +00:00
|
|
|
}
|
2015-12-14 23:14:15 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug( 'Start rendering $' . $renderer->tex .
|
2015-03-16 21:43:20 +00:00
|
|
|
'$ in mode ' . $mode );
|
2012-10-27 14:30:50 +00:00
|
|
|
return $renderer;
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
2014-06-05 21:06:43 +00:00
|
|
|
* Performs the rendering
|
2012-10-27 14:30:50 +00:00
|
|
|
*
|
2014-06-05 21:06:43 +00:00
|
|
|
* @return boolean if rendering was successful.
|
2012-10-27 14:30:50 +00:00
|
|
|
*/
|
|
|
|
abstract public function render();
|
2011-04-09 00:39:40 +00:00
|
|
|
|
2014-06-05 21:06:43 +00:00
|
|
|
/**
|
|
|
|
* @return string Html output that is embedded in the page
|
|
|
|
*/
|
|
|
|
abstract public function getHtmlOutput();
|
2013-04-24 08:21:35 +00:00
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
2013-04-24 08:21:35 +00:00
|
|
|
* texvc error messages
|
|
|
|
* TODO: update to MathML
|
2012-10-27 14:30:50 +00:00
|
|
|
* Returns an internationalized HTML error string
|
|
|
|
*
|
|
|
|
* @param string $msg message key for specific error
|
2015-09-21 16:14:01 +00:00
|
|
|
* @internal param \Varargs $parameters (optional) zero
|
|
|
|
* or more message parameters for specific error
|
|
|
|
*
|
2012-10-27 14:30:50 +00:00
|
|
|
* @return string HTML error string
|
|
|
|
*/
|
2014-01-03 14:29:03 +00:00
|
|
|
public function getError( $msg /*, ... */ ) {
|
2012-09-07 17:43:09 +00:00
|
|
|
$mf = wfMessage( 'math_failure' )->inContentLanguage()->escaped();
|
2013-05-17 07:30:51 +00:00
|
|
|
$parameters = func_get_args();
|
|
|
|
array_shift( $parameters );
|
|
|
|
$errmsg = wfMessage( $msg, $parameters )->inContentLanguage()->escaped();
|
2011-04-09 00:39:40 +00:00
|
|
|
$source = htmlspecialchars( str_replace( "\n", ' ', $this->tex ) );
|
2014-02-19 18:04:07 +00:00
|
|
|
return "<strong class='error texerror'>$mf ($errmsg): $source</strong>\n";
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 06:00:30 +00:00
|
|
|
/**
|
|
|
|
* Return hash of input
|
|
|
|
*
|
|
|
|
* @return string hash
|
|
|
|
*/
|
|
|
|
public function getMd5() {
|
|
|
|
if ( ! $this->md5 ) {
|
|
|
|
$this->md5 = md5( $this->userInputTex );
|
|
|
|
}
|
|
|
|
return $this->md5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the input hash (if user input tex is not available)
|
|
|
|
* @param $md5
|
|
|
|
* @return string hash
|
|
|
|
*/
|
|
|
|
public function setMd5( $md5 ) {
|
|
|
|
$this->md5 = $md5;
|
|
|
|
}
|
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
|
|
|
* Return hash of input
|
|
|
|
*
|
|
|
|
* @return string hash
|
|
|
|
*/
|
|
|
|
public function getInputHash() {
|
|
|
|
// TODO: What happens if $tex is empty?
|
2014-05-28 06:00:30 +00:00
|
|
|
if ( !$this->inputHash ) {
|
2014-05-28 19:31:41 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2014-05-28 06:00:30 +00:00
|
|
|
return $dbr->encodeBlob( pack( "H32", $this->getMd5() ) ); # Binary packed, not hex
|
2014-05-28 19:31:41 +00:00
|
|
|
}
|
2014-05-28 06:00:30 +00:00
|
|
|
return $this->inputHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode binary packed hash from the database to md5 of input_tex
|
2015-07-22 22:10:46 +00:00
|
|
|
* @param string $hash (binary)
|
2014-05-28 06:00:30 +00:00
|
|
|
* @return string md5
|
|
|
|
*/
|
|
|
|
private static function dbHash2md5( $hash ) {
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
$xhash = unpack( 'H32md5', $dbr->decodeBlob( $hash ) . " " );
|
|
|
|
return $xhash['md5'];
|
2012-10-27 14:30:50 +00:00
|
|
|
}
|
2011-04-09 00:39:40 +00:00
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
|
|
|
* Reads rendering data from database
|
|
|
|
*
|
|
|
|
* @return boolean true if read successfully, false otherwise
|
|
|
|
*/
|
2013-04-24 06:01:53 +00:00
|
|
|
public function readFromDatabase() {
|
2011-04-09 00:39:40 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2014-05-28 19:31:41 +00:00
|
|
|
$rpage = $dbr->selectRow( $this->getMathTableName(),
|
|
|
|
$this->dbInArray(),
|
2016-04-12 20:53:25 +00:00
|
|
|
[ 'math_inputhash' => $this->getInputHash() ],
|
2014-05-28 19:31:41 +00:00
|
|
|
__METHOD__ );
|
2012-10-27 14:30:50 +00:00
|
|
|
if ( $rpage !== false ) {
|
2013-09-19 00:37:35 +00:00
|
|
|
$this->initializeFromDatabaseRow( $rpage );
|
2014-05-28 19:31:41 +00:00
|
|
|
$this->storedInDatabase = true;
|
2013-05-14 21:49:06 +00:00
|
|
|
return true;
|
2014-05-28 19:31:41 +00:00
|
|
|
} else {
|
2014-05-27 04:46:53 +00:00
|
|
|
# Missing from the database and/or the render cache
|
2014-05-28 19:31:41 +00:00
|
|
|
$this->storedInDatabase = false;
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|
2015-07-22 22:10:46 +00:00
|
|
|
|
2013-09-19 00:37:35 +00:00
|
|
|
/**
|
2014-05-28 19:31:41 +00:00
|
|
|
* @return array with the database column names
|
2013-09-19 00:37:35 +00:00
|
|
|
*/
|
2014-05-28 19:31:41 +00:00
|
|
|
protected function dbInArray() {
|
2016-04-12 20:53:25 +00:00
|
|
|
$in = [ 'math_inputhash',
|
2014-05-28 19:31:41 +00:00
|
|
|
'math_mathml',
|
|
|
|
'math_inputtex',
|
|
|
|
'math_tex',
|
|
|
|
'math_svg'
|
2016-04-12 20:53:25 +00:00
|
|
|
];
|
2014-05-28 19:31:41 +00:00
|
|
|
return $in;
|
2013-09-19 00:37:35 +00:00
|
|
|
}
|
2011-04-09 00:39:40 +00:00
|
|
|
|
2013-09-19 00:37:35 +00:00
|
|
|
/**
|
2014-05-28 19:31:41 +00:00
|
|
|
* Reads the values from the database but does not overwrite set values with empty values
|
2015-07-22 22:10:46 +00:00
|
|
|
* @param stdClass $rpage (a database row)
|
2013-09-19 00:37:35 +00:00
|
|
|
*/
|
2014-05-28 19:31:41 +00:00
|
|
|
protected function initializeFromDatabaseRow( $rpage ) {
|
|
|
|
$this->inputHash = $rpage->math_inputhash; // MUST NOT BE NULL
|
|
|
|
$this->md5 = self::dbHash2md5( $this->inputHash );
|
|
|
|
if ( ! empty( $rpage->math_mathml ) ) {
|
|
|
|
$this->mathml = utf8_decode( $rpage->math_mathml );
|
|
|
|
}
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( ! empty( $rpage->math_inputtex ) ) {
|
|
|
|
// in the current database the field is probably not set.
|
2014-05-28 19:31:41 +00:00
|
|
|
$this->userInputTex = $rpage->math_inputtex;
|
|
|
|
}
|
|
|
|
if ( ! empty( $rpage->math_tex ) ) {
|
|
|
|
$this->tex = $rpage->math_tex;
|
|
|
|
}
|
|
|
|
if ( ! empty( $rpage->math_svg ) ) {
|
|
|
|
$this->svg = $rpage->math_svg;
|
|
|
|
}
|
|
|
|
$this->changed = false;
|
2013-09-19 00:37:35 +00:00
|
|
|
}
|
2014-05-28 19:31:41 +00:00
|
|
|
|
2011-04-09 00:39:40 +00:00
|
|
|
/**
|
2013-04-24 08:21:35 +00:00
|
|
|
* Writes rendering entry to database.
|
|
|
|
*
|
|
|
|
* WARNING: Use writeCache() instead of this method to be sure that all
|
|
|
|
* renderer specific (such as squid caching) are taken into account.
|
2015-09-21 16:14:01 +00:00
|
|
|
* This function stores the values that are currently present in the class
|
|
|
|
* to the database even if they are empty.
|
2013-04-24 08:21:35 +00:00
|
|
|
*
|
|
|
|
* This function can be seen as protected function.
|
2014-05-28 06:00:30 +00:00
|
|
|
* @param DatabaseBase $dbw
|
2011-04-09 00:39:40 +00:00
|
|
|
*/
|
2013-09-19 00:37:35 +00:00
|
|
|
public function writeToDatabase( $dbw = null ) {
|
2012-10-27 14:30:50 +00:00
|
|
|
# Now save it back to the DB:
|
|
|
|
if ( !wfReadOnly() ) {
|
2015-12-14 23:14:15 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug( 'Store entry for $' . $this->tex .
|
2015-03-18 17:28:34 +00:00
|
|
|
'$ in database (hash:' . $this->getMd5() . ')' );
|
2013-09-19 00:37:35 +00:00
|
|
|
$outArray = $this->dbOutArray();
|
2014-05-28 19:31:41 +00:00
|
|
|
$mathTableName = $this->getMathTableName();
|
|
|
|
if ( $this->isInDatabase() ) {
|
|
|
|
$inputHash = $this->getInputHash();
|
2016-06-03 04:58:44 +00:00
|
|
|
DeferredUpdates::addCallableUpdate( function () use (
|
|
|
|
$dbw, $outArray, $inputHash, $mathTableName
|
2015-03-16 19:51:16 +00:00
|
|
|
) {
|
2016-06-03 04:58:44 +00:00
|
|
|
$dbw = $dbw ?: wfGetDB( DB_MASTER );
|
|
|
|
|
2015-03-16 19:51:16 +00:00
|
|
|
$dbw->update( $mathTableName, $outArray,
|
2016-06-03 04:58:44 +00:00
|
|
|
[ 'math_inputhash' => $inputHash ], __METHOD__ );
|
2015-04-14 19:42:48 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug(
|
2015-03-18 17:28:34 +00:00
|
|
|
'Row updated after db transaction was idle: ' .
|
|
|
|
var_export( $outArray, true ) . " to database" );
|
2015-03-16 19:51:16 +00:00
|
|
|
} );
|
2014-05-28 19:31:41 +00:00
|
|
|
} else {
|
2016-06-03 04:58:44 +00:00
|
|
|
DeferredUpdates::addCallableUpdate( function () use (
|
|
|
|
$dbw, $outArray, $mathTableName
|
2015-03-16 19:51:16 +00:00
|
|
|
) {
|
2016-06-03 04:58:44 +00:00
|
|
|
$dbw = $dbw ?: wfGetDB( DB_MASTER );
|
|
|
|
|
|
|
|
$dbw->insert( $mathTableName, $outArray, __METHOD__, [ 'IGNORE' ] );
|
2015-07-12 19:40:34 +00:00
|
|
|
LoggerFactory::getInstance( 'Math' )->debug(
|
|
|
|
'Row inserted after db transaction was idle ' .
|
|
|
|
var_export( $outArray, true ) . " to database" );
|
|
|
|
if ( $dbw->affectedRows() == 0 ) {
|
|
|
|
// That's the price for the delayed update.
|
|
|
|
LoggerFactory::getInstance( 'Math' )->warning(
|
|
|
|
'Entry could not be written. Might be changed in between.' );
|
2015-03-16 19:51:16 +00:00
|
|
|
}
|
|
|
|
} );
|
2014-05-28 19:31:41 +00:00
|
|
|
}
|
2013-09-19 00:37:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an array that matches the variables of the class to the database columns
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-05-28 19:31:41 +00:00
|
|
|
protected function dbOutArray() {
|
2016-04-12 20:53:25 +00:00
|
|
|
$out = [
|
|
|
|
'math_inputhash' => $this->getInputHash(),
|
2014-05-28 19:31:41 +00:00
|
|
|
'math_mathml' => utf8_encode( $this->mathml ),
|
|
|
|
'math_inputtex' => $this->userInputTex,
|
|
|
|
'math_tex' => $this->tex,
|
|
|
|
'math_svg' => $this->svg
|
2016-04-12 20:53:25 +00:00
|
|
|
];
|
2013-09-19 00:37:35 +00:00
|
|
|
return $out;
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 21:11:39 +00:00
|
|
|
/**
|
|
|
|
* @param MathRestbaseInterface $param
|
|
|
|
*/
|
|
|
|
public function setRestbaseInterface( $param ) {
|
|
|
|
$this->rbi = $param;
|
2016-06-02 14:30:48 +00:00
|
|
|
$this->rbi->setPurge( $this->isPurge() );
|
2016-01-31 21:11:39 +00:00
|
|
|
}
|
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
|
|
|
* Returns sanitized attributes
|
|
|
|
*
|
|
|
|
* @param string $tag element name
|
|
|
|
* @param array $defaults default attributes
|
|
|
|
* @param array $overrides attributes to override defaults
|
|
|
|
* @return array HTML attributes
|
|
|
|
*/
|
2016-04-12 20:53:25 +00:00
|
|
|
protected function getAttributes( $tag, $defaults = [], $overrides = [] ) {
|
2011-04-09 00:39:40 +00:00
|
|
|
$attribs = Sanitizer::validateTagAttributes( $this->params, $tag );
|
|
|
|
$attribs = Sanitizer::mergeAttributes( $defaults, $attribs );
|
|
|
|
$attribs = Sanitizer::mergeAttributes( $attribs, $overrides );
|
|
|
|
return $attribs;
|
|
|
|
}
|
2013-05-14 21:49:06 +00:00
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
2013-09-19 00:37:35 +00:00
|
|
|
* Writes cache. Writes the database entry if values were changed
|
2012-10-27 14:30:50 +00:00
|
|
|
*/
|
|
|
|
public function writeCache() {
|
2015-04-14 19:42:48 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'Math' );
|
2015-03-18 17:28:34 +00:00
|
|
|
$logger->debug( 'Writing of cache requested.' );
|
2013-09-19 00:37:35 +00:00
|
|
|
if ( $this->isChanged() ) {
|
2015-03-18 17:28:34 +00:00
|
|
|
$logger->debug( 'Change detected. Perform writing.' );
|
2013-09-19 00:37:35 +00:00
|
|
|
$this->writeToDatabase();
|
2014-05-28 19:31:41 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2015-03-18 17:28:34 +00:00
|
|
|
$logger->debug( "Nothing was changed. Don't write to database." );
|
2014-05-28 19:31:41 +00:00
|
|
|
return false;
|
2013-09-19 00:37:35 +00:00
|
|
|
}
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|
|
|
|
|
2012-10-27 14:30:50 +00:00
|
|
|
/**
|
|
|
|
* Gets TeX markup
|
|
|
|
*
|
|
|
|
* @return string TeX markup
|
|
|
|
*/
|
|
|
|
public function getTex() {
|
|
|
|
return $this->tex;
|
2011-04-09 00:39:40 +00:00
|
|
|
}
|
2013-04-25 17:07:25 +00:00
|
|
|
|
|
|
|
/**
|
2015-07-22 22:10:46 +00:00
|
|
|
* Gets the rendering mode
|
2013-04-25 17:07:25 +00:00
|
|
|
*
|
2015-07-22 22:10:46 +00:00
|
|
|
* @return string
|
2013-04-25 17:07:25 +00:00
|
|
|
*/
|
|
|
|
public function getMode() {
|
|
|
|
return $this->mode;
|
|
|
|
}
|
|
|
|
|
2014-05-28 06:00:30 +00:00
|
|
|
/**
|
|
|
|
* Sets the rendering mode
|
2015-07-22 22:10:46 +00:00
|
|
|
* @param string $newMode element of the array $wgMathValidModes
|
2014-05-28 06:00:30 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setMode( $newMode ) {
|
2015-07-22 22:10:46 +00:00
|
|
|
if ( in_array( $newMode, self::getValidModes() ) ) {
|
2014-05-28 06:00:30 +00:00
|
|
|
$this->mode = $newMode;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 19:31:41 +00:00
|
|
|
|
2013-04-25 17:07:25 +00:00
|
|
|
/**
|
|
|
|
* Sets the TeX code
|
|
|
|
*
|
|
|
|
* @param string $tex
|
|
|
|
*/
|
|
|
|
public function setTex( $tex ) {
|
2014-05-28 19:31:41 +00:00
|
|
|
if ( $this->tex != $tex ) {
|
|
|
|
$this->changed = true;
|
|
|
|
$this->tex = $tex;
|
|
|
|
}
|
2013-04-25 17:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the MathML XML element
|
|
|
|
* @return string in UTF-8 encoding
|
|
|
|
*/
|
|
|
|
public function getMathml() {
|
2015-02-20 00:22:46 +00:00
|
|
|
if ( !StringUtils::isUtf8( $this->mathml ) ) {
|
2014-05-28 19:31:41 +00:00
|
|
|
$this->setMathml( '' );
|
|
|
|
}
|
2013-04-25 17:07:25 +00:00
|
|
|
return $this->mathml;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $mathml use UTF-8 encoding
|
|
|
|
*/
|
|
|
|
public function setMathml( $mathml ) {
|
|
|
|
$this->changed = true;
|
|
|
|
$this->mathml = $mathml;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the attributes of the math tag
|
2013-05-14 21:49:06 +00:00
|
|
|
*
|
2017-07-10 09:40:12 +00:00
|
|
|
* @return array
|
2013-04-25 17:07:25 +00:00
|
|
|
*/
|
|
|
|
public function getParams() {
|
|
|
|
return $this->params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-10 09:40:12 +00:00
|
|
|
* @param array $params
|
2013-04-25 17:07:25 +00:00
|
|
|
*/
|
|
|
|
public function setParams( $params ) {
|
2013-05-14 21:49:06 +00:00
|
|
|
// $changed is not set to true here, because the attributes do not affect
|
|
|
|
// the rendering in the current implementation.
|
|
|
|
// If this behavior will change in the future $this->tex is no longer a
|
|
|
|
// primary key and the input hash cannot be calculate form $this->tex
|
|
|
|
// only. See the discussion 'Tag extensions in Block mode' on wikitech-l.
|
2013-04-25 17:07:25 +00:00
|
|
|
$this->params = $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the instance was modified i.e., because math was rendered
|
|
|
|
*
|
|
|
|
* @return boolean true if something was changed false otherwise
|
|
|
|
*/
|
|
|
|
public function isChanged() {
|
|
|
|
return $this->changed;
|
|
|
|
}
|
|
|
|
|
2013-05-14 21:49:06 +00:00
|
|
|
/**
|
|
|
|
* Checks if there is an explicit user request to rerender the math-tag.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-09-21 16:14:01 +00:00
|
|
|
function isPurge() {
|
2013-05-14 21:49:06 +00:00
|
|
|
if ( $this->purge ) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-15 17:41:45 +00:00
|
|
|
$refererHeader = RequestContext::getMain()->getRequest()->getHeader( 'REFERER' );
|
|
|
|
if ( $refererHeader ) {
|
|
|
|
parse_str( parse_url( $refererHeader, PHP_URL_QUERY ), $refererParam );
|
|
|
|
if ( isset( $refererParam['action'] ) && $refererParam['action'] === 'purge' ) {
|
|
|
|
LoggerFactory::getInstance( 'Math' )->debug( 'Re-Rendering on user request' );
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-28 19:31:41 +00:00
|
|
|
}
|
2017-03-15 17:41:45 +00:00
|
|
|
return false;
|
2013-05-14 21:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets purge. If set to true the render is forced to rerender and must not
|
|
|
|
* use a cached version.
|
2014-02-09 19:02:15 +00:00
|
|
|
* @param bool $purge
|
2013-05-14 21:49:06 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function setPurge( $purge = true ) {
|
|
|
|
$this->changed = true;
|
|
|
|
$this->purge = $purge;
|
|
|
|
}
|
|
|
|
|
2013-09-19 00:37:35 +00:00
|
|
|
function getLastError() {
|
2013-05-14 21:49:06 +00:00
|
|
|
return $this->lastError;
|
|
|
|
}
|
2014-01-03 14:29:03 +00:00
|
|
|
|
2014-03-17 06:14:02 +00:00
|
|
|
/**
|
|
|
|
*
|
2015-07-22 22:10:46 +00:00
|
|
|
* @param string $mathStyle ('inlineDisplaystyle'|'display'|'inline')
|
2014-03-17 06:14:02 +00:00
|
|
|
*/
|
2015-07-22 22:10:46 +00:00
|
|
|
public function setMathStyle( $mathStyle = 'display' ) {
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( $this->mathStyle !== $mathStyle ) {
|
2014-03-17 06:14:02 +00:00
|
|
|
$this->changed = true;
|
|
|
|
}
|
2015-07-22 22:10:46 +00:00
|
|
|
$this->mathStyle = $mathStyle;
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( $mathStyle == 'inline' ) {
|
2016-01-29 16:50:32 +00:00
|
|
|
$this->inputType = 'inline-tex';
|
|
|
|
} else {
|
|
|
|
$this->inputType = 'tex';
|
|
|
|
}
|
2014-03-17 06:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value of the DisplayStyle attribute
|
2015-07-22 22:10:46 +00:00
|
|
|
* @return string ('inlineDisplaystyle'|'display'|'inline'|'linebreak') the DisplayStyle
|
2014-03-17 06:14:02 +00:00
|
|
|
*/
|
|
|
|
public function getMathStyle() {
|
|
|
|
return $this->mathStyle;
|
|
|
|
}
|
2014-05-28 06:00:30 +00:00
|
|
|
|
2014-01-03 14:29:03 +00:00
|
|
|
/**
|
|
|
|
* Get if the input tex was marked as secure
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isTexSecure() {
|
|
|
|
return $this->texSecure;
|
|
|
|
}
|
|
|
|
|
2014-09-06 01:59:13 +00:00
|
|
|
/**
|
|
|
|
* @global $wgMathDisableTexFilter
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-12-14 22:58:08 +00:00
|
|
|
public function checkTeX() {
|
2015-12-03 07:22:34 +00:00
|
|
|
if ( $this->texSecure || self::getDisableTexFilter() == 'always' ) {
|
2014-09-06 01:59:13 +00:00
|
|
|
// equation was already checked or checking is disabled
|
|
|
|
return true;
|
|
|
|
} else {
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( self::getDisableTexFilter() == 'new' && $this->mode != 'source' ) {
|
2015-09-21 16:14:01 +00:00
|
|
|
if ( $this->readFromDatabase() ) {
|
2014-09-06 01:59:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-04-08 19:27:43 +00:00
|
|
|
return $this->doCheck();
|
2014-01-03 14:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-07 08:32:23 +00:00
|
|
|
|
2014-05-28 19:31:41 +00:00
|
|
|
public function isInDatabase() {
|
|
|
|
if ( $this->storedInDatabase === null ) {
|
|
|
|
$this->readFromDatabase();
|
|
|
|
}
|
|
|
|
return $this->storedInDatabase;
|
|
|
|
}
|
|
|
|
|
2014-02-07 08:32:23 +00:00
|
|
|
/**
|
2014-05-28 06:00:30 +00:00
|
|
|
*
|
|
|
|
* @return string TeX the original tex string specified by the user
|
2014-02-07 08:32:23 +00:00
|
|
|
*/
|
2014-02-09 19:02:15 +00:00
|
|
|
public function getUserInputTex() {
|
2014-02-07 08:32:23 +00:00
|
|
|
return $this->userInputTex;
|
|
|
|
}
|
2013-04-25 17:07:25 +00:00
|
|
|
|
2014-05-28 06:00:30 +00:00
|
|
|
/**
|
2015-07-22 22:10:46 +00:00
|
|
|
* @return string user defined ID
|
2014-05-28 06:00:30 +00:00
|
|
|
*/
|
|
|
|
public function getID() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-09 14:30:55 +00:00
|
|
|
* @param string user defined ID
|
2014-05-28 06:00:30 +00:00
|
|
|
*/
|
|
|
|
public function setID( $id ) {
|
|
|
|
// Changes in the ID affect the container for the math element on the current page
|
|
|
|
// only. Therefore an id change does not affect the $this->changed variable, which
|
|
|
|
// indicates if database relevant fields have been changed.
|
|
|
|
$this->id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2015-07-22 22:10:46 +00:00
|
|
|
* @param string $svg
|
2014-05-28 06:00:30 +00:00
|
|
|
*/
|
|
|
|
public function setSvg( $svg ) {
|
|
|
|
$this->changed = true;
|
|
|
|
$this->svg = trim( $svg );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-18 09:09:29 +00:00
|
|
|
* Gets the SVG image
|
2014-05-28 06:00:30 +00:00
|
|
|
*
|
2014-11-18 09:09:29 +00:00
|
|
|
* @param string $render if set to 'render' (default) and no SVG image exists, the function
|
|
|
|
* tries to generate it on the fly.
|
|
|
|
* Otherwise, if set to 'cached', and there is no SVG in the database
|
|
|
|
* cache, an empty string is returned.
|
|
|
|
*
|
|
|
|
* @return string XML-Document of the rendered SVG
|
2014-05-28 06:00:30 +00:00
|
|
|
*/
|
2015-07-22 22:10:46 +00:00
|
|
|
public function getSvg( /** @noinspection PhpUnusedParameterInspection */ $render = 'render' ) {
|
2014-05-28 06:00:30 +00:00
|
|
|
// Spaces will prevent the image from being displayed correctly in the browser
|
2017-06-20 07:11:57 +00:00
|
|
|
if ( !$this->svg && $this->rbi ) {
|
2016-01-31 21:11:39 +00:00
|
|
|
$this->svg = $this->rbi->getSvg();
|
|
|
|
}
|
2014-05-28 06:00:30 +00:00
|
|
|
return trim( $this->svg );
|
|
|
|
}
|
2014-05-27 04:46:53 +00:00
|
|
|
|
2015-09-21 16:14:01 +00:00
|
|
|
abstract protected function getMathTableName();
|
2014-05-27 04:46:53 +00:00
|
|
|
|
|
|
|
public function getModeStr() {
|
|
|
|
$names = MathHooks::getMathNames();
|
|
|
|
return $names[ $this->getMode() ];
|
|
|
|
}
|
2015-03-16 19:51:16 +00:00
|
|
|
|
2015-09-21 16:14:01 +00:00
|
|
|
public static function getValidModes() {
|
2015-07-22 22:10:46 +00:00
|
|
|
global $wgMathValidModes;
|
|
|
|
return array_map( "MathHooks::mathModeToString", $wgMathValidModes );
|
|
|
|
}
|
|
|
|
|
2015-09-21 16:14:01 +00:00
|
|
|
public static function getDisableTexFilter() {
|
2015-07-22 22:10:46 +00:00
|
|
|
global $wgMathDisableTexFilter;
|
|
|
|
return MathHooks::mathCheckToString( $wgMathDisableTexFilter );
|
|
|
|
}
|
2016-01-29 16:50:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $inputType
|
|
|
|
*/
|
|
|
|
public function setInputType( $inputType ) {
|
|
|
|
$this->inputType = $inputType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getInputType() {
|
|
|
|
return $this->inputType;
|
|
|
|
}
|
2016-04-08 19:27:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function doCheck() {
|
|
|
|
$checker = new MathInputCheckRestbase( $this->tex, $this->getInputType(), $this->rbi );
|
|
|
|
try {
|
|
|
|
if ( $checker->isValid() ) {
|
|
|
|
$this->setTex( $checker->getValidTex() );
|
|
|
|
$this->texSecure = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( MWException $e ) {
|
|
|
|
}
|
|
|
|
$this->lastError = $checker->getError();
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-27 04:46:53 +00:00
|
|
|
}
|