From 5245d0f5550d81019aabb94852a5f27d6431828d Mon Sep 17 00:00:00 2001 From: physikerwelt Date: Thu, 25 Apr 2013 17:07:25 +0000 Subject: [PATCH] Introduce getter and setter for the MathRenderer Adds a new method isChanged() for determining if a value was changed. This is done in preparation for a more elaborated caching method that is handled inside the abstract base class. Change-Id: Ica15f77d96453d30edd3a117c7185c694ad3691e --- MathMathJax.php | 2 +- MathRenderer.php | 118 +++++++++++++++++++++++++++++++++++++ MathSource.php | 2 +- MathTexvc.php | 70 +++++++++++----------- tests/MathDatabaseTest.php | 6 +- tests/MathRendererTest.php | 26 +++++++- 6 files changed, 181 insertions(+), 43 deletions(-) diff --git a/MathMathJax.php b/MathMathJax.php index e0daa5d..2b21318 100644 --- a/MathMathJax.php +++ b/MathMathJax.php @@ -30,7 +30,7 @@ class MathMathJax extends MathRenderer { 'dir' => 'ltr' ) ), - '$ ' . str_replace( "\n", " ", $this->tex ) . ' $' + '$ ' . str_replace( "\n", " ", $this->getTex() ) . ' $' ); } } diff --git a/MathRenderer.php b/MathRenderer.php index 78ba02b..95a2089 100644 --- a/MathRenderer.php +++ b/MathRenderer.php @@ -36,6 +36,7 @@ abstract class MathRenderer { var $mathml = ''; var $conservativeness = 0; var $params = ''; + var $changed = false; protected $recall; /** @@ -230,4 +231,121 @@ abstract class MathRenderer { public function getTex() { return $this->tex; } + + /** + * gets the rendering mode MW_MATH_* + * + * @return int + */ + public function getMode() { + return $this->mode; + } + + /** + * Sets the TeX code + * + * @param string $tex + */ + public function setTex( $tex ) { + $this->changed = true; + $this->tex = $tex; + } + + /** + * 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 MathML XML element + * @return string in UTF-8 encoding + */ + public function getMathml() { + return $this->mathml; + } + + /** + * @param string $mathml use UTF-8 encoding + */ + public function setMathml( $mathml ) { + $this->changed = true; + $this->mathml = $mathml; + } + + /** + * 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; + } + + /** + * Get the attributes of the math tag + * + * @return array() + */ + public function getParams() { + return $this->params; + } + + /** + * @param array() $params + */ + public function setParams( $params ) { + //$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. + $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; + } + } + diff --git a/MathSource.php b/MathSource.php index ebc6ffd..ceecd45 100644 --- a/MathSource.php +++ b/MathSource.php @@ -35,7 +35,7 @@ class MathSource extends MathRenderer { 'dir' => 'ltr' ) ), - '$ ' . str_replace( "\n", " ", $this->tex ) . ' $' + '$ ' . str_replace( "\n", " ", $this->getTex() ) . ' $' ); } diff --git a/MathTexvc.php b/MathTexvc.php index 9fc8e7e..275ed7d 100644 --- a/MathTexvc.php +++ b/MathTexvc.php @@ -46,7 +46,7 @@ class MathTexvc extends MathRenderer { function getHashPath() { $path = $this->getBackend()->getRootStoragePath() . '/math-render/' . $this->getHashSubPath(); - wfDebug( "TeX: getHashPath, hash is: $this->hash, path is: $path\n" ); + wfDebugLog("Math", "TeX: getHashPath, hash is: {$this->getHash()}, path is: $path\n" ); return $path; } @@ -56,9 +56,9 @@ class MathTexvc extends MathRenderer { * @return string Relative directory */ function getHashSubPath() { - return substr( $this->hash, 0, 1 ) - . '/' . substr( $this->hash, 1, 1 ) - . '/' . substr( $this->hash, 2, 1 ); + return substr( $this->getHash(), 0, 1 ) + . '/' . substr( $this->getHash(), 1, 1 ) + . '/' . substr( $this->getHash(), 2, 1 ); } /** @@ -69,7 +69,7 @@ class MathTexvc extends MathRenderer { function getMathImageUrl() { global $wgMathPath; $dir = $this->getHashSubPath(); - return "$wgMathPath/$dir/{$this->hash}.png"; + return "$wgMathPath/$dir/{$this->getHash()}.png"; } /** @@ -85,7 +85,7 @@ class MathTexvc extends MathRenderer { 'img', array( 'class' => 'tex', - 'alt' => $this->tex + 'alt' => $this->getTex(), ), array( 'src' => $url @@ -111,7 +111,7 @@ class MathTexvc extends MathRenderer { $cmd = $wgTexvc . ' ' . $escapedTmpDir . ' ' . $escapedTmpDir . ' ' . - wfEscapeShellArg( $this->tex ) . ' ' . + wfEscapeShellArg( $this->getTex() ) . ' ' . wfEscapeShellArg( 'UTF-8' ) . ' ' . wfEscapeShellArg( $wgTexvcBackgroundColor ); @@ -131,43 +131,43 @@ class MathTexvc extends MathRenderer { } } - $tempFsFile = new TempFSFile( "$tmpDir/{$this->hash}.png" ); + $tempFsFile = new TempFSFile( "$tmpDir/{$this->getHash()}.png" ); $tempFsFile->autocollect(); // destroy file when $tempFsFile leaves scope $retval = substr( $contents, 0, 1 ); $errmsg = ''; if ( ( $retval == 'C' ) || ( $retval == 'M' ) || ( $retval == 'L' ) ) { if ( $retval == 'C' ) { - $this->conservativeness = self::CONSERVATIVE; + $this->setConservativeness( self::CONSERVATIVE ); } elseif ( $retval == 'M' ) { - $this->conservativeness = self::MODERATE; + $this->setConservativeness( self::MODERATE ); } else { - $this->conservativeness = self::LIBERAL; + $this->setConservativeness( self::LIBERAL ); } $outdata = substr( $contents, 33 ); $i = strpos( $outdata, "\000" ); - $this->html = substr( $outdata, 0, $i ); - $this->mathml = substr( $outdata, $i + 1 ); + $this->setHtml( substr( $outdata, 0, $i ) ); + $this->setMathml( substr( $outdata, $i + 1 ) ); } elseif ( ( $retval == 'c' ) || ( $retval == 'm' ) || ( $retval == 'l' ) ) { - $this->html = substr( $contents, 33 ); + $this->setHtml( substr( $contents, 33 ) ); if ( $retval == 'c' ) { - $this->conservativeness = self::CONSERVATIVE; + $this->setConservativeness( self::CONSERVATIVE ) ; } elseif ( $retval == 'm' ) { - $this->conservativeness = self::MODERATE; + $this->setConservativeness( self::MODERATE ); } else { - $this->conservativeness = self::LIBERAL; + $this->setConservativeness( self::LIBERAL ); } - $this->mathml = null; + $this->setMathml( null ); } elseif ( $retval == 'X' ) { - $this->html = null; - $this->mathml = substr( $contents, 33 ); - $this->conservativeness = self::LIBERAL; + $this->setHtml( null ); + $this->setMathml( substr( $contents, 33 ) ); + $this->setConservativeness( self::LIBERAL ); } elseif ( $retval == '+' ) { - $this->html = null; - $this->mathml = null; - $this->conservativeness = self::LIBERAL; + $this->setHtml( null ); + $this->setMathml( null ); + $this->setConservativeness( self::LIBERAL ); } else { $errbit = htmlspecialchars( substr( $contents, 1 ) ); switch( $retval ) { @@ -186,18 +186,18 @@ class MathTexvc extends MathRenderer { } if ( !$errmsg ) { - $this->hash = substr( $contents, 1, 32 ); + $this->setHash( substr( $contents, 1, 32 ) ); } wfRunHooks( 'MathAfterTexvc', array( &$this, &$errmsg ) ); if ( $errmsg ) { return $errmsg; - } elseif ( !preg_match( "/^[a-f0-9]{32}$/", $this->hash ) ) { + } elseif ( !preg_match( "/^[a-f0-9]{32}$/", $this->getHash() ) ) { return $this->getError( 'math_unknown_error' ); - } elseif ( !file_exists( "$tmpDir/{$this->hash}.png" ) ) { + } elseif ( !file_exists( "$tmpDir/{$this->getHash()}.png" ) ) { return $this->getError( 'math_image_error' ); - } elseif ( filesize( "$tmpDir/{$this->hash}.png" ) == 0 ) { + } elseif ( filesize( "$tmpDir/{$this->getHash()}.png" ) == 0 ) { return $this->getError( 'math_image_error' ); } @@ -210,7 +210,7 @@ class MathTexvc extends MathRenderer { } // Store the file at the final storage path... if ( !$backend->quickStore( array( - 'src' => "$tmpDir/{$this->hash}.png", 'dst' => "$hashpath/{$this->hash}.png" + 'src' => "$tmpDir/{$this->getHash()}.png", 'dst' => "$hashpath/{$this->getHash()}.png" ) )->isOK() ) { return $this->getError( 'math_output_error' ); @@ -247,15 +247,15 @@ class MathTexvc extends MathRenderer { * @return string HTML string */ function doHTMLRender() { - if ( $this->mode == MW_MATH_MATHML && $this->mathml != '' ) { + if ( $this->getMode() == MW_MATH_MATHML && $this->getMathml() != '' ) { return Xml::tags( 'math', $this->getAttributes( 'math', array( 'xmlns' => 'http://www.w3.org/1998/Math/MathML' ) ), $this->mathml ); } - if ( ( $this->mode == MW_MATH_PNG ) || ( $this->html == '' ) || - ( ( $this->mode == MW_MATH_SIMPLE ) && ( $this->conservativeness != self::CONSERVATIVE ) ) || - ( ( $this->mode == MW_MATH_MODERN || $this->mode == MW_MATH_MATHML ) && ( $this->conservativeness == self::LIBERAL ) ) + if ( ( $this->getMode() == MW_MATH_PNG ) || ( $this->getHtml() == '' ) || + ( ( $this->getMode() == MW_MATH_SIMPLE ) && ( $this->getConservativeness() != self::CONSERVATIVE ) ) || + ( ( $this->getMode() == MW_MATH_MODERN || $this->getMode() == MW_MATH_MATHML ) && ( $this->getConservativeness() == self::LIBERAL ) ) ) { return $this->getMathImageHTML(); @@ -265,7 +265,7 @@ class MathTexvc extends MathRenderer { array( 'class' => 'texhtml', 'dir' => 'ltr' ) ), - $this->html + $this->getHtml() ); } } @@ -300,7 +300,7 @@ class MathTexvc extends MathRenderer { // Short-circuit the file existence & migration checks return true; } - $filename = $this->getHashPath() . "/{$this->hash}.png"; // final storage path + $filename = $this->getHashPath() . "/{$this->getHash()}.png"; // final storage path $backend = $this->getBackend(); if ( $backend->fileExists( array( 'src' => $filename ) ) ) { if ( $backend->getFileSize( array( 'src' => $filename ) ) == 0 ) { diff --git a/tests/MathDatabaseTest.php b/tests/MathDatabaseTest.php index 5b653fc..f909f7f 100644 --- a/tests/MathDatabaseTest.php +++ b/tests/MathDatabaseTest.php @@ -51,9 +51,9 @@ class MathDatabaseTest extends MediaWikiTestCase { */ public function setValues() { // set some values - $this->renderer->tex = self::SOME_TEX ; - $this->renderer->html = self::SOME_HTML; - $this->renderer->mathml = self::SOME_MATHML; + $this->renderer->setTex( self::SOME_TEX ); + $this->renderer->setHtml( self::SOME_HTML ); + $this->renderer->setMathml( self::SOME_MATHML ); } /** * Checks database access. Writes an etry and reads it back. diff --git a/tests/MathRendererTest.php b/tests/MathRendererTest.php index d985330..ea6c96b 100644 --- a/tests/MathRendererTest.php +++ b/tests/MathRendererTest.php @@ -11,13 +11,17 @@ class MathRendererTest extends MediaWikiTestCase { * @covers MathRenderer::__construct() */ public function testBasics() { - $renderer = $this->getMockForAbstractClass( 'MathRenderer', array ( MathDatabaseTest::SOME_TEX ) ); + $renderer = $this->getMockForAbstractClass( 'MathRenderer' + , array ( MathDatabaseTest::SOME_TEX ) ); // check if the TeX input was corretly passed to the class - $this->assertEquals( MathDatabaseTest::SOME_TEX, $renderer->getTex(), "test getTex" ); + $this->assertEquals( MathDatabaseTest::SOME_TEX, $renderer->getTex() + , "test getTex" ); + $this->assertEquals( $renderer->isChanged(), false + , "test if changed is initially false"); } /** - * Test behavior of writeCache() when nothing was changed + * Test behavior of writeCache() when nothing was changed * @covers MathRenderer::writeCache() */ public function testWriteCacheSkip() { @@ -43,4 +47,20 @@ class MathRendererTest extends MediaWikiTestCase { ->method( 'writeToDatabase' ); $renderer->writeCache(); } + + /** + * Test behavior $change when the rendered hash was changed + * @covers MathRenderer::setHash() + */ + public function testChangeHash() { + $renderer = $this->getMockBuilder( 'MathRenderer' ) + ->setMethods( array( 'render' ) ) + ->disableOriginalConstructor() + ->getMock(); + $this->assertEquals( $renderer->isChanged(), false + , "test if changed is initially false"); + $renderer->setHash('0000'); + $this->assertEquals( $renderer->isChanged(), true + , "assumes that changing a hash sets changed to true"); + } } \ No newline at end of file