From 8bc23cafb44d1984c9e31e2b07d07c3bca5a46c3 Mon Sep 17 00:00:00 2001 From: "physikerwelt (Moritz Schubotz)" Date: Fri, 6 Jun 2014 00:12:33 +0200 Subject: [PATCH] SpecialPage for MathImages Add new special page that displays math images. (Either SVG- or PNG-Images) Change-Id: I6065b474b8197232fcb4e79ae17bb08d9bef3ac4 --- Math.alias.php | 14 +++++ Math.php | 4 ++ SpecialMathShowImage.php | 111 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 Math.alias.php create mode 100644 SpecialMathShowImage.php diff --git a/Math.alias.php b/Math.alias.php new file mode 100644 index 0000000..0613ed6 --- /dev/null +++ b/Math.alias.php @@ -0,0 +1,14 @@ + array( 'MathShowImage', 'MathShowImage' ) // No need to translate! The PageTitle does not appear. +); \ No newline at end of file diff --git a/Math.php b/Math.php index 7df3a06..b97f6a7 100644 --- a/Math.php +++ b/Math.php @@ -211,11 +211,15 @@ $wgAutoloadClasses['MathMathML'] = $dir . 'MathMathML.php'; $wgAutoloadClasses['MathLaTeXML'] = $dir . 'MathLaTeXML.php'; $wgAutoloadClasses['MathInputCheck'] = $dir . 'MathInputCheck.php'; $wgAutoloadClasses['MathInputCheckTexvc'] = $dir . 'MathInputCheckTexvc.php'; +$wgAutoloadClasses['SpecialMathShowImage'] = $dir . 'SpecialMathShowImage.php'; $wgMessagesDirs['Math'] = __DIR__ . '/i18n'; $wgExtensionMessagesFiles['Math'] = $dir . 'Math.i18n.php'; +$wgExtensionMessagesFiles['MathAlias'] = $dir . 'Math.alias.php'; $wgParserTestFiles[] = $dir . 'mathParserTests.txt'; +$wgSpecialPageGroups[ 'MathShowImage' ] = 'other'; +$wgSpecialPages['MathShowImage'] = 'SpecialMathShowImage'; $wgResourceModules['ext.math.styles'] = array( 'localBasePath' => __DIR__ . '/modules', diff --git a/SpecialMathShowImage.php b/SpecialMathShowImage.php new file mode 100644 index 0000000..3d38504 --- /dev/null +++ b/SpecialMathShowImage.php @@ -0,0 +1,111 @@ +getOutput(); + $request = $this->getRequest(); + $out->setArticleBodyOnly( true ); + $out->setArticleRelated( false ); + $out->setRobotPolicy( "noindex,nofollow" ); + $out->disable(); + if ( $success && $this->mode == MW_MATH_PNG ) { + $request->response()->header( "Content-type: image/png;" ); + } else { + $request->response()->header( "Content-type: image/svg+xml; charset=utf-8" ); + } + if ( $success && !( $this->noRender ) ) { + $request->response()->header( 'Cache-Control: public max-age=2419200' ); // 4 weeks + $request->response()->header( 'Vary: User-Agent' ); + } + } + + function execute( $par ) { + $request = $this->getRequest(); + $hash = $request->getText( 'hash', '' ); + $tex = $request->getText( 'tex', ''); + $asciimath = $request->getText( 'asciimath', ''); + $this->mode = $request->getInt( 'mode', MW_MATH_MATHML ); + if ( $hash === '' && $tex === '' && $asciimath === '' ) { + $this->setHeaders( false ); + echo $this->printSvgError( 'No Inputhash specified' ); + } else { + if ( $tex === '' && $asciimath === ''){ + switch ( $this->mode ) { + case MW_MATH_PNG: + $this->renderer = MathTexvc::newFromMd5( $hash ); + break; + case MW_MATH_LATEXML: + $this->renderer = MathLaTeXML::newFromMd5( $hash ); + break; + default: + $this->renderer = MathMathML::newFromMd5( $hash ); + } + $this->noRender = $request->getBool( 'noRender', false ); + if ( $this->noRender ) { + $success = $this->renderer->readFromDatabase(); + } else { + if ( $this->mode == MW_MATH_PNG ) { + $mmlRenderer = MathMathML::newFromMd5( $hash ); + $mmlRenderer->readFromDatabase(); + $this->renderer = new MathTexvc( $mmlRenderer->getUserInputTex() ); + } + $success = $this->renderer->render(); + } + } elseif ( $asciimath === '' ) { + $this->renderer = MathRenderer::getRenderer( $tex , array(), $this->mode ); + $success = $this->renderer->render(); + } else { + $this->renderer = MathRenderer::getRenderer( $asciimath , array( 'type' => 'ascii' ), $this->mode ); + $success = $this->renderer->render(); + } + if ( $success ) { + if ( $this->mode == MW_MATH_PNG ) { + $output = $this->renderer->getPng(); + } else { + $output = $this->renderer->getSvg(); + } + } else { + // Error message in PNG not supported + $output = $this->printSvgError( $this->renderer->getLastError() ); + } + if ( $output == "" ) { + $output = $this->printSvgError( 'No Output produced' ); + $success = false; + } + $this->setHeaders( $success ); + echo $output; + $this->renderer->writeCache(); + } + } + + /** + * Prints the specified error message as svg. + * @param string $msg error message + * @return xml svg image with the error message + */ + private function printSvgError( $msg ) { + global $wgMathDebug; + $result = '' . + '' . htmlspecialchars( $msg ) . ''; + if ( $wgMathDebug ) { + $result .= ''; + } + return $result; + } + +} \ No newline at end of file