Handle invalid cache keys on Special:MathShowImage

Special:MathShowImage takes a hash as an HTTP GET parameter. Show an error if the hash does not
exist.

Bug: T365112
Change-Id: I7d4a6602b8732b0b00cb9cba5800ba2b2fe5044f
This commit is contained in:
physikerwelt 2024-06-26 06:06:02 +02:00
parent 17ac038ae4
commit 8aee6acf24
No known key found for this signature in database
GPG key ID: FCC793EFFA5FB13C
2 changed files with 38 additions and 30 deletions

View file

@ -121,7 +121,7 @@ class RendererFactory {
return $renderer;
}
public function getFromHash( $inputHash ) {
public function getFromHash( $inputHash ): MathRenderer {
$key = $this->cache->makeGlobalKey(
MathRenderer::class,
$inputHash

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Extension\Math;
use InvalidArgumentException;
use MediaWiki\Extension\Math\Render\RendererFactory;
use MediaWiki\SpecialPage\SpecialPage;
@ -79,39 +80,46 @@ class SpecialMathShowImage extends SpecialPage {
if ( $hash === '' && $tex === '' && $asciimath === '' ) {
$this->setHeaders( false );
echo $this->printSvgError( 'No Inputhash specified' );
} else {
if ( $tex === '' && $asciimath === '' ) {
return;
}
if ( $tex === '' && $asciimath === '' ) {
try {
$this->renderer = $this->rendererFactory->getFromHash( $hash );
$this->noRender = $request->getBool( 'noRender', false );
$isInDatabase = $this->renderer->readFromCache();
if ( $isInDatabase || $this->noRender ) {
$success = $isInDatabase;
} else {
$success = $this->renderer->render();
}
} elseif ( $asciimath === '' ) {
$this->renderer = $this->rendererFactory->getRenderer( $tex, [], $this->mode );
$success = $this->renderer->render();
} catch ( InvalidArgumentException $exception ) {
$this->setHeaders( false );
echo $this->printSvgError( $exception->getMessage() );
return;
}
$this->noRender = $request->getBool( 'noRender', false );
$isInDatabase = $this->renderer->readFromCache();
if ( $isInDatabase || $this->noRender ) {
$success = $isInDatabase;
} else {
$this->renderer = $this->rendererFactory->getRenderer(
$asciimath, [ 'type' => 'ascii' ], $this->mode
);
$success = $this->renderer->render();
}
if ( $success ) {
$output = $this->renderer->getSvg();
} else {
$output = $this->printSvgError( $this->renderer->getLastError() );
}
if ( $output == "" ) {
$output = $this->printSvgError( 'No Output produced' );
$success = false;
}
$this->setHeaders( $success );
echo $output;
if ( $success ) {
$this->renderer->writeCache();
}
} elseif ( $asciimath === '' ) {
$this->renderer = $this->rendererFactory->getRenderer( $tex, [], $this->mode );
$success = $this->renderer->render();
} else {
$this->renderer = $this->rendererFactory->getRenderer(
$asciimath, [ 'type' => 'ascii' ], $this->mode
);
$success = $this->renderer->render();
}
if ( $success ) {
$output = $this->renderer->getSvg();
} else {
$output = $this->printSvgError( $this->renderer->getLastError() );
}
if ( $output == "" ) {
$output = $this->printSvgError( 'No Output produced' );
$success = false;
}
$this->setHeaders( $success );
echo $output;
if ( $success ) {
$this->renderer->writeCache();
}
}