mathConfig = $mathConfig; $this->rendererFactory = $rendererFactory; } /** * Sets headers - this should be called from the execute() method of all derived classes! * @param bool $success */ public function setHeaders( $success = true ) { $out = $this->getOutput(); $request = $this->getRequest(); $out->setArticleBodyOnly( true ); $out->setArticleRelated( false ); $out->setRobotPolicy( "noindex,nofollow" ); $out->disable(); $request->response()->header( "Content-type: image/svg+xml; charset=utf-8" ); if ( $success && !( $this->noRender ) ) { $request->response()->header( 'Cache-Control: public, s-maxage=604800, max-age=3600' ); // 1 week (server) 1 hour (client) $request->response()->header( 'Vary: User-Agent' ); } } /** @inheritDoc */ public function execute( $par ) { $request = $this->getRequest(); $hash = $request->getText( 'hash', '' ); $tex = $request->getText( 'tex', '' ); if ( $this->getConfig()->get( 'MathEnableExperimentalInputFormats' ) ) { $asciimath = $request->getText( 'asciimath', '' ); } else { $asciimath = ''; } $this->mode = MathConfig::normalizeRenderingMode( $request->getText( 'mode' ) ); if ( !$this->mathConfig->isValidRenderingMode( $this->mode ) ) { // Fallback to the default if an invalid mode was specified $this->mode = MathConfig::MODE_MATHML; } if ( $hash === '' && $tex === '' && $asciimath === '' ) { $this->setHeaders( false ); echo $this->printSvgError( 'No Inputhash specified' ); return; } if ( $tex === '' && $asciimath === '' ) { try { $this->renderer = $this->rendererFactory->getFromHash( $hash ); } 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 { $success = $this->renderer->render(); } } 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(); } } /** * Prints the specified error message as svg. * @param string $msg error message, HTML escaped * @return string xml svg image with the error message */ private function printSvgError( $msg ) { $result = << $msg SVG; if ( $this->getConfig()->get( MainConfigNames::DebugComments ) ) { $result .= ''; } return $result; } protected function getGroupName(): string { return 'other'; } }