Physikerwelt has uploaded a new change for review. https://gerrit.wikimedia.org/r/174156
Change subject: Add debuging functionality ...................................................................... Add debuging functionality Write debug log to database if $wgMathDebug is enabled. Change-Id: I7ddd297b3ab0e3ec8b46faf55a466fc2ae6f5a9a --- M MathLaTeXML.php M MathMathML.php M MathRenderer.php M tests/MathMathMLTest.php M tests/MathRendererTest.php 5 files changed, 116 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Math refs/changes/56/174156/1 diff --git a/MathLaTeXML.php b/MathLaTeXML.php index 4915058..4f40474 100644 --- a/MathLaTeXML.php +++ b/MathLaTeXML.php @@ -109,7 +109,9 @@ $post = preg_replace( '/&tex=/' , '&tex=literal:', $post , 1); } $this->lastError = ''; + if( $wgMathDebug ) { $renderingStart = microtime( true ); } $requestResult = $this->makeRequest( $host, $post, $res, $this->lastError ); + if( $wgMathDebug ) { $this->setRenderingTime( microtime( true ) - $renderingStart ); } if ( $requestResult ) { $jsonResult = json_decode( $res ); if ( $jsonResult && json_last_error() === JSON_ERROR_NONE ) { @@ -117,7 +119,9 @@ $this->setMathml( $jsonResult->result ); if ( $wgMathDebug ) { $this->setLog( $jsonResult->log ); + $this->setPostData( $post ); $this->setStatusCode( $jsonResult->status_code ); + $this->writeDebugLog(); } return true; } else { diff --git a/MathMathML.php b/MathMathML.php index 6707059..d7dd222 100644 --- a/MathMathML.php +++ b/MathMathML.php @@ -236,7 +236,9 @@ $host = self::pickHost(); $post = $this->getPostData(); $this->lastError = ''; + if( $wgMathDebug ) { $renderingStart = microtime( true ); } $requestResult = $this->makeRequest( $host, $post, $res, $this->lastError ); + if( $wgMathDebug ) { $this->setRenderingTime( microtime( true ) - $renderingStart ); } if ( $requestResult ) { $jsonResult = json_decode( $res ); if ( $jsonResult && json_last_error() === JSON_ERROR_NONE ) { @@ -253,6 +255,8 @@ } if ( $wgMathDebug ) { $this->setLog( $jsonResult->log ); + $this->setPostData( $post ); + $this->writeDebugLog(); } if ( $this->getMode() != MW_MATH_LATEXML && $this->inputType != 'pmml') { $this->setMathml( $jsonResult->mml ); @@ -469,4 +473,5 @@ parent::initializeFromDatabaseRow( $rpage ); } + } diff --git a/MathRenderer.php b/MathRenderer.php index 6784ff4..72e2a6d 100644 --- a/MathRenderer.php +++ b/MathRenderer.php @@ -47,6 +47,10 @@ protected $timestamp; /** @var log messages generated during conversion of mathematical content */ protected $log = ''; + /** @var time used for rendering in ms */ + protected $renderingTime = 0; + /** @var post request sent to rendering server (if applicable) */ + protected $postData = ''; // STATE OF THE CLASS INSTANCE /** @var boolean has variable tex been security-checked */ @@ -685,4 +689,70 @@ $names = MathHooks::getMathNames(); return $names[ $this->getMode() ]; } + + /** + * @return string + */ + public function getPostData() { + return $this->postData; + } + + /** + * @param string $postData + */ + public function setPostData( $postData ) { + $this->postData = $postData; + } + + /** + * @return int time in ms + */ + public function getRenderingTime() { + return $this->renderingTime; + } + + /** + * @param int|long $renderingTime either in ms or as in seconds as long + * @throws MWException + */ + public function setRenderingTime( $renderingTime ) { + $type = gettype($renderingTime); + switch ( $type ) { + case "double": + case "float": + $this->renderingTime = (int) ( $renderingTime * 1000 ); + break; + case "integer": + $this->renderingTime = $renderingTime; + break; + default: + throw new MWException( __METHOD__ . ": does not support type $type" ); + } + } + + /** + * Gets an array that matches the variables of the class to the debug database columns + * @return array + */ + protected function dbDebugOutArray() { + $out = array( 'math_inputhash' => $this->getInputHash(), + 'math_log' => $this->getLog(), + 'math_mode' => $this->getMode(), + 'math_post' => $this->getPostData(), + 'math_rederingtime' => $this->getRenderingTime() + ); + return $out; + } + + protected function writeDebugLog() { + global $wgMathDebug; + if ( $wgMathDebug ) { + $dbw = wfGetDB( DB_MASTER ); + $outArray = $this->dbDebugOutArray(); + $method = __METHOD__; + $dbw->onTransactionIdle( function () use ( $dbw, $outArray, $method ) { + $dbw->insert( 'mathlog', $outArray, $method ); + } ); + } + } } diff --git a/tests/MathMathMLTest.php b/tests/MathMathMLTest.php index aedaefe..f2c56ed 100644 --- a/tests/MathMathMLTest.php +++ b/tests/MathMathMLTest.php @@ -160,6 +160,27 @@ $expected = 'hash=5628b8248b79267ecac656102334d5e3&mode=5'; $this->assertContains( $expected, $real, 'Link to SVG image missing' ); } + + /** + * Checks the creation of the math table with debugging enabled. + * @covers MathHooks::onLoadExtensionSchemaUpdates + */ + public function testDebug() { + $dbr = wfGetDB( DB_SLAVE ); + if ( $dbr->getType() !== 'mysql' ) { + $this->markTestSkipped( 'No math debug support for SQLite' ); + } + $this->setMwGlobals( 'wgMathValidModes', array( MW_MATH_MATHML ) ); + $this->setMwGlobals( 'wgMathDebug', true ); + $renderer = MathRenderer::getRenderer( "a+b", array(), MW_MATH_MATHML ); + $this->assertTrue( $renderer->render( true ) ); + $hash = $renderer->getInputHash(); + $row = $dbr->selectRow("mathlog", "*", array( "math_inputhash" => $hash ) , __METHOD__, + array("order by" => "math_timestamp desc")); + $this->assertContains( "success", $row->math_log ); + $this->assertEquals( "type=inline-TeX&q=%7B%5Cdisplaystyle%20a%2Bb%7D", $row->math_post ); + $this->assertEquals( 5, $row->math_mode); + } } /** diff --git a/tests/MathRendererTest.php b/tests/MathRendererTest.php index 8d90420..310ddec 100644 --- a/tests/MathRendererTest.php +++ b/tests/MathRendererTest.php @@ -137,4 +137,20 @@ // so readFromDatabase will be called again $this->assertEquals( $renderer->checkTex(), true ); } + + public function testSetRenderingTime(){ + /** @var MathRenderer $renderer */ + $renderer = + $this->getMockBuilder( 'MathRenderer' )->setMethods( array( + 'render', + 'getMathTableName', + 'getHtmlOutput', + 'readFromDatabase', + 'setTex' + ) )->setConstructorArgs( array( self::TEXVCCHECK_INPUT ) )->getMock(); + $renderer->setRenderingTime(1.234); + $this->assertEquals( 1234, $renderer->getRenderingTime(), "Check time float input"); + $renderer->setRenderingTime(4321); + $this->assertEquals( 4321, $renderer->getRenderingTime(), "Check time integer input"); + } } \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/174156 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I7ddd297b3ab0e3ec8b46faf55a466fc2ae6f5a9a Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Math Gerrit-Branch: debug Gerrit-Owner: Physikerwelt <w...@physikerwelt.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits