Page MenuHomeWickedGov Phorge

LocalChecker.php
No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None

LocalChecker.php

<?php
namespace MediaWiki\Extension\Math\InputCheck;
use Exception;
use MediaWiki\Extension\Math\Hooks\HookRunner;
use MediaWiki\Extension\Math\MathRenderer;
use MediaWiki\Extension\Math\WikiTexVC\TexVC;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Message\Message;
use Wikimedia\ObjectCache\WANObjectCache;
class LocalChecker extends BaseChecker {
public const VERSION = 1;
private const VALID_TYPES = [ 'tex', 'inline-tex', 'chem' ];
private ?Message $error = null;
private ?string $mathMl = null;
private string $type;
private WANObjectCache $cache;
private bool $isChecked = false;
private ?MathRenderer $context = null;
private ?HookContainer $hookContainer = null;
public function __construct( WANObjectCache $cache, string $tex = '', string $type = 'tex', bool $purge = false ) {
$this->cache = $cache;
parent::__construct( $tex, $purge );
$this->type = $type;
}
public function isValid(): bool {
$this->run();
return parent::isValid();
}
public function getValidTex(): ?string {
$this->run();
return parent::getValidTex();
}
public function run() {
if ( $this->isChecked ) {
return;
}
if ( !in_array( $this->type, self::VALID_TYPES, true ) ) {
$this->error = $this->errorObjectToMessage(
(object)[ "error" => "Unsupported type passed to LocalChecker: " . $this->type ], "LocalCheck" );
return;
}
try {
$cacheInputKey = $this->getInputCacheKey();
if ( $this->purge ) {
$this->cache->delete( $cacheInputKey, WANObjectCache::TTL_INDEFINITE );
}
$result = $this->cache->getWithSetCallback(
$cacheInputKey,
WANObjectCache::TTL_INDEFINITE,
[ $this, 'runCheck' ],
[ 'version' => self::VERSION ],
);
} catch ( Exception $e ) { // @codeCoverageIgnoreStart
// This is impossible since errors are thrown only if the option debug would be set.
$this->error = Message::newFromKey( 'math_failure' );
return;
// @codeCoverageIgnoreEnd
}
if ( $result['status'] === '+' ) {
$this->isValid = true;
$this->validTeX = $result['output'];
$this->mathMl = $result['mathml'];
} else {
$this->error = $this->errorObjectToMessage(
(object)[ "error" => (object)$result["error"] ],
"LocalCheck" );
}
$this->isChecked = true;
}
/**
* Returns the string of the last error.
* @return ?Message
*/
public function getError(): ?Message {
$this->run();
return $this->error;
}
public function getPresentationMathMLFragment(): ?string {
$this->run();
return $this->mathMl;
}
public function getInputCacheKey(): string {
return $this->cache->makeGlobalKey(
self::class,
md5( $this->type . '-' . $this->inputTeX )
);
}
public function runCheck(): array {
if ( $this->type == 'chem' ) {
$options = [ 'usemhchem' => true, 'usemhchemtexified' => true ];
$texifyMhchem = true;
} else {
$options = [];
$texifyMhchem = false;
}
try {
$warnings = [];
$result = ( new TexVC() )->check( $this->inputTeX, $options, $warnings, $texifyMhchem );
} catch ( Exception $e ) { // @codeCoverageIgnoreStart
// This is impossible since errors are thrown only if the option debug would be set.
$this->error = Message::newFromKey( 'math_failure' );
return [];
// @codeCoverageIgnoreEnd
}
if ( $result['status'] === '+' ) {
$result['mathml'] = $result['input']->renderMML();
$out = [
'status' => '+',
'output' => $result['output'],
'mathml' => $result['mathml']
];
} else {
$out = [
'status' => $result['status'],
'error' => $result['error'],
];
}
if ( $this->context !== null && $this->hookContainer !== null ) {
$resultObject = (object)$result;
( new HookRunner( $this->hookContainer ) )->onMathRenderingResultRetrieved(
$this->context,
$resultObject
);
}
return $out;
}
public function setContext( ?MathRenderer $renderer ): void {
$this->context = $renderer;
}
public function setHookContainer( ?HookContainer $hookContainer ): void {
$this->hookContainer = $hookContainer;
}
}

File Metadata

Mime Type
text/x-php
Expires
Fri, Jul 3, 19:42 (1 d, 6 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
8e/bd/385bc8d6cfa98e4811269dcb1a35
Default Alt Text
LocalChecker.php (3 KB)

Event Timeline